import uuid from db.redis import ( add_presence, remove_presence, get_presence ) from integrations.livekit.client import get_livekit_api from livekit import api async def user_join_group(group_id: str | uuid.UUID, user_id: str | uuid.UUID): """ Called when websocket connects or LiveKit webhook received """ await add_presence(str(group_id), str(user_id)) async def user_leave_group(group_id: str | uuid.UUID, user_id: str | uuid.UUID): """ Called when websocket disconnects or LiveKit webhook received """ await remove_presence(str(group_id), str(user_id)) async def list_online_users(group_id: str | uuid.UUID, use_livekit: bool = False): """ Returns online users in a group. If use_livekit is True, fetches directly from LiveKit server. """ group_id_str = str(group_id) if use_livekit: lk_api = get_livekit_api() res = await lk_api.room.list_participants(api.ListParticipantsRequest(room=group_id_str)) online_users = [p.identity for p in res.participants] return online_users return await get_presence(group_id_str)