From a7a72c18072a5d99796f920f9992835906fb489f Mon Sep 17 00:00:00 2001 From: Roardom Date: Sun, 18 Aug 2024 09:42:45 +0000 Subject: [PATCH] update: only select necessary database records for console commands --- .../Commands/AutoDeactivateWarning.php | 52 ++++++---- .../Commands/AutoDisableInactiveUsers.php | 41 ++++---- app/Console/Commands/AutoPreWarning.php | 42 +++----- .../AutoRecycleClaimedTorrentRequests.php | 43 ++++---- .../Commands/AutoRewardResurrection.php | 97 +++++++++---------- app/Console/Commands/AutoWarning.php | 83 ++++++++-------- 6 files changed, 176 insertions(+), 182 deletions(-) diff --git a/app/Console/Commands/AutoDeactivateWarning.php b/app/Console/Commands/AutoDeactivateWarning.php index 1d40c72d8b..335334cffd 100644 --- a/app/Console/Commands/AutoDeactivateWarning.php +++ b/app/Console/Commands/AutoDeactivateWarning.php @@ -54,35 +54,47 @@ final public function handle(): void ->where('active', '=', 1) ->get(); - foreach ($warnings as $warning) { - if ($warning->expires_on <= $current || ($warning->torrenttitle && $warning->torrenttitle->history()->where('user_id', '=', $warning->warneduser->id)->first()->seedtime >= config('hitrun.seedtime'))) { - // Set Records Active To 0 in warnings table - $warning->update(['active' => false]); + Warning::query() + ->where('active', '=', true) + ->where( + fn ($query) => $query + ->where('expires_on', '<=', $current) + ->orWhereHas( + 'torrenttitle.history', + fn ($query) => $query + ->whereColumn('history.user_id', '=', 'warnings.user_id') + ->where('history.seedtime', '>=', config('hitrun.seedtime')) + ) + ) + ->chunkById(100, function ($warnings): void { + foreach ($warnings as $warning) { + // Set Records Active To 0 in warnings table + $warning->update(['active' => false]); - // Send Notifications - if ($warning->torrenttitle) { - $warning->warneduser->notify(new UserWarningExpire($warning->warneduser, $warning->torrenttitle)); - } else { - $warning->warneduser->notify(new UserManualWarningExpire($warning->warneduser, $warning)); + // Send Notifications + if ($warning->torrenttitle) { + $warning->warneduser->notify(new UserWarningExpire($warning->warneduser, $warning->torrenttitle)); + } else { + $warning->warneduser->notify(new UserManualWarningExpire($warning->warneduser, $warning)); + } } - } - } + }); // Calculate User Warning Count and Enable DL Priv If Required. - $warnings = Warning::with('warneduser') + Warning::with('warneduser') ->select(DB::raw('user_id, SUM(active = 1) as value')) ->groupBy('user_id') ->having('value', '<', config('hitrun.max_warnings')) - ->get(); + ->whereRelation('warneduser', 'can_download', '=', false) + ->chunkById(100, function ($warnings): void { + foreach ($warnings as $warning) { + $warning->warneduser->update(['can_download' => 1]); - foreach ($warnings as $warning) { - if ($warning->warneduser->can_download === false) { - $warning->warneduser->update(['can_download' => 1]); + cache()->forget('user:'.$warning->warneduser->passkey); - cache()->forget('user:'.$warning->warneduser->passkey); - Unit3dAnnounce::addUser($warning->warneduser); - } - } + Unit3dAnnounce::addUser($warning->warneduser); + } + }, 'user_id'); $this->comment('Automated Warning Deativation Command Complete'); } diff --git a/app/Console/Commands/AutoDisableInactiveUsers.php b/app/Console/Commands/AutoDisableInactiveUsers.php index 96fc99c3dc..8bf2886b08 100644 --- a/app/Console/Commands/AutoDisableInactiveUsers.php +++ b/app/Console/Commands/AutoDisableInactiveUsers.php @@ -56,28 +56,27 @@ final public function handle(): void $current = Carbon::now(); - $matches = User::whereIntegerInRaw('group_id', config('pruning.group_ids'))->get(); - - $users = $matches->where('created_at', '<', $current->copy()->subDays(config('pruning.account_age'))) + User::query() + ->whereIntegerInRaw('group_id', config('pruning.group_ids')) + ->where('created_at', '<', $current->copy()->subDays(config('pruning.account_age'))) ->where('last_login', '<', $current->copy()->subDays(config('pruning.last_login'))) - ->all(); - - foreach ($users as $user) { - if ($user->seedingTorrents()->doesntExist()) { - $user->update([ - 'group_id' => $disabledGroup[0], - 'can_download' => false, - 'disabled_at' => Carbon::now(), - ]); - - cache()->forget('user:'.$user->passkey); - - Unit3dAnnounce::addUser($user); - - // Send Email - dispatch(new SendDisableUserMail($user)); - } - } + ->whereDoesntHave('seedingTorrents') + ->chunk(100, function ($users) use ($disabledGroup): void { + foreach ($users as $user) { + $user->update([ + 'group_id' => $disabledGroup[0], + 'can_download' => false, + 'disabled_at' => Carbon::now(), + ]); + + cache()->forget('user:'.$user->passkey); + + Unit3dAnnounce::addUser($user); + + // Send Email + dispatch(new SendDisableUserMail($user)); + } + }); $this->comment('Automated User Disable Command Complete'); } diff --git a/app/Console/Commands/AutoPreWarning.php b/app/Console/Commands/AutoPreWarning.php index e6f4c1b448..179da58f17 100644 --- a/app/Console/Commands/AutoPreWarning.php +++ b/app/Console/Commands/AutoPreWarning.php @@ -17,9 +17,7 @@ namespace App\Console\Commands; use App\Models\History; -use App\Models\Warning; use App\Notifications\UserPreWarning; -use Carbon\Carbon; use Illuminate\Console\Command; use Exception; use Illuminate\Support\Facades\DB; @@ -52,43 +50,33 @@ final public function handle(): void return; } - $carbon = new Carbon(); - $prewarn = History::with(['user', 'torrent']) + $prewarn = History::with(['user']) ->whereNull('prewarned_at') ->where('hitrun', '=', 0) ->where('immune', '=', 0) ->where('actual_downloaded', '>', 0) ->where('active', '=', 0) ->where('seedtime', '<=', config('hitrun.seedtime')) - ->where('updated_at', '<', $carbon->copy()->subDays(config('hitrun.prewarn'))) + ->where('updated_at', '<', now()->subDays(config('hitrun.prewarn'))) + ->has('torrent') + ->whereRelation('user.group', 'is_immune', '=', false) + ->whereHas('torrent', fn ($query) => $query->whereRaw('history.actual_downloaded > torrents.size * ?', [config('hitrun.buffer') / 100])) + ->whereDoesntHave('user.warnings', fn ($query) => $query->withTrashed()->whereColumn('warnings.torrent', '=', 'history.torrent_id')) ->get(); $usersWithPreWarnings = []; foreach ($prewarn as $pre) { - if (null === $pre->torrent) { - continue; - } + History::query() + ->where('torrent_id', '=', $pre->torrent_id) + ->where('user_id', '=', $pre->user_id) + ->update([ + 'prewarned_at' => now(), + 'updated_at' => DB::raw('updated_at'), + ]); - if (!$pre->user->group->is_immune && $pre->actual_downloaded > ($pre->torrent->size * (config('hitrun.buffer') / 100))) { - $exsist = Warning::withTrashed() - ->where('torrent', '=', $pre->torrent->id) - ->where('user_id', '=', $pre->user->id) - ->first(); - - if ($exsist === null) { - History::query() - ->where('torrent_id', '=', $pre->torrent_id) - ->where('user_id', '=', $pre->user_id) - ->update([ - 'prewarned_at' => now(), - 'updated_at' => DB::raw('updated_at'), - ]); - - // Add user to usersWithWarnings array - $usersWithPreWarnings[$pre->user->id] = $pre->user; - } - } + // Add user to usersWithWarnings array + $usersWithPreWarnings[$pre->user_id] = $pre->user; } // Send a single notification for each user with warnings diff --git a/app/Console/Commands/AutoRecycleClaimedTorrentRequests.php b/app/Console/Commands/AutoRecycleClaimedTorrentRequests.php index 6bb46ba106..e0b3ad9650 100644 --- a/app/Console/Commands/AutoRecycleClaimedTorrentRequests.php +++ b/app/Console/Commands/AutoRecycleClaimedTorrentRequests.php @@ -16,11 +16,9 @@ namespace App\Console\Commands; -use App\Models\TorrentRequest; use App\Models\TorrentRequestClaim; use App\Repositories\ChatRepository; use Illuminate\Console\Command; -use Illuminate\Support\Carbon; use Exception; use Throwable; @@ -55,28 +53,29 @@ public function __construct(private readonly ChatRepository $chatRepository) */ final public function handle(): void { - $current = Carbon::now(); - $torrentRequests = TorrentRequest::where('claimed', '=', 1) - ->whereNull('filled_by') - ->whereNull('filled_when') - ->whereNull('torrent_id') - ->get(); + TorrentRequestClaim::query() + ->with('request') + ->where('created_at', '<', now()->subDays(7)) + ->whereHas( + 'request', + fn ($query) => $query + ->where('claimed', '=', true) + ->whereNull('filled_by') + ->whereNull('filled_when') + ->whereNull('torrent_id') + ) + ->chunkById(100, function ($claims): void { + foreach ($claims as $claim) { + $trUrl = href_request($claim->request); - foreach ($torrentRequests as $torrentRequest) { - $requestClaim = TorrentRequestClaim::where('request_id', '=', $torrentRequest->id) - ->where('created_at', '<', $current->copy()->subDays(7)) - ->first(); + $this->chatRepository->systemMessage( + \sprintf('[url=%s]%s[/url] claim has been reset due to not being filled within 7 days.', $trUrl, $claim->request->name) + ); - if ($requestClaim) { - $trUrl = href_request($torrentRequest); - $this->chatRepository->systemMessage( - \sprintf('[url=%s]%s[/url] claim has been reset due to not being filled within 7 days.', $trUrl, $torrentRequest->name) - ); - - $requestClaim->delete(); - $torrentRequest->update(['claimed' => null]); - } - } + $claim->request->update(['claimed' => null]); + $claim->delete(); + } + }); $this->comment('Automated Request Claim Reset Command Complete'); } diff --git a/app/Console/Commands/AutoRewardResurrection.php b/app/Console/Commands/AutoRewardResurrection.php index 0129c07750..14e7044a5e 100644 --- a/app/Console/Commands/AutoRewardResurrection.php +++ b/app/Console/Commands/AutoRewardResurrection.php @@ -17,9 +17,7 @@ namespace App\Console\Commands; use App\Models\Resurrection; -use App\Models\History; use App\Models\Torrent; -use App\Models\User; use App\Repositories\ChatRepository; use App\Services\Unit3dAnnounce; use Exception; @@ -58,54 +56,53 @@ public function __construct(private readonly ChatRepository $chatRepository) */ final public function handle(): void { - foreach (Resurrection::where('rewarded', '!=', 1)->oldest()->get() as $resurrection) { - $user = User::find($resurrection->user_id); - - $torrent = Torrent::find($resurrection->torrent_id); - - if (isset($user, $torrent)) { - $history = History::where('torrent_id', '=', $torrent->id) - ->where('user_id', '=', $user->id) - ->where('seedtime', '>=', $resurrection->seedtime) - ->first(); - } - - if (isset($user, $torrent, $history)) { - $resurrection->update(['rewarded' => true]); - - $user->increment('fl_tokens', (int) config('graveyard.reward')); - - // Auto Shout - $appurl = config('app.url'); - - $this->chatRepository->systemMessage( - \sprintf('Ladies and Gents, [url=%s/users/%s]%s[/url] has successfully resurrected [url=%s/torrents/%s]%s[/url].', $appurl, $user->username, $user->username, $appurl, $torrent->id, $torrent->name) - ); - - // Bump Torrent With FL - $torrentUrl = href_torrent($torrent); - - $torrent->update([ - 'bumped_at' => Carbon::now(), - 'free' => 100, - 'fl_until' => Carbon::now()->addDays(3), - ]); - - $this->chatRepository->systemMessage( - \sprintf('Ladies and Gents, [url=%s]%s[/url] has been granted 100%% FreeLeech for 3 days and has been bumped to the top.', $torrentUrl, $torrent->name) - ); - - cache()->forget('announce-torrents:by-infohash:'.$torrent->info_hash); - - Unit3dAnnounce::addTorrent($torrent); - - // Send Private Message - $user->sendSystemNotification( - subject: 'Successful Graveyard Resurrection', - message: \sprintf('You have successfully resurrected [url=%s/torrents/', $appurl).$torrent->id.']'.$torrent->name.'[/url] ! Thank you for bringing a torrent back from the dead! Enjoy the freeleech tokens!', - ); - } - } + Resurrection::query() + ->with(['torrent', 'user']) + ->where('rewarded', '=', false) + ->has('user') + ->whereHas( + 'torrent.history', + fn ($query) => $query + ->whereColumn('resurrections.user_id', '=', 'history.user_id') + ->whereColumn('history.seedtime', '>=', 'resurrections.seedtime') + ) + ->chunk(100, function ($resurrections): void { + foreach ($resurrections as $resurrection) { + $resurrection->update(['rewarded' => true]); + + $resurrection->user->increment('fl_tokens', (int) config('graveyard.reward')); + + // Auto Shout + $appurl = config('app.url'); + + $this->chatRepository->systemMessage( + \sprintf('Ladies and Gents, [url=%s/users/%s]%s[/url] has successfully resurrected [url=%s/torrents/%s]%s[/url].', $appurl, $resurrection->user->username, $resurrection->user->username, $appurl, $resurrection->torrent->id, $resurrection->torrent->name) + ); + + // Bump Torrent With FL + $torrentUrl = href_torrent($resurrection->torrent); + + $resurrection->torrent->update([ + 'bumped_at' => Carbon::now(), + 'free' => 100, + 'fl_until' => Carbon::now()->addDays(3), + ]); + + $this->chatRepository->systemMessage( + \sprintf('Ladies and Gents, [url=%s]%s[/url] has been granted 100%% FreeLeech for 3 days and has been bumped to the top.', $torrentUrl, $resurrection->torrent->name) + ); + + cache()->forget('announce-torrents:by-infohash:'.$resurrection->torrent->info_hash); + + Unit3dAnnounce::addTorrent($resurrection->torrent); + + // Send Private Message + $resurrection->user->sendSystemNotification( + subject: 'Successful Graveyard Resurrection', + message: \sprintf('You have successfully resurrected [url=%s/torrents/', $appurl).$resurrection->torrent->id.']'.$resurrection->torrent->name.'[/url] ! Thank you for bringing a torrent back from the dead! Enjoy the freeleech tokens!', + ); + } + }); $this->comment('Automated Reward Resurrections Command Complete'); } diff --git a/app/Console/Commands/AutoWarning.php b/app/Console/Commands/AutoWarning.php index b1973551e7..6180187057 100644 --- a/app/Console/Commands/AutoWarning.php +++ b/app/Console/Commands/AutoWarning.php @@ -63,43 +63,36 @@ final public function handle(): void ->where('active', '=', 0) ->where('seedtime', '<', config('hitrun.seedtime')) ->where('updated_at', '<', $carbon->copy()->subDays(config('hitrun.grace'))) + ->whereRelation('user.group', 'is_immune', '=', false) + ->whereHas('torrent', fn ($query) => $query->whereRaw('history.actual_downloaded > torrents.size * ?', [config('hitrun.buffer') / 100])) + ->whereDoesntHave('user.warnings', fn ($query) => $query->withTrashed()->whereColumn('warnings.torrent', '=', 'history.torrent_id')) ->get(); $usersWithWarnings = []; foreach ($hitrun as $hr) { - if (!$hr->user->group->is_immune && $hr->actual_downloaded > ($hr->torrent->size * (config('hitrun.buffer') / 100))) { - $exsist = Warning::withTrashed() - ->where('torrent', '=', $hr->torrent->id) - ->where('user_id', '=', $hr->user->id) - ->first(); - - // Insert Warning Into Warnings Table if doesnt already exsist - if ($exsist === null) { - $warning = Warning::create([ - 'user_id' => $hr->user->id, - 'warned_by' => User::SYSTEM_USER_ID, - 'torrent' => $hr->torrent->id, - 'reason' => \sprintf('Hit and Run Warning For Torrent %s', $hr->torrent->name), - 'expires_on' => $carbon->copy()->addDays(config('hitrun.expire')), - 'active' => true, - ]); - - History::query() - ->where('torrent_id', '=', $hr->torrent_id) - ->where('user_id', '=', $hr->user_id) - ->update([ - 'hitrun' => true, - 'updated_at' => DB::raw('updated_at'), - ]); - - // Add +1 To Users Warnings Count In Users Table - $hr->user->increment('hitandruns'); - - // Add user to usersWithWarnings array - $usersWithWarnings[$hr->user->id] = $hr->user; - } - } + Warning::create([ + 'user_id' => $hr->user->id, + 'warned_by' => User::SYSTEM_USER_ID, + 'torrent' => $hr->torrent->id, + 'reason' => \sprintf('Hit and Run Warning For Torrent %s', $hr->torrent->name), + 'expires_on' => $carbon->copy()->addDays(config('hitrun.expire')), + 'active' => true, + ]); + + History::query() + ->where('torrent_id', '=', $hr->torrent_id) + ->where('user_id', '=', $hr->user_id) + ->update([ + 'hitrun' => true, + 'updated_at' => DB::raw('updated_at'), + ]); + + // Add +1 To Users Warnings Count In Users Table + $hr->user->increment('hitandruns'); + + // Add user to usersWithWarnings array + $usersWithWarnings[$hr->user->id] = $hr->user; } // Send a single notification for each user with warnings @@ -108,16 +101,22 @@ final public function handle(): void } // Calculate User Warning Count and Disable DL Priv If Required. - $warnings = Warning::with('warneduser')->select(DB::raw('user_id, count(*) as value'))->where('active', '=', 1)->groupBy('user_id')->having('value', '>=', config('hitrun.max_warnings'))->get(); - - foreach ($warnings as $warning) { - if ($warning->warneduser->can_download) { - $warning->warneduser->update(['can_download' => 0]); - - cache()->forget('user:'.$warning->warneduser->passkey); - Unit3dAnnounce::addUser($warning->warneduser); - } - } + Warning::query() + ->with('warneduser') + ->select(DB::raw('user_id, count(*) as value')) + ->where('active', '=', 1) + ->groupBy('user_id') + ->having('value', '>=', config('hitrun.max_warnings')) + ->whereRelation('warneduser', 'can_download', '=', true) + ->chunkById(100, function ($warnings): void { + foreach ($warnings as $warning) { + $warning->warneduser->update(['can_download' => 0]); + + cache()->forget('user:'.$warning->warneduser->passkey); + + Unit3dAnnounce::addUser($warning->warneduser); + } + }, 'user_id'); $this->comment('Automated User Warning Command Complete'); }