From 4aca7e41fd14a8419358934722116de98b319e64 Mon Sep 17 00:00:00 2001 From: jedi Date: Thu, 25 Apr 2024 23:45:57 +0200 Subject: [PATCH] stash --- README.md | 1 + core/mail/notifications.py | 23 ++++++++++++++++++++++ core/mail/protocol.py | 39 +++++++++++++++++++++++++++++++------- core/server.py | 7 +++++++ 4 files changed, 63 insertions(+), 7 deletions(-) create mode 100644 README.md create mode 100644 core/mail/notifications.py diff --git a/README.md b/README.md new file mode 100644 index 0000000..48adb73 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +ansible-playbook deploy/ansible/playbooks/deploy-c3lf-sys3.yml --inventory=deploy/ansible/inventory.yml diff --git a/core/mail/notifications.py b/core/mail/notifications.py new file mode 100644 index 0000000..494e0f7 --- /dev/null +++ b/core/mail/notifications.py @@ -0,0 +1,23 @@ +from channels.layers import get_channel_layer + + +class NotificationDispatcher: + channel_layer = None + + def __init__(self): + self.channel_layer = get_channel_layer() + + async def dispatch(self, message): + print("Dispatching message:", message) + + async def run_forever(self): + + # Infinite loop to continuously listen for messages + while True: + # Blocking receive to get the message from the channel layer + message = await self.channel_layer.receive() + + if message: + # Process the received message + print("Received message:", message) + await self.dispatch(message) diff --git a/core/mail/protocol.py b/core/mail/protocol.py index bc83230..b57a03c 100644 --- a/core/mail/protocol.py +++ b/core/mail/protocol.py @@ -1,10 +1,9 @@ import logging import aiosmtplib -from asgiref.sync import sync_to_async from channels.layers import get_channel_layer +from channels.db import database_sync_to_async from django.core.files.base import ContentFile - from mail.models import Email, EventAddress, EmailAttachment from notify_sessions.models import SystemEvent from tickets.models import IssueThread @@ -82,6 +81,23 @@ def make_reply(reply_email, references=None, event=None): return reply +def make_notification(message, to, event=None): # TODO where should replies to this go + from email.message import EmailMessage + from core.settings import MAIL_DOMAIN + event = event or "mail" + notification = EmailMessage() + notification["From"] = "notifications@%s" % MAIL_DOMAIN + notification["To"] = to + notification["Subject"] = f"System3 Notification" + # notification["Reply-To"] = f"{event}@{MAIL_DOMAIN}" + # notification["In-Reply-To"] = email.reference + # notification["Message-ID"] = email.id + "@" + MAIL_DOMAIN + + notification.set_content(message) + + return notification + + async def send_smtp(message, log): log.info('Sending message to %s' % message['To']) await aiosmtplib.send(message, hostname="127.0.0.1", port=25, use_tls=False, start_tls=False) @@ -233,7 +249,13 @@ Your c3lf (Cloakroom + Lost&Found) Team'''.format(active_issue_thread.short_uuid active_issue_thread.state = 'pending_open' active_issue_thread.save() - return email, new, reply + notifcation = None + if new: + notifcation = f"New issue {active_issue_thread.short_uuid()} created by {sender}" + else: + notifcation = f"Reply to issue {active_issue_thread.short_uuid()} by {sender}" + + return email, new, reply, notifcation class LMTPHandler: @@ -255,16 +277,19 @@ class LMTPHandler: content = None try: content = envelope.content - email, new, reply = await sync_to_async(receive_email)(envelope, log) + email, new, reply, notifcation = await database_sync_to_async(receive_email)(envelope, log) log.info(f"Created email {email.id}") - systemevent = await sync_to_async(SystemEvent.objects.create)(type='email received', reference=email.id) + systemevent = await database_sync_to_async(SystemEvent.objects.create)(type='email received', reference=email.id) log.info(f"Created system event {systemevent.id}") channel_layer = get_channel_layer() await channel_layer.group_send( 'general', {"type": "generic.event", "name": "send_message_to_frontend", "event_id": systemevent.id, - "message": "email received"} - ) + "message": "email received"}) log.info(f"Sent message to frontend") + if notifcation: + await channel_layer.group_send( + 'general', {"type": "generic.event", "name": "push_notification", "event_id": systemevent.id, + "message": notifcation}) if new and reply: await send_smtp(reply, log) log.info("Sent auto reply") diff --git a/core/server.py b/core/server.py index d08b595..bcccf81 100644 --- a/core/server.py +++ b/core/server.py @@ -12,6 +12,7 @@ django.setup() from helper import init_loop from mail.protocol import LMTPHandler from mail.socket import UnixSocketLMTPController +from mail.notifications import NotificationDispatcher class UvicornServer(uvicorn.Server): @@ -54,6 +55,11 @@ async def lmtp(loop): log.info("LMTP done") +async def notifications(loop): + dispatcher = NotificationDispatcher() + await dispatcher.run_forever() + + def main(): import sdnotify import setproctitle @@ -67,6 +73,7 @@ def main(): loop.create_task(web(loop)) # loop.create_task(tcp(loop)) loop.create_task(lmtp(loop)) + loop.create_task(notifications(loop)) n = sdnotify.SystemdNotifier() n.notify("READY=1") log.info("Server ready")