c3lf-system-3/core/mail/notifications.py

31 lines
1.3 KiB
Python
Raw Normal View History

2024-04-25 21:45:57 +00:00
from channels.layers import get_channel_layer
class NotificationDispatcher:
channel_layer = None
2024-04-25 23:22:08 +00:00
room_group_name = "general"
2024-04-25 21:45:57 +00:00
def __init__(self):
2024-04-25 23:22:08 +00:00
self.channel_layer = get_channel_layer('default')
2024-04-25 22:56:12 +00:00
if not self.channel_layer:
raise Exception("Could not get channel layer")
2024-04-25 21:45:57 +00:00
async def run_forever(self):
# Infinite loop to continuously listen for messages
2024-04-25 22:31:34 +00:00
print("Listening for messages...")
2024-04-25 23:22:08 +00:00
channel_name = await self.channel_layer.new_channel()
await self.channel_layer.group_add(self.room_group_name, channel_name)
print("Channel name:", channel_name)
2024-04-25 21:45:57 +00:00
while True:
# Blocking receive to get the message from the channel layer
2024-04-25 23:22:08 +00:00
message = await self.channel_layer.receive(channel_name)
2024-04-25 21:45:57 +00:00
2024-04-25 23:28:44 +00:00
if (message and 'type' in message and message['type'] == 'generic.event' and 'name' in message and
message['name'] == 'user_notification'):
if 'message' in message and 'event_id' in message:
print("Received message:", message['message'], "with event_id:", message['event_id'])
await self.dispatch(message['message'], message['event_id'])
2024-04-25 22:56:12 +00:00
2024-04-25 23:28:44 +00:00
async def dispatch(self, message, event_id):
print("Dispatching message:", message, "with event_id:", event_id)