Skip to content

Commit de21453

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

15 files changed

+527
-62
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

+17-10
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;
@@ -400,18 +401,24 @@ public function store(StoreTorrentRequest $request): \Illuminate\Http\RedirectRe
400401
file_put_contents(getcwd().'/files/torrents/'.$fileName, Bencode::bencode($decodedTorrent));
401402

402403
$torrent = Torrent::create([
403-
'mediainfo' => TorrentTools::anonymizeMediainfo($request->filled('mediainfo') ? $request->string('mediainfo') : null),
404-
'info_hash' => Bencode::get_infohash($decodedTorrent),
405-
'file_name' => $fileName,
406-
'num_file' => $meta['count'],
407-
'folder' => Bencode::get_name($decodedTorrent),
408-
'size' => $meta['size'],
409-
'nfo' => $request->hasFile('nfo') ? TorrentTools::getNfo($request->file('nfo')) : '',
410-
'user_id' => $user->id,
411-
'moderated_at' => now(),
412-
'moderated_by' => User::SYSTEM_USER_ID,
404+
'mediainfo' => TorrentTools::anonymizeMediainfo($request->filled('mediainfo') ? $request->string('mediainfo') : null),
405+
'info_hash' => Bencode::get_infohash($decodedTorrent),
406+
'file_name' => $fileName,
407+
'num_file' => $meta['count'],
408+
'folder' => Bencode::get_name($decodedTorrent),
409+
'size' => $meta['size'],
410+
'nfo' => $request->hasFile('nfo') ? TorrentTools::getNfo($request->file('nfo')) : '',
411+
'user_id' => $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

+14-20
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,21 +97,20 @@ 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
{
106104
return [
107-
'tmdb' => 'int',
108-
'igdb' => 'int',
109-
'bumped_at' => 'datetime',
110-
'fl_until' => 'datetime',
111-
'du_until' => 'datetime',
112-
'doubleup' => 'bool',
113-
'refundable' => 'bool',
114-
'featured' => 'bool',
115-
'moderated_at' => 'datetime',
116-
'sticky' => 'bool',
105+
'tmdb' => 'int',
106+
'igdb' => 'int',
107+
'bumped_at' => 'datetime',
108+
'fl_until' => 'datetime',
109+
'du_until' => 'datetime',
110+
'doubleup' => 'bool',
111+
'refundable' => 'bool',
112+
'featured' => 'bool',
113+
'sticky' => 'bool',
117114
];
118115
}
119116

@@ -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+
}

0 commit comments

Comments
 (0)