2024-01-18 11:13:05 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
2024-01-25 16:21:26 +00:00
|
|
|
use App\Events\NewPlaylistCollaboratorJoined;
|
|
|
|
use App\Exceptions\CannotRemoveOwnerFromPlaylistException;
|
|
|
|
use App\Exceptions\NotAPlaylistCollaboratorException;
|
2024-01-29 21:58:50 +00:00
|
|
|
use App\Exceptions\OperationNotApplicableForSmartPlaylistException;
|
2024-01-25 16:21:26 +00:00
|
|
|
use App\Exceptions\PlaylistCollaborationTokenExpiredException;
|
2024-01-18 11:13:05 +00:00
|
|
|
use App\Facades\License;
|
|
|
|
use App\Models\Playlist;
|
|
|
|
use App\Models\PlaylistCollaborationToken;
|
|
|
|
use App\Models\User;
|
2024-01-24 22:39:47 +00:00
|
|
|
use App\Values\PlaylistCollaborator;
|
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
2024-01-18 11:13:05 +00:00
|
|
|
|
|
|
|
class PlaylistCollaborationService
|
|
|
|
{
|
2024-03-30 16:49:25 +00:00
|
|
|
public function __construct()
|
2024-01-18 11:13:05 +00:00
|
|
|
{
|
2024-03-30 16:49:25 +00:00
|
|
|
License::requirePlus();
|
|
|
|
}
|
2024-01-25 16:21:26 +00:00
|
|
|
|
2024-03-30 16:49:25 +00:00
|
|
|
public function createToken(Playlist $playlist): PlaylistCollaborationToken
|
|
|
|
{
|
2024-01-29 21:58:50 +00:00
|
|
|
throw_if($playlist->is_smart, OperationNotApplicableForSmartPlaylistException::class);
|
2024-01-18 11:13:05 +00:00
|
|
|
|
|
|
|
return $playlist->collaborationTokens()->create();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function acceptUsingToken(string $token, User $user): Playlist
|
|
|
|
{
|
|
|
|
/** @var PlaylistCollaborationToken $collaborationToken */
|
|
|
|
$collaborationToken = PlaylistCollaborationToken::query()->where('token', $token)->firstOrFail();
|
|
|
|
|
2024-01-25 16:21:26 +00:00
|
|
|
throw_if($collaborationToken->expired, PlaylistCollaborationTokenExpiredException::class);
|
2024-01-18 11:13:05 +00:00
|
|
|
|
|
|
|
if ($collaborationToken->playlist->ownedBy($user)) {
|
|
|
|
return $collaborationToken->playlist;
|
|
|
|
}
|
|
|
|
|
|
|
|
$collaborationToken->playlist->addCollaborator($user);
|
|
|
|
|
2024-01-25 16:21:26 +00:00
|
|
|
// Now that we have at least one external collaborator, the songs in the playlist should be made public.
|
|
|
|
// Here we dispatch an event for that to happen.
|
|
|
|
event(new NewPlaylistCollaboratorJoined($user, $collaborationToken));
|
|
|
|
|
2024-01-18 11:13:05 +00:00
|
|
|
return $collaborationToken->playlist;
|
|
|
|
}
|
2024-01-24 22:39:47 +00:00
|
|
|
|
2024-04-18 11:27:07 +00:00
|
|
|
/** @return Collection<array-key, PlaylistCollaborator> */
|
2024-01-24 22:39:47 +00:00
|
|
|
public function getCollaborators(Playlist $playlist): Collection
|
|
|
|
{
|
|
|
|
return $playlist->collaborators->unless(
|
|
|
|
$playlist->collaborators->contains($playlist->user), // The owner is always a collaborator
|
|
|
|
static fn (Collection $collaborators) => $collaborators->push($playlist->user)
|
|
|
|
)
|
|
|
|
->map(static fn (User $user) => PlaylistCollaborator::fromUser($user));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function removeCollaborator(Playlist $playlist, User $user): void
|
|
|
|
{
|
2024-01-25 16:21:26 +00:00
|
|
|
throw_if($user->is($playlist->user), CannotRemoveOwnerFromPlaylistException::class);
|
|
|
|
throw_if(!$playlist->hasCollaborator($user), NotAPlaylistCollaboratorException::class);
|
|
|
|
|
2024-01-24 22:39:47 +00:00
|
|
|
DB::transaction(static function () use ($playlist, $user): void {
|
|
|
|
$playlist->collaborators()->detach($user);
|
|
|
|
$playlist->songs()->wherePivot('user_id', $user->id)->detach();
|
|
|
|
});
|
|
|
|
}
|
2024-01-18 11:13:05 +00:00
|
|
|
}
|