cached-request-9
Some checks failed
deploy / deploy (push) Failing after 1m1s

This commit is contained in:
Untone 2023-12-19 20:19:16 +03:00
parent 2380e88168
commit 2253bcf956
6 changed files with 26 additions and 56 deletions

View File

@ -64,7 +64,6 @@ type Query {
# inbox
load_chats(limit: Int, offset: Int): ChatResult! # your chats
load_messages_by(by: MessagesBy!, limit: Int, offset: Int): ChatResult!
load_recipients(limit: Int, offset: Int): ChatResult!
search_recipients(query: String!, limit: Int, offset: Int): ChatResult!
search_messages(by: MessagesBy!, limit: Int, offset: Int): ChatResult!
# _service: _Service!

View File

@ -4,7 +4,7 @@ import uuid
from models.chat import Chat, ChatUpdate
from services.auth import login_required
from services.core import cached_authors
from services.core import get_all_authors
from services.presence import notify_chat
from services.rediscache import redis
from services.schema import mutation
@ -50,10 +50,7 @@ async def update_chat(_, info, chat_new: ChatUpdate):
@login_required
async def create_chat(_, info, title="", members=None):
members = members or []
user_id = info.context["user_id"]
authors_by_user, authors_by_id = cached_authors
author = authors_by_user[user_id]
author_id = author["id"]
author_id = info.context["author_id"]
chat = None
if author_id:
if author_id not in members:

View File

@ -3,10 +3,9 @@ import json
from typing import Any, Dict, List, Optional, Union
from models.chat import ChatPayload, Message
from models.member import ChatMember
from resolvers.chats import create_chat
from services.auth import login_required
from services.core import cached_authors, get_my_followed
from services.core import get_all_authors
from services.rediscache import redis
from services.schema import query
@ -65,7 +64,10 @@ async def load_chats(_, info, limit: int = 50, offset: int = 0) -> Dict[str, Uni
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"])
authors_by_user, authors_by_id = cached_authors
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}")
@ -110,24 +112,3 @@ async def load_messages_by(_, info, by, limit: int = 10, offset: int = 0):
}
else:
return {"error": "Cannot access messages of this chat"}
@query.field("load_recipients")
async def load_recipients(_, _info, limit=50, offset=0):
"""load possible chat participants"""
onliners: List[int] = (await redis.execute("SMEMBERS", "authors-online")) or []
r = []
my_followings: List[ChatMember] = get_my_followed()
if len(my_followings) < limit:
authors_by_user, authors_by_id = cached_authors
my_followings = my_followings + list(authors_by_id.values())[offset : limit - len(my_followings)]
my_followings = list(set(my_followings))
for a in my_followings:
a["online"] = bool(a["id"] in list(onliners))
r.append(a)
# NOTE: maybe sort members here
print(f"[resolvers.load] loadRecipients found {len(r)} members")
return {"members": r, "error": None}

View File

@ -4,7 +4,7 @@ from typing import Any, Dict, List, Union
from resolvers.load import load_messages
from services.auth import login_required
from services.core import cached_authors
from services.core import get_all_authors
from services.rediscache import redis
from services.schema import query
@ -18,7 +18,8 @@ async def search_recipients(_, info, text: str, limit: int = 50, offset: int = 0
author_id = info.context["author_id"]
authors_by_user, authors_by_id = cached_authors
authors = get_all_authors()
authors_by_id = {a["id"]: a for a in authors}
existed_chats = await redis.execute("SMEMBERS", f"/chats_by_author/{author_id}")
if existed_chats:

View File

@ -2,7 +2,7 @@ from functools import wraps
from aiohttp import ClientSession
from starlette.exceptions import HTTPException
from services.core import get_all_authors, cached_authors
from services.core import get_author
from settings import AUTH_URL
@ -60,8 +60,7 @@ def login_required(f):
user_id = await check_auth(req)
if user_id:
context["user_id"] = user_id
authors_by_user, authors_by_id = get_all_authors() if not cached_authors else cached_authors
author = authors_by_user.get(user_id)
author = get_author(user_id)
if author and "id" in author:
context["author_id"] = author["id"]
return await f(*args, **kwargs)

View File

@ -29,15 +29,6 @@ def _request_endpoint(query_name, body) -> dict:
def get_all_authors():
global cached_authors, last_update_time
# Check if cached data is available and not expired
if cached_authors is not None and (datetime.now() - last_update_time) < update_interval:
print("[services.core] Returning cached authors data")
return cached_authors
authors_by_user = {}
authors_by_id = {}
query_name = "get_authors_all"
gql = {
@ -45,18 +36,21 @@ def get_all_authors():
"variables": None,
}
# Make a request to load authors
authors = _request_endpoint(query_name, gql)
return _request_endpoint(query_name, gql)
for a in list(authors):
authors_by_user[a["user"]] = a
authors_by_id[a["id"]] = a
# Cache the authors data and update the last update time
cached_authors = authors_by_user, authors_by_id
last_update_time = datetime.now()
def get_author_by_user(user: str):
operation = "GetAuthorId"
query_name = "get_author_id"
gql = {
"query": f"query {operation}($user: String!) {{ {query_name}(user: $user){{ id }} }}",
"operationName": operation,
"variables": {"user": user},
}
return authors_by_user, authors_by_id
author = _request_endpoint(query_name, gql).popitem()
return author
def get_my_followed() -> List[ChatMember]:
@ -67,6 +61,5 @@ def get_my_followed() -> List[ChatMember]:
"variables": None,
}
return _request_endpoint(query_name, gql).get(
"authors", []
) # Ensure you're returning the correct part of the response
result = _request_endpoint(query_name, gql)
return result.get("authors", [])