All notification fix

This commit is contained in:
Calum Mackervoy 2020-02-04 14:44:11 +00:00 committed by Jean-Baptiste Pasquier
parent c1f320d18b
commit 71503c0ea3
1 changed files with 21 additions and 1 deletions

View File

@ -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',