Commit 555c98a8 authored by Jean-Baptiste Pasquier's avatar Jean-Baptiste Pasquier

Merge branch 'all-notification-fix' into 'master'

All notification fix

See merge request !23
parents c1f320d1 71503c0e
Pipeline #7221 passed with stage
in 3 minutes and 8 seconds
......@@ -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',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment