From c6ad0414b0e0be6cd42feb96ec559d1c44154378 Mon Sep 17 00:00:00 2001 From: Untone Date: Thu, 19 Oct 2023 17:43:20 +0300 Subject: [PATCH] actions-notify --- resolvers/messages.py | 7 ++++++- services/presence.py | 6 +++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/resolvers/messages.py b/resolvers/messages.py index b87e4bf..8f554d8 100644 --- a/resolvers/messages.py +++ b/resolvers/messages.py @@ -71,7 +71,7 @@ async def create_message(_, info, chat: str, body: str, reply_to=None): await redis.execute("LPUSH", f"chats/{chat_dict['id']}/unread/{member_id}", str(message_id)) # Отправка уведомления о новом сообщении - await notify_message(new_message, chat_dict["id"]) + await notify_message(new_message, chat_dict["id"], "create") return {"message": new_message, "error": None} @@ -99,6 +99,7 @@ async def update_message(_, info, chat_id: str, message_id: int, body: str): await redis.execute("SET", f"chats/{chat_id}/messages/{message_id}", json.dumps(message)) # TODO: use presence service to notify about updated message + await notify_message(message, chat_id, "update") return {"message": message, "error": None} @@ -128,6 +129,7 @@ async def delete_message(_, info, chat_id: str, message_id: int): await redis.execute("LREM", f"chats/{chat_id}/unread/{member_id}", 0, str(message_id)) # TODO: use presence service to notify about deleted message + await notify_message(message, chat_id, "delete") return {} @@ -149,4 +151,7 @@ async def mark_as_read(_, info, chat_id: str, messages: List[int]): for message_id in messages: await redis.execute("LREM", f"chats/{chat_id}/unread/{author_id}", 0, str(message_id)) + # TODO: seen per author + # await notify_message(message, chat_id, "seen") + return {"error": None} diff --git a/services/presence.py b/services/presence.py index 00c131a..35a2e5c 100644 --- a/services/presence.py +++ b/services/presence.py @@ -4,11 +4,11 @@ from services.rediscache import redis from validators.chat import Message -async def notify_message(message: Message, chat_id: str): +async def notify_message(message: Message, chat_id: str, action = "create"): channel_name = f"chat:{chat_id}" - data = {"payload": message, "kind": "new_message"} + data = {"payload": message, "action": action} try: await redis.publish(channel_name, json.dumps(data)) - print(f"[services.presence] notification was sent successfully {data}") + print(f"[services.presence] ok {data}") except Exception as e: print(f"Failed to publish to channel {channel_name}: {e}")