From 00d7dc6c37c016c979817c243c41a1e24cfb94db Mon Sep 17 00:00:00 2001 From: xiaomlove Date: Sat, 18 Jun 2022 23:17:34 +0800 Subject: [PATCH] improve approval notify --- app/Models/TorrentOperationLog.php | 13 +++---------- app/Repositories/TorrentRepository.php | 14 ++++++++++++-- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/app/Models/TorrentOperationLog.php b/app/Models/TorrentOperationLog.php index 62254082..2fecbbbb 100644 --- a/app/Models/TorrentOperationLog.php +++ b/app/Models/TorrentOperationLog.php @@ -33,19 +33,12 @@ class TorrentOperationLog extends NexusModel } - public static function add(array $params) + public static function add(array $params, $notifyUser = false) { $log = self::query()->create($params); - $notifyActionTypes = [ - self::ACTION_TYPE_APPROVAL_ALLOW, - self::ACTION_TYPE_APPROVAL_DENY, - self::ACTION_TYPE_APPROVAL_NONE, - ]; - if (!in_array($params['action_type'], $notifyActionTypes)) { - do_log("actionType: {$params['action_type']}, do not notify"); - return $log; + if ($notifyUser) { + self::notifyUser($log); } - self::notifyUser($log); return $log; } diff --git a/app/Repositories/TorrentRepository.php b/app/Repositories/TorrentRepository.php index 54924e0d..3f7923bb 100644 --- a/app/Repositories/TorrentRepository.php +++ b/app/Repositories/TorrentRepository.php @@ -479,24 +479,34 @@ class TorrentRepository extends BaseRepository } $torrentUpdate = $torrentOperationLog = []; $torrentUpdate['approval_status'] = $params['approval_status']; + $notifyUser = false; if ($params['approval_status'] == Torrent::APPROVAL_STATUS_ALLOW) { $torrentUpdate['banned'] = 'no'; $torrentUpdate['visible'] = 'yes'; if ($torrent->approval_status != $params['approval_status']) { $torrentOperationLog['action_type'] = TorrentOperationLog::ACTION_TYPE_APPROVAL_ALLOW; } + if ($torrent->approval_status == Torrent::APPROVAL_STATUS_DENY) { + $notifyUser = true; + } } elseif ($params['approval_status'] == Torrent::APPROVAL_STATUS_DENY) { $torrentUpdate['banned'] = 'yes'; $torrentUpdate['visible'] = 'no'; if ($torrent->approval_status != $params['approval_status']) { $torrentOperationLog['action_type'] = TorrentOperationLog::ACTION_TYPE_APPROVAL_DENY; } + if ($torrent->approval_status == Torrent::APPROVAL_STATUS_ALLOW) { + $notifyUser = true; + } } elseif ($params['approval_status'] == Torrent::APPROVAL_STATUS_NONE) { $torrentUpdate['banned'] = 'no'; $torrentUpdate['visible'] = 'yes'; if ($torrent->approval_status != $params['approval_status']) { $torrentOperationLog['action_type'] = TorrentOperationLog::ACTION_TYPE_APPROVAL_NONE; } + if ($torrent->approval_status == Torrent::APPROVAL_STATUS_DENY) { + $notifyUser = true; + } } else { throw new \InvalidArgumentException("Invalid approval_status: " . $params['approval_status']); } @@ -506,7 +516,7 @@ class TorrentRepository extends BaseRepository $torrentOperationLog['comment'] = $params['comment'] ?? ''; } - NexusDB::transaction(function () use ($torrent, $torrentOperationLog, $torrentUpdate) { + NexusDB::transaction(function () use ($torrent, $torrentOperationLog, $torrentUpdate, $notifyUser) { $log = "torrent: " . $torrent->id; if (!empty($torrentUpdate)) { $log .= "[UPDATE_TORRENT]: " . nexus_json_encode($torrentUpdate); @@ -514,7 +524,7 @@ class TorrentRepository extends BaseRepository } if (!empty($torrentOperationLog)) { $log .= "[ADD_TORRENT_OPERATION_LOG]: " . nexus_json_encode($torrentOperationLog); - TorrentOperationLog::add($torrentOperationLog); + TorrentOperationLog::add($torrentOperationLog, $notifyUser); } do_log($log); });