This commit is contained in:
parent
4f3a0dc788
commit
4ecdc08dfc
|
@ -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,10 +115,10 @@ 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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -2,7 +2,8 @@ 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"
|
||||
|
@ -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:
|
||||
|
@ -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}
|
|
@ -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:
|
||||
|
|
Loading…
Reference in New Issue
Block a user