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