cached-storage-authors
All checks were successful
deploy / deploy (push) Successful in 1m3s

This commit is contained in:
2023-12-23 09:11:04 +03:00
parent e81eabd0d0
commit ce02ce0130
5 changed files with 141 additions and 85 deletions

View File

@@ -5,14 +5,17 @@ from typing import Any, Dict, List, Optional, Union
from models.chat import ChatPayload, Message
from resolvers.chats import create_chat
from services.auth import login_required
from services.core import get_all_authors
from services.core import CacheStorage
from services.rediscache import redis
from services.schema import query
async def get_unread_counter(chat_id: str, member_id: int) -> int:
unread = await redis.execute("LLEN", f"chats/{chat_id}/unread/{member_id}")
return unread or 0
if isinstance(unread, int):
return unread
else:
return 0
# NOTE: not an API handler
@@ -55,9 +58,9 @@ async def load_chats(_, info, limit: int = 50, offset: int = 0) -> Dict[str, Uni
chats = []
if author_id:
cids = await redis.execute("SMEMBERS", f"chats_by_author/{author_id}")
if cids:
if isinstance(cids, list):
members_online = (await redis.execute("SMEMBERS", "authors-online")) or []
cids = list(cids)[offset : (offset + limit)]
cids = cids[offset : (offset + limit)]
lock = asyncio.Lock()
if len(cids) == 0:
print(f"[resolvers.load] no chats for user with id={author_id}")
@@ -65,21 +68,18 @@ async def load_chats(_, info, limit: int = 50, offset: int = 0) -> Dict[str, Uni
print(f"[resolvers.load] created chat: {r['chat_id']}")
cids.append(r["chat"]["id"])
authors = get_all_authors()
authors_by_id = {a["id"]: a for a in authors}
for cid in cids:
async with lock:
chat_str: str = await redis.execute("GET", f"chats/{cid}")
print(f"[resolvers.load] redis GET by {cid}: {chat_str}")
if chat_str:
chat_str = await redis.execute("GET", f"chats/{cid}")
if isinstance(chat_str, str):
print(f"[resolvers.load] redis GET by {cid}: {chat_str}")
c: ChatPayload = json.loads(chat_str)
c["messages"] = await load_messages(cid, 5, 0)
c["messages"] = (await load_messages(cid, 5, 0)) or []
c["unread"] = await get_unread_counter(cid, author_id)
member_ids = c["members"].copy()
c["members"] = []
for member_id in member_ids:
a = authors_by_id.get(member_id)
a = CacheStorage.authors_by_id.get(member_id)
if a:
a["online"] = a.get("id") in members_online
c["members"].append(a)
@@ -92,9 +92,9 @@ async def load_chats(_, info, limit: int = 50, offset: int = 0) -> Dict[str, Uni
async def load_messages_by(_, info, by, limit: int = 10, offset: int = 0):
"""load :limit messages of :chat_id with :offset"""
author_id = info.context["author_id"]
author_chats = (await redis.execute("SMEMBERS", "chats_by_author/" + str(author_id))) or []
author_chats = [c for c in author_chats]
if author_chats:
author_chats = (await redis.execute("SMEMBERS", "chats_by_author/" + str(author_id)))
if isinstance(author_chats, list):
author_chats = [c for c in author_chats]
messages = []
by_chat = by.get("chat")
if by_chat in author_chats:
@@ -105,7 +105,7 @@ async def load_messages_by(_, info, by, limit: int = 10, offset: int = 0):
messages = await load_messages(by_chat, limit, offset)
return {
"messages": sorted(
[m for m in messages if m.get("created_at")],
[m for m in messages if m and m.get("created_at")],
key=lambda m: m.get("created_at"),
),
"error": None,