2021-10-08 16:23:45 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
2024-01-29 21:58:50 +00:00
|
|
|
use App\Exceptions\OperationNotApplicableForSmartPlaylistException;
|
2024-01-18 11:13:05 +00:00
|
|
|
use App\Exceptions\PlaylistBothSongsAndRulesProvidedException;
|
2024-01-12 14:41:02 +00:00
|
|
|
use App\Facades\License;
|
2021-10-08 16:23:45 +00:00
|
|
|
use App\Models\Playlist;
|
2022-11-27 15:29:29 +00:00
|
|
|
use App\Models\PlaylistFolder as Folder;
|
2024-05-31 05:40:34 +00:00
|
|
|
use App\Models\Song as Playable;
|
2021-10-08 16:23:45 +00:00
|
|
|
use App\Models\User;
|
2024-01-25 16:21:26 +00:00
|
|
|
use App\Repositories\SongRepository;
|
2022-11-27 15:29:29 +00:00
|
|
|
use App\Values\SmartPlaylistRuleGroupCollection;
|
2024-01-18 11:13:05 +00:00
|
|
|
use Illuminate\Support\Collection;
|
2022-11-27 15:29:29 +00:00
|
|
|
use Illuminate\Support\Facades\DB;
|
2024-01-12 14:41:02 +00:00
|
|
|
use InvalidArgumentException;
|
2022-11-27 15:29:29 +00:00
|
|
|
use Webmozart\Assert\Assert;
|
2021-10-08 16:23:45 +00:00
|
|
|
|
|
|
|
class PlaylistService
|
|
|
|
{
|
2024-04-18 14:36:28 +00:00
|
|
|
public function __construct(private readonly SongRepository $songRepository)
|
2024-01-25 16:21:26 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-11-27 15:29:29 +00:00
|
|
|
public function createPlaylist(
|
|
|
|
string $name,
|
|
|
|
User $user,
|
|
|
|
?Folder $folder = null,
|
2024-05-31 05:40:34 +00:00
|
|
|
array $playables = [],
|
2024-01-12 14:41:02 +00:00
|
|
|
?SmartPlaylistRuleGroupCollection $ruleGroups = null,
|
|
|
|
bool $ownSongsOnly = false
|
2022-11-27 15:29:29 +00:00
|
|
|
): Playlist {
|
|
|
|
if ($folder) {
|
2024-01-18 11:13:05 +00:00
|
|
|
Assert::true($folder->ownedBy($user), 'The playlist folder does not belong to the user');
|
2022-11-27 15:29:29 +00:00
|
|
|
}
|
2021-10-08 16:23:45 +00:00
|
|
|
|
2024-05-31 05:40:34 +00:00
|
|
|
throw_if($playables && $ruleGroups, new PlaylistBothSongsAndRulesProvidedException());
|
2024-01-18 11:13:05 +00:00
|
|
|
|
2024-01-12 14:41:02 +00:00
|
|
|
throw_if($ownSongsOnly && (!$ruleGroups || !License::isPlus()), new InvalidArgumentException(
|
|
|
|
'"Own songs only" option only works with smart playlists and Plus license.'
|
|
|
|
));
|
2021-10-08 16:23:45 +00:00
|
|
|
|
2024-01-12 14:41:02 +00:00
|
|
|
return DB::transaction(
|
2024-05-31 05:40:34 +00:00
|
|
|
static function () use ($name, $user, $playables, $folder, $ruleGroups, $ownSongsOnly): Playlist {
|
2024-01-12 14:41:02 +00:00
|
|
|
$playlist = $user->playlists()->create([
|
|
|
|
'name' => $name,
|
|
|
|
'rules' => $ruleGroups,
|
|
|
|
'own_songs_only' => $ownSongsOnly,
|
|
|
|
]);
|
2022-11-27 15:29:29 +00:00
|
|
|
|
2024-03-27 09:53:05 +00:00
|
|
|
$folder?->playlists()->attach($playlist);
|
|
|
|
|
2024-05-31 05:40:34 +00:00
|
|
|
if (!$playlist->is_smart && $playables) {
|
|
|
|
$playlist->addPlayables($playables, $user);
|
2024-01-12 14:41:02 +00:00
|
|
|
}
|
2022-11-27 15:29:29 +00:00
|
|
|
|
2024-01-12 14:41:02 +00:00
|
|
|
return $playlist;
|
2022-11-27 15:29:29 +00:00
|
|
|
}
|
2024-01-12 14:41:02 +00:00
|
|
|
);
|
2021-10-08 16:23:45 +00:00
|
|
|
}
|
2022-07-04 10:39:02 +00:00
|
|
|
|
2022-11-27 15:29:29 +00:00
|
|
|
public function updatePlaylist(
|
|
|
|
Playlist $playlist,
|
|
|
|
string $name,
|
|
|
|
?Folder $folder = null,
|
|
|
|
?SmartPlaylistRuleGroupCollection $ruleGroups = null,
|
2024-01-12 14:41:02 +00:00
|
|
|
bool $ownSongsOnly = false
|
2022-11-27 15:29:29 +00:00
|
|
|
): Playlist {
|
|
|
|
if ($folder) {
|
2024-01-18 11:13:05 +00:00
|
|
|
Assert::true($playlist->ownedBy($folder->user), 'The playlist folder does not belong to the user');
|
2022-11-27 15:29:29 +00:00
|
|
|
}
|
|
|
|
|
2024-01-12 14:41:02 +00:00
|
|
|
throw_if($ownSongsOnly && (!$playlist->is_smart || !License::isPlus()), new InvalidArgumentException(
|
|
|
|
'"Own songs only" option only works with smart playlists and Plus license.'
|
|
|
|
));
|
|
|
|
|
2022-08-10 14:56:01 +00:00
|
|
|
$playlist->update([
|
|
|
|
'name' => $name,
|
2022-11-27 15:29:29 +00:00
|
|
|
'rules' => $ruleGroups,
|
2024-01-12 14:41:02 +00:00
|
|
|
'own_songs_only' => $ownSongsOnly,
|
2022-08-10 14:56:01 +00:00
|
|
|
]);
|
|
|
|
|
2024-03-27 09:53:05 +00:00
|
|
|
$folder?->playlists()->syncWithoutDetaching($playlist);
|
|
|
|
|
2022-08-10 14:56:01 +00:00
|
|
|
return $playlist;
|
|
|
|
}
|
|
|
|
|
2024-05-31 05:40:34 +00:00
|
|
|
/** @return Collection<array-key, Playable> */
|
|
|
|
public function addPlayablesToPlaylist(
|
|
|
|
Playlist $playlist,
|
|
|
|
Collection|Playable|array $playables,
|
|
|
|
User $user
|
|
|
|
): Collection {
|
|
|
|
return DB::transaction(function () use ($playlist, $playables, $user) {
|
|
|
|
$playables = Collection::wrap($playables);
|
|
|
|
|
|
|
|
$playlist->addPlayables(
|
2024-09-15 13:33:59 +00:00
|
|
|
$playables->filter(static fn ($song): bool => !$playlist->playables->contains($song)),
|
2024-05-31 05:40:34 +00:00
|
|
|
$user
|
|
|
|
);
|
2024-01-25 16:21:26 +00:00
|
|
|
|
|
|
|
// if the playlist is collaborative, make the songs public
|
|
|
|
if ($playlist->is_collaborative) {
|
2024-05-31 05:40:34 +00:00
|
|
|
$this->makePlaylistContentPublic($playlist);
|
2024-01-25 16:21:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// we want a fresh copy of the songs with the possibly updated visibility
|
2024-01-29 21:58:50 +00:00
|
|
|
return $this->songRepository->getManyInCollaborativeContext(
|
2024-05-31 05:40:34 +00:00
|
|
|
ids: $playables->pluck('id')->all(),
|
2024-01-29 21:58:50 +00:00
|
|
|
scopedUser: $user
|
|
|
|
);
|
2024-01-25 16:21:26 +00:00
|
|
|
});
|
2022-07-04 10:39:02 +00:00
|
|
|
}
|
|
|
|
|
2024-05-31 05:40:34 +00:00
|
|
|
public function removePlayablesFromPlaylist(Playlist $playlist, Collection|Playable|array $playables): void
|
2022-07-04 10:39:02 +00:00
|
|
|
{
|
2024-05-31 05:40:34 +00:00
|
|
|
$playlist->removePlayables($playables);
|
2022-07-04 10:39:02 +00:00
|
|
|
}
|
2024-01-25 16:21:26 +00:00
|
|
|
|
2024-05-31 05:40:34 +00:00
|
|
|
public function makePlaylistContentPublic(Playlist $playlist): void
|
2024-01-25 16:21:26 +00:00
|
|
|
{
|
2024-09-15 13:33:59 +00:00
|
|
|
$playlist->playables()->where('is_public', false)->update(['is_public' => true]);
|
2024-01-25 16:21:26 +00:00
|
|
|
}
|
2024-01-29 21:58:50 +00:00
|
|
|
|
2024-05-31 05:40:34 +00:00
|
|
|
public function movePlayablesInPlaylist(Playlist $playlist, array $movingIds, string $target, string $type): void
|
2024-01-29 21:58:50 +00:00
|
|
|
{
|
|
|
|
Assert::oneOf($type, ['before', 'after']);
|
|
|
|
throw_if($playlist->is_smart, OperationNotApplicableForSmartPlaylistException::class);
|
|
|
|
|
|
|
|
DB::transaction(static function () use ($playlist, $movingIds, $target, $type): void {
|
2024-09-15 13:33:59 +00:00
|
|
|
$targetPosition = $playlist->playables()->wherePivot('song_id', $target)->value('position');
|
2024-01-29 21:58:50 +00:00
|
|
|
$insertPosition = $type === 'before' ? $targetPosition : $targetPosition + 1;
|
|
|
|
|
|
|
|
// create a "gap" for the moving songs by incrementing the position of the songs after the target
|
2024-09-15 13:33:59 +00:00
|
|
|
$playlist->playables()
|
2024-01-29 21:58:50 +00:00
|
|
|
->newPivotQuery()
|
|
|
|
->where('position', $type === 'before' ? '>=' : '>', $targetPosition)
|
|
|
|
->whereNotIn('song_id', $movingIds)
|
|
|
|
->increment('position', count($movingIds));
|
|
|
|
|
|
|
|
$values = [];
|
|
|
|
|
|
|
|
foreach ($movingIds as $id) {
|
|
|
|
$values[$id] = ['position' => $insertPosition++];
|
|
|
|
}
|
|
|
|
|
2024-09-15 13:33:59 +00:00
|
|
|
$playlist->playables()->syncWithoutDetaching($values);
|
2024-01-29 21:58:50 +00:00
|
|
|
});
|
|
|
|
}
|
2021-10-08 16:23:45 +00:00
|
|
|
}
|