Skip to content

Commit e4ffc14

Browse files
committed
moved get_connected_users to domain class
1 parent d183961 commit e4ffc14

File tree

2 files changed

+23
-23
lines changed

2 files changed

+23
-23
lines changed

src/backend/domain/pad.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,28 @@ async def release_worker(self) -> None:
340340
await self.cache()
341341
print(f"Released worker {old_worker_id[:8]} from pad {self.id}")
342342

343+
async def get_connected_users(self) -> List[Dict[str, str]]:
344+
"""Get all connected users from the pad users hash as a list of dicts with user_id and username."""
345+
key = f"pad:users:{self.id}"
346+
try:
347+
# Get all users from the hash
348+
all_users = await self._redis.hgetall(key)
349+
350+
# Convert to list of dicts with user_id and username
351+
connected_users = []
352+
for user_id, user_data_str in all_users.items():
353+
user_id_str = user_id.decode() if isinstance(user_id, bytes) else user_id
354+
user_data = json.loads(user_data_str.decode() if isinstance(user_data_str, bytes) else user_data_str)
355+
connected_users.append({
356+
"user_id": user_id_str,
357+
"username": user_data["username"]
358+
})
359+
360+
return connected_users
361+
except Exception as e:
362+
print(f"Error getting connected users from Redis for pad {self.id}: {e}")
363+
return []
364+
343365
def to_dict(self) -> Dict[str, Any]:
344366
"""Convert to dictionary representation"""
345367
return {

src/backend/routers/ws_router.py

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -337,28 +337,6 @@ async def remove_connection(redis_client: aioredis.Redis, pad_id: UUID, user_id:
337337
except Exception as e:
338338
print(f"Error removing connection from Redis: {e}")
339339

340-
async def get_connected_users(redis_client: aioredis.Redis, pad_id: UUID) -> List[Dict[str, str]]:
341-
"""Get all connected users from the pad users hash as a list of dicts with user_id and username."""
342-
key = f"pad:users:{pad_id}"
343-
try:
344-
# Get all users from the hash
345-
all_users = await redis_client.hgetall(key)
346-
347-
# Convert to list of dicts with user_id and username
348-
connected_users = []
349-
for user_id, user_data_str in all_users.items():
350-
user_id_str = user_id.decode() if isinstance(user_id, bytes) else user_id
351-
user_data = json.loads(user_data_str.decode() if isinstance(user_data_str, bytes) else user_data_str)
352-
connected_users.append({
353-
"user_id": user_id_str,
354-
"username": user_data["username"]
355-
})
356-
357-
return connected_users
358-
except Exception as e:
359-
print(f"Error getting connected users from Redis: {e}")
360-
return []
361-
362340
@ws_router.websocket("/ws/pad/{pad_id}")
363341
async def websocket_endpoint(
364342
websocket: WebSocket,
@@ -391,9 +369,9 @@ async def websocket_endpoint(
391369
redis_client = await RedisClient.get_instance()
392370

393371
await add_connection(redis_client, pad_id, str(user.id), user.username, connection_id)
394-
connected_users = await get_connected_users(redis_client, pad_id)
395372

396373
# Send connected message to client with connected users info
374+
connected_users = await pad.get_connected_users()
397375
connected_msg = WebSocketMessage(
398376
type="connected",
399377
pad_id=str(pad_id),

0 commit comments

Comments
 (0)