Revert "update: announce job"

This reverts commit 2524a978ab.
This commit is contained in:
Roardom
2023-07-28 05:20:31 +00:00
parent 624abb8644
commit fa6ef2a65c
+44 -40
View File
@@ -34,15 +34,22 @@ class ProcessAnnounce implements ShouldQueue
use InteractsWithQueue;
use Queueable;
private Torrent $torrent;
private User $user;
private Group $group;
/** @var \Illuminate\Support\Collection<int, Peer> */
private \Illuminate\Support\Collection $peers;
/**
* Create a new job instance.
*/
public function __construct(
private readonly array $queries,
private readonly array $userArray,
private readonly array $groupArray,
private readonly array $torrentArray,
private readonly array $peersArray
protected array $queries,
protected array $userArray,
protected array $groupArray,
protected array $torrentArray,
protected array $peersArray
) {
}
@@ -60,16 +67,18 @@ class ProcessAnnounce implements ShouldQueue
* @throws \Psr\Container\ContainerExceptionInterface
* @throws \Psr\Container\NotFoundExceptionInterface
* @throws \Psr\SimpleCache\InvalidArgumentException
*
* @property \Illuminate\Support\Collection<int, \App\Models\Peer> $peers
*/
public function handle(): void
{
// We can't pass the models directly into the constructor, otherwise laravel will serialize them
// and upon deserialization will fetch the models from the database again, causing extra unneeded queries.
$torrent = new Torrent($this->torrentArray);
$user = new User($this->userArray);
$group = new Group($this->groupArray);
$this->torrent = new Torrent($this->torrentArray);
$this->user = new User($this->userArray);
$this->group = new Group($this->groupArray);
$peers = collect(array_map(fn ($peer) => new Peer($peer), $this->peersArray));
$this->peers = collect(array_map(fn ($peer) => new Peer($peer), $this->peersArray));
// Flag is tripped if new session is created but client reports up/down > 0
$ghost = false;
@@ -82,9 +91,10 @@ class ProcessAnnounce implements ShouldQueue
$ipAddress = base64_decode($this->queries['ip-address']);
// Get The Current Peer
$peer = $peers
$peer = $this->peers
->where('peer_id', '=', $peerId)
->firstWhere('user_id', '=', $user->id);
->where('user_id', '=', $this->user->id)
->first();
// If no Peer record found then create one
if ($peer === null) {
@@ -99,8 +109,8 @@ class ProcessAnnounce implements ShouldQueue
// Get history information
$history = History::firstOrNew(
[
'torrent_id' => $torrent->id,
'user_id' => $user->id,
'torrent_id' => $this->torrent->id,
'user_id' => $this->user->id,
],
[
'uploaded' => 0,
@@ -129,29 +139,29 @@ class ProcessAnnounce implements ShouldQueue
}
// Modification of Upload and Download (Check cache but in case redis data was lost hit DB)
$personalFreeleech = cache()->has('personal_freeleech:'.$user->id);
$freeleechToken = cache()->get('freeleech_token:'.$user->id.':'.$torrent->id) ??
$personalFreeleech = cache()->has('personal_freeleech:'.$this->user->id);
$freeleechToken = cache()->get('freeleech_token:'.$this->user->id.':'.$this->torrent->id) ??
FreeleechToken::query()
->where('user_id', '=', $user->id)
->where('torrent_id', '=', $torrent->id)
->where('user_id', '=', $this->user->id)
->where('torrent_id', '=', $this->torrent->id)
->exists();
if ($personalFreeleech ||
$group->is_freeleech == 1 ||
$this->group->is_freeleech == 1 ||
$freeleechToken ||
config('other.freeleech') == 1) {
$modDownloaded = 0;
} elseif ($torrent->free >= 1) {
} elseif ($this->torrent->free >= 1) {
// FL value in DB are from 0% to 100%.
// Divide it by 100 and multiply it with "downloaded" to get discount download.
$fl_discount = $downloaded * $torrent->free / 100;
$fl_discount = $downloaded * $this->torrent->free / 100;
$modDownloaded = $downloaded - $fl_discount;
} else {
$modDownloaded = $downloaded;
}
if ($torrent->doubleup == 1 ||
$group->is_double_upload == 1 ||
if ($this->torrent->doubleup == 1 ||
$this->group->is_double_upload == 1 ||
config('other.doubleup') == 1) {
$modUploaded = $uploaded * 2;
} else {
@@ -167,8 +177,8 @@ class ProcessAnnounce implements ShouldQueue
$peer->downloaded = $realDownloaded;
$peer->seeder = $this->queries['left'] == 0;
$peer->left = $this->queries['left'];
$peer->torrent_id = $torrent->id;
$peer->user_id = $user->id;
$peer->torrent_id = $this->torrent->id;
$peer->user_id = $this->user->id;
$peer->updateConnectableStateIfNeeded();
$peer->updated_at = now();
@@ -182,7 +192,7 @@ class ProcessAnnounce implements ShouldQueue
$peer->active = true;
$history->active = 1;
$history->immune = (int) ($history->exists ? $history->immune && $group->is_immune : $group->is_immune);
$history->immune = (int) ($history->exists ? $history->immune && $this->group->is_immune : $this->group->is_immune);
break;
case 'completed':
@@ -204,7 +214,7 @@ class ProcessAnnounce implements ShouldQueue
// User Update
if ($modUploaded > 0 || $modDownloaded > 0) {
$user->update([
$this->user->update([
'uploaded' => DB::raw('uploaded + '.(int) $modUploaded),
'downloaded' => DB::raw('downloaded + '.(int) $modDownloaded),
]);
@@ -212,7 +222,7 @@ class ProcessAnnounce implements ShouldQueue
// End User Update
// Torrent Completed Update
$torrent->times_completed += 1;
$this->torrent->times_completed += 1;
break;
case 'stopped':
@@ -233,7 +243,7 @@ class ProcessAnnounce implements ShouldQueue
// User Update
if ($modUploaded > 0 || $modDownloaded > 0) {
$user->update([
$this->user->update([
'uploaded' => DB::raw('uploaded + '.(int) $modUploaded),
'downloaded' => DB::raw('downloaded + '.(int) $modDownloaded),
]);
@@ -258,7 +268,7 @@ class ProcessAnnounce implements ShouldQueue
// User Update
if ($modUploaded > 0 || $modDownloaded > 0) {
$user->update([
$this->user->update([
'uploaded' => DB::raw('uploaded + '.(int) $modUploaded),
'downloaded' => DB::raw('downloaded + '.(int) $modDownloaded),
]);
@@ -304,24 +314,18 @@ class ProcessAnnounce implements ShouldQueue
]))
]);
$otherSeeders = $peers
$otherSeeders = $this->peers
->where('left', '=', 0)
->where('peer_id', '!=', $peerId)
->count();
$otherLeechers = $peers
$otherLeechers = $this->peers
->where('left', '>', 0)
->where('peer_id', '!=', $peerId)
->count();
$newSeeders = $otherSeeders + (int) ($this->queries['left'] == 0 && strtolower($this->queries['event']) !== 'stopped');
$newLeechers = $otherLeechers + (int) ($this->queries['left'] > 0 && strtolower($this->queries['event']) !== 'stopped');
$this->torrent->seeders = $otherSeeders + (int) ($this->queries['left'] == 0 && strtolower($this->queries['event']) !== 'stopped');
$this->torrent->leechers = $otherLeechers + (int) ($this->queries['left'] > 0 && strtolower($this->queries['event']) !== 'stopped');
if ($torrent->seeders !== $newSeeders || $torrent->leechers !== $newLeechers || $event === 'completed') {
Torrent::whereKey($torrent->id)->update([
'seeders' => $newSeeders,
'leechers' => $newLeechers,
'times_completed' => DB::raw('times_completed + '.(int) ($event === 'completed')),
]);
}
$this->torrent->save();
}
}