core/services/notify.py

34 lines
1.1 KiB
Python
Raw Normal View History

2023-10-05 18:46:18 +00:00
import json
2023-10-23 14:47:11 +00:00
from services.rediscache import redis
2023-10-05 18:46:18 +00:00
2023-10-23 14:47:11 +00:00
async def notify_reaction(reaction, action: str = "create"):
2023-10-19 14:42:42 +00:00
channel_name = "reaction"
2023-11-24 01:53:30 +00:00
data = {"payload": reaction, "action": action}
2023-10-05 18:46:18 +00:00
try:
await redis.publish(channel_name, json.dumps(data))
except Exception as e:
2023-11-24 01:53:30 +00:00
print(f"[services.notify] Failed to publish to channel {channel_name}: {e}")
2023-10-05 18:46:18 +00:00
2023-10-23 14:47:11 +00:00
async def notify_shout(shout, action: str = "create"):
2023-10-19 14:42:42 +00:00
channel_name = "shout"
2023-11-24 01:53:30 +00:00
data = {"payload": shout, "action": action}
2023-10-05 18:46:18 +00:00
try:
await redis.publish(channel_name, json.dumps(data))
except Exception as e:
2023-11-24 01:53:30 +00:00
print(f"[services.notify] Failed to publish to channel {channel_name}: {e}")
2023-10-05 18:46:18 +00:00
2023-10-23 14:47:11 +00:00
async def notify_follower(follower: dict, author_id: int, action: str = "follow"):
fields = follower.keys()
for k in fields:
2023-11-03 10:10:22 +00:00
if k not in ["id", "name", "slug", "pic"]:
del follower[k]
2023-10-19 14:42:42 +00:00
channel_name = f"follower:{author_id}"
2023-11-24 01:53:30 +00:00
data = {"payload": follower, "action": action}
2023-10-05 18:46:18 +00:00
try:
await redis.publish(channel_name, json.dumps(data))
except Exception as e:
2023-11-24 01:53:30 +00:00
print(f"[services.notify] Failed to publish to channel {channel_name}: {e}")