core/services/unread.py

20 lines
562 B
Python
Raw Normal View History

2023-10-05 19:59:50 +00:00
import json
2023-10-16 14:50:05 +00:00
from services.redis import redis
2023-10-05 19:59:50 +00:00
2023-10-16 14:50:05 +00:00
async def get_unread_counter(chat_id: str, author_id: int) -> int:
unread = await redis.execute("LLEN", f"chats/{chat_id}/unread/{author_id}")
return unread or 0
2023-10-05 19:59:50 +00:00
2023-10-16 14:50:05 +00:00
async def get_total_unread_counter(author_id: int) -> int:
chats = await redis.execute("GET", f"chats_by_author/{author_id}")
2023-10-05 19:59:50 +00:00
unread = 0
2023-10-16 14:50:05 +00:00
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
2023-10-05 19:59:50 +00:00
return unread