deleted event param use array instead of model

This commit is contained in:
xiaomlove
2025-05-16 22:01:12 +07:00
parent 45c41047da
commit 1381390c8c
8 changed files with 47 additions and 31 deletions
+14 -13
View File
@@ -15,7 +15,7 @@ class FireEvent extends Command
* *
* @var string * @var string
*/ */
protected $signature = 'event:fire {--name=} {--idKey=} {--idKeyOld=""}'; protected $signature = 'event:fire {--name=} {--idKey=} {--idKeyOld=}';
/** /**
* The console command description. * The console command description.
@@ -35,25 +35,26 @@ class FireEvent extends Command
$idKey = $this->option('idKey'); $idKey = $this->option('idKey');
$idKeyOld = $this->option('idKeyOld'); $idKeyOld = $this->option('idKeyOld');
$log = "FireEvent, name: $name, idKey: $idKey, idKeyOld: $idKeyOld"; $log = "FireEvent, name: $name, idKey: $idKey, idKeyOld: $idKeyOld";
$this->info("$log, begin ...");
if (isset(ModelEventEnum::$eventMaps[$name])) { if (isset(ModelEventEnum::$eventMaps[$name])) {
$eventName = ModelEventEnum::$eventMaps[$name]['event']; $eventName = ModelEventEnum::$eventMaps[$name]['event'];
$model = unserialize(NexusDB::cache_get($idKey)); $modelClassName = ModelEventEnum::$eventMaps[$name]['model'];
if ($model instanceof Model) { $modelBasic = new $modelClassName();
$params = [$model]; $modelData = unserialize(NexusDB::cache_get($idKey));
$useArray = str_ends_with($name, '_deleted');
$model = call_user_func_array([$modelBasic, "newInstance"], [$modelData, true]);
//由于 id 不属于 fillable,初始化新对象时是没有值的
$model->id = $modelData['id'];
$params = [$useArray ? $modelData: $model];
if ($idKeyOld) { if ($idKeyOld) {
$modelOld = unserialize(NexusDB::cache_get($idKeyOld)); $modelOldData = unserialize(NexusDB::cache_get($idKeyOld));
if ($modelOld instanceof Model) { $modelOld = call_user_func_array([$modelBasic, "newInstance"], [$modelOldData, true]);
$params[] = $modelOld; $modelOld->id = $modelOldData['id'];
} else { $params[] = $useArray ? $modelOldData: $modelOld;
$log .= ", invalid idKeyOld";
}
} }
$result = call_user_func_array([$eventName, "dispatch"], $params); $result = call_user_func_array([$eventName, "dispatch"], $params);
$log .= ", success call dispatch, result: " . var_export($result, true); $log .= ", success call dispatch, result: " . var_export($result, true);
publish_model_event($name, $model->id); publish_model_event($name, $model->id);
} else {
$log .= ", invalid argument to call, it should be instance of: " . Model::class;
}
} else { } else {
$log .= ", no event match this name"; $log .= ", no event match this name";
} }
+3 -3
View File
@@ -15,16 +15,16 @@ class TorrentDeleted
{ {
use Dispatchable, InteractsWithSockets, SerializesModels; use Dispatchable, InteractsWithSockets, SerializesModels;
public ?Model $model = null; public ?array $data = null;
/** /**
* Create a new event instance. * Create a new event instance.
* *
* @return void * @return void
*/ */
public function __construct(Model $model) public function __construct(array $data)
{ {
$this->model = $model; $this->data = $data;
} }
/** /**
+3 -3
View File
@@ -15,16 +15,16 @@ class UserDeleted
{ {
use Dispatchable, InteractsWithSockets, SerializesModels; use Dispatchable, InteractsWithSockets, SerializesModels;
public ?Model $model = null; public ?array $data = null;
/** /**
* Create a new event instance. * Create a new event instance.
* *
* @return void * @return void
*/ */
public function __construct(Model $model) public function __construct(array $data)
{ {
$this->model = $model; $this->data = $data;
} }
/** /**
@@ -28,7 +28,7 @@ class DeductUserBonusWhenTorrentDeleted implements ShouldQueue
/** /**
* Just a test * Just a test
*/ */
$torrent = $event->model; $torrent = $event->data;
do_log(sprintf("torrent: %d is deleted, and it's pieces_hash is: %s", $torrent->id, $torrent->pieces_hash)); do_log(sprintf("torrent: %d is deleted, and it's pieces_hash is: %s", $torrent['id'], $torrent['pieces_hash']));
} }
} }
+4
View File
@@ -31,6 +31,10 @@ class Torrent extends NexusModel
'last_action' => 'datetime', 'last_action' => 'datetime',
]; ];
protected $hidden = [
'info_hash',
];
public static $commentFields = [ public static $commentFields = [
'id', 'name', 'added', 'visible', 'banned', 'owner', 'sp_state', 'promotion_time_type', 'promotion_until', 'pos_state', 'id', 'name', 'added', 'visible', 'banned', 'owner', 'sp_state', 'promotion_time_type', 'promotion_until', 'pos_state',
'hr', 'picktype', 'picktime', 'last_action', 'leechers', 'seeders', 'times_completed', 'views', 'size', 'cover', 'anonymous', 'hr', 'picktype', 'picktime', 'last_action', 'leechers', 'seeders', 'times_completed', 'views', 'size', 'cover', 'anonymous',
+3
View File
@@ -16,6 +16,9 @@ class TorrentExtra extends NexusModel
{ {
return Attribute::make( return Attribute::make(
get: function ($value) { get: function ($value) {
if (is_null($value)) {
return null;
}
$jsonDecoded = json_decode($value, true); $jsonDecoded = json_decode($value, true);
if (is_array($jsonDecoded)) { if (is_array($jsonDecoded)) {
return $jsonDecoded; return $jsonDecoded;
+1 -1
View File
@@ -6136,7 +6136,7 @@ function build_search_box_category_table($mode, $checkboxValue, $categoryHrefPre
//Category //Category
$html .= sprintf('<tr><td class="embedded" align="left">%s</td></tr>', nexus_trans('label.search_box.category')); $html .= sprintf('<tr><td class="embedded" align="left">%s</td></tr>', nexus_trans('label.search_box.category'));
/** @var \Illuminate\DataBase\Eloquent\Collection $categoryCollection */ /** @var \Illuminate\DataBase\Eloquent\Collection $categoryCollection */
$categoryCollection = $searchBox->categories()->orderBy('sort_index', 'desc')->get(); $categoryCollection = $searchBox->categories()->with('icon')->orderBy('sort_index', 'desc')->get();
if (!empty($options['select_unselect'])) { if (!empty($options['select_unselect'])) {
$categoryCollection->push(new \App\Models\Category(['mode' => -1])); $categoryCollection->push(new \App\Models\Category(['mode' => -1]));
} }
+10 -2
View File
@@ -1340,18 +1340,26 @@ function fire_event(string $name, \Illuminate\Database\Eloquent\Model $model, ?\
$prefix = "fire_event:"; $prefix = "fire_event:";
$idKey = $prefix . \Illuminate\Support\Str::random(); $idKey = $prefix . \Illuminate\Support\Str::random();
$idKeyOld = ""; $idKeyOld = "";
\Nexus\Database\NexusDB::cache_put($idKey, serialize($model), 3600*24*30); \Nexus\Database\NexusDB::cache_put($idKey, serialize($model->toArray()), 3600*24*30);
if ($oldModel) { if ($oldModel) {
$idKeyOld = $prefix . \Illuminate\Support\Str::random(); $idKeyOld = $prefix . \Illuminate\Support\Str::random();
\Nexus\Database\NexusDB::cache_put($idKeyOld, serialize($oldModel), 3600*24*30); \Nexus\Database\NexusDB::cache_put($idKeyOld, serialize($oldModel->toArray()), 3600*24*30);
} }
executeCommand("event:fire --name=$name --idKey=$idKey --idKeyOld=$idKeyOld", "string", true, false); executeCommand("event:fire --name=$name --idKey=$idKey --idKeyOld=$idKeyOld", "string", true, false);
} else { } else {
$eventClass = \App\Enums\ModelEventEnum::$eventMaps[$name]['event']; $eventClass = \App\Enums\ModelEventEnum::$eventMaps[$name]['event'];
if (str_ends_with($name, '_deleted')) {
//if deleted from database, can not pass model instance, use array
$params = [$model->toArray()];
if ($oldModel) {
$params[] = $oldModel->toArray();
}
} else {
$params = [$model]; $params = [$model];
if ($oldModel) { if ($oldModel) {
$params[] = $oldModel; $params[] = $oldModel;
} }
}
call_user_func_array([$eventClass, "dispatch"], $params); call_user_func_array([$eventClass, "dispatch"], $params);
publish_model_event($name, $model->id); publish_model_event($name, $model->id);
} }