From 5890ebc7496b2db9b6ffef057a2ceddbbf1cab6c Mon Sep 17 00:00:00 2001 From: Roardom Date: Wed, 21 Jun 2023 04:10:58 +0000 Subject: [PATCH] refactor: miscellaneous staff controller refactors --- app/Http/Controllers/Staff/BanController.php | 6 +++--- .../Controllers/Staff/ChatBotController.php | 12 ++++++------ .../Controllers/Staff/ChatRoomController.php | 9 ++------- .../Controllers/Staff/ChatStatusController.php | 6 +++--- .../Staff/CheatedTorrentController.php | 8 ++++++-- app/Http/Controllers/Staff/ForumController.php | 17 ++++------------- .../Controllers/Staff/MassActionController.php | 17 +++++++++-------- .../Controllers/Staff/ModerationController.php | 10 ++++------ app/Http/Controllers/Staff/RssController.php | 3 +-- .../Controllers/Staff/VersionController.php | 13 +------------ 10 files changed, 39 insertions(+), 62 deletions(-) diff --git a/app/Http/Controllers/Staff/BanController.php b/app/Http/Controllers/Staff/BanController.php index b28234cb5..e5c7b3d07 100644 --- a/app/Http/Controllers/Staff/BanController.php +++ b/app/Http/Controllers/Staff/BanController.php @@ -51,7 +51,7 @@ class BanController extends Controller $staff = $request->user(); $bannedGroup = cache()->rememberForever('banned_group', fn () => Group::where('slug', '=', 'banned')->pluck('id')); - // \abort_if($user->group->is_modo || $request->user()->id == $user->id, 403); + abort_if($user->group->is_modo || $staff->id === $user->id, 403); $user->update([ 'group_id' => $bannedGroup[0], @@ -70,9 +70,9 @@ class BanController extends Controller ]); cache()->forget('user:'.$user->passkey); + Unit3dAnnounce::addUser($user); - // Send Notifications $user->notify(new UserBan($ban)); return to_route('users.show', ['username' => $username]) @@ -107,9 +107,9 @@ class BanController extends Controller ]); cache()->forget('user:'.$user->passkey); + Unit3dAnnounce::addUser($user); - // Send Notifications $user->notify(new UserBanExpire()); return to_route('users.show', ['username' => $user->username]) diff --git a/app/Http/Controllers/Staff/ChatBotController.php b/app/Http/Controllers/Staff/ChatBotController.php index 4879d621b..5e21a1ae0 100644 --- a/app/Http/Controllers/Staff/ChatBotController.php +++ b/app/Http/Controllers/Staff/ChatBotController.php @@ -74,9 +74,9 @@ class ChatBotController extends Controller */ public function disable(int $id): \Illuminate\Http\RedirectResponse { - $bot = Bot::findOrFail($id); - $bot->active = 0; - $bot->save(); + Bot::findOrFail($id)->update([ + 'active' => 0, + ]); return to_route('staff.bots.index') ->withSuccess('The Bot Has Been Disabled'); @@ -87,9 +87,9 @@ class ChatBotController extends Controller */ public function enable(int $id): \Illuminate\Http\RedirectResponse { - $bot = Bot::findOrFail($id); - $bot->active = 1; - $bot->save(); + Bot::findOrFail($id)->update([ + 'active' => 1, + ]); return to_route('staff.bots.index') ->withSuccess('The Bot Has Been Enabled'); diff --git a/app/Http/Controllers/Staff/ChatRoomController.php b/app/Http/Controllers/Staff/ChatRoomController.php index 746561f56..dcb8858f1 100644 --- a/app/Http/Controllers/Staff/ChatRoomController.php +++ b/app/Http/Controllers/Staff/ChatRoomController.php @@ -90,16 +90,11 @@ class ChatRoomController extends Controller */ public function destroy(int $id): \Illuminate\Http\RedirectResponse { - $chatroom = Chatroom::findOrFail($id); - $users = User::where('chatroom_id', '=', $id)->get(); $default = Chatroom::where('name', '=', config('chat.system_chatroom'))->pluck('id'); - foreach ($users as $user) { - $user->chatroom_id = $default[0]; - $user->save(); - } + User::where('chatroom_id', '=', $id)->update(['chatroom_id' => $default[0]]); - $chatroom->delete(); + Chatroom::findOrFail($id)->delete(); return to_route('staff.rooms.index') ->withSuccess('Chatroom Successfully Deleted'); diff --git a/app/Http/Controllers/Staff/ChatStatusController.php b/app/Http/Controllers/Staff/ChatStatusController.php index 9870a7193..17fd61d93 100644 --- a/app/Http/Controllers/Staff/ChatStatusController.php +++ b/app/Http/Controllers/Staff/ChatStatusController.php @@ -66,9 +66,9 @@ class ChatStatusController extends Controller */ public function edit(int $id): \Illuminate\Contracts\View\Factory|\Illuminate\View\View { - $chatstatus = ChatStatus::findOrFail($id); - - return view('Staff.chat.status.edit', ['chatstatus' => $chatstatus]); + return view('Staff.chat.status.edit', [ + 'chatstatus' => ChatStatus::findOrFail($id), + ]); } /** diff --git a/app/Http/Controllers/Staff/CheatedTorrentController.php b/app/Http/Controllers/Staff/CheatedTorrentController.php index 3338f7335..2c4f5937d 100644 --- a/app/Http/Controllers/Staff/CheatedTorrentController.php +++ b/app/Http/Controllers/Staff/CheatedTorrentController.php @@ -66,7 +66,9 @@ class CheatedTorrentController extends Controller */ public function destroy(int $id): \Illuminate\Http\RedirectResponse { - Torrent::findOrFail($id)->update(['balance_offset' => DB::raw('balance * -1')]); + Torrent::findOrFail($id)->update([ + 'balance_offset' => DB::raw('balance * -1'), + ]); return to_route('staff.cheated_torrents.index') ->withSuccess('Balance successfully reset'); @@ -77,7 +79,9 @@ class CheatedTorrentController extends Controller */ public function massDestroy(): \Illuminate\Http\RedirectResponse { - Torrent::query()->update(['balance_offset' => DB::raw('balance * -1')]); + Torrent::query()->update([ + 'balance_offset' => DB::raw('balance * -1'), + ]); return to_route('staff.cheated_torrents.index') ->withSuccess('All balances successfully reset'); diff --git a/app/Http/Controllers/Staff/ForumController.php b/app/Http/Controllers/Staff/ForumController.php index e92369ce1..216d0eb4f 100644 --- a/app/Http/Controllers/Staff/ForumController.php +++ b/app/Http/Controllers/Staff/ForumController.php @@ -167,28 +167,19 @@ class ForumController extends Controller if ($forum->parent_id == 0) { $category = $forum; - Permission::where('forum_id', '=', $category->id)->delete(); foreach ($category->getForumsInCategory() as $forum) { Permission::where('forum_id', '=', $forum->id)->delete(); - foreach ($forum->topics as $topic) { - $topic->posts()->delete(); - $topic->delete(); - } - + $forum->posts()->delete(); + $forum->topics()->delete(); $forum->delete(); } $category->delete(); } else { - Permission::where('forum_id', '=', $forum->id)->delete(); - - foreach ($forum->topics as $topic) { - $topic->posts()->delete(); - $topic->delete(); - } - + $forum->posts()->delete(); + $forum->topics()->delete(); $forum->delete(); } diff --git a/app/Http/Controllers/Staff/MassActionController.php b/app/Http/Controllers/Staff/MassActionController.php index 2fd134830..587830034 100644 --- a/app/Http/Controllers/Staff/MassActionController.php +++ b/app/Http/Controllers/Staff/MassActionController.php @@ -71,14 +71,15 @@ class MassActionController extends Controller $memberGroup = cache()->rememberForever('member_group', fn () => Group::where('slug', '=', 'user')->pluck('id')); foreach (User::where('group_id', '=', $validatingGroup[0])->get() as $user) { - $user->group_id = $memberGroup[0]; - $user->active = 1; - $user->can_upload = 1; - $user->can_download = 1; - $user->can_request = 1; - $user->can_comment = 1; - $user->can_invite = 1; - $user->save(); + $user->update([ + 'group_id' => $memberGroup[0], + 'active' => 1, + 'can_upload' => 1, + 'can_download' => 1, + 'can_request' => 1, + 'can_comment' => 1, + 'can_invite' => 1, + ]); Unit3dAnnounce::addUser($user); } diff --git a/app/Http/Controllers/Staff/ModerationController.php b/app/Http/Controllers/Staff/ModerationController.php index cb76da28e..e9dc4d954 100644 --- a/app/Http/Controllers/Staff/ModerationController.php +++ b/app/Http/Controllers/Staff/ModerationController.php @@ -53,13 +53,13 @@ class ModerationController extends Controller { $torrent = Torrent::withAnyStatus()->with('user')->findOrFail($id); - if ((int) $request->old_status !== $torrent->status) { + 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 ((int) $request->status === $torrent->status) { + if ($request->integer('status') === $torrent->status) { return to_route('torrent', ['id' => $id]) ->withInput() ->withErrors( @@ -77,16 +77,14 @@ class ModerationController extends Controller switch ($request->status) { case 1: // Approve - $appurl = config('app.url'); - // Announce To Shoutbox if ($torrent->anon === 0) { $this->chatRepository->systemMessage( - sprintf('User [url=%s/users/', $appurl).$torrent->user->username.']'.$torrent->user->username.sprintf('[/url] has uploaded a new '.$torrent->category->name.'. [url=%s/torrents/', $appurl).$id.']'.$torrent->name.'[/url], grab it now! :slight_smile:' + 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/', $appurl).$id.']'.$torrent->name.'[/url], grab it now! :slight_smile:' + 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:' ); } diff --git a/app/Http/Controllers/Staff/RssController.php b/app/Http/Controllers/Staff/RssController.php index f9a2c2d54..9d52d4f92 100644 --- a/app/Http/Controllers/Staff/RssController.php +++ b/app/Http/Controllers/Staff/RssController.php @@ -32,10 +32,9 @@ class RssController extends Controller /** * Display a listing of the RSS resource. */ - public function index($hash = null): \Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View + public function index(): \Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View { return view('Staff.rss.index', [ - 'hash' => $hash, 'public_rss' => Rss::where('is_private', '=', 0)->oldest('position')->get(), ]); } diff --git a/app/Http/Controllers/Staff/VersionController.php b/app/Http/Controllers/Staff/VersionController.php index 072eaddd1..55eec2db0 100644 --- a/app/Http/Controllers/Staff/VersionController.php +++ b/app/Http/Controllers/Staff/VersionController.php @@ -15,32 +15,21 @@ namespace App\Http\Controllers\Staff; use App\Http\Controllers\Controller; use Illuminate\Support\Facades\Http; -use JsonException; /** * @see \Tests\Todo\Feature\Http\Controllers\Staff\VersionControllerTest */ class VersionController extends Controller { - private readonly mixed $versionController; - - public function __construct() - { - $this->versionController = config('unit3d.version'); - } - /** * Check the latest release of UNIT3D and compare them to the local version. - * - * @throws \GuzzleHttp\Exception\GuzzleException - * @throws JsonException */ public function checkVersion(): \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response { $latestVersion = Http::get('//api.github.com/repos/HDInnovations/UNIT3D/releases')[0]['tag_name']; return response([ - 'updated' => ! version_compare($this->versionController, $latestVersion, '<'), + 'updated' => ! version_compare(config('unit3d.version'), $latestVersion, '<'), 'latestversion' => $latestVersion, ]); }