rework voice_rooms api
This commit is contained in:
@ -1,4 +1,5 @@
|
||||
from fastapi import APIRouter, Body
|
||||
from app.models.voice_rooms import JoinRoomRequest
|
||||
from app.services.voice_rooms import VoiceRoomService
|
||||
from app.realtime.voice_hub import voice_hub
|
||||
|
||||
@ -8,10 +9,39 @@ service = VoiceRoomService()
|
||||
|
||||
@router.get("/rooms")
|
||||
async def list_rooms():
|
||||
rooms = await service.list_public_rooms()
|
||||
for r in rooms:
|
||||
r["users"] = len(voice_hub.rooms.get(r["id"], {}))
|
||||
return rooms
|
||||
rooms = await service.list_rooms()
|
||||
result = []
|
||||
|
||||
for room in rooms:
|
||||
room_id = room["id"]
|
||||
users_map = voice_hub.rooms.get(room_id, {})
|
||||
usernames = list(users_map.keys())
|
||||
users_count = len(usernames)
|
||||
|
||||
if room["public"]:
|
||||
# 🟢 публичная — отдаём всё безопасное
|
||||
result.append({
|
||||
"id": room["id"],
|
||||
"name": room["name"],
|
||||
"public": True,
|
||||
"owner": room.get("owner"),
|
||||
"max_users": room.get("max_users"),
|
||||
"users": users_count,
|
||||
"usernames": usernames,
|
||||
"created_at": room["created_at"].isoformat()
|
||||
if room.get("created_at") else None,
|
||||
})
|
||||
else:
|
||||
# 🔒 приватная — ТОЛЬКО метаданные
|
||||
result.append({
|
||||
"name": room["name"],
|
||||
"public": False,
|
||||
"users": users_count,
|
||||
"usernames": usernames,
|
||||
"max_users": room.get("max_users"),
|
||||
})
|
||||
|
||||
return result
|
||||
|
||||
|
||||
@router.post("/rooms")
|
||||
@ -24,5 +54,5 @@ async def create_room(
|
||||
|
||||
|
||||
@router.post("/rooms/join")
|
||||
async def join_private(code: str = Body(...)):
|
||||
return await service.join_by_code(code)
|
||||
async def join_private(payload: JoinRoomRequest):
|
||||
return await service.join_by_code(payload.code)
|
||||
|
||||
5
app/models/voice_rooms.py
Normal file
5
app/models/voice_rooms.py
Normal file
@ -0,0 +1,5 @@
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class JoinRoomRequest(BaseModel):
|
||||
code: str
|
||||
@ -19,12 +19,12 @@ def _serialize(doc):
|
||||
|
||||
class VoiceRoomService:
|
||||
|
||||
async def list_public_rooms(self):
|
||||
rooms = await voice_rooms_collection.find(
|
||||
{"public": True}
|
||||
).sort("created_at", -1).to_list(100)
|
||||
async def list_rooms(self):
|
||||
rooms = await voice_rooms_collection.find({}) \
|
||||
.sort("created_at", -1) \
|
||||
.to_list(100)
|
||||
|
||||
return [_serialize(r) for r in rooms]
|
||||
return rooms
|
||||
|
||||
async def create_room(
|
||||
self,
|
||||
@ -64,4 +64,10 @@ class VoiceRoomService:
|
||||
room = await voice_rooms_collection.find_one({"invite_code": code})
|
||||
if not room:
|
||||
raise HTTPException(404, "Invalid invite code")
|
||||
return _serialize(room)
|
||||
|
||||
return {
|
||||
"id": room["id"],
|
||||
"name": room["name"],
|
||||
"public": False,
|
||||
"max_users": room.get("max_users"),
|
||||
}
|
||||
Reference in New Issue
Block a user