From 2bffcb2059cdc918e3c5eac5586fd58498ea70fe Mon Sep 17 00:00:00 2001 From: Fay Arnold Date: Fri, 30 Apr 2021 14:30:55 +0100 Subject: [PATCH] add webpushnotification dependency and post_save hook for notifications --- djangoldp_notification/models.py | 30 ++++++++++++++++++++++++++++++ setup.cfg | 1 + 2 files changed, 31 insertions(+) diff --git a/djangoldp_notification/models.py b/djangoldp_notification/models.py index 580fe29..0c20bad 100644 --- a/djangoldp_notification/models.py +++ b/djangoldp_notification/models.py @@ -1,4 +1,5 @@ import requests +import json from django.conf import settings from django.contrib.auth import get_user_model from django.core.mail import send_mail @@ -275,6 +276,35 @@ def send_email_on_notification(sender, instance, created, **kwargs): html_message=html_message ) +@receiver(post_save, sender=Notification) +def send_webpush_on_notification(sender, instance, created, **kwargs): + if instance.summary : + if instance.user: # if the person exists and accepts notifications + + try: + # local author + if instance.author.startswith(settings.SITE_URL): + who = str(Model.resolve_id(instance.author.replace(settings.SITE_URL, '')).get_full_name()) + # external author + else: + who = requests.get(instance.author).json()['name'] + except: + who = "Quelqu'un" + + payload = { + "head": "Notification from " + who, + "body": instance.summary, + "id": instance.user.id + } # make the payload that we wanna send? + + url = settings.SITE_URL + reverse("send_webpush") # get the url to post webpush to + + response = requests.post( + url, + data=json.dumps(payload) + ) + + @receiver(post_save, sender=settings.AUTH_USER_MODEL) def create_user_settings(sender, instance, created, **kwargs): try: diff --git a/setup.cfg b/setup.cfg index 3046ee8..c4ee271 100644 --- a/setup.cfg +++ b/setup.cfg @@ -12,6 +12,7 @@ packages = find: install_requires = djangoldp~=2.1.0 djangoldp_account~=2.1 + djangoldp_webpushnotification~=1.0.0 [options.extras_require] include_package_data = True