From 4bf93b565b4f871970d39a41d45a0cfc6c2b2699 Mon Sep 17 00:00:00 2001 From: panzerotto Date: Sat, 2 Aug 2025 08:43:41 +0200 Subject: [PATCH] add filter to room list --- backend/src/app.py | 15 ++++++++++++--- backend/src/room.py | 1 + 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/backend/src/app.py b/backend/src/app.py index 946f38f..4df4376 100644 --- a/backend/src/app.py +++ b/backend/src/app.py @@ -11,7 +11,7 @@ from .room import Room from .song import Song, init_db, get_song_by_title_artist, add_song_in_db, get_song_by_uuid from .song_fetch import query_search, yt_get_audio_url, yt_search_song from .qrcode_gen import generate_qr -from .gps import is_within_range +from .gps import is_within_range, distance_between_coords, Coordinates dotenv.load_dotenv() @@ -75,7 +75,6 @@ def on_leave(data): def join(): room_id = request.args.get("room") code = request.args.get("code") - user_location = request.args.get("location") if room_id is None: return error("Missing room id") @@ -86,7 +85,7 @@ def join(): if room.pin is not None and room.pin != code: return error("Invalid code") - if is_within_range(user_location, room.coord, room.range_size) is False: + if room.distance > room.range_size: return error("You are not within the room range") return {"success": True, "ws": f"/ws/{room_id}"} @@ -164,6 +163,14 @@ def room_new(): @app.get("/api/room") def room(): + lat = request.args.get("lat") + lon = request.args.get("lon") + if lat and lon: + user_coords = Coordinates(latitude=float(lat), longitude=float(lon)) + else: + return error("Missing user coordinates") + + distance = distance_between_coords(user_coords, room.coord) return [ { "id": room.id, @@ -171,8 +178,10 @@ def room(): "private": room.pin is not None, "coords": room.coord, "range": room.range_size, + "distance": distance, } for room in state.rooms.values() + if distance <= room.range_size ] diff --git a/backend/src/room.py b/backend/src/room.py index 5109884..c986340 100644 --- a/backend/src/room.py +++ b/backend/src/room.py @@ -35,6 +35,7 @@ class Room: pin: int | None tags: set[str] range_size: int # in meters ?? + distance: float songs: dict[str, UserScoredSong] # all songs + user score (the playlist) history: list[Song] # all songs previously played