rework voice_rooms api
This commit is contained in:
@ -1,4 +1,5 @@
|
|||||||
from fastapi import APIRouter, Body
|
from fastapi import APIRouter, Body
|
||||||
|
from app.models.voice_rooms import JoinRoomRequest
|
||||||
from app.services.voice_rooms import VoiceRoomService
|
from app.services.voice_rooms import VoiceRoomService
|
||||||
from app.realtime.voice_hub import voice_hub
|
from app.realtime.voice_hub import voice_hub
|
||||||
|
|
||||||
@ -8,10 +9,39 @@ service = VoiceRoomService()
|
|||||||
|
|
||||||
@router.get("/rooms")
|
@router.get("/rooms")
|
||||||
async def list_rooms():
|
async def list_rooms():
|
||||||
rooms = await service.list_public_rooms()
|
rooms = await service.list_rooms()
|
||||||
for r in rooms:
|
result = []
|
||||||
r["users"] = len(voice_hub.rooms.get(r["id"], {}))
|
|
||||||
return rooms
|
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")
|
@router.post("/rooms")
|
||||||
@ -24,5 +54,5 @@ async def create_room(
|
|||||||
|
|
||||||
|
|
||||||
@router.post("/rooms/join")
|
@router.post("/rooms/join")
|
||||||
async def join_private(code: str = Body(...)):
|
async def join_private(payload: JoinRoomRequest):
|
||||||
return await service.join_by_code(code)
|
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:
|
class VoiceRoomService:
|
||||||
|
|
||||||
async def list_public_rooms(self):
|
async def list_rooms(self):
|
||||||
rooms = await voice_rooms_collection.find(
|
rooms = await voice_rooms_collection.find({}) \
|
||||||
{"public": True}
|
.sort("created_at", -1) \
|
||||||
).sort("created_at", -1).to_list(100)
|
.to_list(100)
|
||||||
|
|
||||||
return [_serialize(r) for r in rooms]
|
return rooms
|
||||||
|
|
||||||
async def create_room(
|
async def create_room(
|
||||||
self,
|
self,
|
||||||
@ -64,4 +64,10 @@ class VoiceRoomService:
|
|||||||
room = await voice_rooms_collection.find_one({"invite_code": code})
|
room = await voice_rooms_collection.find_one({"invite_code": code})
|
||||||
if not room:
|
if not room:
|
||||||
raise HTTPException(404, "Invalid invite code")
|
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