2023-10-05 19:59:50 +00:00
|
|
|
from services.redis import redis
|
|
|
|
import json
|
|
|
|
|
|
|
|
|
|
|
|
async def get_unread_counter(chat_id: str, author_id: int):
|
|
|
|
try:
|
|
|
|
unread = await redis.execute(
|
2023-10-11 19:59:05 +00:00
|
|
|
"LLEN", f"chats/{chat_id}/unread/{author_id}"
|
2023-10-05 19:59:50 +00:00
|
|
|
)
|
|
|
|
if unread:
|
|
|
|
return unread
|
|
|
|
except Exception:
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
|
async def get_total_unread_counter(author_id: int):
|
2023-10-11 12:41:04 +00:00
|
|
|
print(f"[services.unread] get_total_unread_counter({author_id})")
|
2023-10-05 19:59:50 +00:00
|
|
|
chats = await redis.execute("GET", f"chats_by_author/{author_id}")
|
|
|
|
unread = 0
|
|
|
|
if chats:
|
|
|
|
chats = json.loads(chats)
|
|
|
|
for chat_id in chats:
|
|
|
|
n = await get_unread_counter(chat_id.decode("utf-8"), author_id)
|
|
|
|
unread += n
|
|
|
|
return unread
|