From 4ecdc08dfceafa8131b240378f6eb4ef118c6edd Mon Sep 17 00:00:00 2001 From: Untone Date: Tue, 14 Nov 2023 21:25:55 +0300 Subject: [PATCH] format+fix --- resolvers/chats.py | 2 +- resolvers/load.py | 17 ++++++++++------- server.py | 4 +--- services/auth.py | 2 +- services/core.py | 14 ++++++-------- services/presence.py | 2 +- 6 files changed, 20 insertions(+), 21 deletions(-) diff --git a/resolvers/chats.py b/resolvers/chats.py index a89a95c..7d9d8df 100644 --- a/resolvers/chats.py +++ b/resolvers/chats.py @@ -99,4 +99,4 @@ async def delete_chat(_, info, chat_id: str): await redis.execute("DEL", f"chats/{chat_id}") await redis.execute("SREM", f"chats_by_author/{author_id}", chat_id) else: - return {"error": "chat not exist"} \ No newline at end of file + return {"error": "chat not exist"} diff --git a/resolvers/load.py b/resolvers/load.py index fc0cd1b..9d88594 100644 --- a/resolvers/load.py +++ b/resolvers/load.py @@ -3,7 +3,7 @@ import json from typing import Any, Dict, List, Optional, Union from services.auth import login_required -from services.core import get_my_followings, members +from services.core import get_my_followings, get_all_authors from services.rediscache import redis from services.schema import query from validators.chat import Message, ChatPayload @@ -17,7 +17,9 @@ async def get_unread_counter(chat_id: str, author_id: int) -> int: # NOTE: not an API handler -async def load_messages(chat_id: str, limit: int = 5, offset: int = 0, ids: Optional[List[int]] = None) -> List[Message]: +async def load_messages( + chat_id: str, limit: int = 5, offset: int = 0, ids: Optional[List[int]] = None +) -> List[Message]: """load :limit messages for :chat_id with :offset""" if ids is None: ids = [] @@ -43,6 +45,7 @@ async def load_messages(chat_id: str, limit: int = 5, offset: int = 0, ids: Opti messages += await load_messages(chat_id, offset, limit, replies) except Exception: import traceback + traceback.print_exc() return messages @@ -73,7 +76,7 @@ async def load_chats(_, info, limit: int = 50, offset: int = 0) -> Dict[str, Uni member_ids = c["members"].copy() c["members"] = [] for member_id in member_ids: - for a in (await get_my_followings()): + for a in await get_my_followings(): a["online"] = a.get("id") in members_online c["members"].append(a) chats.append(c) @@ -112,16 +115,16 @@ async def load_recipients(_, _info, limit=50, offset=0): """load possible chat participants""" onliners = (await redis.execute("SMEMBERS", "authors-online")) or [] r = [] - all_authors: List[ChatMember] = members + all_authors: List[ChatMember] = await get_all_authors() my_followings = await get_my_followings() if len(my_followings) < limit: - my_followings = my_followings + all_authors[0:limit-len(my_followings)] + my_followings = my_followings + all_authors[0 : limit - len(my_followings)] for a in my_followings: a["online"] = a["id"] in onliners r.append(a) - + # NOTE: maybe sort members here print(f"[resolvers.load] loadRecipients found {len(r)} members") - + return {"members": r, "error": None} diff --git a/server.py b/server.py index 96425e5..45a093b 100644 --- a/server.py +++ b/server.py @@ -56,6 +56,4 @@ def exception_handler(_et, exc, _tb): if __name__ == "__main__": sys.excepthook = exception_handler - uvicorn.run( - "main:app", host="0.0.0.0", port=PORT, proxy_headers=True, server_header=True - ) + uvicorn.run("main:app", host="0.0.0.0", port=PORT, proxy_headers=True, server_header=True) diff --git a/services/auth.py b/services/auth.py index 30fb4ed..e78033f 100644 --- a/services/auth.py +++ b/services/auth.py @@ -16,7 +16,7 @@ async def check_auth(req): query_type = "mutation" if INTERNAL_AUTH_SERVER else "query" operation = "GetUserId" - headers = {"Authorization": token, "Content-Type": "application/json"} # "Bearer " + removed + headers = {"Authorization": token, "Content-Type": "application/json"} # "Bearer " + removed gql = { "query": query_type + " " + operation + " { " + query_name + " { user { id } } " + " }", diff --git a/services/core.py b/services/core.py index 208b5d6..db23cb3 100644 --- a/services/core.py +++ b/services/core.py @@ -2,14 +2,15 @@ from httpx import AsyncClient from settings import API_BASE headers = {"Content-Type": "application/json"} -INTERNAL_AUTH_SERVER = 'v2.' in API_BASE +INTERNAL_AUTH_SERVER = "v2." in API_BASE + async def get_all_authors(): query_name = "authorsAll" query_type = "query" operation = "AuthorsAll" query_fields = "id slug userpic name" - headers = {"Content-Type": "application/json"} # "Bearer " + removed + headers = {"Content-Type": "application/json"} # "Bearer " + removed gql = { "query": query_type + " " + operation + " { " + query_name + " { " + query_fields + " } " + " }", @@ -21,6 +22,7 @@ async def get_all_authors(): response = await client.post(API_BASE, headers=headers, json=gql) except Exception: import traceback + traceback.print_exc() print(f"[services.core] {query_name}: {response.status_code} {response.text}") if response.status_code != 200: @@ -34,7 +36,7 @@ async def get_my_followings(): query_type = "query" operation = "LoadMySubscriptions" query_fields = "id slug userpic name" - headers = {"Content-Type": "application/json"} # "Bearer " + removed + headers = {"Content-Type": "application/json"} # "Bearer " + removed gql = { "query": query_type + " " + operation + " { " + query_name + " { authors {" + query_fields + "} } " + " }", @@ -46,6 +48,7 @@ async def get_my_followings(): response = await client.post(API_BASE, headers=headers, json=gql) except Exception: import traceback + traceback.print_exc() print(f"[services.core] {query_name}: {response.status_code} {response.text}") if response.status_code != 200: @@ -53,8 +56,3 @@ async def get_my_followings(): r = response.json() authors = r.get("data", {}).get(query_name).get("authors", []) return authors - -members = get_all_authors() # [ { id slug name userpic }, ..] -print(f"[services.core] cache members amount: {len(members)}") -members_by_slug = {member['slug']: member for member in members} -members_by_id = {member['id']: member for member in members} \ No newline at end of file diff --git a/services/presence.py b/services/presence.py index 35a2e5c..3bbef50 100644 --- a/services/presence.py +++ b/services/presence.py @@ -4,7 +4,7 @@ from services.rediscache import redis from validators.chat import Message -async def notify_message(message: Message, chat_id: str, action = "create"): +async def notify_message(message: Message, chat_id: str, action="create"): channel_name = f"chat:{chat_id}" data = {"payload": message, "action": action} try: