From a8492491b91e516be734851eb4f1a48f73af3592 Mon Sep 17 00:00:00 2001 From: 3wc <3wc.git@doesthisthing.work> Date: Wed, 7 Apr 2021 15:52:32 +0200 Subject: [PATCH 1/3] Fix models.py --- models.py | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/models.py b/models.py index b68ed10..401e686 100644 --- a/models.py +++ b/models.py @@ -1,7 +1,6 @@ -def send_request(target, object_iri, instance, created): - } - ActivityQueueService.send_activity(target, json) +from django.db import models + class VAPIDKeyset(models.Model): public_key = models.BinaryField(max_length=87) private_key = models.BinaryField(max_length=43) @@ -11,7 +10,3 @@ class VAPIDKeyset(models.Model): self.public_key.tobytes()[:10], self.private_key.tobytes()[:10] ) - -@receiver(post_save, sender=Notification) -def send_email_on_notification(sender, instance, created, **kwargs): - if created and instance.summary and getattr(settings,'JABBER_DEFAULT_HOST',False) and instance.user.email: From 764e9aaa3f8c4b6793bce6a326b4607c681d6bb8 Mon Sep 17 00:00:00 2001 From: decentral1se Date: Wed, 7 Apr 2021 15:56:09 +0200 Subject: [PATCH 2/3] Initial boilerplate for accepting subscriptions --- djangoldp_webpushnotification/templates/sw.js | 18 ++++++++++++ djangoldp_webpushnotification/urls.py | 15 ++++++++++ djangoldp_webpushnotification/views.py | 29 +++++++++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 djangoldp_webpushnotification/templates/sw.js create mode 100644 djangoldp_webpushnotification/urls.py create mode 100644 djangoldp_webpushnotification/views.py diff --git a/djangoldp_webpushnotification/templates/sw.js b/djangoldp_webpushnotification/templates/sw.js new file mode 100644 index 0000000..478340a --- /dev/null +++ b/djangoldp_webpushnotification/templates/sw.js @@ -0,0 +1,18 @@ +// Register event listener for the 'push' event. +self.addEventListener("push", function (event) { + // Retrieve the textual payload from event.data (a PushMessageData object). + // Other formats are supported (ArrayBuffer, Blob, JSON), check out the documentation + // on https://developer.mozilla.org/en-US/docs/Web/API/PushMessageData. + const eventInfo = event.data.text(); + const data = JSON.parse(eventInfo); + const head = data.head || "New Notification πŸ•ΊπŸ•Ί"; + const body = data.body || "This is default content. Your notification didn't have one πŸ™„πŸ™„"; + + // Keep the service worker alive until the notification is created. + event.waitUntil( + self.registration.showNotification(head, { + body: body, + icon: "https://i.imgur.com/MZM3K5w.png", + }) + ); +}); diff --git a/djangoldp_webpushnotification/urls.py b/djangoldp_webpushnotification/urls.py new file mode 100644 index 0000000..1551ac2 --- /dev/null +++ b/djangoldp_webpushnotification/urls.py @@ -0,0 +1,15 @@ +from django.urls import include, path +from django.views.generic import TemplateView + +from .views import home, send_push + +urlpatterns = [ + path("send_push", send_push), + path("webpush/", include("webpush.urls")), + path( + "sw.js", + TemplateView.as_view( + template_name="sw.js", content_type="application/x-javascript" + ), + ), +] diff --git a/djangoldp_webpushnotification/views.py b/djangoldp_webpushnotification/views.py new file mode 100644 index 0000000..1b46547 --- /dev/null +++ b/djangoldp_webpushnotification/views.py @@ -0,0 +1,29 @@ +import json + +from django.conf import settings +from django.http.response import HttpResponse, JsonResponse +from django.shortcuts import get_object_or_404, render +from django.views.decorators.csrf import csrf_exempt +from django.views.decorators.http import require_GET, require_POST +from djangoldp_account.models import LDPUser +from webpush import send_user_notification + + +@require_POST +@csrf_exempt +def send_push(request): + try: + body = request.body + data = json.loads(body) + + if "head" not in data or "body" not in data or "id" not in data: + return JsonResponse(status=400, data={"message": "Invalid data format"}) + + user_id = data["id"] + user = get_object_or_404(LDPUser, pk=user_id) + payload = {"head": data["head"], "body": data["body"]} + send_user_notification(user=user, payload=payload, ttl=1000) + + return JsonResponse(status=200, data={"message": "Web push successful"}) + except TypeError: + return JsonResponse(status=500, data={"message": "An error occurred"}) From b86872be246f09d34149d188955ca36fc71f91a7 Mon Sep 17 00:00:00 2001 From: decentral1se Date: Wed, 7 Apr 2021 15:57:33 +0200 Subject: [PATCH 3/3] Add django-webpush to deps --- setup.cfg | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/setup.cfg b/setup.cfg index aadfaa5..3ca61e5 100644 --- a/setup.cfg +++ b/setup.cfg @@ -10,9 +10,10 @@ license = MIT [options] packages = find: install_requires = - djangoldp~=2.1.0 + djangoldp~=2.1 djangoldp_account~=2.1 ecdsa~=0.16.1 + django-webpush~=0.3 [options.extras_require] include_package_data = True