Finish send push notifications #3

Closed
opened 2021-04-07 14:59:57 +00:00 by decentral1se · 2 comments
Owner

Send push notifications

We suggest adding a new custom /send_push/ endpoint to djangoldp-notification, to receive the actual notification data. This method will look up a user based on the ID provided in the notification request, and trigger the in-browser notification by calling the send_user_notification method from django-webpush. We will unit test to validate these assumptions.

Ripped from https://git.startinblox.com/djangoldp-packages/djangoldp/issues/338.

> Send push notifications > > We suggest adding a new custom /send_push/ endpoint to djangoldp-notification, to receive the actual notification data. This method will look up a user based on the ID provided in the notification request, and trigger the in-browser notification by calling the send_user_notification method from django-webpush. We will unit test to validate these assumptions. Ripped from https://git.startinblox.com/djangoldp-packages/djangoldp/issues/338.
Owner
diff --git a/djangoldp_webpushnotification/views.py b/djangoldp_webpushnotification/views.py
index 1b46547..535bacc 100644
--- a/djangoldp_webpushnotification/views.py
+++ b/djangoldp_webpushnotification/views.py
@@ -5,9 +5,12 @@ 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
 
+from djangoldp_account.models import LDPUser
+from djangoldp_webpushnotification.models import VAPIDKeyset
+
 
 @require_POST
 @csrf_exempt
@@ -22,6 +25,12 @@ def send_push(request):
         user_id = data["id"]
         user = get_object_or_404(LDPUser, pk=user_id)
         payload = {"head": data["head"], "body": data["body"]}
+        vapid_key = VAPIDKeyset.objects.first()
+        settings.WEBPUSH_SETTINGS = {
+            'VAPID_PUBLIC_KEY': vapid_key.public_key,
+            'VAPID_PRIVATE_KEY': vapid_key.private_key,
+            'VAPID_ADMIN_EMAIL': 'foo@bar.com',
+        }
         send_user_notification(user=user, payload=payload, ttl=1000)
 
         return JsonResponse(status=200, data={"message": "Web push successful"})
```diff diff --git a/djangoldp_webpushnotification/views.py b/djangoldp_webpushnotification/views.py index 1b46547..535bacc 100644 --- a/djangoldp_webpushnotification/views.py +++ b/djangoldp_webpushnotification/views.py @@ -5,9 +5,12 @@ 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 +from djangoldp_account.models import LDPUser +from djangoldp_webpushnotification.models import VAPIDKeyset + @require_POST @csrf_exempt @@ -22,6 +25,12 @@ def send_push(request): user_id = data["id"] user = get_object_or_404(LDPUser, pk=user_id) payload = {"head": data["head"], "body": data["body"]} + vapid_key = VAPIDKeyset.objects.first() + settings.WEBPUSH_SETTINGS = { + 'VAPID_PUBLIC_KEY': vapid_key.public_key, + 'VAPID_PRIVATE_KEY': vapid_key.private_key, + 'VAPID_ADMIN_EMAIL': 'foo@bar.com', + } send_user_notification(user=user, payload=payload, ttl=1000) return JsonResponse(status=200, data={"message": "Web push successful"}) ```
Author
Owner

Question: Is there one VAPIDKeySet instance per Hubl instance or per DjangoLDP backend? If user1 is on HublA and user2 is on HublB, do we have VAPIDKeySet1 and VAPIDKeySet2? Or just VAPIDKeyset1 for everything?

Question: Is there one `VAPIDKeySet` instance per Hubl instance or per DjangoLDP backend? If user1 is on HublA and user2 is on HublB, do we have VAPIDKeySet1 and VAPIDKeySet2? Or just VAPIDKeyset1 for everything?
This repo is archived. You cannot comment on issues.
No Label
No Milestone
No project
No Assignees
2 Participants
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: autonomic-cooperative/djangoldp-webpushnotification#3
No description provided.