Skip to content

Commit 2be5606

Browse files
committed
(Add) Store torrent moderation message to DB and views
1 parent ff87468 commit 2be5606

15 files changed

+501
-45
lines changed

app/Helpers/TorrentHelper.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,10 @@ public static function approveHelper(int $id): void
5050
$torrent = Torrent::with('user')->withoutGlobalScope(ApprovedScope::class)->findOrFail($id);
5151
$torrent->created_at = Carbon::now();
5252
$torrent->bumped_at = Carbon::now();
53+
// Update the status on this torrent.
54+
// The moderation table status for this torrent is set to approved in this stage already.
55+
// Both places are kept in order to have the torrent status quickly accesible for the announce.
5356
$torrent->status = Torrent::APPROVED;
54-
$torrent->moderated_at = now();
55-
$torrent->moderated_by = (int) auth()->id();
5657

5758
if (!$torrent->free) {
5859
$autoFreeleechs = AutomaticTorrentFreeleech::query()

app/Http/Controllers/API/TorrentController.php

+9-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use App\Models\Keyword;
2828
use App\Models\Movie;
2929
use App\Models\Torrent;
30+
use App\Models\TorrentModerationMessage;
3031
use App\Models\TorrentFile;
3132
use App\Models\Tv;
3233
use App\Models\User;
@@ -182,8 +183,14 @@ public function store(Request $request): \Illuminate\Http\JsonResponse
182183
$torrent->fl_until = Carbon::now()->addDays($request->integer('fl_until'));
183184
}
184185
$torrent->sticky = $user->group->is_modo || $user->group->is_internal ? ($request->input('sticky') ?? 0) : 0;
185-
$torrent->moderated_at = Carbon::now();
186-
$torrent->moderated_by = User::SYSTEM_USER_ID;
186+
187+
// Update the status on this torrent moderation message table.
188+
// The status on the torrent itself will be updated with the TorrentHelper().
189+
// Both places are kept in order to have the torrent status quickly accesible for the announce.
190+
TorrentModerationMessage::create([
191+
'moderated_by' => User::SYSTEM_USER_ID,
192+
'torrent_id' => $torrent->id,
193+
]);
187194

188195
// Set freeleech and doubleup if featured
189196
if ($torrent->featured === true) {

app/Http/Controllers/Staff/ModerationController.php

+23-6
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use App\Models\PrivateMessage;
2424
use App\Models\Scopes\ApprovedScope;
2525
use App\Models\Torrent;
26+
use App\Models\TorrentModerationMessage;
2627
use App\Repositories\ChatRepository;
2728
use App\Services\Unit3dAnnounce;
2829

@@ -52,11 +53,11 @@ public function index(): \Illuminate\Contracts\View\Factory|\Illuminate\View\Vie
5253
->where('status', '=', Torrent::PENDING)
5354
->get(),
5455
'postponed' => Torrent::withoutGlobalScope(ApprovedScope::class)
55-
->with(['user.group', 'moderated.group', 'category', 'type', 'resolution'])
56+
->with(['user.group', 'category', 'type', 'resolution'])
5657
->where('status', '=', Torrent::POSTPONED)
5758
->get(),
5859
'rejected' => Torrent::withoutGlobalScope(ApprovedScope::class)
59-
->with(['user.group', 'moderated.group', 'category', 'type', 'resolution'])
60+
->with(['user.group', 'category', 'type', 'resolution'])
6061
->where('status', '=', Torrent::REJECTED)
6162
->get(),
6263
]);
@@ -108,14 +109,25 @@ public function update(UpdateModerationRequest $request, int $id): \Illuminate\H
108109

109110
TorrentHelper::approveHelper($id);
110111

112+
TorrentModerationMessage::create([
113+
'moderated_by' => $staff->id,
114+
'torrent_id' => $torrent->id,
115+
'status' => Torrent::APPROVED,
116+
]);
117+
111118
return to_route('staff.moderation.index')
112119
->withSuccess('Torrent Approved');
113120

