core/services/unread.py

25 lines
690 B
Python
Raw Normal View History

2024-01-23 01:03:15 +00:00
import json
2023-10-16 14:50:05 +00:00
2024-01-25 19:41:27 +00:00
from services.rediscache 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:
2024-02-21 16:14:58 +00:00
r = await redis.execute('LLEN', f'chats/{chat_id}/unread/{author_id}')
2024-01-23 01:03:15 +00:00
if isinstance(r, str):
return int(r)
elif isinstance(r, int):
return r
else:
return 0
2023-10-05 19:59:50 +00:00
2024-01-25 19:41:27 +00:00
2023-10-16 14:50:05 +00:00
async def get_total_unread_counter(author_id: int) -> int:
2024-02-21 16:14:58 +00:00
chats_set = await redis.execute('SMEMBERS', f'chats_by_author/{author_id}')
2024-01-23 01:03:15 +00:00
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