mirror of
https://github.com/AnimeThemes/animethemes-server.git
synced 2026-07-11 01:24:46 +02:00
feat: added encode type attribute (#820)
This commit is contained in:
@@ -8,6 +8,7 @@ use App\Actions\Storage\Base\UploadAction;
|
||||
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\Wiki\VideoOverlap;
|
||||
use App\Enums\Models\Wiki\VideoSource;
|
||||
use App\Models\Auth\User;
|
||||
@@ -176,12 +177,14 @@ class UploadVideoAction extends UploadAction
|
||||
protected function markEncode(Video $video): void
|
||||
{
|
||||
if ($encoder = $this->encoder) {
|
||||
// Delete any existing encodes if the video is being replaced.
|
||||
// Mark any existing encodes as old if the video is being replaced.
|
||||
Encode::query()
|
||||
->whereBelongsTo($video, Encode::RELATION_VIDEO)
|
||||
->delete();
|
||||
->where(Encode::ATTRIBUTE_TYPE, EncodeType::CURRENT->value)
|
||||
->update([Encode::ATTRIBUTE_TYPE => EncodeType::OLD->value]);
|
||||
|
||||
Encode::query()->create([
|
||||
Encode::ATTRIBUTE_TYPE => EncodeType::CURRENT->value,
|
||||
Encode::ATTRIBUTE_USER => $encoder->getKey(),
|
||||
Encode::ATTRIBUTE_VIDEO => $video->getKey(),
|
||||
]);
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Enums\Models\User;
|
||||
|
||||
/**
|
||||
* Enum EncodeType.
|
||||
*/
|
||||
enum EncodeType: int
|
||||
{
|
||||
case OLD = 0;
|
||||
case CURRENT = 1;
|
||||
}
|
||||
+16
-10
@@ -6,6 +6,7 @@ namespace App\Events\User\Encode;
|
||||
|
||||
use App\Actions\Models\List\Playlist\RemoveTrackAction;
|
||||
use App\Contracts\Events\ManagesTrackEvent;
|
||||
use App\Enums\Models\User\EncodeType;
|
||||
use App\Events\BaseEvent;
|
||||
use App\Models\List\Playlist;
|
||||
use App\Models\List\Playlist\PlaylistTrack;
|
||||
@@ -14,11 +15,11 @@ use Illuminate\Foundation\Events\Dispatchable;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
/**
|
||||
* Class EncodeDeleted.
|
||||
* Class EncodeUpdated.
|
||||
*
|
||||
* @extends BaseEvent<Encode>
|
||||
*/
|
||||
class EncodeDeleted extends BaseEvent implements ManagesTrackEvent
|
||||
class EncodeUpdated extends BaseEvent implements ManagesTrackEvent
|
||||
{
|
||||
use Dispatchable;
|
||||
use SerializesModels;
|
||||
@@ -50,16 +51,21 @@ class EncodeDeleted extends BaseEvent implements ManagesTrackEvent
|
||||
*/
|
||||
public function manageTrack(): void
|
||||
{
|
||||
$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 (
|
||||
$this->getModel()->wasChanged(Encode::ATTRIBUTE_TYPE) &&
|
||||
$this->getModel()->getAttribute(Encode::ATTRIBUTE_TYPE) === EncodeType::OLD->value
|
||||
) {
|
||||
$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();
|
||||
if ($track instanceof PlaylistTrack) {
|
||||
$removeAction = new RemoveTrackAction();
|
||||
|
||||
$removeAction->remove($track->playlist, $track);
|
||||
$removeAction->remove($track->playlist, $track);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -22,6 +22,7 @@ class BelongsTo extends ComponentsSelect
|
||||
protected string $relation = '';
|
||||
protected ?BaseResource $resource = null;
|
||||
protected bool $showCreateOption = false;
|
||||
protected bool $withSubtitle = true;
|
||||
|
||||
/**
|
||||
* This should reload after every method.
|
||||
@@ -84,6 +85,19 @@ class BelongsTo extends ComponentsSelect
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the subtitle should be shown.
|
||||
*
|
||||
* @param bool $condition
|
||||
* @return static
|
||||
*/
|
||||
public function withSubtitle(bool $condition = true): static
|
||||
{
|
||||
$this->withSubtitle = $condition;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Make the field searchable and use laravel scout if available.
|
||||
*
|
||||
@@ -94,7 +108,7 @@ class BelongsTo extends ComponentsSelect
|
||||
{
|
||||
$this->allowHtml();
|
||||
$this->searchable();
|
||||
$this->getOptionLabelUsing(fn ($state) => static::getSearchLabelWithBlade($model::find($state)));
|
||||
$this->getOptionLabelUsing(fn ($state) => static::getSearchLabelWithBlade($model::find($state), $this->withSubtitle));
|
||||
|
||||
$eagerLoads = method_exists($model, 'getEagerLoadsForSubtitle')
|
||||
? $model::getEagerLoadsForSubtitle()
|
||||
@@ -109,7 +123,7 @@ class BelongsTo extends ComponentsSelect
|
||||
->query(fn (Builder $query) => $query->with($eagerLoads))
|
||||
->take(25)
|
||||
->get()
|
||||
->mapWithKeys(fn (BaseModel $model) => [$model->getKey() => static::getSearchLabelWithBlade($model)])
|
||||
->mapWithKeys(fn (BaseModel $model) => [$model->getKey() => static::getSearchLabelWithBlade($model, $this->withSubtitle)])
|
||||
->toArray();
|
||||
});
|
||||
}
|
||||
@@ -121,7 +135,7 @@ class BelongsTo extends ComponentsSelect
|
||||
->with($eagerLoads)
|
||||
->take(25)
|
||||
->get()
|
||||
->mapWithKeys(fn (BaseModel|User $model) => [$model->getKey() => static::getSearchLabelWithBlade($model)])
|
||||
->mapWithKeys(fn (BaseModel|User $model) => [$model->getKey() => static::getSearchLabelWithBlade($model, $this->withSubtitle)])
|
||||
->toArray();
|
||||
});
|
||||
}
|
||||
@@ -130,13 +144,14 @@ class BelongsTo extends ComponentsSelect
|
||||
* Use the blade to make the results.
|
||||
*
|
||||
* @param BaseModel|User $model
|
||||
* @param bool $withSubtitle
|
||||
* @return string
|
||||
*/
|
||||
public static function getSearchLabelWithBlade(BaseModel|User $model): string
|
||||
public static function getSearchLabelWithBlade(BaseModel|User $model, bool $withSubtitle = true): string
|
||||
{
|
||||
return view('filament.components.select')
|
||||
->with('name', $model->getName())
|
||||
->with('subtitle', $model->getSubtitle())
|
||||
->with('subtitle', $withSubtitle ? $model->getSubtitle() : null)
|
||||
->with('image', $model instanceof User ? $model->getFilamentAvatarUrl() : null)
|
||||
->render();
|
||||
}
|
||||
|
||||
@@ -149,6 +149,7 @@ class FeaturedTheme extends BaseResource
|
||||
BelongsTo::make(FeaturedThemeModel::ATTRIBUTE_ENTRY)
|
||||
->resource(EntryResource::class)
|
||||
->live(true)
|
||||
->required()
|
||||
->rules([
|
||||
fn (Get $get) => function () use ($get) {
|
||||
return [
|
||||
@@ -165,6 +166,7 @@ class FeaturedTheme extends BaseResource
|
||||
Select::make(FeaturedThemeModel::ATTRIBUTE_VIDEO)
|
||||
->label(__('filament.resources.singularLabel.video'))
|
||||
->relationship(FeaturedThemeModel::RELATION_VIDEO, Video::ATTRIBUTE_FILENAME)
|
||||
->required()
|
||||
->rules([
|
||||
fn (Get $get) => function () use ($get) {
|
||||
return [
|
||||
|
||||
@@ -152,7 +152,7 @@ class UploadVideoTableAction extends UploadTableAction
|
||||
->resource(UserResource::class)
|
||||
->label(__('filament.actions.storage.upload.fields.encoder.name'))
|
||||
->helperText(__('filament.actions.storage.upload.fields.encoder.help'))
|
||||
->visible(Auth::user()->hasRole(Role::ADMIN->value))
|
||||
->withSubtitle(false)
|
||||
->default(Auth::id()),
|
||||
]),
|
||||
]),
|
||||
@@ -185,7 +185,6 @@ class UploadVideoTableAction extends UploadTableAction
|
||||
...VideoDiscordNotificationBulkAction::make()->getForm($form)->getComponents(),
|
||||
]),
|
||||
])
|
||||
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
@@ -178,7 +178,7 @@ class User extends Authenticatable implements MustVerifyEmail, Nameable, HasSubt
|
||||
*/
|
||||
public function getSubtitle(): string
|
||||
{
|
||||
return $this->email;
|
||||
return strval($this->getKey());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -5,8 +5,9 @@ declare(strict_types=1);
|
||||
namespace App\Models\User;
|
||||
|
||||
use App\Contracts\Models\Nameable;
|
||||
use App\Enums\Models\User\EncodeType;
|
||||
use App\Events\User\Encode\EncodeCreated;
|
||||
use App\Events\User\Encode\EncodeDeleted;
|
||||
use App\Events\User\Encode\EncodeUpdated;
|
||||
use App\Models\Auth\User;
|
||||
use App\Models\Wiki\Video;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
@@ -16,6 +17,7 @@ use Illuminate\Support\Str;
|
||||
/**
|
||||
* Class Encode.
|
||||
*
|
||||
* @property EncodeType $type
|
||||
* @property User $user
|
||||
* @property int $user_id
|
||||
* @property Video $video
|
||||
@@ -26,6 +28,7 @@ class Encode extends Model implements Nameable
|
||||
final public const TABLE = 'encodes';
|
||||
|
||||
final public const ATTRIBUTE_ID = 'encode_id';
|
||||
final public const ATTRIBUTE_TYPE = 'type';
|
||||
final public const ATTRIBUTE_USER = 'user_id';
|
||||
final public const ATTRIBUTE_VIDEO = 'video_id';
|
||||
|
||||
@@ -55,7 +58,7 @@ class Encode extends Model implements Nameable
|
||||
*/
|
||||
protected $dispatchesEvents = [
|
||||
'created' => EncodeCreated::class,
|
||||
'deleted' => EncodeDeleted::class,
|
||||
'updated' => EncodeUpdated::class,
|
||||
];
|
||||
|
||||
/**
|
||||
@@ -64,10 +67,23 @@ class Encode extends Model implements Nameable
|
||||
* @var list<string>
|
||||
*/
|
||||
protected $fillable = [
|
||||
Encode::ATTRIBUTE_TYPE,
|
||||
Encode::ATTRIBUTE_USER,
|
||||
Encode::ATTRIBUTE_VIDEO,
|
||||
];
|
||||
|
||||
/**
|
||||
* Get the attributes that should be cast.
|
||||
*
|
||||
* @return array<string, string>
|
||||
*/
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
Encode::ATTRIBUTE_TYPE => EncodeType::class,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get name.
|
||||
*
|
||||
|
||||
@@ -400,12 +400,12 @@ class Video extends BaseModel implements Streamable, Likeable, Viewable, HasAggr
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the encode that the video owns.
|
||||
* Get the encodes of the video.
|
||||
*
|
||||
* @return HasOne<Encode, $this>
|
||||
* @return HasMany<Encode, $this>
|
||||
*/
|
||||
public function encode(): HasOne
|
||||
public function encodes(): HasMany
|
||||
{
|
||||
return $this->hasOne(Encode::class, Encode::ATTRIBUTE_VIDEO);
|
||||
return $this->hasMany(Encode::class, Encode::ATTRIBUTE_VIDEO);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Models\User\Encode;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
if (! Schema::hasColumn(Encode::TABLE, Encode::ATTRIBUTE_TYPE)) {
|
||||
Schema::table(Encode::TABLE, function (Blueprint $table) {
|
||||
$table->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);
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -347,7 +347,7 @@ return [
|
||||
'confirmButtonText' => 'Upload',
|
||||
'fields' => [
|
||||
'encoder' => [
|
||||
'help' => 'The user that encoded the video. Default is the authenticated user. This field is only visible for admins.',
|
||||
'help' => 'The user that encoded the video. Default is the authenticated user.',
|
||||
'name' => 'Encoder',
|
||||
],
|
||||
'file' => [
|
||||
|
||||
Reference in New Issue
Block a user