ban/unban torrent send notification

This commit is contained in:
xiaomlove
2022-06-14 01:07:05 +08:00
parent 4e20c339d9
commit bcaaa020b1
18 changed files with 283 additions and 49 deletions
+7 -6
View File
@@ -15,6 +15,7 @@ use App\Models\SearchBox;
use App\Models\Snatch;
use App\Models\Tag;
use App\Models\Torrent;
use App\Models\TorrentOperationLog;
use App\Models\User;
use App\Repositories\AgentAllowRepository;
use App\Repositories\AttendanceRepository;
@@ -77,12 +78,12 @@ class Test extends Command
*/
public function handle()
{
$a = [];
if ($a) {
echo 'Bad';
} else {
echo 'OK';
}
$arr = [
'uid' => 1,
'torrent_id' => 1,
'action_type' => 'ban',
];
TorrentOperationLog::query()->create($arr);
}
+27
View File
@@ -0,0 +1,27 @@
<?php
namespace App\Models;
class StaffMessage extends NexusModel
{
protected $table = 'staffmessages';
protected $fillable = [
'sender', 'added', 'subject', 'msg', 'answeredby', 'answered', 'answer'
];
protected $casts = [
'added' => 'datetime',
];
public function send_user()
{
return $this->belongsTo(User::class, 'sender')->withDefault(['id' => 0, 'username' => 'System']);
}
public function answer_user()
{
return $this->belongsTo(User::class, 'answeredby');
}
}
+64
View File
@@ -0,0 +1,64 @@
<?php
namespace App\Models;
class TorrentOperationLog extends NexusModel
{
protected $table = 'torrent_operation_logs';
public $timestamps = true;
protected $fillable = ['uid', 'torrent_id', 'action_type', 'comment'];
const ACTION_TYPE_BAN = 'ban';
const ACTION_TYPE_CANCEL_BAN = 'cancel_ban';
public static array $actionTypes = [
self::ACTION_TYPE_BAN => ['text' => 'Ban'],
self::ACTION_TYPE_CANCEL_BAN => ['text' => 'Cancel ban'],
];
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);
if (!in_array($params['action_type'], [self::ACTION_TYPE_CANCEL_BAN, self::ACTION_TYPE_BAN])) {
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);
}
}