2015-12-13 04:42:28 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2021-10-08 16:23:45 +00:00
|
|
|
use App\Casts\SmartPlaylistRulesCast;
|
2024-01-18 11:13:05 +00:00
|
|
|
use App\Facades\License as LicenseFacade;
|
2024-09-15 13:33:59 +00:00
|
|
|
use App\Models\Song as Playable;
|
2022-11-27 15:29:29 +00:00
|
|
|
use App\Values\SmartPlaylistRuleGroupCollection;
|
2022-08-10 14:56:01 +00:00
|
|
|
use Carbon\Carbon;
|
2022-07-18 11:00:37 +00:00
|
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
2020-11-14 16:57:25 +00:00
|
|
|
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;
|
2021-10-08 16:23:45 +00:00
|
|
|
use Illuminate\Support\Collection;
|
2024-01-18 11:13:05 +00:00
|
|
|
use Illuminate\Support\Str;
|
2021-01-05 16:52:16 +00:00
|
|
|
use Laravel\Scout\Searchable;
|
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
|
2022-08-10 14:56:01 +00:00
|
|
|
* @property string $name
|
|
|
|
* @property bool $is_smart
|
|
|
|
* @property int $user_id
|
|
|
|
* @property User $user
|
2024-09-15 13:33:59 +00:00
|
|
|
* @property Collection<array-key, Playable> $playables
|
2022-11-27 15:29:29 +00:00
|
|
|
* @property ?SmartPlaylistRuleGroupCollection $rule_groups
|
|
|
|
* @property ?SmartPlaylistRuleGroupCollection $rules
|
2022-08-10 14:56:01 +00:00
|
|
|
* @property Carbon $created_at
|
2024-01-12 14:41:02 +00:00
|
|
|
* @property bool $own_songs_only
|
2024-04-18 11:27:07 +00:00
|
|
|
* @property Collection<array-key, User> $collaborators
|
2024-01-25 16:21:26 +00:00
|
|
|
* @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
|
2024-04-18 11:27:07 +00:00
|
|
|
* @property-read Collection<array-key, PlaylistFolder> $folders
|
2016-08-03 10:42:11 +00:00
|
|
|
*/
|
2015-12-13 04:42:28 +00:00
|
|
|
class Playlist extends Model
|
|
|
|
{
|
2021-01-05 16:52:16 +00:00
|
|
|
use Searchable;
|
2020-11-14 16:57:25 +00:00
|
|
|
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 = [];
|
2021-10-08 16:23:45 +00:00
|
|
|
|
2015-12-13 04:42:28 +00:00
|
|
|
protected $casts = [
|
2021-10-08 16:23:45 +00:00
|
|
|
'rules' => SmartPlaylistRulesCast::class,
|
2024-01-12 14:41:02 +00:00
|
|
|
'own_songs_only' => 'bool',
|
2015-12-13 04:42:28 +00:00
|
|
|
];
|
2021-10-08 16:23:45 +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-03-27 09:53:05 +00:00
|
|
|
protected $with = ['user', 'collaborators', 'folders'];
|
2024-01-18 11:13:05 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2024-09-15 13:33:59 +00:00
|
|
|
public function playables(): BelongsToMany
|
2015-12-13 04:42:28 +00:00
|
|
|
{
|
2024-09-15 13:33:59 +00:00
|
|
|
return $this->belongsToMany(Playable::class)
|
2024-03-28 09:18:14 +00:00
|
|
|
->withTimestamps()
|
2024-01-29 21:58:50 +00:00
|
|
|
->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);
|
|
|
|
}
|
2018-11-03 23:25:08 +00:00
|
|
|
|
2024-03-27 09:53:05 +00:00
|
|
|
public function folders(): BelongsToMany
|
2022-08-10 14:56:01 +00:00
|
|
|
{
|
2024-03-27 09:53:05 +00:00
|
|
|
return $this->belongsToMany(PlaylistFolder::class, null, null, 'folder_id');
|
2022-08-10 14:56:01 +00:00
|
|
|
}
|
|
|
|
|
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
|
2018-11-03 23:25:08 +00:00
|
|
|
{
|
2024-10-06 19:21:30 +00:00
|
|
|
return Attribute::get(fn (): bool => (bool) $this->rule_groups?->isNotEmpty())->shouldCache();
|
2021-10-08 16:23:45 +00:00
|
|
|
}
|
|
|
|
|
2022-07-18 11:00:37 +00:00
|
|
|
protected function ruleGroups(): Attribute
|
2021-10-08 16:23:45 +00:00
|
|
|
{
|
2022-07-18 11:00:37 +00:00
|
|
|
// aliasing the attribute to avoid confusion
|
|
|
|
return Attribute::get(fn () => $this->rules);
|
2018-11-03 23:25:08 +00:00
|
|
|
}
|
2021-01-05 16:52:16 +00:00
|
|
|
|
2024-02-24 15:37:01 +00:00
|
|
|
protected function cover(): Attribute
|
|
|
|
{
|
2024-10-06 19:21:30 +00:00
|
|
|
return Attribute::get(static fn (?string $value): ?string => playlist_cover_url($value))->shouldCache();
|
2024-02-24 15:37:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function coverPath(): Attribute
|
|
|
|
{
|
|
|
|
return Attribute::get(function () {
|
|
|
|
$cover = Arr::get($this->attributes, 'cover');
|
|
|
|
|
|
|
|
return $cover ? playlist_cover_path($cover) : null;
|
2024-10-06 19:21:30 +00:00
|
|
|
})->shouldCache();
|
2024-02-24 15:37:01 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
{
|
2024-03-27 09:53:05 +00:00
|
|
|
return $this->folders->contains($folder);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getFolder(?User $contextUser = null): ?PlaylistFolder
|
|
|
|
{
|
|
|
|
return $this->folders->firstWhere(
|
|
|
|
fn (PlaylistFolder $folder) => $folder->user->is($contextUser ?? $this->user)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getFolderId(?User $user = null): ?string
|
|
|
|
{
|
|
|
|
return $this->getFolder($user)?->id;
|
2024-01-18 11:13:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function addCollaborator(User $user): void
|
|
|
|
{
|
|
|
|
if (!$this->hasCollaborator($user)) {
|
|
|
|
$this->collaborators()->attach($user);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-27 09:53:05 +00:00
|
|
|
public function hasCollaborator(User $collaborator): bool
|
2024-01-18 11:13:05 +00:00
|
|
|
{
|
2024-03-27 09:53:05 +00:00
|
|
|
return $this->collaborators->contains(static fn (User $user): bool => $collaborator->is($user));
|
2024-01-18 11:13:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-09-15 13:33:59 +00:00
|
|
|
* @param Collection|array<array-key, Playable>|Playable|array<string> $playables
|
2024-01-18 11:13:05 +00:00
|
|
|
*/
|
2024-09-15 13:33:59 +00:00
|
|
|
public function addPlayables(Collection|Playable|array $playables, ?User $collaborator = null): void
|
2024-01-18 11:13:05 +00:00
|
|
|
{
|
|
|
|
$collaborator ??= $this->user;
|
2024-09-15 13:33:59 +00:00
|
|
|
$maxPosition = $this->playables()->getQuery()->max('position') ?? 0;
|
2024-01-18 11:13:05 +00:00
|
|
|
|
2024-09-15 13:33:59 +00:00
|
|
|
if (!is_array($playables)) {
|
|
|
|
$playables = Collection::wrap($playables)->pluck('id')->all();
|
2024-01-18 11:13:05 +00:00
|
|
|
}
|
|
|
|
|
2024-01-29 21:58:50 +00:00
|
|
|
$data = [];
|
|
|
|
|
2024-09-15 13:33:59 +00:00
|
|
|
foreach ($playables as $playable) {
|
|
|
|
$data[$playable] = [
|
2024-01-29 21:58:50 +00:00
|
|
|
'position' => ++$maxPosition,
|
|
|
|
'user_id' => $collaborator->id,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2024-09-15 13:33:59 +00:00
|
|
|
$this->playables()->attach($data);
|
2024-01-18 11:13:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-09-15 13:33:59 +00:00
|
|
|
* @param Collection<array-key, Playable>|Playable|array<string> $playables
|
2024-01-18 11:13:05 +00:00
|
|
|
*/
|
2024-09-15 13:33:59 +00:00
|
|
|
public function removePlayables(Collection|Playable|array $playables): void
|
2024-01-18 11:13:05 +00:00
|
|
|
{
|
2024-09-15 13:33:59 +00:00
|
|
|
if (!is_array($playables)) {
|
|
|
|
$playables = Collection::wrap($playables)->pluck('id')->all();
|
2024-01-18 11:13:05 +00:00
|
|
|
}
|
|
|
|
|
2024-09-15 13:33:59 +00:00
|
|
|
$this->playables()->detach($playables);
|
2024-01-18 11:13:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function isCollaborative(): Attribute
|
|
|
|
{
|
2024-10-06 19:21:30 +00:00
|
|
|
return Attribute::get(
|
|
|
|
fn (): bool => !$this->is_smart && LicenseFacade::isPlus() && $this->collaborators->isNotEmpty()
|
|
|
|
)->shouldCache();
|
2024-01-18 11:13:05 +00:00
|
|
|
}
|
|
|
|
|
2024-06-04 11:10:44 +00:00
|
|
|
/** @inheritdoc */
|
2021-01-05 16:52:16 +00:00
|
|
|
public function toSearchableArray(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'id' => $this->id,
|
|
|
|
'name' => $this->name,
|
|
|
|
];
|
|
|
|
}
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|