From dab7ed1bb5b26b97d3dedc2c809804a7c1f06c0e Mon Sep 17 00:00:00 2001
From: Carina Antunes <carina.oliveira.antunes@cern.ch>
Date: Sat, 5 Jun 2021 19:52:07 +0200
Subject: [PATCH] email improvements

---
 .env                                                |  3 +++
 docker/activemq/conf/activemq.xml                   |  6 +++---
 .../email_templates/base_email.html                 |  2 +-
 notifications_consumer/processors/dlq/processor.py  |  7 +++++++
 .../dlq/templates/simple_notification.html          |  6 ++++++
 .../processors/email/processor.py                   | 12 +++++++++---
 .../email/templates/notification_body.html          |  8 +++++---
 .../email/templates/simple_notification.txt         | 10 ++++------
 .../email_daily/templates/daily_notification.txt    |  2 +-
 .../processors/safaripush/processor.py              |  3 +--
 .../processors/webpush/processor.py                 |  3 +--
 scripts/docker-send-email.py                        | 13 ++++++++-----
 12 files changed, 49 insertions(+), 26 deletions(-)

diff --git a/.env b/.env
index 0b49132..7be22b1 100644
--- a/.env
+++ b/.env
@@ -28,3 +28,6 @@ EMAIL_WHITELIST=["user@cern.ch"]
 
 CERN_OIDC_CLIENT_ID=notifications-dev
 CERN_OIDC_CLIENT_SECRET=fill-me
+
+EMAIL_AES_SECRET_KEY=fill-mefill-mefill-mefill-mefill
+
diff --git a/docker/activemq/conf/activemq.xml b/docker/activemq/conf/activemq.xml
index 8e0c6d0..7123d50 100644
--- a/docker/activemq/conf/activemq.xml
+++ b/docker/activemq/conf/activemq.xml
@@ -37,7 +37,7 @@
                             />
                             <redeliveryPolicy
                                     queue="np.routing"
-                                    maximumRedeliveries="6"
+                                    maximumRedeliveries="2"
                                     initialRedeliveryDelay="10000"
                                     backOffMultiplier="3"
                                     maximumRedeliveryDelay="-1"
@@ -45,7 +45,7 @@
                             />
                             <redeliveryPolicy
                                     queue="np.webpush"
-                                    maximumRedeliveries="6"
+                                    maximumRedeliveries="2"
                                     initialRedeliveryDelay="10000"
                                     backOffMultiplier="3"
                                     maximumRedeliveryDelay="-1"
@@ -53,7 +53,7 @@
                             />
                             <redeliveryPolicy
                                     queue="np.safaripush"
-                                    maximumRedeliveries="6"
+                                    maximumRedeliveries="2"
                                     initialRedeliveryDelay="10000"
                                     backOffMultiplier="3"
                                     maximumRedeliveryDelay="-1"
diff --git a/notifications_consumer/email_templates/base_email.html b/notifications_consumer/email_templates/base_email.html
index ef696e6..91241dd 100644
--- a/notifications_consumer/email_templates/base_email.html
+++ b/notifications_consumer/email_templates/base_email.html
@@ -36,7 +36,7 @@
           <tr>
             <td style="text-align: center; color: #aaa; font-size: 11px; padding-bottom: 10px;">
               <p style="padding: 10px;">
-                This notification has been sent by CERN Notifications. </br>
+                This notification has been sent by CERN Notifications. <br/>
                 <a href="{{ base_url }}/#subscriptions">Manage your subscriptions</a> |
                 <a href="{{ base_url }}/#manage">Manage your channels</a> | 
                 <a href="{{ base_url }}/unsubscribe/{{ unsubscribe_blob }}/{{ unsubscribe_mail }}">Unsubscribe</a>
diff --git a/notifications_consumer/processors/dlq/processor.py b/notifications_consumer/processors/dlq/processor.py
index 4dbe858..18eaa7d 100644
--- a/notifications_consumer/processors/dlq/processor.py
+++ b/notifications_consumer/processors/dlq/processor.py
@@ -56,11 +56,18 @@ class DLQProcessor(Processor):
         except Exception:
             pass
 
+        q_headers = kwargs['headers'] or kwargs
+        try:
+            q_headers = json.dumps(q_headers, sort_keys=False, indent=2)
+        except Exception:
+            pass
+
         context = {
             "queue": kwargs["headers"]["destination"],
             "sender": Config.SERVICE_NAME,
             "summary": summary,
             "message_body": message,
+            "q_headers": q_headers
         }
 
         email = create_email(
diff --git a/notifications_consumer/processors/dlq/templates/simple_notification.html b/notifications_consumer/processors/dlq/templates/simple_notification.html
index 4d84652..2719d35 100644
--- a/notifications_consumer/processors/dlq/templates/simple_notification.html
+++ b/notifications_consumer/processors/dlq/templates/simple_notification.html
@@ -7,6 +7,12 @@
                     <p style="color: #3071AB;"><b>{{ summary }}</b></p>
                 </td>
             </tr>
+            <tr>
+                <td style="padding:10px">
+                    <p><b>Header:</b></p>
+                    <pre><code>{{ q_headers }}</code></pre>
+                </td>
+            </tr>
             <tr>
                 <td style="padding:10px">
                     <p><b>Message:</b></p>
diff --git a/notifications_consumer/processors/email/processor.py b/notifications_consumer/processors/email/processor.py
index aa50903..d187748 100644
--- a/notifications_consumer/processors/email/processor.py
+++ b/notifications_consumer/processors/email/processor.py
@@ -1,5 +1,6 @@
 """Email notifications handling."""
 import logging
+from datetime import datetime
 from typing import Dict
 
 from notifications_consumer.config import ENV_DEV, Config
@@ -26,13 +27,19 @@ class EmailProcessor(Processor):
 
     def process(self, **kwargs):
         """Process the message and send an email."""
-        logging.debug("I'm the %r and i'm consuming with %r", self, kwargs)
+        logging.debug("%s - status:running - kwargs:%r", self, kwargs)
 
         recipient_email = kwargs["email"]
         if Config.ENV == ENV_DEV and recipient_email not in Config.EMAIL_WHITELIST:
             logging.info("Email recipient <%s> isn't on the EMAIL_WHITELIST. Email sent is skipped.", recipient_email)
             return
 
+        created_at = kwargs.get("created_at", "")
+        try:
+            created_at = datetime.strptime(created_at, "%Y-%m-%dT%H:%M:%S.%fZ").strftime("%m/%d/%Y, %H:%M:%S")
+        except Exception:
+            logging.exception("Failed to process created at date")
+
         subject = f'[{kwargs["channel_name"]}] {kwargs["summary"]}'
         context = {
             "message_body": kwargs["message_body"],
@@ -43,6 +50,7 @@ class EmailProcessor(Processor):
             "link": kwargs["link"],
             "notification_priority": NotificationPriority,
             "priority": kwargs.get("priority", ""),
+            "created_at": created_at,
         }
 
         text_template = Config.TEMPLATES.get_template("email/simple_notification.txt")
@@ -59,8 +67,6 @@ class EmailProcessor(Processor):
 
     def read_message(self, message: Dict):
         """Read the message."""
-        logging.debug("I'm the %r and i'm reading the message", self)
-
         logging.debug(message)
 
         return message
diff --git a/notifications_consumer/processors/email/templates/notification_body.html b/notifications_consumer/processors/email/templates/notification_body.html
index 956f452..443154e 100644
--- a/notifications_consumer/processors/email/templates/notification_body.html
+++ b/notifications_consumer/processors/email/templates/notification_body.html
@@ -1,6 +1,6 @@
 <table border="0" cellpadding="0" cellspacing="0" style="border-radius: 5px; background: #f9f9f9; min-width: 550px;">
   <td style="padding:10px 20px 0 20px">
-    <p style="line-height: 145%;">
+    <p>
       <a style="text-decoration:none" href="{{ link }}">
         <b>
           {% if priority == notification_priority.IMPORTANT %}
@@ -8,12 +8,14 @@
               &#9888;
             </span>
           {% endif %}
-          <span style="color: #3071AB;">{{ summary }}</span></b>
+          <span style="color: #3071AB; font-size: 1.2em;">{{ summary }}</span></b>
       </a>
+        <br/>
+        <span style="color: #aaa;">{{ created_at }}</span>
     </p>
   </td>
   <tr>
-    <td style="padding: 20px">
+    <td style="padding: 0 20px">
       <p>
         {{ message_body|safe }}
       </p>
diff --git a/notifications_consumer/processors/email/templates/simple_notification.txt b/notifications_consumer/processors/email/templates/simple_notification.txt
index 1701cee..5f1dce2 100644
--- a/notifications_consumer/processors/email/templates/simple_notification.txt
+++ b/notifications_consumer/processors/email/templates/simple_notification.txt
@@ -1,7 +1,5 @@
-{{ channel_name }}
-----------------------------
-
-{{ message_body }}
-
------
+{{ message_body|striptags }}
+-
+{{ created_at }}
+-
 This notification has been sent by CERN Notifications.
diff --git a/notifications_consumer/processors/email_daily/templates/daily_notification.txt b/notifications_consumer/processors/email_daily/templates/daily_notification.txt
index 62d15a3..0728318 100644
--- a/notifications_consumer/processors/email_daily/templates/daily_notification.txt
+++ b/notifications_consumer/processors/email_daily/templates/daily_notification.txt
@@ -10,5 +10,5 @@
     {% endfor %}
 
 {% endfor %}
-------
+-
 This notification has been sent by CERN Notifications.
diff --git a/notifications_consumer/processors/safaripush/processor.py b/notifications_consumer/processors/safaripush/processor.py
index 5657f0d..1b70310 100644
--- a/notifications_consumer/processors/safaripush/processor.py
+++ b/notifications_consumer/processors/safaripush/processor.py
@@ -24,7 +24,7 @@ class SafariPushProcessor(Processor):
 
     def process(self, **kwargs):
         """Process the message and send a push notification."""
-        logging.debug("I'm the %r and i'm consuming with %r", self, kwargs)
+        logging.debug("%s - status:running - kwargs:%r", self, kwargs)
 
         device_token = kwargs["devicetoken"]
 
@@ -40,7 +40,6 @@ class SafariPushProcessor(Processor):
 
     def read_message(self, message: Dict):
         """Read the message."""
-        logging.debug("I'm the %r and i'm reading the message", self)
         logging.debug(message)
 
         return message
diff --git a/notifications_consumer/processors/webpush/processor.py b/notifications_consumer/processors/webpush/processor.py
index 085581d..94fef94 100644
--- a/notifications_consumer/processors/webpush/processor.py
+++ b/notifications_consumer/processors/webpush/processor.py
@@ -24,7 +24,7 @@ class WebPushProcessor(Processor):
 
     def process(self, **kwargs):
         """Process the message and send a push notification."""
-        logging.debug("I'm the %r and i'm consuming with %r", self, kwargs)
+        logging.debug("%s - status:running - kwargs:%r", self, kwargs)
 
         device_token = kwargs["devicetoken"]
         encoding = kwargs.get("encoding", None)
@@ -41,7 +41,6 @@ class WebPushProcessor(Processor):
 
     def read_message(self, message: Dict):
         """Read the message."""
-        logging.debug("I'm the %r and i'm reading the message", self)
         logging.debug(message)
 
         return message
diff --git a/scripts/docker-send-email.py b/scripts/docker-send-email.py
index 5098edd..7c994bd 100644
--- a/scripts/docker-send-email.py
+++ b/scripts/docker-send-email.py
@@ -1,14 +1,17 @@
 """Utility to send one message inside docker environment."""
 import stomp
 
-
 conn = stomp.Connection([("activemq", 61613)])
 conn.connect("admin", "admin", wait=True)
 message_body = r"""{
-"channel_name":"My Channel",
-"message_body":"<p>Dear subscribers,</p>\n\n<p>This week news follows.&nbsp;</p>\n\n<p>&nbsp;</p>\n\n<p><strong>Topic 1</strong></p>\n\n<ul>\n\t<li>bla&nbsp;</li>\n\t<li>bla</li>\n\t<li>bla</li>\n</ul>\n\n<p>&nbsp;</p>\n\n<p><strong>Topic 2</strong></p>\n\n<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>\n\n<p>&nbsp;</p>\n\n<p>See you next week,</p>\n\n<p>Myself.</p>\n",
-"summary":"This week news!",
-"email":"user@cern.ch"
+"channel_name": "My Channel",
+"channel_id": "123",
+"message_body": "<p>Dear subscribers,</p>\n\n<p>This week news follows.&nbsp;</p>\n\n<p>&nbsp;</p>\n\n<p><strong>Topic 1</strong></p>\n\n<ul>\n\t<li>bla&nbsp;</li>\n\t<li>bla</li>\n\t<li>bla</li>\n</ul>\n\n<p>&nbsp;</p>\n\n<p><strong>Topic 2</strong></p>\n\n<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>\n\n<p>&nbsp;</p>\n\n<p>See you next week,</p>\n\n<p>Myself.</p>\n",
+"summary": "This week news!",
+"email": "user@cern.ch",
+"created_at": "2021-02-26T13:59:40.754Z",
+"notification_id": "BD19EEA4-9DCA-48D9-A577-5DE9D2BF374A",
+"link": "http://toto.cern.ch"
 } """
 conn.send(body=message_body, destination="/queue/np.email", headers={"persistent": "true"})
 conn.disconnect()
-- 
GitLab