unread-fixes

This commit is contained in:
2024-01-23 04:03:15 +03:00
parent 6f5b5c364a
commit e0395b0ab6
7 changed files with 45 additions and 969 deletions

View File

@@ -1,16 +1,22 @@
from services.rediscache import redis
import json
async def get_unread_counter(chat_id: str, author_id: int) -> int:
unread: int = await redis.execute("LLEN", f"chats/{chat_id}/unread/{author_id}") or 0
return unread
r = await redis.execute("LLEN", f"chats/{chat_id}/unread/{author_id}")
if isinstance(r, str):
return int(r)
elif isinstance(r, int):
return r
else:
return 0
async def get_total_unread_counter(author_id: int) -> int:
chats_set = await redis.execute("SMEMBERS", f"chats_by_author/{author_id}")
unread = 0
if chats_set:
for chat_id in list(chats_set):
n = await get_unread_counter(chat_id, author_id)
unread += n
return unread
s = 0
if isinstance(chats_set, str):
chats_set = json.loads(chats_set)
if isinstance(chats_set, list):
for chat_id in chats_set:
s += await get_unread_counter(chat_id, author_id)
return s