From 9d1a4e90c9d75c245b12340cbea29ef46562d28c Mon Sep 17 00:00:00 2001 From: Untone Date: Mon, 18 Dec 2023 21:32:49 +0300 Subject: [PATCH] load-recipients-fix-2 --- resolvers/load.py | 61 ++++++++++++++++++++++++----------------------- services/auth.py | 4 +++- services/core.py | 8 ++++++- 3 files changed, 41 insertions(+), 32 deletions(-) diff --git a/resolvers/load.py b/resolvers/load.py index 08bc7a5..d30db16 100644 --- a/resolvers/load.py +++ b/resolvers/load.py @@ -52,36 +52,37 @@ async def load_messages( @login_required async def load_chats(_, info, limit: int = 50, offset: int = 0) -> Dict[str, Union[List[Dict[str, Any]], None]]: """load :limit chats of current user with :offset""" - author_id = info.context["author_id"] - cids = await redis.execute("SMEMBERS", f"chats_by_author/{author_id}") - chats = [] - if cids: - members_online = (await redis.execute("SMEMBERS", "authors-online")) or [] - cids = list(cids)[offset : (offset + limit)] - lock = asyncio.Lock() - if len(cids) == 0: - print(f"[resolvers.load] no chats for user with id={author_id}") - r = await create_chat(None, info, members=[1]) # member with id = 1 is discours - print(f"[resolvers.load] created chat: {r['chat_id']}") - cids.append(r["chat"]["id"]) - all_authors: List[ChatMember] = await get_all_authors() - authors = {a["id"]: a for a in all_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: - c: ChatPayload = json.loads(chat_str) - c["messages"] = await load_messages(cid, 5, 0) - c["unread"] = await get_unread_counter(cid, author_id) - member_ids = c["members"].copy() - c["members"] = [] - for member_id in member_ids: - a = authors.get(member_id) - if a: - a["online"] = a.get("id") in members_online - c["members"].append(a) - chats.append(c) + author_id = info.context.get("author_id") + if author_id: + cids = await redis.execute("SMEMBERS", f"chats_by_author/{author_id}") + chats = [] + if cids: + members_online = (await redis.execute("SMEMBERS", "authors-online")) or [] + cids = list(cids)[offset : (offset + limit)] + lock = asyncio.Lock() + if len(cids) == 0: + print(f"[resolvers.load] no chats for user with id={author_id}") + r = await create_chat(None, info, members=[1]) # member with id = 1 is discours + print(f"[resolvers.load] created chat: {r['chat_id']}") + cids.append(r["chat"]["id"]) + all_authors: List[ChatMember] = await get_all_authors() + authors = {a["id"]: a for a in all_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: + c: ChatPayload = json.loads(chat_str) + c["messages"] = await load_messages(cid, 5, 0) + c["unread"] = await get_unread_counter(cid, author_id) + member_ids = c["members"].copy() + c["members"] = [] + for member_id in member_ids: + a = authors.get(member_id) + if a: + a["online"] = a.get("id") in members_online + c["members"].append(a) + chats.append(c) return {"chats": chats, "error": None} diff --git a/services/auth.py b/services/auth.py index 3de8d99..152f1f5 100644 --- a/services/auth.py +++ b/services/auth.py @@ -49,7 +49,7 @@ async def check_auth(req) -> str | None: print(f"[services.auth] {e}") if not user_id: - raise HTTPException(status_code=401,detail="Unauthorized") + raise HTTPException(status_code=401, detail="Unauthorized") def login_required(f): @@ -57,7 +57,9 @@ def login_required(f): async def decorated_function(*args, **kwargs): info = args[1] context = info.context + print("[login_required] context: " + str(context)) req = context.get("request") + print("[login_required] request: " + str(req)) user_id = await check_auth(req) if user_id: context["user_id"] = user_id diff --git a/services/core.py b/services/core.py index d3ce8bb..854dc07 100644 --- a/services/core.py +++ b/services/core.py @@ -23,7 +23,13 @@ async def get_all_authors(limit: int = 50, offset: int = 0) -> List[ChatMember]: query_name = "load_authors_all" gql = { - "query": "query { " + query_name + "(limit: " + str(limit) + ", offset: " + str(offset) +") { id slug pic name } }", + "query": "query { " + + query_name + + "(limit: " + + str(limit) + + ", offset: " + + str(offset) + + ") { id slug pic name } }", "variables": None, }