114121
case Torrent::REJECTED:
115122
$torrent->update([
116-
'status' => Torrent::REJECTED,
117-
'moderated_at' => now(),
123+
'status' => Torrent::REJECTED,
124+
]);
125+
126+
TorrentModerationMessage::create([
118127
'moderated_by' => $staff->id,
128+
'torrent_id' => $torrent->id,
129+
'status' => Torrent::REJECTED,
130+
'message' => $request->message,
119131
]);
120132

121133
$conversation = Conversation::create(['subject' => 'Your upload, '.$torrent->name.', has been rejected by '.$staff->username]);
@@ -137,9 +149,14 @@ public function update(UpdateModerationRequest $request, int $id): \Illuminate\H
137149

138150
case Torrent::POSTPONED:
139151
$torrent->update([
140-
'status' => Torrent::POSTPONED,
141-
'moderated_at' => now(),
152+
'status' => Torrent::POSTPONED,
153+
]);
154+
155+
TorrentModerationMessage::create([
142156
'moderated_by' => $staff->id,
157+
'torrent_id' => $torrent->id,
158+
'status' => Torrent::POSTPONED,
159+
'message' => $request->message,
143160
]);
144161

145162
$conversation = Conversation::create(['subject' => 'Your upload, '.$torrent->name.', has been postponed by '.$staff->username]);

app/Http/Controllers/TorrentController.php

+9-2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
use App\Models\Resolution;
3434
use App\Models\Scopes\ApprovedScope;
3535
use App\Models\Torrent;
36+
use App\Models\TorrentModerationMessage;
3637
use App\Models\TorrentFile;
3738
use App\Models\Tv;
3839
use App\Models\Type;
@@ -408,10 +409,16 @@ public function store(StoreTorrentRequest $request): \Illuminate\Http\RedirectRe
408409
'size' => $meta['size'],
409410
'nfo' => $request->hasFile('nfo') ? TorrentTools::getNfo($request->file('nfo')) : '',
410411
'user_id' => $user->id,
411-
'moderated_at' => now(),
412-
'moderated_by' => User::SYSTEM_USER_ID,
413412
] + $request->safe()->except(['torrent']));
414413

414+
// Update the status on this torrent moderation message table.
415+
// The status on the torrent itself will be updated with the TorrentHelper().
416+
// Both places are kept in order to have the torrent status quickly accesible for the announce.
417+
TorrentModerationMessage::create([
418+
'moderated_by' => User::SYSTEM_USER_ID,
419+
'torrent_id' => $torrent->id,
420+
]);
421+
415422
// Populate the status/seeders/leechers/times_completed fields for the external tracker
416423
$torrent->refresh();
417424

app/Models/Torrent.php

+5-11
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@
6464
* @property int $highspeed
6565
* @property bool $featured
6666
* @property int $status
67-
* @property \Illuminate\Support\Carbon|null $moderated_at
68-
* @property int|null $moderated_by
6967
* @property int $anon
7068
* @property bool $sticky
7169
* @property int $sd
@@ -99,7 +97,7 @@ class Torrent extends Model
9997
/**
10098
* Get the attributes that should be cast.
10199
*
102-
* @return array{tmdb: 'int', igdb: 'int', bumped_at: 'datetime', fl_until: 'datetime', du_until: 'datetime', doubleup: 'bool', refundable: 'bool', featured: 'bool', moderated_at: 'datetime', sticky: 'bool'}
100+
* @return array{tmdb: 'int', igdb: 'int', bumped_at: 'datetime', fl_until: 'datetime', du_until: 'datetime', doubleup: 'bool', refundable: 'bool', featured: 'bool', sticky: 'bool'}
103101
*/
104102
protected function casts(): array
105103
{
@@ -112,7 +110,6 @@ protected function casts(): array
112110
'doubleup' => 'bool',
113111
'refundable' => 'bool',
114112
'featured' => 'bool',
115-
'moderated_at' => 'datetime',
116113
'sticky' => 'bool',
117114
];
118115
}
@@ -561,16 +558,13 @@ public function playlists(): \Illuminate\Database\Eloquent\Relations\BelongsToMa
561558
}
562559

