feat: Add load_playlist method to PlayerService and update task handlers for playlist management
This commit is contained in:
@@ -429,6 +429,26 @@ class PlayerService:
|
|||||||
|
|
||||||
await self._broadcast_state()
|
await self._broadcast_state()
|
||||||
|
|
||||||
|
async def load_playlist(self, playlist_id: int) -> None:
|
||||||
|
"""Load a specific playlist by ID."""
|
||||||
|
session = self.db_session_factory()
|
||||||
|
try:
|
||||||
|
playlist_repo = PlaylistRepository(session)
|
||||||
|
playlist = await playlist_repo.get_by_id(playlist_id)
|
||||||
|
if playlist and playlist.id:
|
||||||
|
sounds = await playlist_repo.get_playlist_sounds(playlist.id)
|
||||||
|
await self._handle_playlist_reload(playlist, sounds)
|
||||||
|
logger.info(
|
||||||
|
"Loaded playlist: %s (%s sounds)",
|
||||||
|
playlist.name,
|
||||||
|
len(sounds),
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
logger.warning("Playlist not found: %s", playlist_id)
|
||||||
|
finally:
|
||||||
|
await session.close()
|
||||||
|
await self._broadcast_state()
|
||||||
|
|
||||||
async def _handle_playlist_reload(
|
async def _handle_playlist_reload(
|
||||||
self,
|
self,
|
||||||
current_playlist: Playlist,
|
current_playlist: Playlist,
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
"""Task execution handlers for different task types."""
|
"""Task execution handlers for different task types."""
|
||||||
|
|
||||||
import uuid
|
|
||||||
from typing import Any, Dict, Optional
|
from typing import Any, Dict, Optional
|
||||||
|
|
||||||
from sqlmodel.ext.asyncio.session import AsyncSession
|
from sqlmodel.ext.asyncio.session import AsyncSession
|
||||||
@@ -66,8 +65,12 @@ class TaskHandlerRegistry:
|
|||||||
|
|
||||||
if user_id:
|
if user_id:
|
||||||
# Recharge specific user
|
# Recharge specific user
|
||||||
user_uuid = uuid.UUID(user_id) if isinstance(user_id, str) else user_id
|
try:
|
||||||
stats = await self.credit_service.recharge_user_credits(user_uuid)
|
user_id_int = int(user_id)
|
||||||
|
except (ValueError, TypeError) as e:
|
||||||
|
raise TaskExecutionError(f"Invalid user_id format: {user_id}") from e
|
||||||
|
|
||||||
|
stats = await self.credit_service.recharge_user_credits(user_id_int)
|
||||||
logger.info(f"Recharged credits for user {user_id}: {stats}")
|
logger.info(f"Recharged credits for user {user_id}: {stats}")
|
||||||
else:
|
else:
|
||||||
# Recharge all users (system task)
|
# Recharge all users (system task)
|
||||||
@@ -83,12 +86,13 @@ class TaskHandlerRegistry:
|
|||||||
raise TaskExecutionError("sound_id parameter is required for PLAY_SOUND tasks")
|
raise TaskExecutionError("sound_id parameter is required for PLAY_SOUND tasks")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
sound_uuid = uuid.UUID(sound_id) if isinstance(sound_id, str) else sound_id
|
# Handle both integer and string sound IDs
|
||||||
|
sound_id_int = int(sound_id)
|
||||||
except (ValueError, TypeError) as e:
|
except (ValueError, TypeError) as e:
|
||||||
raise TaskExecutionError(f"Invalid sound_id format: {sound_id}") from e
|
raise TaskExecutionError(f"Invalid sound_id format: {sound_id}") from e
|
||||||
|
|
||||||
# Get the sound from database
|
# Get the sound from database
|
||||||
sound = await self.sound_repository.get_by_id(sound_uuid)
|
sound = await self.sound_repository.get_by_id(sound_id_int)
|
||||||
if not sound:
|
if not sound:
|
||||||
raise TaskExecutionError(f"Sound not found: {sound_id}")
|
raise TaskExecutionError(f"Sound not found: {sound_id}")
|
||||||
|
|
||||||
@@ -111,17 +115,18 @@ class TaskHandlerRegistry:
|
|||||||
raise TaskExecutionError("playlist_id parameter is required for PLAY_PLAYLIST tasks")
|
raise TaskExecutionError("playlist_id parameter is required for PLAY_PLAYLIST tasks")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
playlist_uuid = uuid.UUID(playlist_id) if isinstance(playlist_id, str) else playlist_id
|
# Handle both integer and string playlist IDs
|
||||||
|
playlist_id_int = int(playlist_id)
|
||||||
except (ValueError, TypeError) as e:
|
except (ValueError, TypeError) as e:
|
||||||
raise TaskExecutionError(f"Invalid playlist_id format: {playlist_id}") from e
|
raise TaskExecutionError(f"Invalid playlist_id format: {playlist_id}") from e
|
||||||
|
|
||||||
# Get the playlist from database
|
# Get the playlist from database
|
||||||
playlist = await self.playlist_repository.get_by_id(playlist_uuid)
|
playlist = await self.playlist_repository.get_by_id(playlist_id_int)
|
||||||
if not playlist:
|
if not playlist:
|
||||||
raise TaskExecutionError(f"Playlist not found: {playlist_id}")
|
raise TaskExecutionError(f"Playlist not found: {playlist_id}")
|
||||||
|
|
||||||
# Load playlist in player
|
# Load playlist in player
|
||||||
await self.player_service.load_playlist(playlist_uuid)
|
await self.player_service.load_playlist(playlist_id_int)
|
||||||
|
|
||||||
# Set play mode if specified
|
# Set play mode if specified
|
||||||
if play_mode in ["continuous", "loop", "loop_one", "random", "single"]:
|
if play_mode in ["continuous", "loop", "loop_one", "random", "single"]:
|
||||||
@@ -132,6 +137,6 @@ class TaskHandlerRegistry:
|
|||||||
self.player_service.set_shuffle(True)
|
self.player_service.set_shuffle(True)
|
||||||
|
|
||||||
# Start playing
|
# Start playing
|
||||||
self.player_service.play()
|
await self.player_service.play()
|
||||||
|
|
||||||
logger.info(f"Started playing playlist {playlist.name} via scheduled task")
|
logger.info(f"Started playing playlist {playlist.name} via scheduled task")
|
||||||
Reference in New Issue
Block a user