diff --git a/app/Console/Commands/FireEvent.php b/app/Console/Commands/FireEvent.php index 9b6374f5..1d744ede 100644 --- a/app/Console/Commands/FireEvent.php +++ b/app/Console/Commands/FireEvent.php @@ -15,7 +15,7 @@ class FireEvent extends Command * * @var string */ - protected $signature = 'event:fire {--name=} {--idKey=} {--idKeyOld=""}'; + protected $signature = 'event:fire {--name=} {--idKey=} {--idKeyOld=}'; /** * The console command description. @@ -35,25 +35,26 @@ class FireEvent extends Command $idKey = $this->option('idKey'); $idKeyOld = $this->option('idKeyOld'); $log = "FireEvent, name: $name, idKey: $idKey, idKeyOld: $idKeyOld"; + $this->info("$log, begin ..."); if (isset(ModelEventEnum::$eventMaps[$name])) { $eventName = ModelEventEnum::$eventMaps[$name]['event']; - $model = unserialize(NexusDB::cache_get($idKey)); - if ($model instanceof Model) { - $params = [$model]; - if ($idKeyOld) { - $modelOld = unserialize(NexusDB::cache_get($idKeyOld)); - if ($modelOld instanceof Model) { - $params[] = $modelOld; - } else { - $log .= ", invalid idKeyOld"; - } - } - $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; + $modelClassName = ModelEventEnum::$eventMaps[$name]['model']; + $modelBasic = new $modelClassName(); + $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) { + $modelOldData = unserialize(NexusDB::cache_get($idKeyOld)); + $modelOld = call_user_func_array([$modelBasic, "newInstance"], [$modelOldData, true]); + $modelOld->id = $modelOldData['id']; + $params[] = $useArray ? $modelOldData: $modelOld; } + $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 .= ", no event match this name"; } diff --git a/app/Events/TorrentDeleted.php b/app/Events/TorrentDeleted.php index 88c53e4f..e469790d 100644 --- a/app/Events/TorrentDeleted.php +++ b/app/Events/TorrentDeleted.php @@ -15,16 +15,16 @@ class TorrentDeleted { use Dispatchable, InteractsWithSockets, SerializesModels; - public ?Model $model = null; + public ?array $data = null; /** * Create a new event instance. * * @return void */ - public function __construct(Model $model) + public function __construct(array $data) { - $this->model = $model; + $this->data = $data; } /** diff --git a/app/Events/UserDeleted.php b/app/Events/UserDeleted.php index 099624d3..b291a8a2 100644 --- a/app/Events/UserDeleted.php +++ b/app/Events/UserDeleted.php @@ -15,16 +15,16 @@ class UserDeleted { use Dispatchable, InteractsWithSockets, SerializesModels; - public ?Model $model = null; + public ?array $data = null; /** * Create a new event instance. * * @return void */ - public function __construct(Model $model) + public function __construct(array $data) { - $this->model = $model; + $this->data = $data; } /** diff --git a/app/Listeners/DeductUserBonusWhenTorrentDeleted.php b/app/Listeners/DeductUserBonusWhenTorrentDeleted.php index 0a0c2304..0b466994 100644 --- a/app/Listeners/DeductUserBonusWhenTorrentDeleted.php +++ b/app/Listeners/DeductUserBonusWhenTorrentDeleted.php @@ -28,7 +28,7 @@ class DeductUserBonusWhenTorrentDeleted implements ShouldQueue /** * Just a test */ - $torrent = $event->model; - do_log(sprintf("torrent: %d is deleted, and it's pieces_hash is: %s", $torrent->id, $torrent->pieces_hash)); + $torrent = $event->data; + do_log(sprintf("torrent: %d is deleted, and it's pieces_hash is: %s", $torrent['id'], $torrent['pieces_hash'])); } } diff --git a/app/Models/Torrent.php b/app/Models/Torrent.php index 27b2beb2..bf660c5e 100644 --- a/app/Models/Torrent.php +++ b/app/Models/Torrent.php @@ -31,6 +31,10 @@ class Torrent extends NexusModel 'last_action' => 'datetime', ]; + protected $hidden = [ + 'info_hash', + ]; + public static $commentFields = [ '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', diff --git a/app/Models/TorrentExtra.php b/app/Models/TorrentExtra.php index 74477536..abc4f207 100644 --- a/app/Models/TorrentExtra.php +++ b/app/Models/TorrentExtra.php @@ -16,6 +16,9 @@ class TorrentExtra extends NexusModel { return Attribute::make( get: function ($value) { + if (is_null($value)) { + return null; + } $jsonDecoded = json_decode($value, true); if (is_array($jsonDecoded)) { return $jsonDecoded; diff --git a/include/functions.php b/include/functions.php index 643c4824..2cbe8db0 100644 --- a/include/functions.php +++ b/include/functions.php @@ -6136,7 +6136,7 @@ function build_search_box_category_table($mode, $checkboxValue, $categoryHrefPre //Category $html .= sprintf('%s', nexus_trans('label.search_box.category')); /** @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'])) { $categoryCollection->push(new \App\Models\Category(['mode' => -1])); } diff --git a/include/globalfunctions.php b/include/globalfunctions.php index bf40545e..d3f89229 100644 --- a/include/globalfunctions.php +++ b/include/globalfunctions.php @@ -1340,17 +1340,25 @@ function fire_event(string $name, \Illuminate\Database\Eloquent\Model $model, ?\ $prefix = "fire_event:"; $idKey = $prefix . \Illuminate\Support\Str::random(); $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) { $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); } else { $eventClass = \App\Enums\ModelEventEnum::$eventMaps[$name]['event']; - $params = [$model]; - if ($oldModel) { - $params[] = $oldModel; + 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]; + if ($oldModel) { + $params[] = $oldModel; + } } call_user_func_array([$eventClass, "dispatch"], $params); publish_model_event($name, $model->id);