koel/app/Models/Playlist.php

200 lines
5.4 KiB
PHP
Raw Normal View History

2015-12-13 04:42:28 +00:00
<?php
namespace App\Models;
use App\Casts\SmartPlaylistRulesCast;
2024-01-18 11:13:05 +00:00
use App\Facades\License as LicenseFacade;
use App\Values\SmartPlaylistRuleGroupCollection;
use Carbon\Carbon;
2022-07-18 11:00:37 +00:00
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
2015-12-14 13:22:39 +00:00
use Illuminate\Database\Eloquent\Model;
2017-08-05 18:55:02 +00:00
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
2024-01-18 11:13:05 +00:00
use Illuminate\Database\Eloquent\Relations\HasMany;
2024-02-24 15:37:01 +00:00
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
2024-01-18 11:13:05 +00:00
use Illuminate\Support\Str;
use Laravel\Scout\Searchable;
use LogicException;
2015-12-13 04:42:28 +00:00
2016-08-03 10:42:11 +00:00
/**
2024-01-18 11:13:05 +00:00
* @property string $id
* @property string $name
* @property bool $is_smart
* @property int $user_id
* @property User $user
* @property ?string $folder_id
* @property ?PlaylistFolder $folder
* @property Collection|array<array-key, Song> $songs
* @property array<string> $song_ids
* @property ?SmartPlaylistRuleGroupCollection $rule_groups
* @property ?SmartPlaylistRuleGroupCollection $rules
* @property Carbon $created_at
* @property bool $own_songs_only
2024-01-18 11:13:05 +00:00
* @property Collection|array<array-key, User> $collaborators
* @property-read bool $is_collaborative
2024-02-24 15:37:01 +00:00
* @property-read ?string $cover The playlist cover's URL
* @property-read ?string $cover_path
2016-08-03 10:42:11 +00:00
*/
2015-12-13 04:42:28 +00:00
class Playlist extends Model
{
use Searchable;
use HasFactory;
2015-12-13 04:42:28 +00:00
protected $hidden = ['user_id', 'created_at', 'updated_at'];
2024-01-18 11:13:05 +00:00
protected $guarded = [];
2015-12-13 04:42:28 +00:00
protected $casts = [
'rules' => SmartPlaylistRulesCast::class,
'own_songs_only' => 'bool',
2015-12-13 04:42:28 +00:00
];
2024-01-18 11:13:05 +00:00
public $incrementing = false;
protected $keyType = 'string';
2018-11-25 21:21:46 +00:00
protected $appends = ['is_smart'];
2024-01-18 11:13:05 +00:00
protected $with = ['collaborators'];
protected static function booted(): void
{
static::creating(static function (Playlist $playlist): void {
$playlist->id ??= Str::uuid()->toString();
});
}
2015-12-13 04:42:28 +00:00
2018-08-24 15:27:19 +00:00
public function songs(): BelongsToMany
2015-12-13 04:42:28 +00:00
{
return $this->belongsToMany(Song::class)->withTimestamps()
->withPivot('position')
->orderByPivot('position');
2015-12-13 04:42:28 +00:00
}
2018-08-24 15:27:19 +00:00
public function user(): BelongsTo
2015-12-13 04:42:28 +00:00
{
return $this->belongsTo(User::class);
}
public function folder(): BelongsTo
{
return $this->belongsTo(PlaylistFolder::class);
}
2024-01-18 11:13:05 +00:00
public function collaborationTokens(): HasMany
{
return $this->hasMany(PlaylistCollaborationToken::class);
}
public function collaborators(): BelongsToMany
{
return $this->belongsToMany(User::class, 'playlist_collaborators')->withTimestamps();
}
2022-07-18 11:00:37 +00:00
protected function isSmart(): Attribute
{
return Attribute::get(fn (): bool => (bool) $this->rule_groups?->isNotEmpty());
}
2022-07-18 11:00:37 +00:00
protected function ruleGroups(): Attribute
{
2022-07-18 11:00:37 +00:00
// aliasing the attribute to avoid confusion
return Attribute::get(fn () => $this->rules);
}
public function songIds(): Attribute
{
throw_if($this->is_smart, new LogicException('Smart playlist contents are generated dynamically.'));
return Attribute::get(fn () => $this->songs->pluck('id')->all());
}
2024-02-24 15:37:01 +00:00
protected function cover(): Attribute
{
return Attribute::get(static fn (?string $value): ?string => playlist_cover_url($value));
}
protected function coverPath(): Attribute
{
return Attribute::get(function () {
$cover = Arr::get($this->attributes, 'cover');
return $cover ? playlist_cover_path($cover) : null;
});
}
2024-01-18 11:13:05 +00:00
public function ownedBy(User $user): bool
{
return $this->user_id === $user->id;
}
public function inFolder(PlaylistFolder $folder): bool
{
return $this->folder_id === $folder->id;
}
public function addCollaborator(User $user): void
{
if (!$this->hasCollaborator($user)) {
$this->collaborators()->attach($user);
}
}
public function hasCollaborator(User $user): bool
{
return $this->collaborators->contains(static function (User $collaborator) use ($user): bool {
return $collaborator->is($user);
});
}
/**
* @param Collection|array<array-key, Song>|Song|array<string> $songs
*/
public function addSongs(Collection|Song|array $songs, ?User $collaborator = null): void
{
$collaborator ??= $this->user;
$maxPosition = $this->songs()->max('position') ?? 0;
2024-01-18 11:13:05 +00:00
if (!is_array($songs)) {
$songs = Collection::wrap($songs)->pluck('id')->all();
}
$data = [];
foreach ($songs as $song) {
$data[$song] = [
'position' => ++$maxPosition,
'user_id' => $collaborator->id,
];
}
$this->songs()->attach($data);
2024-01-18 11:13:05 +00:00
}
/**
* @param Collection|array<array-key, Song>|Song|array<string> $songs
*/
public function removeSongs(Collection|Song|array $songs): void
{
if (!is_array($songs)) {
$songs = Collection::wrap($songs)->pluck('id')->all();
}
$this->songs()->detach($songs);
}
protected function isCollaborative(): Attribute
{
return Attribute::get(fn (): bool => !$this->is_smart &&
LicenseFacade::isPlus()
&& $this->collaborators->isNotEmpty());
2024-01-18 11:13:05 +00:00
}
/** @return array<mixed> */
public function toSearchableArray(): array
{
return [
'id' => $this->id,
'name' => $this->name,
];
}
2015-12-13 04:42:28 +00:00
}