563560
/**
564-
* Torrent Has Been Moderated By.
561+
* Has Many Moderation Messages.
565562
*
566-
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo<User, $this>
563+
* @return \Illuminate\Database\Eloquent\Relations\HasMany<TorrentModerationMessage, $this>
567564
*/
568-
public function moderated(): \Illuminate\Database\Eloquent\Relations\BelongsTo
565+
public function moderationMessages(): \Illuminate\Database\Eloquent\Relations\HasMany
569566
{
570-
return $this->belongsTo(User::class, 'moderated_by')->withDefault([
571-
'username' => 'System',
572-
'id' => User::SYSTEM_USER_ID,
573-
]);
567+
return $this->hasMany(TorrentModerationMessage::class)->orderBy('created_at', 'desc');
574568
}
575569

576570
/**
+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* NOTICE OF LICENSE.
7+
*
8+
* UNIT3D Community Edition is open-sourced software licensed under the GNU Affero General Public License v3.0
9+
* The details is bundled with this project in the file LICENSE.txt.
10+
*
11+
* @project UNIT3D Community Edition
12+
*
13+
* @author HDVinnie <hdinnovations@protonmail.com>
14+
* @license https://www.gnu.org/licenses/agpl-3.0.en.html/ GNU Affero General Public License v3.0
15+
*/
16+
17+
namespace App\Models;
18+
19+
use App\Traits\Auditable;
20+
use Illuminate\Database\Eloquent\Factories\HasFactory;
21+
use Illuminate\Database\Eloquent\Model;
22+
23+
/**
24+
* App\Models\TorrentModerationMessage.
25+
*
26+
* @property int $id
27+
* @property int $moderated_by
28+
* @property int $torrent_id
29+
* @property int $status
30+
* @property string|null $message
31+
* @property \Illuminate\Support\Carbon|null $created_at
32+
* @property \Illuminate\Support\Carbon|null $updated_at
33+
*/
34+
class TorrentModerationMessage extends Model
35+
{
36+
use Auditable;
37+
38+
/** @use HasFactory<\Database\Factories\TorrentModerationMessageFactory> */
39+
use HasFactory;
40+
41+
protected $guarded = [];
42+
43+
/**
44+
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo<Torrent, $this>
45+
*/
46+
public function torrent(): \Illuminate\Database\Eloquent\Relations\BelongsTo
47+
{
48+
return $this->belongsTo(Torrent::class);
49+
}
50+
51+
/**
52+
* Belongs To A Moderator.
53+
*
54+
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo<User, $this>
55+
*/
56+
public function moderator(): \Illuminate\Database\Eloquent\Relations\BelongsTo
57+
{
58+
return $this->belongsTo(User::class, 'moderated_by')->withDefault([
59+
'username' => 'System',
60+
'id' => User::SYSTEM_USER_ID,
61+
]);
62+
}
63+
}

database/factories/TorrentFactory.php

