From edadbf00481f4009c50e982bb012cb1566316c00 Mon Sep 17 00:00:00 2001 From: xiaomlove <1939737565@qq.com> Date: Tue, 17 Dec 2024 13:12:58 +0800 Subject: [PATCH] delete snatched check user and torrent --- app/Filament/Resources/Torrent/TorrentResource.php | 11 ++++++++++- app/Repositories/UserRepository.php | 10 +++++++--- include/functions.php | 3 ++- public/announce.php | 4 ++-- 4 files changed, 21 insertions(+), 7 deletions(-) diff --git a/app/Filament/Resources/Torrent/TorrentResource.php b/app/Filament/Resources/Torrent/TorrentResource.php index b237c04b..4fa5049d 100644 --- a/app/Filament/Resources/Torrent/TorrentResource.php +++ b/app/Filament/Resources/Torrent/TorrentResource.php @@ -16,6 +16,7 @@ use App\Repositories\TorrentRepository; use Filament\Facades\Filament; use Filament\Forms; +use Filament\Pages\Actions\Action; use Filament\Resources\Form; use Filament\Resources\Resource; use Filament\Resources\Table; @@ -113,7 +114,8 @@ public static function table(Table $table): Table ->defaultSort('id', 'desc') ->filters(self::getFilters()) ->actions(self::getActions()) - ->bulkActions(self::getBulkActions()); + ->bulkActions(self::getBulkActions()) + ; } @@ -339,6 +341,13 @@ private static function getActions(): array $actions[] = Tables\Actions\DeleteAction::make('delete')->using(function ($record) { deletetorrent($record->id); }); +// $actions[] = Tables\Actions\Action::make('view') +// ->action(function (Torrent $record) { +// return [ +// 'modelContent' => new HtmlString("ssss") +// ]; +// }) +// ; } return $actions; } diff --git a/app/Repositories/UserRepository.php b/app/Repositories/UserRepository.php index 74a5493a..0614de11 100644 --- a/app/Repositories/UserRepository.php +++ b/app/Repositories/UserRepository.php @@ -10,6 +10,7 @@ use App\Models\LoginLog; use App\Models\Message; use App\Models\Setting; +use App\Models\Snatch; use App\Models\User; use App\Models\UserBanLog; use App\Models\UserMeta; @@ -636,9 +637,10 @@ public function destroy(Collection|int $id, $reasonKey = 'user.destroy_by_admin' } else { $uidArr = $id->pluck('id')->toArray(); } + $uidStr = implode(',', $uidArr); $users = User::query()->with('language')->whereIn('id', $uidArr)->get(); - if (empty($uidArr)) { - return; + if ($users->isEmpty()) { + return true; } $tables = [ 'users' => 'id', @@ -655,7 +657,7 @@ public function destroy(Collection|int $id, $reasonKey = 'user.destroy_by_admin' 'oauth_auth_codes' => 'user_id', ]; foreach ($tables as $table => $key) { - \Nexus\Database\NexusDB::table($table)->whereIn($key, $uidArr)->delete(); + NexusDB::statement(sprintf("delete from `%s` where `%s` in (%s)", $table, $key, $uidStr)); } do_log("[DESTROY_USER]: " . json_encode($uidArr), 'error'); $userBanLogs = []; @@ -667,6 +669,8 @@ public function destroy(Collection|int $id, $reasonKey = 'user.destroy_by_admin' ]; } UserBanLog::query()->insert($userBanLogs); + //delete by user, make sure torrent is deleted + NexusDB::statement(sprintf('DELETE FROM snatched WHERE userid IN (%s) and not exists (select 1 from torrents where id = snatched.torrentid)', $uidStr)); if (is_int($id)) { do_action("user_delete", $id); fire_event("user_destroyed", $users->first()); diff --git a/include/functions.php b/include/functions.php index 22bbe1bb..a787626e 100644 --- a/include/functions.php +++ b/include/functions.php @@ -3126,7 +3126,8 @@ function deletetorrent($id, $notify = false) { $idStr = implode(', ', $idArr ?: [0]); $torrent_dir = get_setting('main.torrent_dir'); \Nexus\Database\NexusDB::statement("DELETE FROM torrents WHERE id in ($idStr)"); - \Nexus\Database\NexusDB::statement("DELETE FROM snatched WHERE torrentid in ($idStr)"); + //delete by torrent, make sure user is deleted + \Nexus\Database\NexusDB::statement("DELETE FROM snatched WHERE torrentid in ($idStr) and not exists (select 1 from users where id = snatched.userid)"); foreach(array("peers", "files", "comments") as $x) { \Nexus\Database\NexusDB::statement("DELETE FROM $x WHERE torrent in ($idStr)"); } diff --git a/public/announce.php b/public/announce.php index f341bba8..12d99a42 100644 --- a/public/announce.php +++ b/public/announce.php @@ -616,8 +616,8 @@ if ($snatchInfo['downloaded'] >= $requiredDownloaded) { $nowStr = date('Y-m-d H:i:s'); $sql = sprintf( - "insert into hit_and_runs (uid, torrent_id, snatched_id, created_at, updated_at) values (%d, %d, %d, '%s', '%s') on duplicate key update created_at = '%s', updated_at = '%s'", - $userid, $torrentid, $snatchInfo['id'], $nowStr, $nowStr, $nowStr, $nowStr + "insert into hit_and_runs (uid, torrent_id, snatched_id, created_at, updated_at) values (%d, %d, %d, '%s', '%s') on duplicate key update updated_at = '%s'", + $userid, $torrentid, $snatchInfo['id'], $nowStr, $nowStr, $nowStr ); $affectedRows = sql_query($sql); do_log("$hrLog, total downloaded: {$snatchInfo['downloaded']} >= required: $requiredDownloaded, [INSERT_H&R], sql: $sql, affectedRows: $affectedRows");