diff --git a/backend/src/app.py b/backend/src/app.py index e171851..2024da5 100644 --- a/backend/src/app.py +++ b/backend/src/app.py @@ -9,6 +9,8 @@ from .room import Room from .song import Song, init_db, get_song_by_title_artist, add_song_in_db from .song_fetch import lastfm_query_search, download_song_mp3 +from typing import Any + dotenv.load_dotenv() @@ -21,6 +23,18 @@ db_conn = get_connection() state = State(app, db_conn.cursor()) init_db(state.db) +state.rooms[1000] = Room( + id=1000, + coord=(1.0, 5.5), + name="Test Room", + pin=None, + tags=set(), + songs={}, + history=[], + playing=[Song(uuid="", title="", artist="<artist>", tags=[], image_id="<img>", youtube_id="<yt>")], + playing_idx=0, +) + def error(msg: str, status: int = 400) -> Response: res = jsonify({"success": False, "error": msg}) @@ -28,6 +42,17 @@ def error(msg: str, status: int = 400) -> Response: return res +def ws_broadcast(event: str, *, room_id: int | None = None, data: Any) -> None: + final = {} + + if room_id is not None: + final["room_id"] = room_id + + final["data"] = data + + emit(event, final, broadcast=True, namespace="/") + + @app.get("/api/join") def join(): room_id = request.args.get("room") @@ -144,6 +169,8 @@ def add_song(): ## add in DB add_song_in_db(info.artist, info.title, info.tags, info.img_id, yt_id) + ## HERE: add song in the room list + return {"artist": info.artist, "title": info.title, "tags": info.tags, "image_id": info.img_id} diff --git a/backend/src/room.py b/backend/src/room.py index bee8b7f..8b67e4f 100644 --- a/backend/src/room.py +++ b/backend/src/room.py @@ -38,7 +38,8 @@ class Room: songs: dict[str, UserScoredSong] # all songs + user score (the playlist) history: list[Song] # all songs previously played - playing: list[Song] # queue + ## playing queue info + playing: list[Song] playing_idx: int def renew_queue(self): diff --git a/backend/src/song.py b/backend/src/song.py index 39871cc..3507904 100644 --- a/backend/src/song.py +++ b/backend/src/song.py @@ -18,7 +18,7 @@ def init_db(db: Cursor): """) -@dataclass(frozen=True) +@dataclass(frozen=True, slots=True, kw_only=True) class Song: uuid: str title: str