diff --git a/app/Repositories/ClaimRepository.php b/app/Repositories/ClaimRepository.php index 99e23bd9..0884971f 100644 --- a/app/Repositories/ClaimRepository.php +++ b/app/Repositories/ClaimRepository.php @@ -3,6 +3,7 @@ namespace App\Repositories; use App\Models\Claim; use App\Models\Message; +use App\Models\Peer; use App\Models\Snatch; use App\Models\Torrent; use App\Models\User; @@ -26,6 +27,20 @@ class ClaimRepository extends BaseRepository } public function store($uid, $torrentId) + { + $snatch = $this->canBeClaimByUser($torrentId, $uid); + $insert = [ + 'uid' => $uid, + 'torrent_id' => $torrentId, + 'snatched_id' => $snatch->id, + 'seed_time_begin' => $snatch->seedtime, + 'uploaded_begin' => $snatch->uploaded, + ]; + $this->clearStatsCache($uid); + return Claim::query()->create($insert); + } + + public function canBeClaimByUser(int $torrentId, int $uid) { $isEnabled = Claim::getConfigIsEnabled(); if (!$isEnabled) { @@ -35,6 +50,15 @@ class ClaimRepository extends BaseRepository if ($exists) { throw new \RuntimeException(nexus_trans("torrent.claim_already")); } + $snatch = Snatch::query()->where('userid', $uid)->where('torrentid', $torrentId)->first(); + if (!$snatch) { + throw new \RuntimeException(nexus_trans("torrent.no_snatch")); + } + $claimTorrentTTL = Claim::getConfigTorrentTTL(); + $torrent = Torrent::query()->findOrFail($torrentId, Torrent::$commentFields); + if ($torrent->added->addDays($claimTorrentTTL)->gte(Carbon::now())) { + throw new \RuntimeException(nexus_trans("torrent.can_no_be_claimed_yet")); + } if (!apply_filter('user_has_role_work_seeding', false, $uid)) { $max = Claim::getConfigTorrentUpLimit(); $count = Claim::query()->where('uid', $uid)->count(); @@ -47,24 +71,7 @@ class ClaimRepository extends BaseRepository throw new \RuntimeException(nexus_trans("torrent.claim_number_reach_user_maximum")); } } - $snatch = Snatch::query()->where('userid', $uid)->where('torrentid', $torrentId)->first(); - if (!$snatch) { - throw new \RuntimeException(nexus_trans("torrent.no_snatch")); - } - $claimTorrentTTL = Claim::getConfigTorrentTTL(); - $torrent = Torrent::query()->findOrFail($torrentId, Torrent::$commentFields); - if ($torrent->added->addDays($claimTorrentTTL)->gte(Carbon::now())) { - throw new \RuntimeException(nexus_trans("torrent.can_no_be_claimed_yet")); - } - $insert = [ - 'uid' => $uid, - 'torrent_id' => $torrentId, - 'snatched_id' => $snatch->id, - 'seed_time_begin' => $snatch->seedtime, - 'uploaded_begin' => $snatch->uploaded, - ]; - $this->clearStatsCache($uid); - return Claim::query()->create($insert); + return $snatch; } public function update(array $params, $id) @@ -336,4 +343,47 @@ class ClaimRepository extends BaseRepository } } + + public function claimAllSeeding(int $uid) + { + $page = 1; + $size = 1000; + $total = 0; + while (true) { + $peers = Peer::query() + ->where('uid', $uid) + ->where('seeder', 'yes') + ->where('to_go', 0) + ->groupBy('torrentid') + ->forPage($page, $size) + ->get(['torrentid']); + if ($peers->isEmpty()) { + break; + } + $torrentIdArr = $peers->pluck('torrentid')->toArray(); + $snatches = Snatch::query() + ->whereIn('torrentid', $torrentIdArr) + ->where('userid', $uid) + ->groupBy('torrentid') + ->get(['id', 'torrentid', 'seedtime', 'uploaded']); + if ($snatches->isNotEmpty()) { + $values = []; + $nowStr = now()->toDateTimeString(); + $sql = "insert into claims (uid, torrent_id, snatched_id, seed_time_begin, uploaded_begin, created_at, uploaded_at)"; + foreach ($snatches as $snatch) { + $values[] = sprintf( + "(%s, %s, %s, %s, %s, '%s', '%s')", + $uid, $snatch->torrentid, $snatch->id, $snatch->seedtime, $snatch->uploaded, $nowStr, $nowStr + ); + } + $sql .= sprintf(" values %s on duplicate key update updated_at = '%s'", implode(', ', $values), $nowStr); + NexusDB::statement($sql); + } + $count = $snatches->count(); + $total += $count; + do_log("page: $page, insert rows count: " . $count); + $page++; + } + return $total; + } } diff --git a/include/constants.php b/include/constants.php index 07e1d5d5..9a3f5585 100644 --- a/include/constants.php +++ b/include/constants.php @@ -1,6 +1,6 @@ checkTrackerReportAuthKey($_REQUEST['authkey']); + $decrypted = $torrentRep->checkTrackerReportAuthKey($authkey); if (empty($decrypted)) { throw new \InvalidArgumentException("Invalid authkey: $authkey"); } diff --git a/public/scrape.php b/public/scrape.php index 13d382db..2dc70b69 100644 --- a/public/scrape.php +++ b/public/scrape.php @@ -1,16 +1,5 @@ $torrent_details]; +\Nexus\Database\NexusDB::cache_put($cacheKey, $d, 900); benc_resp($d);