* @license https://www.gnu.org/licenses/agpl-3.0.en.html/ GNU Affero General Public License v3.0 */ namespace App\Http\Controllers\Staff; use App\Helpers\TorrentHelper; use App\Http\Controllers\Controller; use App\Http\Requests\Staff\UpdateModerationRequest; use App\Models\PrivateMessage; use App\Models\Torrent; use App\Repositories\ChatRepository; use App\Services\Unit3dAnnounce; /** * @see \Tests\Todo\Feature\Http\Controllers\Staff\ModerationControllerTest */ class ModerationController extends Controller { /** * ModerationController Constructor. */ public function __construct(private readonly ChatRepository $chatRepository) { } /** * Torrent Moderation Panel. */ public function index(): \Illuminate\Contracts\View\Factory|\Illuminate\View\View { return view('Staff.moderation.index', [ 'current' => now(), 'pending' => Torrent::with(['user:id,username,group_id', 'user.group', 'category', 'type'])->pending()->get(), 'postponed' => Torrent::with(['user:id,username,group_id', 'user.group', 'category', 'type'])->postponed()->get(), 'rejected' => Torrent::with(['user:id,username,group_id', 'user.group', 'category', 'type'])->rejected()->get(), ]); } /** * Update a torrent's moderation status. */ public function update(UpdateModerationRequest $request, int $id): \Illuminate\Http\RedirectResponse { $torrent = Torrent::withAnyStatus()->with('user')->findOrFail($id); if ($request->integer('old_status') !== $torrent->status) { return to_route('torrent', ['id' => $id]) ->withInput() ->withErrors('Torrent has already been moderated since this page was loaded.'); } if ($request->integer('status') === $torrent->status) { return to_route('torrent', ['id' => $id]) ->withInput() ->withErrors( match ($torrent->status) { 0 => 'Torrent already pending.', 1 => 'Torrent already approved.', 2 => 'Torrent already rejected.', 3 => 'Torrent already postponed.', default => 'Invalid moderation status.' } ); } $staff = auth()->user(); switch ($request->status) { case 1: // Approve // Announce To Shoutbox if ($torrent->anon === 0) { $this->chatRepository->systemMessage( sprintf('User [url=%s/users/', config('app.url')).$torrent->user->username.']'.$torrent->user->username.sprintf('[/url] has uploaded a new '.$torrent->category->name.'. [url=%s/torrents/', config('app.url')).$id.']'.$torrent->name.'[/url], grab it now! :slight_smile:' ); } else { $this->chatRepository->systemMessage( sprintf('An anonymous user has uploaded a new '.$torrent->category->name.'. [url=%s/torrents/', config('app.url')).$id.']'.$torrent->name.'[/url], grab it now! :slight_smile:' ); } TorrentHelper::approveHelper($id); return to_route('staff.moderation.index') ->withSuccess('Torrent Approved'); case 2: // Reject $torrent->markRejected(); PrivateMessage::create([ 'sender_id' => $staff->id, 'receiver_id' => $torrent->user_id, 'subject' => 'Your upload, '.$torrent->name.' ,has been rejected by '.$staff->username, 'message' => "Greetings, \n\nYour upload ".$torrent->name." has been rejected. Please see below the message from the staff member.\n\n".$request->message, ]); Unit3dAnnounce::addTorrent($torrent); return to_route('staff.moderation.index') ->withSuccess('Torrent Rejected'); case 3: // Postpone $torrent->markPostponed(); PrivateMessage::create([ 'sender_id' => $staff->id, 'receiver_id' => $torrent->user_id, 'subject' => 'Your upload, '.$torrent->name.' ,has been postponed by '.$staff->username, 'message' => "Greetings, \n\nYour upload, ".$torrent->name." ,has been postponed. Please see below the message from the staff member.\n\n".$request->message, ]); Unit3dAnnounce::addTorrent($torrent); return to_route('staff.moderation.index') ->withSuccess('Torrent Postponed'); default: // Undefined status return to_route('torrent', ['id' => $id]) ->withErrors('Invalid moderation status.'); } } }