core/services/presence.py

42 lines
1.1 KiB
Python
Raw Normal View History

2023-10-05 18:46:18 +00:00
import json
from orm.reaction import Reaction
from orm.shout import Shout
2023-10-16 14:51:08 +00:00
from orm.user import Author
2023-10-05 18:46:18 +00:00
from services.redis import redis
async def notify_reaction(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: 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}")
2023-10-16 14:51:08 +00:00
async def notify_follower(follower: Author, author_id: int):
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}")