Files
Messenger/app/realtime/presence.py
benya 46dc601c84
Some checks failed
CI / test (push) Failing after 18s
feat(realtime): live online/offline events and unified search
- add websocket events user_online/user_offline
- broadcast presence changes on first connect and final disconnect only
- apply live presence updates in web chat store and realtime hook
- move public discover into unified left search (users + groups/channels)
- remove separate Discover Chats dialog/menu entry
2026-03-08 02:12:11 +03:00

54 lines
1.5 KiB
Python

from redis.exceptions import RedisError
from app.utils.redis_client import get_redis_client
async def mark_user_online(user_id: int) -> bool:
try:
redis = get_redis_client()
key = f"presence:user:{user_id}"
count = await redis.incr(key)
if count == 1:
await redis.expire(key, 3600)
return True
return False
except RedisError:
return False
async def mark_user_offline(user_id: int) -> bool:
try:
redis = get_redis_client()
key = f"presence:user:{user_id}"
value = await redis.decr(key)
if value <= 0:
await redis.delete(key)
return True
return False
except RedisError:
return False
async def is_user_online(user_id: int) -> bool:
try:
redis = get_redis_client()
value = await redis.get(f"presence:user:{user_id}")
return bool(value and str(value).isdigit() and int(value) > 0)
except RedisError:
return False
async def get_users_online_map(user_ids: list[int]) -> dict[int, bool]:
if not user_ids:
return {}
try:
redis = get_redis_client()
keys = [f"presence:user:{user_id}" for user_id in user_ids]
values = await redis.mget(keys)
return {
user_id: bool(value and str(value).isdigit() and int(value) > 0)
for user_id, value in zip(user_ids, values, strict=False)
}
except RedisError:
return {user_id: False for user_id in user_ids}