formatted
This commit is contained in:
parent
4ddc424f29
commit
154633c114
|
@ -1,6 +1,7 @@
|
|||
## `inbox`: Сервер для внутренних переписок
|
||||
|
||||
### ENV
|
||||
|
||||
- REDIS_URL
|
||||
- AUTH_URL
|
||||
- API_BASE
|
||||
|
@ -8,4 +9,6 @@
|
|||
### Как это работает
|
||||
|
||||
__Redis__:
|
||||
- Для каждого пользователя создаётся запись в хранилищах `chats_by_author/<chat_id>` и `chats/<chat_id>` и канал redis `chat:<chat_id>`, в котором публикуюутся обновления всех переписок.
|
||||
|
||||
- Для каждого пользователя создаётся запись в хранилищах `chats_by_author/<chat_id>` и `chats/<chat_id>` и канал
|
||||
redis `chat:<chat_id>`, в котором публикуюутся обновления всех переписок.
|
||||
|
|
2
main.py
2
main.py
|
@ -1,9 +1,11 @@
|
|||
import os
|
||||
from importlib import import_module
|
||||
from os.path import exists
|
||||
|
||||
from ariadne import load_schema_from_path, make_executable_schema
|
||||
from ariadne.asgi import GraphQL
|
||||
from starlette.applications import Starlette
|
||||
|
||||
from services.redis import redis
|
||||
from services.schema import resolvers
|
||||
from settings import DEV_SERVER_PID_FILE_NAME, SENTRY_DSN, MODE
|
||||
|
|
|
@ -20,6 +20,7 @@ itsdangerous = "^2.1.2"
|
|||
|
||||
[tool.poetry.dev-dependencies]
|
||||
pytest = "^7.4.2"
|
||||
black = { version = "^23.9.1", python = ">=3.12" }
|
||||
|
||||
[tool.black]
|
||||
line-length = 120
|
||||
|
|
|
@ -1,14 +1,13 @@
|
|||
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,
|
||||
update_message,
|
||||
mark_as_read,
|
||||
)
|
||||
from resolvers.load import load_chats, load_messages_by, load_recipients
|
||||
from resolvers.search import search_recipients
|
||||
|
||||
|
||||
__all__ = [
|
||||
# inbox
|
||||
"load_chats",
|
||||
|
|
|
@ -1,10 +1,11 @@
|
|||
import json
|
||||
import uuid
|
||||
from datetime import datetime, timezone
|
||||
from validators.inbox import Chat
|
||||
|
||||
from services.auth import login_required
|
||||
from services.redis import redis
|
||||
from services.schema import mutation
|
||||
from validators.inbox import Chat
|
||||
|
||||
|
||||
@mutation.field("updateChat")
|
||||
|
@ -107,4 +108,3 @@ async def delete_chat(_, info, chat_id: str):
|
|||
await redis.execute("SREM", f"chats_by_author/{author_id}", chat_id)
|
||||
else:
|
||||
return {"error": "chat not exist"}
|
||||
|
||||
|
|
|
@ -1,13 +1,15 @@
|
|||
import asyncio
|
||||
import json
|
||||
from typing import Any, Dict, List, Optional, Union
|
||||
|
||||
from services.auth import login_required
|
||||
from services.core import get_author, get_network
|
||||
from services.redis import redis
|
||||
from services.auth import login_required
|
||||
from services.schema import query
|
||||
from validators.inbox import Message, Chat, ChatMember
|
||||
from .chats import create_chat
|
||||
from .unread import get_unread_counter
|
||||
import asyncio
|
||||
|
||||
|
||||
# NOTE: not an API handler
|
||||
async def load_messages(
|
||||
|
|
|
@ -1,11 +1,12 @@
|
|||
import json
|
||||
from datetime import datetime, timezone
|
||||
from typing import List
|
||||
from validators.inbox import Message
|
||||
|
||||
from services.auth import login_required
|
||||
from services.presence import notify_message
|
||||
from services.redis import redis
|
||||
from services.schema import mutation
|
||||
from validators.inbox import Message
|
||||
|
||||
|
||||
@mutation.field("createMessage")
|
||||
|
@ -26,16 +27,15 @@ async def create_message(_, info, chat: str, body: str, reply_to=None):
|
|||
else:
|
||||
chat_dict = json.loads(chat_data)
|
||||
print(chat_dict)
|
||||
message_id = (
|
||||
await redis.execute("GET", f"chats/{chat_dict['id']}/next_message_id")
|
||||
) or 0
|
||||
message_id = int(message_id)
|
||||
message_id = await redis.execute("GET", f"chats/{chat_dict['id']}/next_message_id")
|
||||
message_id = int(message_id) if message_id else 0
|
||||
new_message: Message = {
|
||||
"chat": chat_dict["id"],
|
||||
"id": message_id,
|
||||
"author": author_id,
|
||||
"body": body,
|
||||
"createdAt": int(datetime.now(tz=timezone.utc).timestamp()),
|
||||
"updatedAt": None
|
||||
}
|
||||
if reply_to:
|
||||
new_message["replyTo"] = reply_to
|
||||
|
@ -110,10 +110,10 @@ async def delete_message(_, info, chat_id: str, message_id: int):
|
|||
return {"error": "chat not exist"}
|
||||
chat = json.loads(chat)
|
||||
|
||||
message = await redis.execute("GET", f"chats/{chat_id}/messages/{str(message_id)}")
|
||||
if not message:
|
||||
message_data = await redis.execute("GET", f"chats/{chat_id}/messages/{str(message_id)}")
|
||||
if not message_data:
|
||||
return {"error": "message not exist"}
|
||||
message: Message = json.loads(message)
|
||||
message: Message = json.loads(message_data)
|
||||
if message["author"] != author_id:
|
||||
return {"error": "access denied"}
|
||||
|
||||
|
|
|
@ -1,10 +1,11 @@
|
|||
import json
|
||||
from typing import Dict, Union, List, Any
|
||||
from datetime import datetime, timezone, timedelta
|
||||
from typing import Dict, Union, List, Any
|
||||
|
||||
from resolvers.load import load_messages
|
||||
from services.auth import login_required
|
||||
from services.core import get_network
|
||||
from services.redis import redis
|
||||
from resolvers.load import load_messages
|
||||
from services.schema import query
|
||||
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
from services.redis import redis
|
||||
import json
|
||||
|
||||
from services.auth import login_required
|
||||
from services.redis import redis
|
||||
|
||||
|
||||
async def get_unread_counter(chat_id: str, author_id: int) -> int:
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
import json
|
||||
from functools import wraps
|
||||
from httpx import AsyncClient, HTTPError
|
||||
from settings import AUTH_URL
|
||||
|
||||
from httpx import AsyncClient, HTTPError
|
||||
|
||||
from settings import AUTH_URL
|
||||
|
||||
INTERNAL_AUTH_SERVER = "v2.discours" in AUTH_URL or "testapi.discours" in AUTH_URL
|
||||
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import json
|
||||
|
||||
from services.redis import redis
|
||||
from validators.inbox import Message
|
||||
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import redis.asyncio as aredis
|
||||
|
||||
from settings import REDIS_URL
|
||||
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
from typing import Dict, Optional, List
|
||||
from typing import TypedDict, Optional, List
|
||||
|
||||
class Message(Dict):
|
||||
|
||||
class Message(TypedDict):
|
||||
id: int
|
||||
chat: str
|
||||
author: int
|
||||
|
@ -10,17 +11,19 @@ class Message(Dict):
|
|||
createdAt: int
|
||||
updatedAt: Optional[int]
|
||||
|
||||
class Chat(Dict):
|
||||
|
||||
class Chat(TypedDict):
|
||||
id: str
|
||||
members: List[int]
|
||||
admins: List[int]
|
||||
title: str
|
||||
updatedAt: int
|
||||
updatedAt: Optional[int]
|
||||
createdAt: int
|
||||
createdBy: int
|
||||
description: Optional[str]
|
||||
|
||||
class ChatMember(Dict):
|
||||
|
||||
class ChatMember(TypedDict):
|
||||
id: int
|
||||
slug: str
|
||||
name: str
|
||||
|
|
Loading…
Reference in New Issue
Block a user