From 0c75902a64a646f6d14c78568776571487d28661 Mon Sep 17 00:00:00 2001 From: Untone Date: Mon, 16 Oct 2023 17:50:05 +0300 Subject: [PATCH] fix-unread --- services/unread.py | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/services/unread.py b/services/unread.py index 42ed1090..abe627e0 100644 --- a/services/unread.py +++ b/services/unread.py @@ -1,22 +1,19 @@ -from services.redis import redis import json - -async def get_unread_counter(chat_id: str, author_id: int): - try: - unread = await redis.execute( - "LLEN", f"chats/{chat_id}/unread/{author_id}" - ) - return unread or 0 - except Exception: - return 0 +from services.redis import redis -async def get_total_unread_counter(author_id: int): - print(f"[services.unread] get_total_unread_counter({author_id})") - chats = await redis.execute("SMEMBERS", f"chats_by_author/{author_id}") +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 + + +async def get_total_unread_counter(author_id: int) -> int: + chats = await redis.execute("GET", f"chats_by_author/{author_id}") unread = 0 - for chat_id in list(chats): - n = await get_unread_counter(chat_id, author_id) - unread += n + 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