add model event

This commit is contained in:
xiaomlove
2024-10-10 21:04:35 +08:00
parent 24936277db
commit 90c9cd48ac
7 changed files with 70 additions and 5 deletions

View File

@@ -91,3 +91,4 @@ MEILISEARCH_MASTER_KEY=
CACHE_KEY_AGENT_ALLOW=all_agent_allows
CACHE_KEY_AGENT_DENY=all_agent_denies
CHANNEL_NAME_SETTING=channel_setting
CHANNEL_NAME_MODEL_EVENT=channel_model_event

View File

@@ -6,6 +6,7 @@ use App\Events\NewsCreated;
use App\Events\TorrentCreated;
use App\Events\TorrentDeleted;
use App\Events\TorrentUpdated;
use App\Events\UserCreated;
use App\Events\UserDestroyed;
use App\Events\UserDisabled;
use App\Events\UserEnabled;
@@ -31,15 +32,18 @@ class FireEvent extends Command
*
* @var string
*/
protected $description = 'Fire a event, options: --name, --idKey --idKeyOld';
protected $description = 'Fire an event, options: --name, --idKey --idKeyOld';
protected array $eventMaps = [
"torrent_created" => ['event' => TorrentCreated::class, 'model' => Torrent::class],
"torrent_updated" => ['event' => TorrentUpdated::class, 'model' => Torrent::class],
"torrent_deleted" => ['event' => TorrentDeleted::class, 'model' => Torrent::class],
"user_created" => ['event' => UserCreated::class, 'model' => User::class],
"user_destroyed" => ['event' => UserDestroyed::class, 'model' => User::class],
"user_disabled" => ['event' => UserDisabled::class, 'model' => User::class],
"user_enabled" => ['event' => UserEnabled::class, 'model' => User::class],
"news_created" => ['event' => NewsCreated::class, 'model' => News::class],
];
@@ -69,6 +73,7 @@ class FireEvent extends Command
}
$result = call_user_func_array([$eventName, "dispatch"], $params);
$log .= ", success call dispatch, result: " . var_export($result, true);
publish_model_event($name, $model->id);
} else {
$log .= ", invalid argument to call, it should be instance of: " . Model::class;
}

View File

@@ -0,0 +1,39 @@
<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class UserCreated
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public ?Model $model = null;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(Model $model)
{
$this->model = $model;
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('channel-name');
}
}

View File

@@ -179,7 +179,8 @@ class User extends Authenticatable implements FilamentUser, HasName
protected $fillable = [
'username', 'email', 'passhash', 'secret', 'stylesheet', 'editsecret', 'added', 'modcomment', 'enabled', 'status',
'leechwarn', 'leechwarnuntil', 'page', 'class', 'uploaded', 'downloaded', 'clientselect', 'showclienterror', 'last_home',
'seedbonus', 'bonuscomment', 'downloadpos', 'vip_added', 'vip_until', 'title', 'invites', 'attendance_card', 'seed_points_per_hour'
'seedbonus', 'bonuscomment', 'downloadpos', 'vip_added', 'vip_until', 'title', 'invites', 'attendance_card',
'seed_points_per_hour', 'passkey',
];
/**

View File

@@ -132,7 +132,8 @@ class UserRepository extends BaseRepository
'stylesheet' => $setting['defstylesheet'],
'added' => now()->toDateTimeString(),
'status' => User::STATUS_CONFIRMED,
'class' => $class
'class' => $class,
'passkey' => md5($username.date("Y-m-d H:i:s").$passhash)
];
$user = new User($data);
if (!empty($params['id'])) {
@@ -143,7 +144,7 @@ class UserRepository extends BaseRepository
$user->id = $params['id'];
}
$user->save();
fire_event("user_created", $user);
return $user;
}

View File

@@ -1247,6 +1247,9 @@ function get_snatch_info($torrentId, $userId)
return mysql_fetch_assoc(sql_query(sprintf('select * from snatched where torrentid = %s and userid = %s order by id desc limit 1', $torrentId, $userId)));
}
/**
* 完整的 Laravel 事件, 在 php 端有监听者的需要触发. 同样会执行 publish_model_event()
*/
function fire_event(string $name, \Illuminate\Database\Eloquent\Model $model, \Illuminate\Database\Eloquent\Model $oldModel = null): void
{
$prefix = "fire_event:";
@@ -1259,3 +1262,16 @@ function fire_event(string $name, \Illuminate\Database\Eloquent\Model $model, \I
}
executeCommand("event:fire --name=$name --idKey=$idKey --idKeyOld=$idKeyOld", "string", true, false);
}
/**
* 仅仅是往 redis 发布事件, php 端无监听者仅在其他平台有需要的触发这个即可, 较轻量
*/
function publish_model_event(string $event, int $id): void
{
$channel = nexus_env("CHANNEL_NAME_MODEL_EVENT");
if (!empty($channel)) {
\Nexus\Database\NexusDB::redis()->publish($channel, json_encode(["event" => $event, "id" => $id]));
} else {
do_log("event: $event, id: $id, channel: $channel, channel is empty!", "error");
}
}

