Files
nexusphp/app/Models/TorrentOperationLog.php

75 lines
2.4 KiB
PHP
Raw Normal View History

2022-06-14 01:07:05 +08:00
<?php
namespace App\Models;
2022-06-15 15:43:33 +08:00
use Nexus\Database\NexusDB;
2022-06-14 01:07:05 +08:00
class TorrentOperationLog extends NexusModel
{
protected $table = 'torrent_operation_logs';
public $timestamps = true;
protected $fillable = ['uid', 'torrent_id', 'action_type', 'comment'];
2022-06-15 15:43:33 +08:00
const ACTION_TYPE_APPROVAL_NONE = 'approval_none';
const ACTION_TYPE_APPROVAL_ALLOW = 'approval_allow';
const ACTION_TYPE_APPROVAL_DENY = 'approval_deny';
2022-06-14 01:07:05 +08:00
public static array $actionTypes = [
2022-06-15 15:43:33 +08:00
self::ACTION_TYPE_APPROVAL_NONE => ['text' => 'Approval none'],
self::ACTION_TYPE_APPROVAL_ALLOW => ['text' => 'Approval allow'],
self::ACTION_TYPE_APPROVAL_DENY => ['text' => 'Approval deny'],
2022-06-14 01:07:05 +08:00
];
public function user()
{
return $this->belongsTo(User::class, 'uid')->select(User::$commonFields);
}
public function torrent()
{
return $this->belongsTo(Torrent::class, 'torrent_id')->select(Torrent::$commentFields);
}
public static function add(array $params)
{
$log = self::query()->create($params);
2022-06-16 02:29:11 +08:00
$notifyActionTypes = [
self::ACTION_TYPE_APPROVAL_ALLOW,
self::ACTION_TYPE_APPROVAL_DENY,
self::ACTION_TYPE_APPROVAL_NONE,
];
if (!in_array($params['action_type'], $notifyActionTypes)) {
2022-06-14 01:07:05 +08:00
do_log("actionType: {$params['action_type']}, do not notify");
return $log;
}
self::notifyUser($log);
return $log;
}
private static function notifyUser(self $torrentOperationLog)
{
$actionType = $torrentOperationLog->action_type;
$receiver = $torrentOperationLog->torrent->user;
$locale = $receiver->locale;
$subject = nexus_trans("torrent.operation_log.$actionType.notify_subject", [], $locale);
$msg = nexus_trans("torrent.operation_log.$actionType.notify_msg", [
'torrent_name' => $torrentOperationLog->torrent->name,
'detail_url' => sprintf('%s/details.php?id=%s', getSchemeAndHttpHost(), $torrentOperationLog->torrent_id),
'operator' => $torrentOperationLog->user->username,
'reason' => $torrentOperationLog->comment,
], $locale);
$message = [
'sender' => 0,
'receiver' => $receiver->id,
'subject' => $subject,
'msg' => $msg,
'added' => now(),
];
Message::query()->insert($message);
2022-06-15 15:43:33 +08:00
NexusDB::cache_del("user_{$receiver->id}_inbox_count");
2022-06-14 01:07:05 +08:00
}
}