From ae296935499641a494257ec91b97d83fc9cbe562 Mon Sep 17 00:00:00 2001 From: xiaomlove Date: Thu, 25 Apr 2024 02:15:56 +0800 Subject: [PATCH] refactor event --- app/Console/Commands/FireEvent.php | 24 ++++++++++++++++++------ app/Events/NewsCreated.php | 7 ++++--- app/Events/TorrentCreated.php | 7 ++++--- app/Events/TorrentDeleted.php | 7 ++++--- app/Events/TorrentUpdated.php | 7 ++++--- app/Events/UserDestroyed.php | 7 ++++--- app/Events/UserDisabled.php | 7 ++++--- app/Events/UserEnabled.php | 7 ++++--- app/Listeners/FetchTorrentImdb.php | 2 +- app/Listeners/RemoveOauthTokens.php | 2 +- app/Listeners/SyncTorrentToEs.php | 4 ++-- include/constants.php | 2 +- include/functions.php | 5 ++++- public/takeedit.php | 2 +- public/takeupload.php | 2 +- 15 files changed, 57 insertions(+), 35 deletions(-) diff --git a/app/Console/Commands/FireEvent.php b/app/Console/Commands/FireEvent.php index bc5103b3..7bb35382 100644 --- a/app/Console/Commands/FireEvent.php +++ b/app/Console/Commands/FireEvent.php @@ -4,10 +4,16 @@ namespace App\Console\Commands; use App\Events\NewsCreated; use App\Events\TorrentCreated; +use App\Events\TorrentDeleted; +use App\Events\TorrentUpdated; use App\Events\UserDestroyed; use App\Events\UserDisabled; use App\Events\UserEnabled; +use App\Models\News; +use App\Models\Torrent; +use App\Models\User; use Illuminate\Console\Command; +use Illuminate\Database\Eloquent\Model; class FireEvent extends Command { @@ -26,11 +32,13 @@ class FireEvent extends Command protected $description = 'Fire a event, options: --name, --id'; protected array $eventMaps = [ - "torrent_created" => TorrentCreated::class, - "user_destroyed" => UserDestroyed::class, - "user_disabled" => UserDisabled::class, - "user_enabled" => UserEnabled::class, - "news_created" => NewsCreated::class, + "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_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], ]; /** @@ -44,7 +52,11 @@ class FireEvent extends Command $id = $this->option('id'); $log = "FireEvent, name: $name, id: $id"; if (isset($this->eventMaps[$name])) { - $result = call_user_func([$this->eventMaps[$name], "dispatch"], $id); + $eventName = $this->eventMaps[$name]['event']; + $modelName = $this->eventMaps[$name]['model']; + /** @var Model $model */ + $model = new $modelName(); + $result = call_user_func([$eventName, "dispatch"], $model::query()->find($id)); $this->info("$log, success call dispatch, result: " . var_export($result, true)); } else { $this->error("$log, no event match this name"); diff --git a/app/Events/NewsCreated.php b/app/Events/NewsCreated.php index 1528f749..06692c42 100644 --- a/app/Events/NewsCreated.php +++ b/app/Events/NewsCreated.php @@ -7,6 +7,7 @@ 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; @@ -14,16 +15,16 @@ class NewsCreated { use Dispatchable, InteractsWithSockets, SerializesModels; - public int $id; + public ?Model $model = null; /** * Create a new event instance. * * @return void */ - public function __construct(int $id) + public function __construct(Model $model) { - $this->id = $id; + $this->model = $model; } /** diff --git a/app/Events/TorrentCreated.php b/app/Events/TorrentCreated.php index 5ba47795..5fb87b52 100644 --- a/app/Events/TorrentCreated.php +++ b/app/Events/TorrentCreated.php @@ -7,6 +7,7 @@ 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; @@ -14,16 +15,16 @@ class TorrentCreated { use Dispatchable, InteractsWithSockets, SerializesModels; - public int $torrentId; + public ?Model $model = null; /** * Create a new event instance. * * @return void */ - public function __construct(int $torrentId) + public function __construct(Model $model) { - $this->torrentId = $torrentId; + $this->model = $model; } /** diff --git a/app/Events/TorrentDeleted.php b/app/Events/TorrentDeleted.php index 6fb539c4..88c53e4f 100644 --- a/app/Events/TorrentDeleted.php +++ b/app/Events/TorrentDeleted.php @@ -7,6 +7,7 @@ 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; @@ -14,16 +15,16 @@ class TorrentDeleted { use Dispatchable, InteractsWithSockets, SerializesModels; - public int $torrentId; + public ?Model $model = null; /** * Create a new event instance. * * @return void */ - public function __construct(int $torrentId) + public function __construct(Model $model) { - $this->torrentId = $torrentId; + $this->model = $model; } /** diff --git a/app/Events/TorrentUpdated.php b/app/Events/TorrentUpdated.php index 8c5a4410..1f42f0b1 100644 --- a/app/Events/TorrentUpdated.php +++ b/app/Events/TorrentUpdated.php @@ -7,6 +7,7 @@ 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; @@ -14,16 +15,16 @@ class TorrentUpdated { use Dispatchable, InteractsWithSockets, SerializesModels; - public int $torrentId; + public ?Model $model = null; /** * Create a new event instance. * * @return void */ - public function __construct(int $torrentId) + public function __construct(Model $model) { - $this->torrentId = $torrentId; + $this->model = $model; } /** diff --git a/app/Events/UserDestroyed.php b/app/Events/UserDestroyed.php index 84756866..c4cf2d04 100644 --- a/app/Events/UserDestroyed.php +++ b/app/Events/UserDestroyed.php @@ -7,6 +7,7 @@ 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; @@ -14,16 +15,16 @@ class UserDestroyed { use Dispatchable, InteractsWithSockets, SerializesModels; - public int $id; + public ?Model $model = null; /** * Create a new event instance. * * @return void */ - public function __construct(int $id) + public function __construct(Model $model) { - $this->id = $id; + $this->model = $model; } /** diff --git a/app/Events/UserDisabled.php b/app/Events/UserDisabled.php index 6d388141..5fcce272 100644 --- a/app/Events/UserDisabled.php +++ b/app/Events/UserDisabled.php @@ -7,6 +7,7 @@ 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; @@ -14,16 +15,16 @@ class UserDisabled { use Dispatchable, InteractsWithSockets, SerializesModels; - public int $id; + public ?Model $model = null; /** * Create a new event instance. * * @return void */ - public function __construct(int $id) + public function __construct(Model $model) { - $this->id = $id; + $this->model = $model; } /** diff --git a/app/Events/UserEnabled.php b/app/Events/UserEnabled.php index 31de6df6..9736a40e 100644 --- a/app/Events/UserEnabled.php +++ b/app/Events/UserEnabled.php @@ -7,6 +7,7 @@ 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; @@ -14,16 +15,16 @@ class UserEnabled { use Dispatchable, InteractsWithSockets, SerializesModels; - public int $id; + public ?Model $model = null; /** * Create a new event instance. * * @return void */ - public function __construct(int $id) + public function __construct(Model $model) { - $this->id = $id; + $this->model = $model; } /** diff --git a/app/Listeners/FetchTorrentImdb.php b/app/Listeners/FetchTorrentImdb.php index ad8b211a..351d568b 100644 --- a/app/Listeners/FetchTorrentImdb.php +++ b/app/Listeners/FetchTorrentImdb.php @@ -27,7 +27,7 @@ class FetchTorrentImdb implements ShouldQueue */ public function handle($event) { - $torrentId = $event->torrentId; + $torrentId = $event->model?->id ?? 0; $torrentRep = new TorrentRepository(); $torrentRep->fetchImdb($torrentId); do_log("fetchImdb for torrent: $torrentId done!"); diff --git a/app/Listeners/RemoveOauthTokens.php b/app/Listeners/RemoveOauthTokens.php index d1d82de9..50d02560 100644 --- a/app/Listeners/RemoveOauthTokens.php +++ b/app/Listeners/RemoveOauthTokens.php @@ -27,7 +27,7 @@ class RemoveOauthTokens implements ShouldQueue */ public function handle($event) { - $uid = $event->id; + $uid = $event->model?->id ?? 0; $modelNames = [ Passport::$authCodeModel, Passport::$tokenModel, diff --git a/app/Listeners/SyncTorrentToEs.php b/app/Listeners/SyncTorrentToEs.php index 627e5b40..ad4f991d 100644 --- a/app/Listeners/SyncTorrentToEs.php +++ b/app/Listeners/SyncTorrentToEs.php @@ -31,10 +31,10 @@ class SyncTorrentToEs implements ShouldQueue */ public function handle($event) { - $id = $event->torrentId; + $id = $event->model?->id ?? 0; $searchRep = new SearchRepository(); $result = $searchRep->updateTorrent($id); - do_log("result: " . var_export($result, true)); + do_log(sprintf("updateTorrent: %s result: %s", $id, var_export($result, true))); } diff --git a/include/constants.php b/include/constants.php index e0857936..01685d1d 100644 --- a/include/constants.php +++ b/include/constants.php @@ -1,6 +1,6 @@ has($_id)) { $torrentRep->delPiecesHashCache($torrentInfo->get($_id)->pieces_hash); } - do_action("torrent_delete", $_id); do_log("delete torrent: $_id", "error"); unlink(getFullDirectory("$torrent_dir/$_id.torrent")); \App\Models\TorrentOperationLog::add([ @@ -3145,6 +3144,10 @@ function deletetorrent($id, $notify = false) { } $meiliSearchRep = new \App\Repositories\MeiliSearchRepository(); $meiliSearchRep->deleteDocuments($idArr); + if (is_int($id)) { + do_action("torrent_delete", $id); + fire_event("torrent_deleted", $id); + } } function pager($rpp, $count, $href, $opts = array(), $pagename = "page") { diff --git a/public/takeedit.php b/public/takeedit.php index 9ae0ba2c..e9735b56 100644 --- a/public/takeedit.php +++ b/public/takeedit.php @@ -232,7 +232,7 @@ if (user_can('torrent-set-price') && $paidTorrentEnabled) { $sql = "UPDATE torrents SET " . join(",", $updateset) . " WHERE id = $id"; do_log("[UPDATE_TORRENT]: $sql"); $affectedRows = sql_query($sql) or sqlerr(__FILE__, __LINE__); - +fire_event("torrent_updated", $id); $dateTimeStringNow = date("Y-m-d H:i:s"); /** diff --git a/public/takeupload.php b/public/takeupload.php index bcdfd47f..92d3df3d 100644 --- a/public/takeupload.php +++ b/public/takeupload.php @@ -448,7 +448,7 @@ $meiliSearch = new \App\Repositories\MeiliSearchRepository(); $meiliSearch->doImportFromDatabase($id); //trigger event -executeCommand("event:fire --name=torrent_created --id=$id", "string", true, false); +fire_event("torrent_created", $id); //===notify people who voted on offer thanks CoLdFuSiOn :) if ($is_offer)