From a4ed8f257ea9a9bb3b89ab8848fbecf5f0104f20 Mon Sep 17 00:00:00 2001 From: Kyrch Date: Wed, 3 Sep 2025 16:02:35 -0300 Subject: [PATCH] clean: remove encode model (#952) --- .../Storage/Admin/Dump/DumpUserAction.php | 2 - .../Storage/Wiki/Video/UploadVideoAction.php | 40 +++--- app/Enums/Models/User/EncodeType.php | 11 -- app/Events/User/Encode/EncodeCreated.php | 58 --------- app/Events/User/Encode/EncodeUpdated.php | 54 -------- app/Models/User/Encode.php | 119 ------------------ app/Models/Wiki/Video.php | 9 -- ...2025_05_03_024718_create_encodes_table.php | 38 ------ ...02_add_type_attribute_to_encodes_table.php | 35 ------ 9 files changed, 26 insertions(+), 340 deletions(-) delete mode 100644 app/Enums/Models/User/EncodeType.php delete mode 100644 app/Events/User/Encode/EncodeCreated.php delete mode 100644 app/Events/User/Encode/EncodeUpdated.php delete mode 100644 app/Models/User/Encode.php delete mode 100644 database/migrations/2025_05_03_024718_create_encodes_table.php delete mode 100644 database/migrations/2025_05_20_062302_add_type_attribute_to_encodes_table.php diff --git a/app/Actions/Storage/Admin/Dump/DumpUserAction.php b/app/Actions/Storage/Admin/Dump/DumpUserAction.php index 5a43bcfd5..697e0bc7c 100644 --- a/app/Actions/Storage/Admin/Dump/DumpUserAction.php +++ b/app/Actions/Storage/Admin/Dump/DumpUserAction.php @@ -6,7 +6,6 @@ namespace App\Actions\Storage\Admin\Dump; use App\Concerns\Repositories\Admin\ReconcilesDumpRepositories; use App\Models\Aggregate\LikeAggregate; -use App\Models\User\Encode; use App\Models\User\Like; use App\Models\User\Notification; use App\Models\User\Report; @@ -29,7 +28,6 @@ class DumpUserAction extends DumpAction protected function allowedTables(): array { return [ - Encode::TABLE, Like::TABLE, LikeAggregate::TABLE, Notification::TABLE, diff --git a/app/Actions/Storage/Wiki/Video/UploadVideoAction.php b/app/Actions/Storage/Wiki/Video/UploadVideoAction.php index 73f7c227c..c6f9aed39 100644 --- a/app/Actions/Storage/Wiki/Video/UploadVideoAction.php +++ b/app/Actions/Storage/Wiki/Video/UploadVideoAction.php @@ -4,14 +4,17 @@ declare(strict_types=1); namespace App\Actions\Storage\Wiki\Video; +use App\Actions\Http\Api\List\Playlist\Track\StoreTrackAction; +use App\Actions\Models\List\Playlist\RemoveTrackAction; use App\Actions\Storage\Base\UploadAction; use App\Actions\Storage\Wiki\UploadedFileAction; use App\Actions\Storage\Wiki\Video\Script\UploadScriptAction; use App\Constants\Config\VideoConstants; use App\Contracts\Actions\Storage\StorageResults; -use App\Enums\Models\User\EncodeType; +use App\Enums\Models\List\PlaylistVisibility; use App\Models\Auth\User; -use App\Models\User\Encode; +use App\Models\List\Playlist; +use App\Models\List\Playlist\PlaylistTrack; use App\Models\Wiki\Anime\Theme\AnimeThemeEntry; use App\Models\Wiki\Video; use BackedEnum; @@ -60,7 +63,7 @@ class UploadVideoAction extends UploadAction $this->uploadScript($video); - $this->markEncode($video); + $this->addToPlaylist($video); DB::commit(); @@ -146,21 +149,30 @@ class UploadVideoAction extends UploadAction } /** - * Mark the encoder for the video. + * Add the track to the user that encoded the video. */ - protected function markEncode(Video $video): void + protected function addToPlaylist(Video $video): void { if ($encoder = $this->encoder) { - // Mark any existing encodes as old if the video is being replaced. - Encode::query() - ->whereBelongsTo($video, Encode::RELATION_VIDEO) - ->where(Encode::ATTRIBUTE_TYPE, EncodeType::CURRENT->value) - ->update([Encode::ATTRIBUTE_TYPE => EncodeType::OLD->value]); + $playlist = Playlist::query()->firstOrCreate([ + Playlist::ATTRIBUTE_NAME => 'Encodes', + Playlist::ATTRIBUTE_USER => $encoder->getKey(), + ], [ + Playlist::ATTRIBUTE_VISIBILITY => PlaylistVisibility::PRIVATE->value, + Playlist::ATTRIBUTE_DESCRIPTION => 'Auto-generated playlist for encodes.', + ]); - Encode::query()->create([ - Encode::ATTRIBUTE_TYPE => EncodeType::CURRENT->value, - Encode::ATTRIBUTE_USER => $encoder->getKey(), - Encode::ATTRIBUTE_VIDEO => $video->getKey(), + $track = $playlist->tracks()->getQuery() + ->where(PlaylistTrack::ATTRIBUTE_VIDEO, $video->getKey()) + ->first(); + + if ($track instanceof PlaylistTrack) { + new RemoveTrackAction()->remove($playlist, $track); + } + + new StoreTrackAction()->store($playlist, PlaylistTrack::query(), [ + PlaylistTrack::ATTRIBUTE_ENTRY => $video->animethemeentries->first()->getKey(), + PlaylistTrack::ATTRIBUTE_VIDEO => $video->getKey(), ]); } } diff --git a/app/Enums/Models/User/EncodeType.php b/app/Enums/Models/User/EncodeType.php deleted file mode 100644 index 5fa6f1532..000000000 --- a/app/Enums/Models/User/EncodeType.php +++ /dev/null @@ -1,11 +0,0 @@ - - */ -class EncodeCreated extends BaseEvent implements ManagesTrackEvent -{ - use Dispatchable; - use SerializesModels; - - public function __construct(Encode $encode) - { - parent::__construct($encode); - } - - public function getModel(): Encode - { - return $this->model; - } - - public function manageTrack(): void - { - $video = $this->getModel()->video; - - if ($video->animethemeentries->isEmpty()) { - return; - } - - $playlist = Playlist::query()->firstOrCreate([ - Playlist::ATTRIBUTE_NAME => 'Encodes', - Playlist::ATTRIBUTE_USER => $this->getModel()->user_id, - ], [ - Playlist::ATTRIBUTE_VISIBILITY => PlaylistVisibility::PRIVATE->value, - Playlist::ATTRIBUTE_DESCRIPTION => 'Auto-generated playlist for encodes.', - ]); - - $action = new StoreTrackAction(); - - $action->store($playlist, PlaylistTrack::query(), [ - PlaylistTrack::ATTRIBUTE_ENTRY => $video->animethemeentries->first()->getKey(), - PlaylistTrack::ATTRIBUTE_VIDEO => $video->getKey(), - ]); - } -} diff --git a/app/Events/User/Encode/EncodeUpdated.php b/app/Events/User/Encode/EncodeUpdated.php deleted file mode 100644 index 9ef41d8ae..000000000 --- a/app/Events/User/Encode/EncodeUpdated.php +++ /dev/null @@ -1,54 +0,0 @@ - - */ -class EncodeUpdated extends BaseEvent implements ManagesTrackEvent -{ - use Dispatchable; - use SerializesModels; - - public function __construct(Encode $encode) - { - parent::__construct($encode); - } - - public function getModel(): Encode - { - return $this->model; - } - - public function manageTrack(): void - { - if ( - $this->getModel()->wasChanged(Encode::ATTRIBUTE_TYPE) && - $this->getModel()->getAttribute(Encode::ATTRIBUTE_TYPE) === EncodeType::OLD - ) { - $track = PlaylistTrack::query() - ->with([PlaylistTrack::RELATION_PLAYLIST, PlaylistTrack::RELATION_VIDEO]) - ->whereRelation(PlaylistTrack::RELATION_PLAYLIST, Playlist::ATTRIBUTE_NAME, 'Encodes') - ->whereBelongsTo($this->getModel()->video, PlaylistTrack::RELATION_VIDEO) - ->first(); - - if ($track instanceof PlaylistTrack) { - $removeAction = new RemoveTrackAction(); - - $removeAction->remove($track->playlist, $track); - } - } - } -} diff --git a/app/Models/User/Encode.php b/app/Models/User/Encode.php deleted file mode 100644 index 1e3c8713d..000000000 --- a/app/Models/User/Encode.php +++ /dev/null @@ -1,119 +0,0 @@ - EncodeCreated::class, - 'updated' => EncodeUpdated::class, - ]; - - /** - * The attributes that are mass assignable. - * - * @var list - */ - protected $fillable = [ - Encode::ATTRIBUTE_TYPE, - Encode::ATTRIBUTE_USER, - Encode::ATTRIBUTE_VIDEO, - ]; - - /** - * Get the attributes that should be cast. - * - * @return array - */ - protected function casts(): array - { - return [ - Encode::ATTRIBUTE_TYPE => EncodeType::class, - ]; - } - - public function getName(): string - { - return Str::of($this->user->getName()) - ->append(' ') - ->append($this->video->getName()) - ->__toString(); - } - - public function getSubtitle(): string - { - return ''; - } - - /** - * @return BelongsTo - */ - public function user(): BelongsTo - { - return $this->belongsTo(User::class, Encode::ATTRIBUTE_USER); - } - - /** - * @return BelongsTo - */ - public function video(): BelongsTo - { - return $this->belongsTo(Video::class, Encode::ATTRIBUTE_VIDEO); - } -} diff --git a/app/Models/Wiki/Video.php b/app/Models/Wiki/Video.php index 3a8d68fed..ae00fd33a 100644 --- a/app/Models/Wiki/Video.php +++ b/app/Models/Wiki/Video.php @@ -26,7 +26,6 @@ use App\Http\Resources\Pivot\Wiki\Resource\AnimeThemeEntryVideoResource; use App\Models\BaseModel; use App\Models\List\Playlist; use App\Models\List\Playlist\PlaylistTrack; -use App\Models\User\Encode; use App\Models\Wiki\Anime\Theme\AnimeThemeEntry; use App\Models\Wiki\Video\VideoScript; use App\Pivots\Wiki\AnimeThemeEntryVideo; @@ -353,12 +352,4 @@ class Video extends BaseModel implements HasAggregateLikes, HasAggregateViews, L return $this->hasMany(PlaylistTrack::class, PlaylistTrack::ATTRIBUTE_VIDEO) ->whereRelation(PlaylistTrack::RELATION_PLAYLIST, Playlist::ATTRIBUTE_VISIBILITY, PlaylistVisibility::PUBLIC->value); } - - /** - * @return HasMany - */ - public function encodes(): HasMany - { - return $this->hasMany(Encode::class, Encode::ATTRIBUTE_VIDEO); - } } diff --git a/database/migrations/2025_05_03_024718_create_encodes_table.php b/database/migrations/2025_05_03_024718_create_encodes_table.php deleted file mode 100644 index ba15933a5..000000000 --- a/database/migrations/2025_05_03_024718_create_encodes_table.php +++ /dev/null @@ -1,38 +0,0 @@ -id(Encode::ATTRIBUTE_ID); - $table->unsignedBigInteger(Encode::ATTRIBUTE_USER); - $table->foreign(Encode::ATTRIBUTE_USER)->references(User::ATTRIBUTE_ID)->on(User::TABLE)->cascadeOnDelete(); - $table->unsignedBigInteger(Encode::ATTRIBUTE_VIDEO); - $table->foreign(Encode::ATTRIBUTE_VIDEO)->references(Video::ATTRIBUTE_ID)->on(Video::TABLE)->cascadeOnDelete(); - $table->timestamps(6); - }); - } - } - - /** - * Reverse the migrations. - */ - public function down(): void - { - Schema::dropIfExists(Encode::TABLE); - } -}; diff --git a/database/migrations/2025_05_20_062302_add_type_attribute_to_encodes_table.php b/database/migrations/2025_05_20_062302_add_type_attribute_to_encodes_table.php deleted file mode 100644 index c7b0370c6..000000000 --- a/database/migrations/2025_05_20_062302_add_type_attribute_to_encodes_table.php +++ /dev/null @@ -1,35 +0,0 @@ -integer(Encode::ATTRIBUTE_TYPE)->after(Encode::ATTRIBUTE_VIDEO); - }); - } - } - - /** - * Reverse the migrations. - */ - public function down(): void - { - if (Schema::hasColumn(Encode::TABLE, Encode::ATTRIBUTE_TYPE)) { - Schema::table(Encode::TABLE, function (Blueprint $table) { - $table->dropColumn(Encode::ATTRIBUTE_TYPE); - }); - } - } -};