Files
nexusphp/app/Models/TorrentOperationLog.php

79 lines
2.6 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';
const ACTION_TYPE_EDIT = 'edit';
const ACTION_TYPE_DELETE = 'delete';
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'],
self::ACTION_TYPE_EDIT => ['text' => 'Edit'],
self::ACTION_TYPE_DELETE => ['text' => 'Delete'],
2022-06-14 01:07:05 +08:00
];
2022-08-16 18:31:04 +08:00
public function getActionTypeTextAttribute()
{
return nexus_trans("torrent.operation_log.{$this->action_type}.type_text");
}
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);
}
2022-06-18 23:17:34 +08:00
public static function add(array $params, $notifyUser = false)
2022-06-14 01:07:05 +08:00
{
$log = self::query()->create($params);
2022-06-18 23:17:34 +08:00
if ($notifyUser) {
self::notifyUser($log);
2022-06-14 01:07:05 +08:00
}
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,
2022-06-18 13:16:36 +08:00
'detail_url' => sprintf('details.php?id=%s', $torrentOperationLog->torrent_id),
2022-06-14 01:07:05 +08:00
'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-16 02:40:36 +08:00
NexusDB::cache_del("user_{$receiver->id}_unread_message_count");
2022-06-18 13:16:36 +08:00
NexusDB::cache_del("user_{$receiver->id}_inbox_count");
2022-07-03 14:00:07 +08:00
do_log("notify user: {$receiver->id}, $subject");
2022-06-14 01:07:05 +08:00
}
}