mirror of
https://github.com/lkddi/nexusphp.git
synced 2026-04-23 11:27:24 +08:00
refactor event
This commit is contained in:
@@ -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");
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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!");
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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)));
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
defined('VERSION_NUMBER') || define('VERSION_NUMBER', '1.8.12');
|
||||
defined('RELEASE_DATE') || define('RELEASE_DATE', '2024-04-24');
|
||||
defined('RELEASE_DATE') || define('RELEASE_DATE', '2024-04-25');
|
||||
defined('IN_TRACKER') || define('IN_TRACKER', false);
|
||||
defined('PROJECTNAME') || define("PROJECTNAME","NexusPHP");
|
||||
defined('NEXUSPHPURL') || define("NEXUSPHPURL","https://nexusphp.org");
|
||||
|
||||
@@ -3133,7 +3133,6 @@ function deletetorrent($id, $notify = false) {
|
||||
if ($torrentInfo->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") {
|
||||
|
||||
+1
-1
@@ -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");
|
||||
|
||||
/**
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user