mirror of
https://github.com/lkddi/nexusphp.git
synced 2026-04-14 20:40:49 +08:00
refactor model event again
This commit is contained in:
@@ -14,6 +14,8 @@ use App\Models\Torrent;
|
|||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
use Illuminate\Console\Command;
|
use Illuminate\Console\Command;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Nexus\Database\NexusDB;
|
||||||
|
use Symfony\Component\Console\Command\Command as CommandAlias;
|
||||||
|
|
||||||
class FireEvent extends Command
|
class FireEvent extends Command
|
||||||
{
|
{
|
||||||
@@ -22,14 +24,14 @@ class FireEvent extends Command
|
|||||||
*
|
*
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
protected $signature = 'event:fire {--name=} {--id=}';
|
protected $signature = 'event:fire {--name=} {--idKey=} {--idKeyOld=""}';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The console command description.
|
* The console command description.
|
||||||
*
|
*
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
protected $description = 'Fire a event, options: --name, --id';
|
protected $description = 'Fire a event, options: --name, --idKey --idKeyOld';
|
||||||
|
|
||||||
protected array $eventMaps = [
|
protected array $eventMaps = [
|
||||||
"torrent_created" => ['event' => TorrentCreated::class, 'model' => Torrent::class],
|
"torrent_created" => ['event' => TorrentCreated::class, 'model' => Torrent::class],
|
||||||
@@ -49,18 +51,32 @@ class FireEvent extends Command
|
|||||||
public function handle()
|
public function handle()
|
||||||
{
|
{
|
||||||
$name = $this->option('name');
|
$name = $this->option('name');
|
||||||
$id = $this->option('id');
|
$idKey = $this->option('idKey');
|
||||||
$log = "FireEvent, name: $name, id: $id";
|
$idKeyOld = $this->option('idKeyOld');
|
||||||
|
$log = "FireEvent, name: $name, idKey: $idKey, idKeyOld: $idKeyOld";
|
||||||
if (isset($this->eventMaps[$name])) {
|
if (isset($this->eventMaps[$name])) {
|
||||||
$eventName = $this->eventMaps[$name]['event'];
|
$eventName = $this->eventMaps[$name]['event'];
|
||||||
$modelName = $this->eventMaps[$name]['model'];
|
$model = unserialize(NexusDB::cache_get($idKey));
|
||||||
/** @var Model $model */
|
if ($model instanceof Model) {
|
||||||
$model = new $modelName();
|
$params = [$model];
|
||||||
$result = call_user_func([$eventName, "dispatch"], $model::query()->find($id));
|
if ($idKeyOld) {
|
||||||
$this->info("$log, success call dispatch, result: " . var_export($result, true));
|
$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);
|
||||||
|
} else {
|
||||||
|
$log .= ", invalid argument to call, it should be instance of: " . Model::class;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
$this->error("$log, no event match this name");
|
$log .= ", no event match this name";
|
||||||
}
|
}
|
||||||
return Command::SUCCESS;
|
$this->info($log);
|
||||||
|
do_log($log);
|
||||||
|
return CommandAlias::SUCCESS;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,14 +17,17 @@ class TorrentUpdated
|
|||||||
|
|
||||||
public ?Model $model = null;
|
public ?Model $model = null;
|
||||||
|
|
||||||
|
public ?Model $modelOld = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new event instance.
|
* Create a new event instance.
|
||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function __construct(Model $model)
|
public function __construct(Model $model, Model $modelOld)
|
||||||
{
|
{
|
||||||
$this->model = $model;
|
$this->model = $model;
|
||||||
|
$this->modelOld = $modelOld;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
34
app/Listeners/DeductUserBonusWhenTorrentDeleted.php
Normal file
34
app/Listeners/DeductUserBonusWhenTorrentDeleted.php
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Listeners;
|
||||||
|
|
||||||
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||||
|
use Illuminate\Queue\InteractsWithQueue;
|
||||||
|
|
||||||
|
class DeductUserBonusWhenTorrentDeleted
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Create the event listener.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle the event.
|
||||||
|
*
|
||||||
|
* @param object $event
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function handle($event)
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* 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));
|
||||||
|
}
|
||||||
|
}
|
||||||
38
app/Listeners/TestTorrentUpdated.php
Normal file
38
app/Listeners/TestTorrentUpdated.php
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Listeners;
|
||||||
|
|
||||||
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||||
|
use Illuminate\Queue\InteractsWithQueue;
|
||||||
|
|
||||||
|
class TestTorrentUpdated
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Create the event listener.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle the event.
|
||||||
|
*
|
||||||
|
* @param object $event
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function handle($event)
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Just a test
|
||||||
|
*/
|
||||||
|
$torrentNew = $event->model;
|
||||||
|
$torrentOld = $event->modelOld;
|
||||||
|
do_log(sprintf(
|
||||||
|
"torrent: %d is updated, old descr: %s, new descr: %s",
|
||||||
|
$torrentNew->id, $torrentOld->descr, $torrentNew->descr
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,13 +4,16 @@ namespace App\Providers;
|
|||||||
|
|
||||||
use App\Events\SeedBoxRecordUpdated;
|
use App\Events\SeedBoxRecordUpdated;
|
||||||
use App\Events\TorrentCreated;
|
use App\Events\TorrentCreated;
|
||||||
|
use App\Events\TorrentDeleted;
|
||||||
use App\Events\TorrentUpdated;
|
use App\Events\TorrentUpdated;
|
||||||
use App\Events\UserDestroyed;
|
use App\Events\UserDestroyed;
|
||||||
use App\Events\UserDisabled;
|
use App\Events\UserDisabled;
|
||||||
|
use App\Listeners\DeductUserBonusWhenTorrentDeleted;
|
||||||
use App\Listeners\FetchTorrentImdb;
|
use App\Listeners\FetchTorrentImdb;
|
||||||
use App\Listeners\RemoveOauthTokens;
|
use App\Listeners\RemoveOauthTokens;
|
||||||
use App\Listeners\RemoveSeedBoxRecordCache;
|
use App\Listeners\RemoveSeedBoxRecordCache;
|
||||||
use App\Listeners\SyncTorrentToEs;
|
use App\Listeners\SyncTorrentToEs;
|
||||||
|
use App\Listeners\TestTorrentUpdated;
|
||||||
use Illuminate\Auth\Events\Registered;
|
use Illuminate\Auth\Events\Registered;
|
||||||
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
|
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
|
||||||
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
|
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
|
||||||
@@ -27,15 +30,19 @@ class EventServiceProvider extends ServiceProvider
|
|||||||
Registered::class => [
|
Registered::class => [
|
||||||
SendEmailVerificationNotification::class,
|
SendEmailVerificationNotification::class,
|
||||||
],
|
],
|
||||||
TorrentUpdated::class => [
|
|
||||||
SyncTorrentToEs::class,
|
|
||||||
],
|
|
||||||
SeedBoxRecordUpdated::class => [
|
SeedBoxRecordUpdated::class => [
|
||||||
RemoveSeedBoxRecordCache::class,
|
RemoveSeedBoxRecordCache::class,
|
||||||
],
|
],
|
||||||
|
TorrentUpdated::class => [
|
||||||
|
SyncTorrentToEs::class,
|
||||||
|
TestTorrentUpdated::class,
|
||||||
|
],
|
||||||
TorrentCreated::class => [
|
TorrentCreated::class => [
|
||||||
FetchTorrentImdb::class,
|
FetchTorrentImdb::class,
|
||||||
],
|
],
|
||||||
|
TorrentDeleted::class => [
|
||||||
|
DeductUserBonusWhenTorrentDeleted::class,
|
||||||
|
],
|
||||||
UserDisabled::class => [
|
UserDisabled::class => [
|
||||||
RemoveOauthTokens::class,
|
RemoveOauthTokens::class,
|
||||||
],
|
],
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ use Illuminate\Support\Arr;
|
|||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
use Illuminate\Support\Facades\Auth;
|
use Illuminate\Support\Facades\Auth;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
|
use Illuminate\Support\Str;
|
||||||
use Nexus\Database\NexusDB;
|
use Nexus\Database\NexusDB;
|
||||||
|
|
||||||
class UserRepository extends BaseRepository
|
class UserRepository extends BaseRepository
|
||||||
@@ -199,7 +200,7 @@ class UserRepository extends BaseRepository
|
|||||||
});
|
});
|
||||||
do_log("user: $uid, $modCommentText");
|
do_log("user: $uid, $modCommentText");
|
||||||
$this->clearCache($targetUser);
|
$this->clearCache($targetUser);
|
||||||
fire_event("user_disabled", $uid);
|
fire_event("user_disabled", $targetUser);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -226,7 +227,7 @@ class UserRepository extends BaseRepository
|
|||||||
$targetUser->updateWithModComment($update, $modCommentText);
|
$targetUser->updateWithModComment($update, $modCommentText);
|
||||||
do_log("user: $uid, $modCommentText, update: " . nexus_json_encode($update));
|
do_log("user: $uid, $modCommentText, update: " . nexus_json_encode($update));
|
||||||
$this->clearCache($targetUser);
|
$this->clearCache($targetUser);
|
||||||
fire_event("user_enabled", $uid);
|
fire_event("user_enabled", $targetUser);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -631,11 +632,10 @@ class UserRepository extends BaseRepository
|
|||||||
}
|
}
|
||||||
if (is_int($id)) {
|
if (is_int($id)) {
|
||||||
$uidArr = Arr::wrap($id);
|
$uidArr = Arr::wrap($id);
|
||||||
$users = User::query()->with('language')->whereIn('id', $uidArr)->get(['id', 'username', 'lang']);
|
|
||||||
} else {
|
} else {
|
||||||
$users = $id;
|
$uidArr = $id->pluck('id')->toArray();
|
||||||
$uidArr = $users->pluck('id')->toArray();
|
|
||||||
}
|
}
|
||||||
|
$users = User::query()->with('language')->whereIn('id', $uidArr)->get();
|
||||||
if (empty($uidArr)) {
|
if (empty($uidArr)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -668,7 +668,7 @@ class UserRepository extends BaseRepository
|
|||||||
UserBanLog::query()->insert($userBanLogs);
|
UserBanLog::query()->insert($userBanLogs);
|
||||||
if (is_int($id)) {
|
if (is_int($id)) {
|
||||||
do_action("user_delete", $id);
|
do_action("user_delete", $id);
|
||||||
fire_event("user_destroyed", $id);
|
fire_event("user_destroyed", $users->first());
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
defined('VERSION_NUMBER') || define('VERSION_NUMBER', '1.8.12');
|
defined('VERSION_NUMBER') || define('VERSION_NUMBER', '1.8.12');
|
||||||
defined('RELEASE_DATE') || define('RELEASE_DATE', '2024-04-25');
|
defined('RELEASE_DATE') || define('RELEASE_DATE', '2024-04-26');
|
||||||
defined('IN_TRACKER') || define('IN_TRACKER', false);
|
defined('IN_TRACKER') || define('IN_TRACKER', false);
|
||||||
defined('PROJECTNAME') || define("PROJECTNAME","NexusPHP");
|
defined('PROJECTNAME') || define("PROJECTNAME","NexusPHP");
|
||||||
defined('NEXUSPHPURL') || define("NEXUSPHPURL","https://nexusphp.org");
|
defined('NEXUSPHPURL') || define("NEXUSPHPURL","https://nexusphp.org");
|
||||||
|
|||||||
@@ -3114,9 +3114,9 @@ function loggedinorreturn($mainpage = false) {
|
|||||||
|
|
||||||
function deletetorrent($id, $notify = false) {
|
function deletetorrent($id, $notify = false) {
|
||||||
$idArr = is_array($id) ? $id : [$id];
|
$idArr = is_array($id) ? $id : [$id];
|
||||||
$torrentInfo = \Nexus\Database\NexusDB::table("torrents")
|
$torrentInfo = \App\Models\Torrent::query()
|
||||||
->whereIn("id", $idArr)
|
->whereIn("id", $idArr)
|
||||||
->get(['id', 'pieces_hash'])
|
->get()
|
||||||
->KeyBy("id")
|
->KeyBy("id")
|
||||||
;
|
;
|
||||||
$torrentRep = new \App\Repositories\TorrentRepository();
|
$torrentRep = new \App\Repositories\TorrentRepository();
|
||||||
@@ -3146,7 +3146,7 @@ function deletetorrent($id, $notify = false) {
|
|||||||
$meiliSearchRep->deleteDocuments($idArr);
|
$meiliSearchRep->deleteDocuments($idArr);
|
||||||
if (is_int($id)) {
|
if (is_int($id)) {
|
||||||
do_action("torrent_delete", $id);
|
do_action("torrent_delete", $id);
|
||||||
fire_event("torrent_deleted", $id);
|
fire_event("torrent_deleted", $torrentInfo->get($id));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1237,7 +1237,14 @@ function get_snatch_info($torrentId, $userId)
|
|||||||
return mysql_fetch_assoc(sql_query(sprintf('select * from snatched where torrentid = %s and userid = %s order by id desc limit 1', $torrentId, $userId)));
|
return mysql_fetch_assoc(sql_query(sprintf('select * from snatched where torrentid = %s and userid = %s order by id desc limit 1', $torrentId, $userId)));
|
||||||
}
|
}
|
||||||
|
|
||||||
function fire_event(string $name, int $id): void
|
function fire_event(string $name, \Illuminate\Database\Eloquent\Model $model, \Illuminate\Database\Eloquent\Model $oldModel = null): void
|
||||||
{
|
{
|
||||||
executeCommand("event:fire --name=$name --id=$id", "string", true, false);
|
$idKey = \Illuminate\Support\Str::random();
|
||||||
|
$idKeyOld = "";
|
||||||
|
\Nexus\Database\NexusDB::cache_put($idKey, serialize($model));
|
||||||
|
if ($oldModel) {
|
||||||
|
$idKeyOld = \Illuminate\Support\Str::random();
|
||||||
|
\Nexus\Database\NexusDB::cache_put($idKeyOld, serialize($oldModel));
|
||||||
|
}
|
||||||
|
executeCommand("event:fire --name=$name --idKey=$idKey --idKeyOld=$idKeyOld", "string", true, false);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -51,7 +51,7 @@ if ($action == 'add')
|
|||||||
if (mysql_affected_rows() != 1) {
|
if (mysql_affected_rows() != 1) {
|
||||||
stderr($lang_news['std_error'], $lang_news['std_something_weird_happened']);
|
stderr($lang_news['std_error'], $lang_news['std_something_weird_happened']);
|
||||||
}
|
}
|
||||||
fire_event("news_created", mysql_insert_id());
|
fire_event("news_created", \App\Models\News::query()->find(mysql_insert_id()));
|
||||||
header("Location: " . get_protocol_prefix() . "$BASEURL/index.php");
|
header("Location: " . get_protocol_prefix() . "$BASEURL/index.php");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ $row = mysql_fetch_array($res);
|
|||||||
$torrentAddedTimeString = $row['added'];
|
$torrentAddedTimeString = $row['added'];
|
||||||
if (!$row)
|
if (!$row)
|
||||||
die();
|
die();
|
||||||
|
$torrentOld = \App\Models\Torrent::query()->find($id);
|
||||||
if ($CURUSER["id"] != $row["owner"] && !user_can('torrentmanage'))
|
if ($CURUSER["id"] != $row["owner"] && !user_can('torrentmanage'))
|
||||||
bark($lang_takeedit['std_not_owner']);
|
bark($lang_takeedit['std_not_owner']);
|
||||||
$oldcatmode = get_single_value("categories","mode","WHERE id=".sqlesc($row['category']));
|
$oldcatmode = get_single_value("categories","mode","WHERE id=".sqlesc($row['category']));
|
||||||
@@ -232,7 +232,7 @@ if (user_can('torrent-set-price') && $paidTorrentEnabled) {
|
|||||||
$sql = "UPDATE torrents SET " . join(",", $updateset) . " WHERE id = $id";
|
$sql = "UPDATE torrents SET " . join(",", $updateset) . " WHERE id = $id";
|
||||||
do_log("[UPDATE_TORRENT]: $sql");
|
do_log("[UPDATE_TORRENT]: $sql");
|
||||||
$affectedRows = sql_query($sql) or sqlerr(__FILE__, __LINE__);
|
$affectedRows = sql_query($sql) or sqlerr(__FILE__, __LINE__);
|
||||||
fire_event("torrent_updated", $id);
|
fire_event("torrent_updated", \App\Models\Torrent::query()->find($id), $torrentOld);
|
||||||
$dateTimeStringNow = date("Y-m-d H:i:s");
|
$dateTimeStringNow = date("Y-m-d H:i:s");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -448,7 +448,7 @@ $meiliSearch = new \App\Repositories\MeiliSearchRepository();
|
|||||||
$meiliSearch->doImportFromDatabase($id);
|
$meiliSearch->doImportFromDatabase($id);
|
||||||
|
|
||||||
//trigger event
|
//trigger event
|
||||||
fire_event("torrent_created", $id);
|
fire_event("torrent_created", \App\Models\Torrent::query()->find($id));
|
||||||
|
|
||||||
//===notify people who voted on offer thanks CoLdFuSiOn :)
|
//===notify people who voted on offer thanks CoLdFuSiOn :)
|
||||||
if ($is_offer)
|
if ($is_offer)
|
||||||
|
|||||||
Reference in New Issue
Block a user