Skip to content

Commit

Permalink
refactor event
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaomlove committed Apr 24, 2024
1 parent 52c3365 commit ae29693
Show file tree
Hide file tree
Showing 15 changed files with 57 additions and 35 deletions.
24 changes: 18 additions & 6 deletions app/Console/Commands/FireEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@

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
{
Expand All @@ -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],
];

/**
Expand All @@ -44,7 +52,11 @@ public function handle()
$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");
Expand Down
7 changes: 4 additions & 3 deletions app/Events/NewsCreated.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,24 @@
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 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;
}

/**
Expand Down
7 changes: 4 additions & 3 deletions app/Events/TorrentCreated.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,24 @@
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 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;
}

/**
Expand Down
7 changes: 4 additions & 3 deletions app/Events/TorrentDeleted.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,24 @@
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 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;
}

/**
Expand Down
7 changes: 4 additions & 3 deletions app/Events/TorrentUpdated.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,24 @@
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 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;
}

/**
Expand Down
7 changes: 4 additions & 3 deletions app/Events/UserDestroyed.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,24 @@
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 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;
}

/**
Expand Down
7 changes: 4 additions & 3 deletions app/Events/UserDisabled.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,24 @@
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 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;
}

/**
Expand Down
7 changes: 4 additions & 3 deletions app/Events/UserEnabled.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,24 @@
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 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;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion app/Listeners/FetchTorrentImdb.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function __construct()
*/
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!");
Expand Down
2 changes: 1 addition & 1 deletion app/Listeners/RemoveOauthTokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function __construct()
*/
public function handle($event)
{
$uid = $event->id;
$uid = $event->model?->id ?? 0;
$modelNames = [
Passport::$authCodeModel,
Passport::$tokenModel,
Expand Down
4 changes: 2 additions & 2 deletions app/Listeners/SyncTorrentToEs.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ public function __construct()
*/
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)));

}

Expand Down
2 changes: 1 addition & 1 deletion include/constants.php
Original file line number Diff line number Diff line change
@@ -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");
Expand Down
5 changes: 4 additions & 1 deletion include/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand All @@ -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") {
Expand Down
2 changes: 1 addition & 1 deletion public/takeedit.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ function bark($msg) {
$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");

/**
Expand Down
2 changes: 1 addition & 1 deletion public/takeupload.php
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ function checkTorrentDict($dict, $key, $type = null)
$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)
Expand Down

0 comments on commit ae29693

Please sign in to comment.