From f278f759fbe98e8a700484d648ea511c60017fec Mon Sep 17 00:00:00 2001
From: Jean-Baptiste Pasquier <contact@jbpasquier.eu>
Date: Wed, 2 Oct 2019 16:08:05 +0200
Subject: [PATCH] update: fix !8

---
 djangoldp_notification/models.py            | 44 +++++++++++++--------
 djangoldp_notification/templates/email.html |  2 +-
 2 files changed, 28 insertions(+), 18 deletions(-)

diff --git a/djangoldp_notification/models.py b/djangoldp_notification/models.py
index f47ebcb..2c5a3d4 100644
--- a/djangoldp_notification/models.py
+++ b/djangoldp_notification/models.py
@@ -79,22 +79,32 @@ def send_request(target, object_iri):
 @receiver(post_save, sender=Notification)
 def send_email_on_notification(sender, instance, created, **kwargs):
     if created and instance.summary and settings.JABBER_DEFAULT_HOST and instance.user.email:
-        html_message = loader.render_to_string(
-            'email.html',
-            {
-                'on': settings.JABBER_DEFAULT_HOST,
-                'instance': instance,
-                'author': requests.get(instance.author).json()['name'] or 'Unknown user'
-            }
-        )
-        send_mail(
-            'Notification on ' + settings.JABBER_DEFAULT_HOST,
-            instance.summary,
-            settings.EMAIL_HOST_USER or "noreply@" + settings.JABBER_DEFAULT_HOST,
-            [instance.user.email],
-            fail_silently=True,
-            html_message=html_message
-        )
+        try: 
+            who = requests.get(instance.author).json()['name'] or 'Unknown Person' # I've no idea how to handle dead links.
+            where = requests.get(instance.object).json()['name'] or 'some unknown place' # So let's get to the unknown :)
+            if(instance.author == instance.object):
+                where = "has sent you a private message"
+            else:
+                where = "mention you on " + where
+            html_message = loader.render_to_string(
+                'email.html',
+                {
+                    'on': settings.JABBER_DEFAULT_HOST,
+                    'instance': instance,
+                    'author': who,
+                    'object': where
+                }
+            )
+            send_mail(
+                'Notification on ' + settings.JABBER_DEFAULT_HOST,
+                instance.summary,
+                settings.EMAIL_HOST_USER or "noreply@" + settings.JABBER_DEFAULT_HOST,
+                [instance.user.email],
+                fail_silently=True,
+                html_message=html_message
+            )
+        except:
+            logging.error('Djangoldp_notifications: Can\'t mail the user')
     else:
         if created:
-            raise Exception('Misconfiguration, missing JABBER_DEFAULT_HOST or incomplete instance')
+            raise Exception('Djangoldp_notifications: Misconfiguration, missing JABBER_DEFAULT_HOST or no mail for user found')
diff --git a/djangoldp_notification/templates/email.html b/djangoldp_notification/templates/email.html
index 2907e39..57f8d37 100644
--- a/djangoldp_notification/templates/email.html
+++ b/djangoldp_notification/templates/email.html
@@ -4,7 +4,7 @@
     </head>
     <body>
         <div>
-            <p style='font-style:italic;color:#777'>A new {{ instance.type }} from <b>{{ author }}</b> on {{ on }}</p>
+            <p style='font-style:italic;color:#777'><b>{{ author }}</b> {{ object }} from  on {{ on }}</p>
             <p>{{ instance.summary }}</p>
         </div>
         <div style='margin-top:10px;'>
-- 
GitLab