mirror of
https://github.com/AnimeThemes/animethemes-server.git
synced 2026-07-11 01:24:46 +02:00
refactor: add observers (#1028)
This commit is contained in:
@@ -4,7 +4,6 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Events\Discord\DiscordThread;
|
||||
|
||||
use App\Actions\Discord\DiscordThreadAction;
|
||||
use App\Events\Base\Admin\AdminDeletedEvent;
|
||||
use App\Models\Discord\DiscordThread;
|
||||
|
||||
@@ -16,21 +15,10 @@ class DiscordThreadDeleted extends AdminDeletedEvent
|
||||
public function __construct(DiscordThread $thread)
|
||||
{
|
||||
parent::__construct($thread);
|
||||
$this->deleteThread();
|
||||
}
|
||||
|
||||
protected function getDiscordMessageDescription(): string
|
||||
{
|
||||
return "Discord Thread '**{$this->getModel()->getName()}**' has been deleted.";
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the thread on discord.
|
||||
*/
|
||||
protected function deleteThread(): void
|
||||
{
|
||||
DiscordThreadAction::getHttp()
|
||||
->delete('/thread', ['id' => $this->getModel()->getKey()])
|
||||
->throw();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Events\Discord\DiscordThread;
|
||||
|
||||
use App\Actions\Discord\DiscordThreadAction;
|
||||
use App\Events\Base\Admin\AdminUpdatedEvent;
|
||||
use App\Models\Discord\DiscordThread;
|
||||
|
||||
@@ -16,7 +15,6 @@ class DiscordThreadUpdated extends AdminUpdatedEvent
|
||||
public function __construct(DiscordThread $thread)
|
||||
{
|
||||
parent::__construct($thread);
|
||||
$this->updateThread();
|
||||
$this->initializeEmbedFields($thread);
|
||||
}
|
||||
|
||||
@@ -24,14 +22,4 @@ class DiscordThreadUpdated extends AdminUpdatedEvent
|
||||
{
|
||||
return "Discord Thread '**{$this->getModel()->getName()}**' has been updated.";
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the thread on discord.
|
||||
*/
|
||||
protected function updateThread(): void
|
||||
{
|
||||
DiscordThreadAction::getHttp()
|
||||
->put('/thread', $this->getModel()->toArray())
|
||||
->throw();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,8 +18,6 @@ class ThemeCreated extends WikiCreatedEvent implements UpdateRelatedIndicesEvent
|
||||
public function __construct(AnimeTheme $theme)
|
||||
{
|
||||
parent::__construct($theme);
|
||||
|
||||
$this->updateFirstTheme();
|
||||
}
|
||||
|
||||
protected function getDiscordMessageDescription(): string
|
||||
@@ -36,17 +34,4 @@ class ThemeCreated extends WikiCreatedEvent implements UpdateRelatedIndicesEvent
|
||||
$entry->videos->each(fn (Video $video) => $video->searchable());
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the sequence attribute of the first theme when creating a new sequence theme.
|
||||
*/
|
||||
protected function updateFirstTheme(): void
|
||||
{
|
||||
if ($this->getModel()->sequence >= 2) {
|
||||
$this->getModel()->anime->animethemes()->getQuery()
|
||||
->where(AnimeTheme::ATTRIBUTE_SEQUENCE)
|
||||
->where(AnimeTheme::ATTRIBUTE_TYPE, $this->getModel()->type)
|
||||
->update([AnimeTheme::ATTRIBUTE_SEQUENCE => 1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,33 +4,16 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Events\Wiki\Group;
|
||||
|
||||
use App\Contracts\Events\UpdateRelatedIndicesEvent;
|
||||
use App\Events\Base\Wiki\WikiCreatedEvent;
|
||||
use App\Models\Wiki\Anime\AnimeTheme;
|
||||
use App\Models\Wiki\Anime\Theme\AnimeThemeEntry;
|
||||
use App\Models\Wiki\Group;
|
||||
use App\Models\Wiki\Video;
|
||||
|
||||
/**
|
||||
* @extends WikiCreatedEvent<Group>
|
||||
*/
|
||||
class GroupCreated extends WikiCreatedEvent implements UpdateRelatedIndicesEvent
|
||||
class GroupCreated extends WikiCreatedEvent
|
||||
{
|
||||
protected function getDiscordMessageDescription(): string
|
||||
{
|
||||
return "Group '**{$this->getModel()->getName()}**' has been created.";
|
||||
}
|
||||
|
||||
public function updateRelatedIndices(): void
|
||||
{
|
||||
$group = $this->getModel()->load(Group::RELATION_VIDEOS);
|
||||
|
||||
$group->animethemes->each(function (AnimeTheme $theme): void {
|
||||
$theme->searchable();
|
||||
$theme->animethemeentries->each(function (AnimeThemeEntry $entry): void {
|
||||
$entry->searchable();
|
||||
$entry->videos->each(fn (Video $video) => $video->searchable());
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,18 +4,14 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Events\Wiki\Group;
|
||||
|
||||
use App\Contracts\Events\UpdateRelatedIndicesEvent;
|
||||
use App\Events\Base\Wiki\WikiDeletedEvent;
|
||||
use App\Filament\Resources\Wiki\Group as GroupFilament;
|
||||
use App\Models\Wiki\Anime\AnimeTheme;
|
||||
use App\Models\Wiki\Anime\Theme\AnimeThemeEntry;
|
||||
use App\Models\Wiki\Group;
|
||||
use App\Models\Wiki\Video;
|
||||
|
||||
/**
|
||||
* @extends WikiDeletedEvent<Group>
|
||||
*/
|
||||
class GroupDeleted extends WikiDeletedEvent implements UpdateRelatedIndicesEvent
|
||||
class GroupDeleted extends WikiDeletedEvent
|
||||
{
|
||||
protected function getDiscordMessageDescription(): string
|
||||
{
|
||||
@@ -31,17 +27,4 @@ class GroupDeleted extends WikiDeletedEvent implements UpdateRelatedIndicesEvent
|
||||
{
|
||||
return GroupFilament::getUrl('view', ['record' => $this->getModel()]);
|
||||
}
|
||||
|
||||
public function updateRelatedIndices(): void
|
||||
{
|
||||
$group = $this->getModel()->load(Group::RELATION_VIDEOS);
|
||||
|
||||
$group->animethemes->each(function (AnimeTheme $theme): void {
|
||||
$theme->searchable();
|
||||
$theme->animethemeentries->each(function (AnimeThemeEntry $entry): void {
|
||||
$entry->searchable();
|
||||
$entry->videos->each(fn (Video $video) => $video->searchable());
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,9 +7,7 @@ namespace App\Events\Wiki\Group;
|
||||
use App\Contracts\Events\UpdateRelatedIndicesEvent;
|
||||
use App\Events\BaseEvent;
|
||||
use App\Models\Wiki\Anime\AnimeTheme;
|
||||
use App\Models\Wiki\Anime\Theme\AnimeThemeEntry;
|
||||
use App\Models\Wiki\Group;
|
||||
use App\Models\Wiki\Video;
|
||||
|
||||
/**
|
||||
* @extends BaseEvent<Group>
|
||||
@@ -26,11 +24,6 @@ class GroupDeleting extends BaseEvent implements UpdateRelatedIndicesEvent
|
||||
$theme->group()->dissociate();
|
||||
$theme->save();
|
||||
});
|
||||
$theme->searchable();
|
||||
$theme->animethemeentries->each(function (AnimeThemeEntry $entry): void {
|
||||
$entry->searchable();
|
||||
$entry->videos->each(fn (Video $video) => $video->searchable());
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,33 +4,16 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Events\Wiki\Group;
|
||||
|
||||
use App\Contracts\Events\UpdateRelatedIndicesEvent;
|
||||
use App\Events\Base\Wiki\WikiRestoredEvent;
|
||||
use App\Models\Wiki\Anime\AnimeTheme;
|
||||
use App\Models\Wiki\Anime\Theme\AnimeThemeEntry;
|
||||
use App\Models\Wiki\Group;
|
||||
use App\Models\Wiki\Video;
|
||||
|
||||
/**
|
||||
* @extends WikiRestoredEvent<Group>
|
||||
*/
|
||||
class GroupRestored extends WikiRestoredEvent implements UpdateRelatedIndicesEvent
|
||||
class GroupRestored extends WikiRestoredEvent
|
||||
{
|
||||
protected function getDiscordMessageDescription(): string
|
||||
{
|
||||
return "Group '**{$this->getModel()->getName()}**' has been restored.";
|
||||
}
|
||||
|
||||
public function updateRelatedIndices(): void
|
||||
{
|
||||
$group = $this->getModel()->load(Group::RELATION_VIDEOS);
|
||||
|
||||
$group->animethemes->each(function (AnimeTheme $theme): void {
|
||||
$theme->searchable();
|
||||
$theme->animethemeentries->each(function (AnimeThemeEntry $entry): void {
|
||||
$entry->searchable();
|
||||
$entry->videos->each(fn (Video $video) => $video->searchable());
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,17 +4,13 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Events\Wiki\Group;
|
||||
|
||||
use App\Contracts\Events\UpdateRelatedIndicesEvent;
|
||||
use App\Events\Base\Wiki\WikiUpdatedEvent;
|
||||
use App\Models\Wiki\Anime\AnimeTheme;
|
||||
use App\Models\Wiki\Anime\Theme\AnimeThemeEntry;
|
||||
use App\Models\Wiki\Group;
|
||||
use App\Models\Wiki\Video;
|
||||
|
||||
/**
|
||||
* @extends WikiUpdatedEvent<Group>
|
||||
*/
|
||||
class GroupUpdated extends WikiUpdatedEvent implements UpdateRelatedIndicesEvent
|
||||
class GroupUpdated extends WikiUpdatedEvent
|
||||
{
|
||||
public function __construct(Group $group)
|
||||
{
|
||||
@@ -26,17 +22,4 @@ class GroupUpdated extends WikiUpdatedEvent implements UpdateRelatedIndicesEvent
|
||||
{
|
||||
return "Group '**{$this->getModel()->getName()}**' has been updated.";
|
||||
}
|
||||
|
||||
public function updateRelatedIndices(): void
|
||||
{
|
||||
$group = $this->getModel()->load(Group::RELATION_VIDEOS);
|
||||
|
||||
$group->animethemes->each(function (AnimeTheme $theme): void {
|
||||
$theme->searchable();
|
||||
$theme->animethemeentries->each(function (AnimeThemeEntry $entry): void {
|
||||
$entry->searchable();
|
||||
$entry->videos->each(fn (Video $video) => $video->searchable());
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,8 +10,10 @@ use App\Discord\DiscordEmbedField;
|
||||
use App\Enums\Models\Admin\ActionLogStatus;
|
||||
use App\Models\Auth\User;
|
||||
use App\Models\BaseModel;
|
||||
use App\Observers\Admin\ActionLogObserver;
|
||||
use Filament\Actions\Action;
|
||||
use Filament\Facades\Filament;
|
||||
use Illuminate\Database\Eloquent\Attributes\ObservedBy;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\MorphTo;
|
||||
@@ -41,6 +43,7 @@ use Throwable;
|
||||
* @property int $user_id
|
||||
* @property User $user
|
||||
*/
|
||||
#[ObservedBy(ActionLogObserver::class)]
|
||||
class ActionLog extends Model implements Nameable
|
||||
{
|
||||
final public const string TABLE = 'action_logs';
|
||||
@@ -104,26 +107,6 @@ class ActionLog extends Model implements Nameable
|
||||
*/
|
||||
protected $primaryKey = ActionLog::ATTRIBUTE_ID;
|
||||
|
||||
/**
|
||||
* Boostrap the model.
|
||||
*/
|
||||
protected static function boot(): void
|
||||
{
|
||||
parent::boot();
|
||||
|
||||
static::creating(function (ActionLog $actionLog): void {
|
||||
if ($actionLog->status === ActionLogStatus::RUNNING) {
|
||||
Session::put('currentActionLog', $actionLog->batch_id);
|
||||
}
|
||||
});
|
||||
|
||||
static::updating(function (ActionLog $actionLog): void {
|
||||
if ($actionLog->status === ActionLogStatus::FINISHED || $actionLog->status === ActionLogStatus::FAILED) {
|
||||
Session::forget('currentActionLog');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* When an exception is thrown, the current action logs should be handled.
|
||||
*/
|
||||
|
||||
@@ -8,7 +8,9 @@ use App\Events\Discord\DiscordThread\DiscordThreadDeleted;
|
||||
use App\Events\Discord\DiscordThread\DiscordThreadUpdated;
|
||||
use App\Models\BaseModel;
|
||||
use App\Models\Wiki\Anime;
|
||||
use App\Observers\Discord\DiscordThreadObserver;
|
||||
use Database\Factories\Discord\DiscordThreadFactory;
|
||||
use Illuminate\Database\Eloquent\Attributes\ObservedBy;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
@@ -20,6 +22,7 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
*
|
||||
* @method static DiscordThreadFactory factory(...$parameters)
|
||||
*/
|
||||
#[ObservedBy(DiscordThreadObserver::class)]
|
||||
class DiscordThread extends BaseModel
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
@@ -6,6 +6,8 @@ namespace App\Models\User\Submission;
|
||||
|
||||
use App\Models\Auth\User;
|
||||
use App\Models\BaseModel;
|
||||
use App\Observers\User\Submission\SubmissionVirtualObserver;
|
||||
use Illuminate\Database\Eloquent\Attributes\ObservedBy;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\Relation;
|
||||
|
||||
@@ -18,6 +20,7 @@ use Illuminate\Database\Eloquent\Relations\Relation;
|
||||
* @property int $user_id
|
||||
* @property User $user
|
||||
*/
|
||||
#[ObservedBy(SubmissionVirtualObserver::class)]
|
||||
class SubmissionVirtual extends BaseModel
|
||||
{
|
||||
final public const string TABLE = 'submission_virtuals';
|
||||
@@ -68,17 +71,6 @@ class SubmissionVirtual extends BaseModel
|
||||
'model',
|
||||
];
|
||||
|
||||
protected static function boot()
|
||||
{
|
||||
parent::boot();
|
||||
|
||||
static::creating(function (SubmissionVirtual $virtual): void {
|
||||
if (class_exists($model = $virtual->model_type)) {
|
||||
$virtual->model_type = Relation::getMorphAlias($model);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public function getName(): string
|
||||
{
|
||||
return strval($this->getKey());
|
||||
|
||||
@@ -21,9 +21,11 @@ use App\Models\Wiki\Anime;
|
||||
use App\Models\Wiki\Anime\Theme\AnimeThemeEntry;
|
||||
use App\Models\Wiki\Group;
|
||||
use App\Models\Wiki\Song;
|
||||
use App\Observers\Wiki\Anime\AnimeThemeObserver;
|
||||
use App\Scopes\WithoutInsertSongScope;
|
||||
use Database\Factories\Wiki\Anime\AnimeThemeFactory;
|
||||
use Elastic\ScoutDriverPlus\Searchable;
|
||||
use Illuminate\Database\Eloquent\Attributes\ObservedBy;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
@@ -47,6 +49,7 @@ use Illuminate\Support\Stringable;
|
||||
*
|
||||
* @method static AnimeThemeFactory factory(...$parameters)
|
||||
*/
|
||||
#[ObservedBy(AnimeThemeObserver::class)]
|
||||
class AnimeTheme extends BaseModel implements InteractsWithSchema, SoftDeletable
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Observers\Admin;
|
||||
|
||||
use App\Enums\Models\Admin\ActionLogStatus;
|
||||
use App\Models\Admin\ActionLog;
|
||||
use Illuminate\Support\Facades\Session;
|
||||
|
||||
class ActionLogObserver
|
||||
{
|
||||
/**
|
||||
* Handle the ActionLog "creating" event.
|
||||
*/
|
||||
public function creating(ActionLog $actionLog): void
|
||||
{
|
||||
if ($actionLog->status === ActionLogStatus::RUNNING) {
|
||||
Session::put('currentActionLog', $actionLog->batch_id);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the ActionLog "updating" event.
|
||||
*/
|
||||
public function updating(ActionLog $actionLog): void
|
||||
{
|
||||
if ($actionLog->status === ActionLogStatus::RUNNING) {
|
||||
Session::put('currentActionLog', $actionLog->batch_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Observers\Discord;
|
||||
|
||||
use App\Actions\Discord\DiscordThreadAction;
|
||||
use App\Models\Discord\DiscordThread;
|
||||
|
||||
class DiscordThreadObserver
|
||||
{
|
||||
/**
|
||||
* Handle the DiscordThread "updated" event.
|
||||
*/
|
||||
public function updated(DiscordThread $thread): void
|
||||
{
|
||||
DiscordThreadAction::getHttp()
|
||||
->put('/thread', $thread->toArray())
|
||||
->throw();
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the DiscordThread "deleted" event.
|
||||
*/
|
||||
public function deleted(DiscordThread $thread): void
|
||||
{
|
||||
DiscordThreadAction::getHttp()
|
||||
->delete('/thread', ['id' => $thread->getKey()])
|
||||
->throw();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Observers\User\Submission;
|
||||
|
||||
use App\Models\User\Submission\SubmissionVirtual;
|
||||
use Illuminate\Database\Eloquent\Relations\Relation;
|
||||
|
||||
class SubmissionVirtualObserver
|
||||
{
|
||||
/**
|
||||
* Handle the SubmissionVirtual "creating" event.
|
||||
*/
|
||||
public function creating(SubmissionVirtual $virtual): void
|
||||
{
|
||||
if (class_exists($model = $virtual->model_type)) {
|
||||
$virtual->model_type = Relation::getMorphAlias($model);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Observers\Wiki\Anime;
|
||||
|
||||
use App\Models\Wiki\Anime\AnimeTheme;
|
||||
|
||||
class AnimeThemeObserver
|
||||
{
|
||||
/**
|
||||
* Handle the AnimeTheme "created" event.
|
||||
*/
|
||||
public function created(AnimeTheme $theme): void
|
||||
{
|
||||
// Update the sequence attribute of the first theme when creating a new sequence theme.
|
||||
if ($theme->sequence >= 2) {
|
||||
$theme->anime->animethemes()->getQuery()
|
||||
->where(AnimeTheme::ATTRIBUTE_SEQUENCE)
|
||||
->where(AnimeTheme::ATTRIBUTE_TYPE, $theme->type)
|
||||
->update([AnimeTheme::ATTRIBUTE_SEQUENCE => 1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user