Files
core/services/unread.py

23 lines
630 B
Python
Raw Normal View History

2023-10-05 22:59:50 +03: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 22:59:05 +03:00
"LLEN", f"chats/{chat_id}/unread/{author_id}"
2023-10-05 22:59:50 +03:00
)
2023-10-13 15:20:06 +03:00
return unread or 0
2023-10-05 22:59:50 +03:00
except Exception:
return 0
async def get_total_unread_counter(author_id: int):
2023-10-11 15:41:04 +03:00
print(f"[services.unread] get_total_unread_counter({author_id})")
2023-10-13 15:10:56 +03:00
chats = await redis.execute("SMEMBERS", f"chats_by_author/{author_id}")
2023-10-05 22:59:50 +03:00
unread = 0
2023-10-13 15:10:56 +03:00
for chat_id in list(chats):
2023-10-13 15:13:01 +03:00
n = await get_unread_counter(chat_id, author_id)
2023-10-13 15:10:56 +03:00
unread += n
2023-10-05 22:59:50 +03:00
return unread