inbox/services/presence.py

25 lines
843 B
Python
Raw Normal View History

2023-10-04 17:14:06 +00:00
import json
2023-10-14 12:59:43 +00:00
2023-10-14 14:55:51 +00:00
from services.rediscache import redis
2023-11-22 12:09:24 +00:00
from models.chat import Message, ChatUpdate
2023-10-04 17:14:06 +00:00
2023-11-16 14:52:39 +00:00
async def notify_message(message: Message, action="create"):
2023-11-22 12:09:24 +00:00
channel_name = f"message:{message['chat_id']}"
2023-10-19 14:43:20 +00:00
data = {"payload": message, "action": action}
2023-10-04 17:14:06 +00:00
try:
2023-10-04 21:20:43 +00:00
await redis.publish(channel_name, json.dumps(data))
2023-10-19 14:43:20 +00:00
print(f"[services.presence] ok {data}")
2023-10-04 17:14:06 +00:00
except Exception as e:
print(f"Failed to publish to channel {channel_name}: {e}")
2023-11-16 14:52:39 +00:00
2023-11-28 09:05:39 +00:00
async def notify_chat(chat: ChatUpdate, member_id: int, action="create"):
2023-11-16 14:52:39 +00:00
channel_name = f"chat:{member_id}"
data = {"payload": chat, "action": action}
try:
await redis.publish(channel_name, json.dumps(data))
print(f"[services.presence] ok {data}")
except Exception as e:
print(f"Failed to publish to channel {channel_name}: {e}")