diff --git a/README.md b/README.md index cd17cda..f29a0ce 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -### `inbox`: Сервер для внутренних переписок +### `inbox`: Сервер для внутренних переписок Для @@ -9,8 +9,5 @@ ### Как это работает -__Redis__: +__Redis__: - Для каждого пользователя создаётся запись в хранилищах `chats_by_author/` и `chats/` и канал redis `chat:`, в котором публикуюутся обновления всех переписок. - -__SSE__: - - Каждый пользователь подписывается на свой канал по урлу `/sse/` \ No newline at end of file diff --git a/orm/author.py b/orm/author.py index b2b5a99..17530f8 100644 --- a/orm/author.py +++ b/orm/author.py @@ -38,6 +38,4 @@ class Author(Base): lastSeen = Column(DateTime, nullable=False, default=datetime.now) deletedAt = Column(DateTime, nullable=True, comment="Deleted at") links = Column(JSONType, nullable=True, comment="Links") - ratings = relationship( - AuthorRating, foreign_keys=AuthorRating.author, nullable=True - ) + ratings = relationship(AuthorRating, foreign_keys=AuthorRating.author) diff --git a/requirements.txt b/requirements.txt index d65a05f..c5e2ee8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -9,7 +9,7 @@ uvicorn httpx itsdangerous pydantic -psycopg2 +psycopg2-binary ######## development deps isort brunette diff --git a/resolvers/messages.py b/resolvers/messages.py index ca5dc0d..5725a7d 100644 --- a/resolvers/messages.py +++ b/resolvers/messages.py @@ -54,9 +54,10 @@ async def create_message(_, info, chat: str, body: str, reply_to=None): # subscribe on updates channel_name = ( - f"private:{author_id}" if not chat["title"] else f"group:{chat['id']}" + f"chat:{chat['id']}" if not chat["title"] else f"group:{chat['id']}" ) - redis.execute("PUBLISH", channel_name, json.dumps(new_message)) + new_message["kind"] = "new_message" + await redis.execute_pubsub("PUBLISH", channel_name, json.dumps(new_message)) return {"message": new_message, "error": None} diff --git a/server.py b/server.py index 4e9f774..22916c1 100644 --- a/server.py +++ b/server.py @@ -4,6 +4,7 @@ from settings import PORT def exception_handler(exception_type, exception, traceback, debug_hook=sys.excepthook): + print(traceback) print("%s: %s" % (exception_type.__name__, exception)) diff --git a/services/redis.py b/services/redis.py index 07bd266..f61389c 100644 --- a/services/redis.py +++ b/services/redis.py @@ -28,6 +28,15 @@ class RedisCache: except Exception: pass + async def execute_pubsub(self, command, *args, **kwargs): + while not self._redis: + await asyncio.sleep(1) + try: + print("[redis] " + command + " " + " ".join(args)) + return await self._redis.execute_pubsub(command, *args, **kwargs) + except Exception: + pass + async def subscribe(self, *channels): if not self._redis: await self.connect()