refactor: Improve code readability by formatting query parameters in user endpoints and enhancing error handling in sound playback

This commit is contained in:
JSC
2025-08-19 22:09:50 +02:00
parent a660cc1861
commit 560ccd3f7e
10 changed files with 177 additions and 84 deletions

View File

@@ -99,6 +99,40 @@ class SocketManager:
else:
logger.info("Unknown client %s disconnected", sid)
@self.sio.event
async def play_sound(sid: str, data: dict) -> None:
"""Handle play sound event from client."""
user_id = self.socket_users.get(sid)
if not user_id:
logger.warning("Play sound request from unknown client %s", sid)
return
sound_id = data.get("sound_id")
if not sound_id:
logger.warning(
"Play sound request missing sound_id from user %s", user_id,
)
return
try:
# Import here to avoid circular imports
from app.api.v1.sounds import play_sound_internal
# Call the internal play sound function
await play_sound_internal(int(sound_id), user_id)
logger.info("User %s played sound %s via WebSocket", user_id, sound_id)
except Exception as e:
logger.exception(
"Error playing sound %s for user %s: %s", sound_id, user_id, e,
)
# Emit error back to user
await self.sio.emit(
"sound_play_error",
{"sound_id": sound_id, "error": str(e)},
room=sid,
)
async def send_to_user(self, user_id: str, event: str, data: dict) -> bool:
"""Send a message to a specific user's room."""
room_id = self.user_rooms.get(user_id)