This commit is contained in:
parent
531eb1f028
commit
95a237f349
4
main.py
4
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()
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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}")
|
||||
|
|
|
@ -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([])
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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())
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user