-2
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,6 @@ public function definition(): array
6666
'highspeed' => $this->faker->boolean(),
6767
'featured' => false,
6868
'status' => Torrent::APPROVED,
69-
'moderated_at' => now(),
70-
'moderated_by' => 1,
7169
'anon' => $this->faker->boolean(),
7270
'sticky' => $this->faker->boolean(),
7371
'sd' => $this->faker->boolean(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* NOTICE OF LICENSE.
7+
*
8+
* UNIT3D Community Edition is open-sourced software licensed under the GNU Affero General Public License v3.0
9+
* The details is bundled with this project in the file LICENSE.txt.
10+
*
11+
* @project UNIT3D Community Edition
12+
*
13+
* @author HDVinnie <hdinnovations@protonmail.com>
14+
* @license https://www.gnu.org/licenses/agpl-3.0.en.html/ GNU Affero General Public License v3.0
15+
*/
16+
17+
namespace Database\Factories;
18+
19+
use App\Models\Torrent;
20+
use App\Models\TorrentModerationMessage;
21+
use Illuminate\Database\Eloquent\Factories\Factory;
22+
23+
/** @extends Factory<TorrentModerationMessage> */
24+
class TorrentModerationMessageFactory extends Factory
25+
{
26+
/**
27+
* The name of the factory's corresponding model.
28+
*/
29+
protected $model = TorrentModerationMessage::class;
30+
31+
/**
32+
* Define the model's default state.
33+
*/
34+
public function definition(): array
35+
{
36+
return [
37+
'moderated_by' => 1,
38+
'torrent_id' => Torrent::factory(),
39+
'message' => $this->faker->sentence(),
40+
];
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* NOTICE OF LICENSE.
7+
*
8+
* UNIT3D Community Edition is open-sourced software licensed under the GNU Affero General Public License v3.0
9+
* The details is bundled with this project in the file LICENSE.txt.
10+
*
11+
* @project UNIT3D Community Edition
12+
*
13+
* @author Roardom <roardom@protonmail.com>
14+
* @license https://www.gnu.org/licenses/agpl-3.0.en.html/ GNU Affero General Public License v3.0
15+
*/
16+
17+
use Illuminate\Database\Migrations\Migration;
18+
use Illuminate\Database\Schema\Blueprint;
19+
use Illuminate\Support\Facades\DB;
20+
use Illuminate\Support\Facades\Schema;
21+
22+
return new class () extends Migration {
23+
/**
24+
* Run the migrations.
25+
*/
26+
public function up(): void
27+
{
28+
Schema::create('torrent_moderation_messages', function (Blueprint $table): void {
29+
$table->increments('id');
30+
$table->unsignedInteger('moderated_by')->index();
31+
$table->unsignedInteger('torrent_id')->index();
32+
$table->smallInteger('status')->default(0);
33+
$table->text('message')->nullable();
34+
$table->timestamps();
35+
});
36+
37+
// Migrate "moderated_by" to new table using chunking
38+
DB::table('torrents')->where('moderated_by', '!=', null)->orderBy('id')->chunk(10000, function ($torrents): void {
39+
$insertData = [];
40+
41+
foreach ($torrents as $torrent) {
42+
// Convert to array to prevent PHPStan errors
43+
$torrentArray = (array) $torrent;
44+
45+
$insertData[] = [
46+
'moderated_by' => $torrentArray['moderated_by'],
47+
'torrent_id' => $torrentArray['id'],
48+
'status' => $torrentArray['status'],
49+
'message' => null,
50+
'created_at' => $torrentArray['moderated_at'],
51+
'updated_at' => $torrentArray['moderated_at'],
52+
];
53+
}
54+
55+
// Only insert if there's data to insert
56+
if (!empty($insertData)) {
57+
DB::table('torrent_moderation_messages')->insert($insertData);
58+
}
59+
});
60+
61+
Schema::table('torrents', function (Blueprint $table): void {
62+
$table->dropColumn('moderated_at');
63+
$table->dropColumn('moderated_by');
64+
});
65+
}
66+
67+
/**
68+
* Reverse the migrations.
69+
*/
70+
public function down(): void
71+
{
72+
Schema::table('torrents', function (Blueprint $table): void {
73+
$table->unsignedInteger('moderated_by')->nullable()->index()->after('status');
74+
$table->dateTime('moderated_at')->nullable()->after('status');
75+
});
76+
77+
$torrentModerationMessages = DB::table('torrent_moderation_messages')->get();
78+
79+
foreach ($torrentModerationMessages as $message) {
80+
DB::table('torrents')->where('id', $message->torrent_id)->update([
81+
'moderated_by' => $message->moderated_by,
82+
'moderated_at' => $message->created_at,
83+
]);
84+
}
85+
86+
Schema::dropIfExists('torrent_moderation_messages');
87+
}
88+
};

0 commit comments

Comments
 (0)