core/services/presence.py

43 lines
1.1 KiB
Python
Raw Normal View History

2023-10-05 18:46:18 +00:00
import json
from services.redis import redis
async def notify_reaction(reaction):
2023-10-16 14:51:08 +00:00
channel_name = "new_reaction"
data = {
"payload": reaction,
"kind": f"new_reaction{reaction.kind}"
}
2023-10-05 18:46:18 +00:00
try:
await redis.publish(channel_name, json.dumps(data))
except Exception as e:
print(f"Failed to publish to channel {channel_name}: {e}")
async def notify_shout(shout):
2023-10-16 14:51:08 +00:00
channel_name = "new_shout"
data = {
"payload": shout,
"kind": "new_shout"
}
2023-10-05 18:46:18 +00:00
try:
await redis.publish(channel_name, json.dumps(data))
except Exception as e:
print(f"Failed to publish to channel {channel_name}: {e}")
async def notify_follower(follower: dict, author_id: int):
fields = follower.keys()
for k in fields:
if k not in ["id", "name", "slug", "userpic"]:
del follower[k]
2023-10-16 14:51:08 +00:00
channel_name = f"followers:{author_id}"
2023-10-05 18:46:18 +00:00
data = {
2023-10-16 14:51:08 +00:00
"payload": follower,
2023-10-05 18:46:18 +00:00
"kind": "new_follower",
}
try:
await redis.publish(channel_name, json.dumps(data))
except Exception as e:
print(f"Failed to publish to channel {channel_name}: {e}")