From f425fb55531dccd4bc503f89dcd1cf5e02c5cfc0 Mon Sep 17 00:00:00 2001 From: Zach Clendenen Date: Thu, 17 Sep 2026 16:56:34 -0500 Subject: [PATCH] refactor(streaming): read the joinable filter from one place The join walk and the joinable listing each spelled out what makes a session joinable, kept in step by a comment. Both now ask access. --- backend/endpoints/streaming.py | 28 ++++++++-------------------- backend/handler/streaming/access.py | 23 +++++++++++++++++++---- 2 files changed, 27 insertions(+), 24 deletions(-) diff --git a/backend/endpoints/streaming.py b/backend/endpoints/streaming.py index 038715c79..9bdfa745a 100644 --- a/backend/endpoints/streaming.py +++ b/backend/endpoints/streaming.py @@ -1103,16 +1103,13 @@ async def join_session( session = await get_live_session(candidate.key) if session is None: continue - # Same filter the joinable listing applies, so the button and the - # join agree on what is joinable. A member that fails it is not the - # answer and must not mask a later one that is. - if not session.get("multiplayer"): - continue if not session_platform_matches(session, platform): continue - if session.get("user_id") == request.user.id: - continue - if not access.session_rom_is_visible(request, session): + # A member the caller cannot join is not the answer and must not + # mask a later one that is. + if not access.session_is_joinable( + request, session, access.session_rom(session) + ): continue found = (candidate, session) break @@ -1510,19 +1507,10 @@ async def list_joinable_sessions( sessions: list[dict[str, Any]] = [] async for container_key, s in iter_live_sessions(): - if not s.get("multiplayer"): - continue - if s.get("user_id") == request.user.id: - continue if rom_id is not None and s.get("rom_id") != rom_id: continue - session_rom_id = s.get("rom_id") - rom = ( - db_rom_handler.get_rom_simple(session_rom_id) - if session_rom_id is not None - else None - ) - if not access.rom_is_visible(request, rom): + rom = access.session_rom(s) + if not access.session_is_joinable(request, s, rom): continue user_id = s.get("user_id") @@ -1532,7 +1520,7 @@ async def list_joinable_sessions( "container": container_key, "label": _joinable_container_label(grouped, container_key), "platform": s.get("platform"), - "rom_id": session_rom_id, + "rom_id": s.get("rom_id"), "rom_name": s.get("rom_name"), "host_username": host.username if host else None, "claimed_at": s.get("claimed_at"), diff --git a/backend/handler/streaming/access.py b/backend/handler/streaming/access.py index 6239be5cb..276e85616 100644 --- a/backend/handler/streaming/access.py +++ b/backend/handler/streaming/access.py @@ -54,12 +54,27 @@ def rom_is_visible(request: Request, rom: Rom | None) -> bool: return get_permissions(request).can_see_rom(rom.id, rom.platform_id) +def session_rom(session: dict[str, Any]) -> Rom | None: + """The ROM a session is running, None for a desktop or a deleted entry.""" + rom_id = session.get("rom_id") + return db_rom_handler.get_rom_simple(rom_id) if rom_id is not None else None + + def session_rom_is_visible(request: Request, session: dict[str, Any]) -> bool: """Can the caller see the ROM a session is running?""" - rom_id = session.get("rom_id") - if rom_id is None: - return True - return rom_is_visible(request, db_rom_handler.get_rom_simple(rom_id)) + return rom_is_visible(request, session_rom(session)) + + +def session_is_joinable( + request: Request, session: dict[str, Any], rom: Rom | None +) -> bool: + """Whether a live session is one this caller may ask to join: its host opted + into multiplayer, it is somebody else's, and its ROM is not hidden.""" + return ( + bool(session.get("multiplayer")) + and session.get("user_id") != request.user.id + and rom_is_visible(request, rom) + ) def assert_session_rom_visible(