koel/app/Services/PlaylistService.php

151 lines
5.2 KiB
PHP
Raw Normal View History

<?php
namespace App\Services;
use App\Exceptions\OperationNotApplicableForSmartPlaylistException;
2024-01-18 11:13:05 +00:00
use App\Exceptions\PlaylistBothSongsAndRulesProvidedException;
use App\Facades\License;
use App\Models\Playlist;
use App\Models\PlaylistFolder as Folder;
use App\Models\Song as Playable;
use App\Models\User;
use App\Repositories\SongRepository;
use App\Values\SmartPlaylistRuleGroupCollection;
2024-01-18 11:13:05 +00:00
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use InvalidArgumentException;
use Webmozart\Assert\Assert;
class PlaylistService
{
2024-04-18 14:36:28 +00:00
public function __construct(private readonly SongRepository $songRepository)
{
}
public function createPlaylist(
string $name,
User $user,
?Folder $folder = null,
array $playables = [],
?SmartPlaylistRuleGroupCollection $ruleGroups = null,
bool $ownSongsOnly = false
): 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');
}
throw_if($playables && $ruleGroups, new PlaylistBothSongsAndRulesProvidedException());
2024-01-18 11:13:05 +00:00
throw_if($ownSongsOnly && (!$ruleGroups || !License::isPlus()), new InvalidArgumentException(
'"Own songs only" option only works with smart playlists and Plus license.'
));
return DB::transaction(
static function () use ($name, $user, $playables, $folder, $ruleGroups, $ownSongsOnly): Playlist {
$playlist = $user->playlists()->create([
'name' => $name,
'rules' => $ruleGroups,
'own_songs_only' => $ownSongsOnly,
]);
$folder?->playlists()->attach($playlist);
if (!$playlist->is_smart && $playables) {
$playlist->addPlayables($playables, $user);
}
return $playlist;
}
);
}
2022-07-04 10:39:02 +00:00
public function updatePlaylist(
Playlist $playlist,
string $name,
?Folder $folder = null,
?SmartPlaylistRuleGroupCollection $ruleGroups = null,
bool $ownSongsOnly = false
): 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');
}
throw_if($ownSongsOnly && (!$playlist->is_smart || !License::isPlus()), new InvalidArgumentException(
'"Own songs only" option only works with smart playlists and Plus license.'
));
$playlist->update([
'name' => $name,
'rules' => $ruleGroups,
'own_songs_only' => $ownSongsOnly,
]);
$folder?->playlists()->syncWithoutDetaching($playlist);
return $playlist;
}
/** @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)),
$user
);
// if the playlist is collaborative, make the songs public
if ($playlist->is_collaborative) {
$this->makePlaylistContentPublic($playlist);
}
// we want a fresh copy of the songs with the possibly updated visibility
return $this->songRepository->getManyInCollaborativeContext(
ids: $playables->pluck('id')->all(),
scopedUser: $user
);
});
2022-07-04 10:39:02 +00:00
}
public function removePlayablesFromPlaylist(Playlist $playlist, Collection|Playable|array $playables): void
2022-07-04 10:39:02 +00:00
{
$playlist->removePlayables($playables);
2022-07-04 10:39:02 +00:00
}
public function makePlaylistContentPublic(Playlist $playlist): void
{
2024-09-15 13:33:59 +00:00
$playlist->playables()->where('is_public', false)->update(['is_public' => true]);
}
public function movePlayablesInPlaylist(Playlist $playlist, array $movingIds, string $target, string $type): void
{
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');
$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()
->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);
});
}
}