feat: Emit sound play count change event to connected clients after playing a sound

This commit is contained in:
JSC
2025-07-16 13:54:50 +02:00
parent 7226d87a77
commit ae238d3d18
2 changed files with 21 additions and 0 deletions

View File

@@ -59,6 +59,19 @@ def play_sound(sound_id: int):
) )
if success: if success:
# Get updated sound data to emit the new play count
sound = Sound.query.get(sound_id)
if sound:
# Emit sound_changed event to all connected clients
try:
from app.services.socketio_service import SocketIOService
SocketIOService.emit_sound_play_count_changed(sound_id, sound.play_count)
except Exception as e:
# Don't fail the request if socket emission fails
import logging
logger = logging.getLogger(__name__)
logger.warning(f"Failed to emit sound_play_count_changed event: {e}")
return jsonify({"message": "Sound playing", "sound_id": sound_id}) return jsonify({"message": "Sound playing", "sound_id": sound_id})
return ( return (
jsonify({"error": "Sound not found or cannot be played"}), jsonify({"error": "Sound not found or cannot be played"}),

View File

@@ -46,6 +46,14 @@ class SocketIOService:
{"credits": new_credits}, {"credits": new_credits},
) )
@staticmethod
def emit_sound_play_count_changed(sound_id: int, new_play_count: int) -> None:
"""Emit sound_play_count_changed event to all connected clients."""
SocketIOService.emit_to_all(
"sound_play_count_changed",
{"sound_id": sound_id, "play_count": new_play_count},
)
@staticmethod @staticmethod
def emit_credits_required(user_id: int, credits_needed: int) -> None: def emit_credits_required(user_id: int, credits_needed: int) -> None:
"""Emit an event when credits are required.""" """Emit an event when credits are required."""