format+fix
Some checks failed
deploy / deploy (push) Failing after 1m7s

This commit is contained in:
Untone 2023-11-14 21:25:55 +03:00
parent 4f3a0dc788
commit 4ecdc08dfc
6 changed files with 20 additions and 21 deletions

View File

@ -99,4 +99,4 @@ async def delete_chat(_, info, chat_id: str):
await redis.execute("DEL", f"chats/{chat_id}") await redis.execute("DEL", f"chats/{chat_id}")
await redis.execute("SREM", f"chats_by_author/{author_id}", chat_id) await redis.execute("SREM", f"chats_by_author/{author_id}", chat_id)
else: else:
return {"error": "chat not exist"} return {"error": "chat not exist"}

View File

@ -3,7 +3,7 @@ import json
from typing import Any, Dict, List, Optional, Union from typing import Any, Dict, List, Optional, Union
from services.auth import login_required 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.rediscache import redis
from services.schema import query from services.schema import query
from validators.chat import Message, ChatPayload 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 # 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""" """load :limit messages for :chat_id with :offset"""
if ids is None: if ids is None:
ids = [] 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) messages += await load_messages(chat_id, offset, limit, replies)
except Exception: except Exception:
import traceback import traceback
traceback.print_exc() traceback.print_exc()
return messages 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() member_ids = c["members"].copy()
c["members"] = [] c["members"] = []
for member_id in member_ids: 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 a["online"] = a.get("id") in members_online
c["members"].append(a) c["members"].append(a)
chats.append(c) chats.append(c)
@ -112,16 +115,16 @@ async def load_recipients(_, _info, limit=50, offset=0):
"""load possible chat participants""" """load possible chat participants"""
onliners = (await redis.execute("SMEMBERS", "authors-online")) or [] onliners = (await redis.execute("SMEMBERS", "authors-online")) or []
r = [] r = []
all_authors: List[ChatMember] = members all_authors: List[ChatMember] = await get_all_authors()
my_followings = await get_my_followings() my_followings = await get_my_followings()
if len(my_followings) < limit: 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: for a in my_followings:
a["online"] = a["id"] in onliners a["online"] = a["id"] in onliners
r.append(a) r.append(a)
# NOTE: maybe sort members here # NOTE: maybe sort members here
print(f"[resolvers.load] loadRecipients found {len(r)} members") print(f"[resolvers.load] loadRecipients found {len(r)} members")
return {"members": r, "error": None} return {"members": r, "error": None}

View File

@ -56,6 +56,4 @@ def exception_handler(_et, exc, _tb):
if __name__ == "__main__": if __name__ == "__main__":
sys.excepthook = exception_handler sys.excepthook = exception_handler
uvicorn.run( uvicorn.run("main:app", host="0.0.0.0", port=PORT, proxy_headers=True, server_header=True)
"main:app", host="0.0.0.0", port=PORT, proxy_headers=True, server_header=True
)

View File

@ -16,7 +16,7 @@ async def check_auth(req):
query_type = "mutation" if INTERNAL_AUTH_SERVER else "query" query_type = "mutation" if INTERNAL_AUTH_SERVER else "query"
operation = "GetUserId" operation = "GetUserId"
headers = {"Authorization": token, "Content-Type": "application/json"} # "Bearer " + removed headers = {"Authorization": token, "Content-Type": "application/json"} # "Bearer " + removed
gql = { gql = {
"query": query_type + " " + operation + " { " + query_name + " { user { id } } " + " }", "query": query_type + " " + operation + " { " + query_name + " { user { id } } " + " }",

View File

@ -2,14 +2,15 @@ from httpx import AsyncClient
from settings import API_BASE from settings import API_BASE
headers = {"Content-Type": "application/json"} headers = {"Content-Type": "application/json"}
INTERNAL_AUTH_SERVER = 'v2.' in API_BASE INTERNAL_AUTH_SERVER = "v2." in API_BASE
async def get_all_authors(): async def get_all_authors():
query_name = "authorsAll" query_name = "authorsAll"
query_type = "query" query_type = "query"
operation = "AuthorsAll" operation = "AuthorsAll"
query_fields = "id slug userpic name" query_fields = "id slug userpic name"
headers = {"Content-Type": "application/json"} # "Bearer " + removed headers = {"Content-Type": "application/json"} # "Bearer " + removed
gql = { gql = {
"query": query_type + " " + operation + " { " + query_name + " { " + query_fields + " } " + " }", "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) response = await client.post(API_BASE, headers=headers, json=gql)
except Exception: except Exception:
import traceback import traceback
traceback.print_exc() traceback.print_exc()
print(f"[services.core] {query_name}: {response.status_code} {response.text}") print(f"[services.core] {query_name}: {response.status_code} {response.text}")
if response.status_code != 200: if response.status_code != 200:
@ -34,7 +36,7 @@ async def get_my_followings():
query_type = "query" query_type = "query"
operation = "LoadMySubscriptions" operation = "LoadMySubscriptions"
query_fields = "id slug userpic name" query_fields = "id slug userpic name"
headers = {"Content-Type": "application/json"} # "Bearer " + removed headers = {"Content-Type": "application/json"} # "Bearer " + removed
gql = { gql = {
"query": query_type + " " + operation + " { " + query_name + " { authors {" + query_fields + "} } " + " }", "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) response = await client.post(API_BASE, headers=headers, json=gql)
except Exception: except Exception:
import traceback import traceback
traceback.print_exc() traceback.print_exc()
print(f"[services.core] {query_name}: {response.status_code} {response.text}") print(f"[services.core] {query_name}: {response.status_code} {response.text}")
if response.status_code != 200: if response.status_code != 200:
@ -53,8 +56,3 @@ async def get_my_followings():
r = response.json() r = response.json()
authors = r.get("data", {}).get(query_name).get("authors", []) authors = r.get("data", {}).get(query_name).get("authors", [])
return 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}

View File

@ -4,7 +4,7 @@ from services.rediscache import redis
from validators.chat import Message 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}" channel_name = f"chat:{chat_id}"
data = {"payload": message, "action": action} data = {"payload": message, "action": action}
try: try: