From 1b2d504b3b6856d605ab863c86e70eb961806046 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A0=20Casaj=C3=BAs?= Date: Mon, 21 Mar 2022 18:33:18 +0100 Subject: [PATCH] Send a notification to the user when a message has been quarantined --- email_handler.py | 22 +++++++++++++++++++ .../notification/message-quarantine.html | 4 ++++ tests/test_email_handler.py | 12 +++++++++- 3 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 templates/notification/message-quarantine.html diff --git a/email_handler.py b/email_handler.py index e4f2167c..e16f1e6c 100644 --- a/email_handler.py +++ b/email_handler.py @@ -581,6 +581,28 @@ def apply_dmarc_policy(alias: Alias, contact: Contact, msg: Message) -> Optional blocked=True, commit=True, ) + + notification_title = f"{alias.email} has a new mail in quarantine" + notifications = ( + Notification.filter_by(user_id=alias.user_id) + .order_by(Notification.read, Notification.created_at.desc()) + .limit(10) + .all() + ) # load a record more to know whether there's more + already_notified = False + for notification in notifications: + if notification.title == notification_title: + already_notified = True + break + + if not already_notified: + Notification.create( + user_id=alias.user_id, + title=notification_title, + message=Notification.render( + "notification/message-quarantine.html", alias=alias + ), + ) return status.E519 return None diff --git a/templates/notification/message-quarantine.html b/templates/notification/message-quarantine.html new file mode 100644 index 00000000..4970572b --- /dev/null +++ b/templates/notification/message-quarantine.html @@ -0,0 +1,4 @@ +
+ There is a new quarantined message for {{ alias.email }}. Please go to your quarantined emails to review it. +
+ diff --git a/tests/test_email_handler.py b/tests/test_email_handler.py index 98d9ad94..5f88c8b9 100644 --- a/tests/test_email_handler.py +++ b/tests/test_email_handler.py @@ -4,7 +4,14 @@ from aiosmtpd.smtp import Envelope import email_handler from app.email import headers, status -from app.models import User, Alias, AuthorizedAddress, IgnoredEmail, EmailLog +from app.models import ( + User, + Alias, + AuthorizedAddress, + IgnoredEmail, + EmailLog, + Notification, +) from email_handler import ( get_mailbox_from_mail_from, should_ignore, @@ -85,6 +92,9 @@ def test_dmarc_quarantine(flask_client): email_log = email_logs[0] assert email_log.blocked assert email_log.refused_email_id + notifications = Notification.filter_by(user_id=user.id).all() + assert len(notifications) == 1 + assert f"{alias.email} has a new mail in quarantine" == notifications[0].title def test_gmail_dmarc_softfail(flask_client):