2015-12-13 04:42:28 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2022-08-09 18:45:11 +00:00
|
|
|
use App\Builders\SongBuilder;
|
2024-05-19 05:49:42 +00:00
|
|
|
use App\Casts\Podcast\EpisodeMetadataCast;
|
2024-06-04 11:10:44 +00:00
|
|
|
use App\Casts\SongLyricsCast;
|
|
|
|
use App\Casts\SongStorageCast;
|
|
|
|
use App\Casts\SongTitleCast;
|
2024-05-31 05:40:34 +00:00
|
|
|
use App\Enums\PlayableType;
|
2024-04-18 17:20:14 +00:00
|
|
|
use App\Enums\SongStorageType;
|
2024-02-23 18:36:02 +00:00
|
|
|
use App\Models\Concerns\SupportsDeleteWhereValueNotIn;
|
2024-02-05 11:50:06 +00:00
|
|
|
use App\Values\SongStorageMetadata\DropboxMetadata;
|
2024-02-04 20:31:01 +00:00
|
|
|
use App\Values\SongStorageMetadata\LocalMetadata;
|
|
|
|
use App\Values\SongStorageMetadata\S3CompatibleMetadata;
|
2024-04-26 13:35:26 +00:00
|
|
|
use App\Values\SongStorageMetadata\S3LambdaMetadata;
|
|
|
|
use App\Values\SongStorageMetadata\SftpMetadata;
|
|
|
|
use App\Values\SongStorageMetadata\SongStorageMetadata;
|
2022-07-27 15:32:36 +00:00
|
|
|
use Carbon\Carbon;
|
2024-07-07 13:29:37 +00:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
2022-07-06 11:05:21 +00:00
|
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
2020-11-14 16:57:25 +00:00
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
2015-12-13 04:42:28 +00:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2017-08-05 16:56:11 +00:00
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
2018-11-03 23:25:08 +00:00
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
2022-08-01 10:42:33 +00:00
|
|
|
use Illuminate\Support\Str;
|
2020-12-23 10:53:00 +00:00
|
|
|
use Laravel\Scout\Searchable;
|
2024-05-19 05:49:42 +00:00
|
|
|
use PhanAn\Poddle\Values\EpisodeMetadata;
|
2024-02-05 21:17:41 +00:00
|
|
|
use Throwable;
|
2015-12-13 04:42:28 +00:00
|
|
|
|
|
|
|
/**
|
2020-12-22 20:11:22 +00:00
|
|
|
* @property string $path
|
|
|
|
* @property string $title
|
2024-07-07 13:29:37 +00:00
|
|
|
* @property ?Album $album
|
2024-01-03 17:02:18 +00:00
|
|
|
* @property User $uploader
|
2024-05-19 05:49:42 +00:00
|
|
|
* @property ?Artist $artist
|
|
|
|
* @property ?Artist $album_artist
|
2020-12-22 20:11:22 +00:00
|
|
|
* @property float $length
|
|
|
|
* @property string $lyrics
|
|
|
|
* @property int $track
|
|
|
|
* @property int $disc
|
|
|
|
* @property int $album_id
|
2022-09-23 06:21:29 +00:00
|
|
|
* @property int|null $year
|
|
|
|
* @property string $genre
|
2020-12-22 20:11:22 +00:00
|
|
|
* @property string $id
|
|
|
|
* @property int $artist_id
|
|
|
|
* @property int $mtime
|
2022-07-27 15:32:36 +00:00
|
|
|
* @property ?bool $liked Whether the song is liked by the current user (dynamically calculated)
|
|
|
|
* @property ?int $play_count The number of times the song has been played by the current user (dynamically calculated)
|
|
|
|
* @property Carbon $created_at
|
2024-01-03 17:02:18 +00:00
|
|
|
* @property int $owner_id
|
|
|
|
* @property bool $is_public
|
2024-01-09 23:26:16 +00:00
|
|
|
* @property User $owner
|
2024-02-05 11:50:06 +00:00
|
|
|
* @property-read SongStorageMetadata $storage_metadata
|
2024-04-18 17:20:14 +00:00
|
|
|
* @property SongStorageType $storage
|
2024-01-18 11:13:05 +00:00
|
|
|
*
|
|
|
|
* // The following are only available for collaborative playlists
|
|
|
|
* @property-read ?string $collaborator_email The email of the user who added the song to the playlist
|
|
|
|
* @property-read ?string $collaborator_name The name of the user who added the song to the playlist
|
2024-06-04 09:44:33 +00:00
|
|
|
* @property-read ?string $collaborator_avatar The avatar of the user who added the song to the playlist
|
2024-01-24 22:39:47 +00:00
|
|
|
* @property-read ?int $collaborator_id The ID of the user who added the song to the playlist
|
2024-01-18 11:13:05 +00:00
|
|
|
* @property-read ?string $added_at The date the song was added to the playlist
|
2024-05-31 14:51:10 +00:00
|
|
|
* @property-read PlayableType $type
|
2024-05-19 05:49:42 +00:00
|
|
|
*
|
|
|
|
* // Podcast episode properties
|
|
|
|
* @property ?EpisodeMetadata $episode_metadata
|
|
|
|
* @property ?string $episode_guid
|
2024-05-31 14:51:10 +00:00
|
|
|
* @property ?string $podcast_id
|
2024-05-19 05:49:42 +00:00
|
|
|
* @property ?Podcast $podcast
|
2015-12-13 04:42:28 +00:00
|
|
|
*/
|
|
|
|
class Song extends Model
|
|
|
|
{
|
2020-11-14 16:57:25 +00:00
|
|
|
use HasFactory;
|
2020-12-23 10:53:00 +00:00
|
|
|
use Searchable;
|
2022-08-01 10:42:33 +00:00
|
|
|
use SupportsDeleteWhereValueNotIn;
|
2016-09-26 07:32:16 +00:00
|
|
|
|
2022-08-01 10:42:33 +00:00
|
|
|
public const ID_REGEX = '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}';
|
|
|
|
|
2021-06-05 10:47:56 +00:00
|
|
|
public $incrementing = false;
|
2015-12-13 04:42:28 +00:00
|
|
|
protected $guarded = [];
|
|
|
|
|
2022-07-06 11:05:21 +00:00
|
|
|
protected $hidden = ['updated_at', 'path', 'mtime'];
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2015-12-20 18:09:34 +00:00
|
|
|
protected $casts = [
|
2024-06-04 11:10:44 +00:00
|
|
|
'title' => SongTitleCast::class,
|
|
|
|
'lyrics' => SongLyricsCast::class,
|
2015-12-21 02:18:00 +00:00
|
|
|
'length' => 'float',
|
2016-03-22 08:22:39 +00:00
|
|
|
'mtime' => 'int',
|
2016-03-28 13:18:09 +00:00
|
|
|
'track' => 'int',
|
2017-12-03 10:02:31 +00:00
|
|
|
'disc' => 'int',
|
2024-01-03 17:02:18 +00:00
|
|
|
'is_public' => 'bool',
|
2024-06-04 11:10:44 +00:00
|
|
|
'storage' => SongStorageCast::class,
|
2024-05-19 05:49:42 +00:00
|
|
|
'episode_metadata' => EpisodeMetadataCast::class,
|
2015-12-20 18:09:34 +00:00
|
|
|
];
|
|
|
|
|
2024-05-19 05:49:42 +00:00
|
|
|
protected $with = ['album', 'artist', 'podcast'];
|
|
|
|
|
2020-09-06 18:21:39 +00:00
|
|
|
protected $keyType = 'string';
|
2018-11-03 23:25:08 +00:00
|
|
|
|
2022-08-01 10:42:33 +00:00
|
|
|
protected static function booted(): void
|
|
|
|
{
|
2024-06-07 12:11:45 +00:00
|
|
|
static::creating(static fn (Song $song) => $song->id ??= Str::uuid()->toString());
|
2022-08-01 10:42:33 +00:00
|
|
|
}
|
|
|
|
|
2024-06-05 15:49:23 +00:00
|
|
|
public static function query(?PlayableType $type = null, ?User $user = null): SongBuilder
|
2022-08-09 18:45:11 +00:00
|
|
|
{
|
2024-06-05 15:49:23 +00:00
|
|
|
return parent::query()
|
2024-07-07 13:29:37 +00:00
|
|
|
->when($type, static fn (Builder $query) => match ($type) { // @phpstan-ignore-line phpcs:ignore
|
|
|
|
PlayableType::SONG => $query->whereNull('songs.podcast_id'),
|
|
|
|
PlayableType::PODCAST_EPISODE => $query->whereNotNull('songs.podcast_id'),
|
|
|
|
default => $query,
|
2024-06-05 15:49:23 +00:00
|
|
|
})
|
2024-07-07 13:29:37 +00:00
|
|
|
->when($user, static fn (SongBuilder $query) => $query->forUser($user)); // @phpstan-ignore-line
|
2022-08-09 18:45:11 +00:00
|
|
|
}
|
|
|
|
|
2024-01-03 17:02:18 +00:00
|
|
|
public function owner(): BelongsTo
|
|
|
|
{
|
|
|
|
return $this->belongsTo(User::class);
|
|
|
|
}
|
|
|
|
|
2022-08-09 18:45:11 +00:00
|
|
|
public function newEloquentBuilder($query): SongBuilder
|
|
|
|
{
|
|
|
|
return new SongBuilder($query);
|
|
|
|
}
|
|
|
|
|
2021-06-05 10:47:56 +00:00
|
|
|
public function artist(): BelongsTo
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Artist::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function album(): BelongsTo
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Album::class);
|
|
|
|
}
|
|
|
|
|
2024-05-19 05:49:42 +00:00
|
|
|
public function podcast(): BelongsTo
|
2022-10-12 09:27:35 +00:00
|
|
|
{
|
2024-05-19 05:49:42 +00:00
|
|
|
return $this->belongsTo(Podcast::class);
|
2022-10-12 09:27:35 +00:00
|
|
|
}
|
|
|
|
|
2021-06-05 10:47:56 +00:00
|
|
|
public function playlists(): BelongsToMany
|
|
|
|
{
|
|
|
|
return $this->belongsToMany(Playlist::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function interactions(): HasMany
|
|
|
|
{
|
|
|
|
return $this->hasMany(Interaction::class);
|
|
|
|
}
|
|
|
|
|
2024-10-06 19:21:30 +00:00
|
|
|
protected function albumArtist(): Attribute
|
|
|
|
{
|
|
|
|
return Attribute::get(fn () => $this->album?->artist)->shouldCache();
|
|
|
|
}
|
|
|
|
|
2024-05-31 14:51:10 +00:00
|
|
|
protected function type(): Attribute
|
|
|
|
{
|
|
|
|
return Attribute::get(fn () => $this->podcast_id ? PlayableType::PODCAST_EPISODE : PlayableType::SONG);
|
|
|
|
}
|
|
|
|
|
2024-01-03 17:02:18 +00:00
|
|
|
public function accessibleBy(User $user): bool
|
|
|
|
{
|
2024-05-19 05:49:42 +00:00
|
|
|
if ($this->isEpisode()) {
|
|
|
|
return $user->subscribedToPodcast($this->podcast);
|
|
|
|
}
|
|
|
|
|
2024-01-27 11:24:34 +00:00
|
|
|
return $this->is_public || $this->ownedBy($user);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function ownedBy(User $user): bool
|
|
|
|
{
|
|
|
|
return $this->owner_id === $user->id;
|
2024-01-03 17:02:18 +00:00
|
|
|
}
|
|
|
|
|
2024-02-04 20:31:01 +00:00
|
|
|
protected function storageMetadata(): Attribute
|
|
|
|
{
|
2024-10-06 19:21:30 +00:00
|
|
|
return (new Attribute(
|
2024-02-05 11:50:06 +00:00
|
|
|
get: function (): SongStorageMetadata {
|
2024-02-05 21:17:41 +00:00
|
|
|
try {
|
|
|
|
switch ($this->storage) {
|
2024-04-26 13:35:26 +00:00
|
|
|
case SongStorageType::SFTP:
|
|
|
|
preg_match('/^sftp:\\/\\/(.*)/', $this->path, $matches);
|
|
|
|
return SftpMetadata::make($matches[1]);
|
|
|
|
|
2024-04-18 17:20:14 +00:00
|
|
|
case SongStorageType::S3:
|
2024-02-05 22:27:47 +00:00
|
|
|
preg_match('/^s3:\\/\\/(.*)\\/(.*)/', $this->path, $matches);
|
2024-02-05 21:17:41 +00:00
|
|
|
return S3CompatibleMetadata::make($matches[1], $matches[2]);
|
|
|
|
|
2024-04-18 17:20:14 +00:00
|
|
|
case SongStorageType::S3_LAMBDA:
|
2024-02-05 22:27:47 +00:00
|
|
|
preg_match('/^s3:\\/\\/(.*)\\/(.*)/', $this->path, $matches);
|
2024-04-26 13:35:26 +00:00
|
|
|
return S3LambdaMetadata::make($matches[1], $matches[2]);
|
2024-02-05 21:17:41 +00:00
|
|
|
|
2024-04-18 17:20:14 +00:00
|
|
|
case SongStorageType::DROPBOX:
|
2024-02-05 21:17:41 +00:00
|
|
|
preg_match('/^dropbox:\\/\\/(.*)/', $this->path, $matches);
|
|
|
|
return DropboxMetadata::make($matches[1]);
|
|
|
|
|
|
|
|
default:
|
|
|
|
return LocalMetadata::make($this->path);
|
|
|
|
}
|
|
|
|
} catch (Throwable) {
|
|
|
|
return LocalMetadata::make($this->path);
|
2024-02-05 11:50:06 +00:00
|
|
|
}
|
2024-02-04 20:31:01 +00:00
|
|
|
}
|
2024-10-06 19:21:30 +00:00
|
|
|
))->shouldCache();
|
2024-02-04 20:31:01 +00:00
|
|
|
}
|
|
|
|
|
2022-08-09 18:45:11 +00:00
|
|
|
public static function getPathFromS3BucketAndKey(string $bucket, string $key): string
|
2015-12-13 04:42:28 +00:00
|
|
|
{
|
2022-08-09 18:45:11 +00:00
|
|
|
return "s3://$bucket/$key";
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|
2016-04-17 15:38:06 +00:00
|
|
|
|
2024-06-04 13:35:00 +00:00
|
|
|
/** @inheritdoc */
|
2020-12-23 10:53:00 +00:00
|
|
|
public function toSearchableArray(): array
|
|
|
|
{
|
|
|
|
$array = [
|
|
|
|
'id' => $this->id,
|
|
|
|
'title' => $this->title,
|
2024-05-19 05:49:42 +00:00
|
|
|
'type' => $this->type->value,
|
2020-12-23 10:53:00 +00:00
|
|
|
];
|
|
|
|
|
2024-05-19 05:49:42 +00:00
|
|
|
if ($this->episode_metadata?->description) {
|
|
|
|
$array['episode_description'] = $this->episode_metadata->description;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->artist && !$this->artist->is_unknown && !$this->artist->is_various) {
|
2020-12-23 10:53:00 +00:00
|
|
|
$array['artist'] = $this->artist->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $array;
|
|
|
|
}
|
|
|
|
|
2024-05-19 05:49:42 +00:00
|
|
|
public function isEpisode(): bool
|
|
|
|
{
|
2024-05-31 05:40:34 +00:00
|
|
|
return $this->type === PlayableType::PODCAST_EPISODE;
|
2024-05-19 05:49:42 +00:00
|
|
|
}
|
|
|
|
|
2020-12-22 20:11:22 +00:00
|
|
|
public function __toString(): string
|
2017-06-24 20:46:55 +00:00
|
|
|
{
|
|
|
|
return $this->id;
|
|
|
|
}
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|