diff --git a/main.py b/main.py index a2c1a95..a553332 100644 --- a/main.py +++ b/main.py @@ -19,10 +19,6 @@ schema = make_executable_schema(load_schema_from_path("inbox.graphql"), resolver async def start_up(): - # load authors cache - authors = await get_all_authors() - print(f"[main] {len(authors)} authors precached") - if MODE == "dev": if exists(DEV_SERVER_PID_FILE_NAME): await redis.connect() diff --git a/resolvers/__init__.py b/resolvers/__init__.py index 339805b..34688c4 100644 --- a/resolvers/__init__.py +++ b/resolvers/__init__.py @@ -2,6 +2,12 @@ from resolvers.chats import create_chat, delete_chat, update_chat from resolvers.load import load_chats, load_messages_by, load_recipients from resolvers.messages import create_message, delete_message, mark_as_read, update_message from resolvers.search import search_messages, search_recipients +from services.core import get_all_authors +import asyncio + + +# load authors cache +asyncio.create_task(get_all_authors()) __all__ = [ # inbox diff --git a/resolvers/chats.py b/resolvers/chats.py index aff8d87..672d659 100644 --- a/resolvers/chats.py +++ b/resolvers/chats.py @@ -21,9 +21,7 @@ async def update_chat(_, info, chat_new: ChatUpdate): :param chat_new: dict with chat data :return: Result { error chat } """ - user_id = info.context["user_id"] - author = authors_by_user[user_id] - author_id = author["id"] + author_id = info.context["author_id"] chat_id = chat_new["id"] chat_str = await redis.execute("GET", f"chats/{chat_id}") if not chat_str: @@ -57,6 +55,7 @@ async def create_chat(_, info, title="", members=None): user_id = info.context["user_id"] author = authors_by_user[user_id] author_id = author["id"] + chat = None if author_id: if author_id not in members: members.append(int(author_id)) @@ -101,9 +100,7 @@ async def create_chat(_, info, title="", members=None): @mutation.field("delete_chat") @login_required async def delete_chat(_, info, chat_id: str): - user_id = info.context["user_id"] - author = authors_by_user[user_id] - author_id = author["id"] + author_id = info.context["author_id"] chat_str = await redis.execute("GET", f"chats/{chat_id}") if chat_str: chat: Chat = json.loads(chat_str) diff --git a/resolvers/load.py b/resolvers/load.py index b8ac1cc..b79587e 100644 --- a/resolvers/load.py +++ b/resolvers/load.py @@ -6,7 +6,7 @@ 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 authors_by_id, get_my_followed, authors_by_user +from services.core import authors_by_id, get_my_followed from services.rediscache import redis from services.schema import query @@ -52,9 +52,7 @@ 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""" - user_id = info.context["user_id"] - author = authors_by_user[user_id] - author_id = author["id"] + author_id = info.context["author_id"] chats = [] if author_id: cids = await redis.execute("SMEMBERS", f"chats_by_author/{author_id}") @@ -90,9 +88,7 @@ async def load_chats(_, info, limit: int = 50, offset: int = 0) -> Dict[str, Uni @login_required async def load_messages_by(_, info, by, limit: int = 10, offset: int = 0): """load :limit messages of :chat_id with :offset""" - user_id = info.context["user_id"] - author = authors_by_user[user_id] - author_id = author["id"] + author_id = info.context["author_id"] author_chats = (await redis.execute("SMEMBERS", "chats_by_author/" + str(author_id))) or [] author_chats = [c for c in author_chats] if author_chats: diff --git a/resolvers/messages.py b/resolvers/messages.py index c4797d4..03ca47a 100644 --- a/resolvers/messages.py +++ b/resolvers/messages.py @@ -3,7 +3,6 @@ import time from models.chat import Message from services.auth import login_required -from services.core import authors_by_user from services.presence import notify_message from services.rediscache import redis from services.schema import mutation @@ -13,10 +12,7 @@ from services.schema import mutation @login_required async def create_message(_, info, chat_id: str, body: str, reply_to=None): """Создание сообщения с телом :body для чата :chat_id с возможным ответом на :reply_to""" - - user_id = info.context["user_id"] - author = authors_by_user[user_id] - author_id = author["id"] + author_id = info.context["author_id"] # Получение данных чата из Redis chat_data = await redis.execute("GET", f"chats/{chat_id}") diff --git a/resolvers/search.py b/resolvers/search.py index 5fb8c51..8fb58d7 100644 --- a/resolvers/search.py +++ b/resolvers/search.py @@ -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 authors_by_id, authors_by_user +from services.core import authors_by_id from services.rediscache import redis from services.schema import query @@ -16,9 +16,7 @@ async def search_recipients(_, info, text: str, limit: int = 50, offset: int = 0 # TODO: maybe redis scan? - user_id = info.context["user_id"] - author = authors_by_user[user_id] - author_id = author["id"] + author_id = info.context["author_id"] existed_chats = await redis.execute("SMEMBERS", f"/chats_by_author/{author_id}") if existed_chats: @@ -41,9 +39,7 @@ async def search_recipients(_, info, text: str, limit: int = 50, offset: int = 0 async def search_messages( _, info, by: Dict[str, Union[str, int]], limit: int, offset: int ) -> Dict[str, Union[List[Dict[str, Any]], None]]: - user_id = info.context["user_id"] - author = authors_by_user[user_id] - author_id = author["id"] + author_id = info.context["author_id"] lookup_chats = set((await redis.execute("SMEMBERS", f"chats_by_author/{author_id}")) or []) messages_set = set([]) diff --git a/services/auth.py b/services/auth.py index 18e6c81..926c5dc 100644 --- a/services/auth.py +++ b/services/auth.py @@ -2,6 +2,7 @@ from functools import wraps from aiohttp import ClientSession from starlette.exceptions import HTTPException +from services.core import authors_by_user from settings import AUTH_URL @@ -59,6 +60,9 @@ def login_required(f): user_id = await check_auth(req) if user_id: context["user_id"] = user_id + author = authors_by_user.get(user_id) + if author and "id" in author: + context["author_id"] = author["id"] return await f(*args, **kwargs) return decorated_function diff --git a/services/core.py b/services/core.py index 0b1babc..fd92e77 100644 --- a/services/core.py +++ b/services/core.py @@ -42,6 +42,7 @@ async def get_all_authors() -> List[ChatMember]: print(a) authors_by_user[a["user"]] = a authors_by_id[a["id"]] = a + print(f"[main] {len(authors)} authors precached") return list(authors_by_id.values())