This commit is contained in:
j3d1 2024-04-25 23:45:57 +02:00
parent 9ebd172cb7
commit 6bfebc1f5f
4 changed files with 60 additions and 4 deletions

1
README.md Normal file
View file

@ -0,0 +1 @@
ansible-playbook deploy/ansible/playbooks/deploy-c3lf-sys3.yml --inventory=deploy/ansible/inventory.yml

View file

@ -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)

View file

@ -4,7 +4,6 @@ import aiosmtplib
from channels.layers import get_channel_layer from channels.layers import get_channel_layer
from channels.db import database_sync_to_async from channels.db import database_sync_to_async
from django.core.files.base import ContentFile from django.core.files.base import ContentFile
from mail.models import Email, EventAddress, EmailAttachment from mail.models import Email, EventAddress, EmailAttachment
from notify_sessions.models import SystemEvent from notify_sessions.models import SystemEvent
from tickets.models import IssueThread from tickets.models import IssueThread
@ -82,6 +81,23 @@ def make_reply(reply_email, references=None, event=None):
return reply 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): async def send_smtp(message):
await aiosmtplib.send(message, hostname="127.0.0.1", port=25, use_tls=False, start_tls=False) 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.state = 'pending_open'
active_issue_thread.save() 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: class LMTPHandler:
@ -263,9 +285,12 @@ class LMTPHandler:
channel_layer = get_channel_layer() channel_layer = get_channel_layer()
await channel_layer.group_send( await channel_layer.group_send(
'general', {"type": "generic.event", "name": "send_message_to_frontend", "event_id": systemevent.id, '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") 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: if new and reply:
log.info('Sending message to %s' % reply['To']) log.info('Sending message to %s' % reply['To'])
await send_smtp(reply) await send_smtp(reply)

View file

@ -12,6 +12,7 @@ django.setup()
from helper import init_loop from helper import init_loop
from mail.protocol import LMTPHandler from mail.protocol import LMTPHandler
from mail.socket import UnixSocketLMTPController from mail.socket import UnixSocketLMTPController
from mail.notifications import NotificationDispatcher
class UvicornServer(uvicorn.Server): class UvicornServer(uvicorn.Server):
@ -54,6 +55,11 @@ async def lmtp(loop):
log.info("LMTP done") log.info("LMTP done")
async def notifications(loop):
dispatcher = NotificationDispatcher()
await dispatcher.run_forever()
def main(): def main():
import sdnotify import sdnotify
import setproctitle import setproctitle
@ -67,6 +73,7 @@ def main():
loop.create_task(web(loop)) loop.create_task(web(loop))
# loop.create_task(tcp(loop)) # loop.create_task(tcp(loop))
loop.create_task(lmtp(loop)) loop.create_task(lmtp(loop))
loop.create_task(notifications(loop))
n = sdnotify.SystemdNotifier() n = sdnotify.SystemdNotifier()
n.notify("READY=1") n.notify("READY=1")
log.info("Server ready") log.info("Server ready")