approval change to single action

This commit is contained in:
xiaomlove
2022-06-15 15:43:33 +08:00
parent 4fbb279ed3
commit a8ca0ba831
14 changed files with 277 additions and 109 deletions
+10 -5
View File
@@ -15,7 +15,7 @@ class Torrent extends NexusModel
'size', 'added', 'type', 'numfiles', 'owner', 'nfo', 'sp_state', 'promotion_time_type',
'promotion_until', 'anonymous', 'url', 'pos_state', 'cache_stamp', 'picktype', 'picktime',
'last_reseed', 'pt_gen', 'technical_info', 'leechers', 'seeders', 'cover', 'last_action',
'times_completed', 'approval_status'
'times_completed', 'approval_status', 'banned', 'visible',
];
private static $globalPromotionState;
@@ -132,19 +132,19 @@ class Torrent extends NexusModel
const BONUS_REWARD_VALUES = [50, 100, 200, 500, 1000];
const APPROVAL_STATUS_NONE = 0;
const APPROVAL_STATUS_YES = 1;
const APPROVAL_STATUS_NO = 2;
const APPROVAL_STATUS_ALLOW = 1;
const APPROVAL_STATUS_DENY = 2;
public static array $approvalStatus = [
self::APPROVAL_STATUS_NONE => [
'text' => 'None',
'icon' => '<svg t="1655184824967" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="34118" width="16" height="16"><path d="M450.267 772.245l0 92.511 92.511 0 0-92.511L450.267 772.245zM689.448 452.28c13.538-24.367 20.311-50.991 20.311-79.875 0-49.938-19.261-92.516-57.765-127.713-38.517-35.197-90.114-52.8-154.797-52.8-61.077 0-110.191 16.4-147.342 49.188-37.16 32.798-59.497 80.032-67.014 141.703l83.486 9.927c7.218-46.025 22.41-79.875 45.576-101.533 23.166-21.665 52.047-32.494 86.647-32.494 35.802 0 66.038 11.957 90.711 35.874 24.667 23.92 37.01 51.675 37.01 83.266 0 17.451-4.222 33.55-12.642 48.284-8.425 14.747-26.698 34.526-54.83 59.346s-47.607 43.701-58.442 56.637c-14.741 17.754-25.424 35.354-32.037 52.797-9.028 23.172-13.537 50.701-13.537 82.584 0 5.418 0.146 13.539 0.45 24.374l78.069 0c0.599-32.495 2.855-55.966 6.772-70.4 3.903-14.44 9.926-27.229 18.047-38.363 8.127-11.123 25.425-28.43 51.901-51.895C649.43 506.288 675.908 476.656 689.448 452.28L689.448 452.28z" p-id="34119" fill="#e78d0f"></path></svg>',
],
self::APPROVAL_STATUS_YES => [
self::APPROVAL_STATUS_ALLOW => [
'text' => 'Allow',
'icon' => '<svg t="1655145688503" class="icon" viewBox="0 0 1413 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="16225" width="16" height="16"><path d="M1381.807797 107.47394L1274.675044 0.438669 465.281736 809.880718l-322.665524-322.714266L35.434718 594.152982l430.041982 430.041982 107.084012-107.035271-0.243705-0.292446z" fill="#1afa29" p-id="16226"></path></svg>',
],
self::APPROVAL_STATUS_NO => [
self::APPROVAL_STATUS_DENY => [
'text' => 'Deny',
'icon' => '<svg t="1655184952662" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="35029" width="16" height="16"><path d="M220.8 812.8l22.4 22.4 272-272 272 272 48-44.8-275.2-272 275.2-272-48-48-272 275.2-272-275.2-22.4 25.6-22.4 22.4 272 272-272 272z" fill="#d81e06" p-id="35030"></path></svg>',
],
@@ -401,4 +401,9 @@ class Torrent extends NexusModel
{
return $this->hasMany(Reward::class, 'torrentid');
}
public function operationLogs(): \Illuminate\Database\Eloquent\Relations\HasMany
{
return $this->hasMany(TorrentOperationLog::class, 'torrent_id');
}
}
+10 -5
View File
@@ -2,6 +2,8 @@
namespace App\Models;
use Nexus\Database\NexusDB;
class TorrentOperationLog extends NexusModel
{
protected $table = 'torrent_operation_logs';
@@ -10,12 +12,14 @@ class TorrentOperationLog extends NexusModel
protected $fillable = ['uid', 'torrent_id', 'action_type', 'comment'];
const ACTION_TYPE_BAN = 'ban';
const ACTION_TYPE_CANCEL_BAN = 'cancel_ban';
const ACTION_TYPE_APPROVAL_NONE = 'approval_none';
const ACTION_TYPE_APPROVAL_ALLOW = 'approval_allow';
const ACTION_TYPE_APPROVAL_DENY = 'approval_deny';
public static array $actionTypes = [
self::ACTION_TYPE_BAN => ['text' => 'Ban'],
self::ACTION_TYPE_CANCEL_BAN => ['text' => 'Cancel ban'],
self::ACTION_TYPE_APPROVAL_NONE => ['text' => 'Approval none'],
self::ACTION_TYPE_APPROVAL_ALLOW => ['text' => 'Approval allow'],
self::ACTION_TYPE_APPROVAL_DENY => ['text' => 'Approval deny'],
];
public function user()
@@ -32,7 +36,7 @@ class TorrentOperationLog extends NexusModel
public static function add(array $params)
{
$log = self::query()->create($params);
if (!in_array($params['action_type'], [self::ACTION_TYPE_CANCEL_BAN, self::ACTION_TYPE_BAN])) {
if (!in_array($params['action_type'], [self::ACTION_TYPE_APPROVAL_ALLOW, self::ACTION_TYPE_APPROVAL_DENY])) {
do_log("actionType: {$params['action_type']}, do not notify");
return $log;
}
@@ -60,5 +64,6 @@ class TorrentOperationLog extends NexusModel
'added' => now(),
];
Message::query()->insert($message);
NexusDB::cache_del("user_{$receiver->id}_inbox_count");
}
}
+16
View File
@@ -36,4 +36,20 @@ class BaseRepository
}
}
/**
* @param $user
* @param null $fields
* @return User
*/
protected function getUser($user, $fields = null): User
{
if ($user instanceof User) {
return $user;
}
if ($fields === null) {
$fields = User::$commonFields;
}
return User::query()->findOrFail(intval($user), $fields);
}
}
+114
View File
@@ -16,9 +16,11 @@ use App\Models\SearchBox;
use App\Models\Setting;
use App\Models\Snatch;
use App\Models\Source;
use App\Models\StaffMessage;
use App\Models\Standard;
use App\Models\Team;
use App\Models\Torrent;
use App\Models\TorrentOperationLog;
use App\Models\TorrentSecret;
use App\Models\User;
use Carbon\Carbon;
@@ -422,5 +424,117 @@ class TorrentRepository extends BaseRepository
}
public function buildApprovalModal($user, $torrentId)
{
$user = $this->getUser($user);
if ($user->class < Setting::get('authority.torrentmanage')) {
throw new \RuntimeException("No permission !");
}
$torrent = Torrent::query()->findOrFail($torrentId, ['id', 'approval_status', 'banned']);
$radios = [];
foreach (Torrent::$approvalStatus as $key => $value) {
if ($torrent->approval_status == $key) {
$checked = " checked";
} else {
$checked = "";
}
$radios[] = sprintf(
'<label><input type="radio" name="params[approval_status]" value="%s"%s>%s</label>',
$key, $checked, nexus_trans("torrent.approval.status_text.$key")
);
}
$id = "torrent-approval";
$rows = [];
$rowStyle = "display: flex; padding: 10px; align-items: center";
$labelStyle = "width: 80px";
$formId = "$id-form";
$rows[] = sprintf(
'<div class="%s-row" style="%s"><div style="%s">%s: </div><div>%s</div></div>',
$id, $rowStyle, $labelStyle,nexus_trans('torrent.approval.status_label'), implode('', $radios)
);
$rows[] = sprintf(
'<div class="%s-row" style="%s"><div style="%s">%s: </div><div><textarea name="params[comment]" rows="4" cols="40"></textarea></div></div>',
$id, $rowStyle, $labelStyle, nexus_trans('torrent.approval.comment_label')
);
$rows[] = sprintf('<input type="hidden" name="params[torrent_id]" value="%s" />', $torrent->id);
$html = sprintf('<div id="%s-box" style="padding: 15px 30px"><form id="%s">%s</form></div>', $id, $formId, implode('', $rows));
return [
'id' => $id,
'form_id' => $formId,
'title' => nexus_trans('torrent.approval.modal_title'),
'content' => $html,
];
}
public function approval($user, array $params): array
{
$user = $this->getUser($user);
$torrent = Torrent::query()->findOrFail($params['torrent_id'], ['id', 'banned', 'approval_status', 'visible', 'owner']);
if ($torrent->approval_status == $params['approval_status']) {
//No change
return $params;
}
$torrentUpdate = $torrentOperationLog = $staffMsg = [];
$torrentUpdate['approval_status'] = $params['approval_status'];
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;
}
} 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;
}
} elseif ($params['approval_status'] == Torrent::APPROVAL_STATUS_NONE) {
if ($torrent->approval_status != $params['approval_status']) {
$torrentOperationLog['action_type'] = TorrentOperationLog::ACTION_TYPE_APPROVAL_NONE;
}
} else {
throw new \InvalidArgumentException("Invalid approval_status: " . $params['approval_status']);
}
if (isset($torrentOperationLog['action_type'])) {
$torrentOperationLog['uid'] = $user->id;
$torrentOperationLog['torrent_id'] = $torrent->id;
$torrentOperationLog['comment'] = $params['comment'] ?? '';
}
if ($torrent->banned == 'yes' && $torrent->owner == $user->id) {
$torrentUrl = sprintf('%s/details.php?id=%s', getSchemeAndHttpHost(), $torrent->id);
$staffMsg = [
'sender' => $user->id,
'subject' => nexus_trans('torrent.owner_update_torrent_subject', ['detail_url' => $torrentUrl, 'torrent_name' => $_POST['name']]),
'msg' => nexus_trans('torrent.owner_update_torrent_msg', ['detail_url' => $torrentUrl, 'torrent_name' => $_POST['name']]),
'added' => now(),
];
}
NexusDB::transaction(function () use ($torrent, $torrentOperationLog, $torrentUpdate, $staffMsg) {
$log = "";
if (!empty($torrentUpdate)) {
$log .= "[UPDATE_TORRENT]: " . nexus_json_encode($torrentUpdate);
$torrent->update($torrentUpdate);
}
if (!empty($torrentOperationLog)) {
$log .= "[ADD_TORRENT_OPERATION_LOG]: " . nexus_json_encode($torrentOperationLog);
TorrentOperationLog::add($torrentOperationLog);
}
if (!empty($staffMsg)) {
$log .= "[INSERT_STAFF_MESSAGE]: " . nexus_json_encode($staffMsg);
StaffMessage::query()->insert($staffMsg);
NexusDB::cache_del('staff_new_message_count');
}
do_log($log);
});
return $params;
}
}