Skip to content

Commit

Permalink
Merge pull request #4070 from Roardom/offload-console-to-db
Browse files Browse the repository at this point in the history
  • Loading branch information
HDVinnie authored Aug 31, 2024
2 parents 798633f + a7a72c1 commit a5e1814
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 182 deletions.
52 changes: 32 additions & 20 deletions app/Console/Commands/AutoDeactivateWarning.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down
41 changes: 20 additions & 21 deletions app/Console/Commands/AutoDisableInactiveUsers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down
42 changes: 15 additions & 27 deletions app/Console/Commands/AutoPreWarning.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
43 changes: 21 additions & 22 deletions app/Console/Commands/AutoRecycleClaimedTorrentRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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');
}
Expand Down
97 changes: 47 additions & 50 deletions app/Console/Commands/AutoRewardResurrection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
}
Expand Down
Loading

0 comments on commit a5e1814

Please sign in to comment.