From 71503c0ea3e52377bfbb01ff3649842b6dab541f Mon Sep 17 00:00:00 2001 From: Calum Mackervoy Date: Tue, 4 Feb 2020 14:44:11 +0000 Subject: [PATCH] All notification fix --- djangoldp_notification/models.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/djangoldp_notification/models.py b/djangoldp_notification/models.py index ddc9ff8..02ade0a 100644 --- a/djangoldp_notification/models.py +++ b/djangoldp_notification/models.py @@ -37,6 +37,19 @@ class Notification(Model): def __str__(self): return '{}'.format(self.type) + def save(self, *args, **kwargs): + # I cannot send a notification to myself + if self.author.startswith(settings.SITE_URL): + try: + # author is a WebID.. convert to local representation + author = Model.resolve(self.author.replace(settings.SITE_URL, ''))[1] + except NoReverseMatch: + author = None + if author == self.user: + return + + super(Notification, self).save(*args, **kwargs) + class Subscription(Model): object = models.URLField() @@ -61,6 +74,7 @@ def send_notification(sender, instance, created, **kwargs): except NoReverseMatch: return + # dispatch a notification for every Subscription on this resource for subscription in Subscription.objects.filter(models.Q(object=url_resource) | models.Q(object=url_container)): process = Thread(target=send_request, args=[subscription.inbox, url_resource, instance, created]) process.start() @@ -73,9 +87,11 @@ def send_request(target, object_iri, instance, created): request_type = "creation" if created else "update" try: + # local inbox if target.startswith(settings.SITE_URL): user = Model.resolve_parent(target.replace(settings.SITE_URL, '')) Notification.objects.create(user=user, object=object_iri, type=request_type, author=author) + # external inbox else: json = { "@context": settings.LDP_RDF_CONTEXT, @@ -92,14 +108,18 @@ def send_request(target, object_iri, instance, created): @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: + # get author name, and store in who try: + # local author if instance.author.startswith(settings.SITE_URL): who = str(Model.resolve_id(instance.author.replace(settings.SITE_URL, ''))) + # external author else: who = requests.get(instance.author).json()['name'] except: who = "Unknown Person" + # get identifier for resource triggering notification, and store in where try: if instance.object.startswith(settings.SITE_URL): where = str(Model.resolve_id(instance.object.replace(settings.SITE_URL, ''))) @@ -111,7 +131,7 @@ def send_email_on_notification(sender, instance, created, **kwargs): if who == where: where = "has sent you a private message" else: - where = "mention you on " + where + where = "mentioned you on " + where html_message = loader.render_to_string( 'email.html',