View File

@@ -151,6 +151,7 @@ $secret = mksecret();
$wantpasshash = md5($secret . $wantpassword . $secret);
$editsecret = ($verification == 'admin' ? '' : $secret);
$invite_count = (int) $invite_count;
$passkey = md5($wantusername.date("Y-m-d H:i:s").$wantpasshash);
$wantusername = sqlesc($wantusername);
$wantpasshash = sqlesc($wantpasshash);
@@ -167,8 +168,9 @@ $res_check_user = sql_query("SELECT * FROM users WHERE username = " . $wantusern
if(mysql_num_rows($res_check_user) == 1)
bark($lang_takesignup['std_username_exists']);
$ret = sql_query("INSERT INTO users (username, passhash, secret, editsecret, email, country, gender, status, class, invites, ".($type == 'invite' ? "invited_by," : "")." added, last_access, lang, stylesheet".($showschool == 'yes' ? ", school" : "").", uploaded) VALUES (" . $wantusername . "," . $wantpasshash . "," . $secret . "," . $editsecret . "," . $email . "," . $country . "," . $gender . ", 'pending', ".$defaultclass_class.",". $invite_count .", ".($type == 'invite' ? "'$inviter'," : "") ." '". date("Y-m-d H:i:s") ."' , " . " '". date("Y-m-d H:i:s") ."' , ".$sitelangid . ",".$defcss.($showschool == 'yes' ? ",".$school : "").",".($iniupload_main > 0 ? $iniupload_main : 0).")") or sqlerr(__FILE__, __LINE__);
$ret = sql_query("INSERT INTO users (username, passhash, passkey, secret, editsecret, email, country, gender, status, class, invites, ".($type == 'invite' ? "invited_by," : "")." added, last_access, lang, stylesheet".($showschool == 'yes' ? ", school" : "").", uploaded) VALUES (" . $wantusername . "," . $wantpasshash . "," . sqlesc($passkey) . "," . $secret . "," . $editsecret . "," . $email . "," . $country . "," . $gender . ", 'pending', ".$defaultclass_class.",". $invite_count .", ".($type == 'invite' ? "'$inviter'," : "") ." '". date("Y-m-d H:i:s") ."' , " . " '". date("Y-m-d H:i:s") ."' , ".$sitelangid . ",".$defcss.($showschool == 'yes' ? ",".$school : "").",".($iniupload_main > 0 ? $iniupload_main : 0).")") or sqlerr(__FILE__, __LINE__);
$id = mysql_insert_id();
fire_event("user_created", \App\Models\User::query()->first($id, \App\Models\User::$commonFields));
$tmpInviteCount = get_setting('main.tmp_invite_count');
if ($tmpInviteCount > 0) {
$userRep = new \App\Repositories\UserRepository();