Skip to content

A self-hosted media server and file management system

5.7 KB
Raw Blame
<?php

namespace App\Models;

use App\Enums\ImageType;
use App\Enums\MediaType;
use App\Enums\SeriesRelationType;
use App\Traits\HasEditableFields;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Support\Collection;

class Series extends Model {
    use HasEditableFields, HasFactory;

    /**
     * id                   -> int8 (pk) (index)
     * uuid                 -> uuid (unique) (index)
     * folder_id            -> int8 (fk) (nullable) (index)
     * composite_id         -> varchar(255) (index)
     *
     * title                -> varchar(255) (nullable)
     * description          -> text (nullable)
     * studio               -> varchar(255) (nullable)
     * rating               -> int2 (nullable)
     * seasons              -> int4 (nullable)
     * episodes             -> int4 (nullable)
     * films                -> int4 (nullable)
     * thumbnail_url        -> varchar(255) (nullable)
     * editor_id            -> int8 (fk) (nullable)
     *
     * created_at           -> timestamp (nullable)
     * updated_at           -> timestamp (nullable)
     *
     * total_size           -> int8
     * raw_thumbnail_url    -> varchar(255) (nullable)
     * primary_media_type   -> int2 (enum)
     *
     * edited_at            -> timestamp (nullable)
     * started_at           -> date (nullable)
     * ended_at             -> date (nullable)
     *
     * file_count           -> uint4 (default=0)
     *
     * avg_intro_duration   -> float(2) (default=90)
     *
     * primary_poster_id    -> int8 (fk) (nullable)
     * poster_updated_at    -> timestampTz (nullable)
     *
     * primary_banner_id    -> int8 (fk) (nullable)
     * banner_updated_at    -> timestampTz (nullable)
     */
    protected $fillable = [
        'uuid',
        'folder_id',
        'editor_id',
        'composite_id',
        'title',
        'description',
        'studio',
        'rating',
        'seasons',
        'episodes',
        'films',
        'avg_intro_duration',
        'started_at',
        'ended_at',
        'file_count',
        'thumbnail_url',
        'edited_at',
        'downloads_enabled',

        'primary_poster_id',
        'poster_updated_at',

        'primary_banner_id',
        'banner_updated_at',
    ];

    protected $casts = [
        'primary_media_type' => MediaType::class,
        'avg_intro_duration' => 'float',

        'created_at' => 'datetime',
        'updated_at' => 'datetime',

        'edited_at' => 'datetime',

        'poster_updated_at' => 'datetime',
        'banner_updated_at' => 'datetime',

        'downloads_enabled' => 'boolean',

        'rating' => 'integer',
        'seasons' => 'integer',
        'episodes' => 'integer',
        'films' => 'integer',
    ];

    public const IGNORED_EDITABLE_FIELDS = ['avg_intro_duration', 'rating', 'seasons', 'episodes', 'films', 'start_date', 'end_date', 'studio'];

    public function folder(): BelongsTo {
        return $this->belongsTo(Folder::class);
    }

    public function videos(): HasMany {
        return $this->hasMany(Video::class, 'folder_id', 'folder_id');
    }

    public function editor(): BelongsTo {
        return $this->belongsTo(User::class);
    }

    public function folderTags(): HasMany {
        return $this->hasMany(FolderTag::class);
    }

    public function sizeHistory(): HasMany {
        return $this->hasMany(SeriesSizeHistory::class);
    }

    public function images(): MorphMany {
        return $this->morphMany(Image::class, 'imageable', null, null, 'uuid');
    }

    public function primaryPoster(): BelongsTo {
        return $this->belongsTo(Image::class, 'primary_poster_id', 'id');
    }

    public function primaryBanner(): BelongsTo {
        return $this->belongsTo(Image::class, 'primary_banner_id', 'id');
    }

    public function directRelations(): BelongsToMany {
        return $this->belongsToMany(Series::class, 'series_relations', 'series_id', 'related_series_id')->withPivot('type')->withTimestamps();
    }

    public function inverseRelations(): BelongsToMany {
        return $this->belongsToMany(Series::class, 'series_relations', 'related_series_id', 'series_id')->withPivot('type');
    }

    public function allRelations(): Collection {
        $this->loadMissing(['directRelations', 'inverseRelations']);

        $direct = $this->directRelations->map(fn (Series $s) => [
            'series' => $s,
            'type' => SeriesRelationType::tryFrom($s->pivot->type),
        ]);

        $inverse = $this->inverseRelations->map(fn (Series $s) => [
            'series' => $s,
            'type' => SeriesRelationType::tryFrom($s->pivot->type)?->inverse(),
        ]);

        // should sort this by type or something
        return $direct->concat($inverse)->filter(fn ($r) => $r['type'] !== null)->sortBy(fn ($r) => $r['series']->id)->values();
    }

    // This is only for demo reset so it is not super important, only include fields that may have bad content
    protected function getEditableFields(): array {
        return [
            'editor_id',
            'title',
            'description',
            'studio',
            'thumbnail_url',
        ];
    }

    public static function getEditableImageTypes(): array {
        return [
            ImageType::POSTER->value,
            ImageType::BANNER->value,
        ];
    }

    public function downloadsEnabled(): bool {
        if (! $this->folder?->category?->downloadsEnabled()) {
            return false;
        }

        return $this->downloads_enabled;
    }
}