feat: Implement main playlist restrictions; add internal method for sound addition and update tests
This commit is contained in:
@@ -51,6 +51,16 @@ class PlaylistService:
|
||||
self.playlist_repo = PlaylistRepository(session)
|
||||
self.sound_repo = SoundRepository(session)
|
||||
|
||||
async def _is_main_playlist(self, playlist_id: int) -> bool:
|
||||
"""Check if the given playlist is the main playlist."""
|
||||
try:
|
||||
playlist = await self.playlist_repo.get_by_id(playlist_id)
|
||||
except Exception:
|
||||
logger.exception("Failed to check if playlist is main: %s", playlist_id)
|
||||
return False
|
||||
else:
|
||||
return playlist is not None and playlist.is_main
|
||||
|
||||
async def get_playlist_by_id(self, playlist_id: int) -> Playlist:
|
||||
"""Get a playlist by ID."""
|
||||
playlist = await self.playlist_repo.get_by_id(playlist_id)
|
||||
@@ -145,6 +155,13 @@ class PlaylistService:
|
||||
is_current: bool | None = None,
|
||||
) -> Playlist:
|
||||
"""Update a playlist."""
|
||||
# Check if this is the main playlist
|
||||
if await self._is_main_playlist(playlist_id):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="The main playlist cannot be edited",
|
||||
)
|
||||
|
||||
playlist = await self.get_playlist_by_id(playlist_id)
|
||||
|
||||
update_data: dict[str, Any] = {}
|
||||
@@ -186,6 +203,13 @@ class PlaylistService:
|
||||
|
||||
async def delete_playlist(self, playlist_id: int, user_id: int) -> None:
|
||||
"""Delete a playlist."""
|
||||
# Check if this is the main playlist
|
||||
if await self._is_main_playlist(playlist_id):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="The main playlist cannot be deleted",
|
||||
)
|
||||
|
||||
playlist = await self.get_playlist_by_id(playlist_id)
|
||||
|
||||
if not playlist.is_deletable:
|
||||
@@ -247,6 +271,13 @@ class PlaylistService:
|
||||
position: int | None = None,
|
||||
) -> None:
|
||||
"""Add a sound to a playlist."""
|
||||
# Check if this is the main playlist
|
||||
if await self._is_main_playlist(playlist_id):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Sounds cannot be added to the main playlist",
|
||||
)
|
||||
|
||||
# Verify playlist exists
|
||||
await self.get_playlist_by_id(playlist_id)
|
||||
|
||||
@@ -295,6 +326,13 @@ class PlaylistService:
|
||||
user_id: int,
|
||||
) -> None:
|
||||
"""Remove a sound from a playlist."""
|
||||
# Check if this is the main playlist
|
||||
if await self._is_main_playlist(playlist_id):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Sounds cannot be removed from the main playlist",
|
||||
)
|
||||
|
||||
# Verify playlist exists
|
||||
await self.get_playlist_by_id(playlist_id)
|
||||
|
||||
@@ -377,8 +415,23 @@ class PlaylistService:
|
||||
"total_play_count": total_plays,
|
||||
}
|
||||
|
||||
async def add_sound_to_main_playlist(self, sound_id: int, user_id: int) -> None:
|
||||
async def add_sound_to_main_playlist(
|
||||
self, sound_id: int, user_id: int, # noqa: ARG002
|
||||
) -> None:
|
||||
"""Add a sound to the global main playlist."""
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Sounds cannot be added to the main playlist",
|
||||
)
|
||||
|
||||
async def _add_sound_to_main_playlist_internal(
|
||||
self, sound_id: int, user_id: int,
|
||||
) -> None:
|
||||
"""Add sound to main playlist bypassing restrictions.
|
||||
|
||||
This method is intended for internal system use only (e.g., extraction service).
|
||||
It bypasses the main playlist modification restrictions.
|
||||
"""
|
||||
main_playlist = await self.get_main_playlist()
|
||||
|
||||
if main_playlist.id is None:
|
||||
@@ -395,7 +448,7 @@ class PlaylistService:
|
||||
):
|
||||
await self.playlist_repo.add_sound_to_playlist(main_playlist_id, sound_id)
|
||||
logger.info(
|
||||
"Added sound %s to main playlist for user %s",
|
||||
"Added sound %s to main playlist for user %s (internal)",
|
||||
sound_id,
|
||||
user_id,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user