fix: Make Redis optional for city rooms online count

- Handle Redis connection errors gracefully
- Return rooms even if Redis is unavailable
- This fixes 500 error on /api/city/rooms endpoint
This commit is contained in:
Apple
2025-12-05 03:18:58 -08:00
parent 1f97586699
commit 33fcb04f65

View File

@@ -1165,8 +1165,13 @@ async def get_city_rooms(limit: int = 100, offset: int = 0):
try: try:
rooms = await repo_city.get_all_rooms(limit=limit, offset=offset) rooms = await repo_city.get_all_rooms(limit=limit, offset=offset)
# Додати online count (приблизно) # Додати online count (приблизно) - опціонально, якщо Redis недоступний
online_count = await PresenceRedis.get_online_count() online_count = 0
try:
online_count = await PresenceRedis.get_online_count()
except Exception as redis_error:
logger.warning(f"Redis unavailable for online count: {redis_error}")
online_count = 0
result = [] result = []
for room in rooms: for room in rooms:
@@ -1246,6 +1251,15 @@ async def get_city_rooms_api():
try: try:
rooms = await repo_city.get_all_rooms(limit=100, offset=0) rooms = await repo_city.get_all_rooms(limit=100, offset=0)
# Отримати online count опціонально (якщо Redis доступний)
online_count = 0
try:
from lib.presence_redis import PresenceRedis
online_count = await PresenceRedis.get_online_count()
except Exception:
# Redis недоступний - не критично
pass
result = [] result = []
for room in rooms: for room in rooms:
result.append({ result.append({
@@ -1257,7 +1271,7 @@ async def get_city_rooms_api():
"matrix_room_id": room.get("matrix_room_id"), "matrix_room_id": room.get("matrix_room_id"),
"is_public": room.get("is_public", True), "is_public": room.get("is_public", True),
"room_role": room.get("room_role"), "room_role": room.get("room_role"),
"members_online": room.get("members_online") or 0, "members_online": online_count if room.get("is_default") else max(1, online_count // 2),
"zone": room.get("zone"), "zone": room.get("zone"),
"room_type": room.get("room_type", "city"), "room_type": room.get("room_type", "city"),
}) })