diff --git a/server/__pycache__/__init__.cpython-37.pyc b/server/__pycache__/__init__.cpython-37.pyc new file mode 100644 index 0000000..abe0f43 Binary files /dev/null and b/server/__pycache__/__init__.cpython-37.pyc differ diff --git a/server/__pycache__/admin.cpython-37.pyc b/server/__pycache__/admin.cpython-37.pyc new file mode 100644 index 0000000..907f3d0 Binary files /dev/null and b/server/__pycache__/admin.cpython-37.pyc differ diff --git a/server/__pycache__/models.cpython-37.pyc b/server/__pycache__/models.cpython-37.pyc new file mode 100644 index 0000000..cda65b6 Binary files /dev/null and b/server/__pycache__/models.cpython-37.pyc differ diff --git a/server/__pycache__/urls.cpython-37.pyc b/server/__pycache__/urls.cpython-37.pyc new file mode 100644 index 0000000..9244a65 Binary files /dev/null and b/server/__pycache__/urls.cpython-37.pyc differ diff --git a/server/__pycache__/views.cpython-37.pyc b/server/__pycache__/views.cpython-37.pyc new file mode 100644 index 0000000..966eb14 Binary files /dev/null and b/server/__pycache__/views.cpython-37.pyc differ diff --git a/server/__pycache__/wsgi.cpython-37.pyc b/server/__pycache__/wsgi.cpython-37.pyc new file mode 100644 index 0000000..ba8cd31 Binary files /dev/null and b/server/__pycache__/wsgi.cpython-37.pyc differ diff --git a/server/migrations/__pycache__/0001_initial.cpython-37.pyc b/server/migrations/__pycache__/0001_initial.cpython-37.pyc new file mode 100644 index 0000000..1c58d03 Binary files /dev/null and b/server/migrations/__pycache__/0001_initial.cpython-37.pyc differ diff --git a/server/migrations/__pycache__/0002_auto_20210315_1108.cpython-37.pyc b/server/migrations/__pycache__/0002_auto_20210315_1108.cpython-37.pyc new file mode 100644 index 0000000..b4cb4ab Binary files /dev/null and b/server/migrations/__pycache__/0002_auto_20210315_1108.cpython-37.pyc differ diff --git a/server/migrations/__pycache__/0003_delete_todo.cpython-37.pyc b/server/migrations/__pycache__/0003_delete_todo.cpython-37.pyc new file mode 100644 index 0000000..bdba30a Binary files /dev/null and b/server/migrations/__pycache__/0003_delete_todo.cpython-37.pyc differ diff --git a/server/migrations/__pycache__/__init__.cpython-37.pyc b/server/migrations/__pycache__/__init__.cpython-37.pyc new file mode 100644 index 0000000..aa88342 Binary files /dev/null and b/server/migrations/__pycache__/__init__.cpython-37.pyc differ diff --git a/server/templates/home.html b/server/templates/home.html new file mode 100644 index 0000000..36f02d7 --- /dev/null +++ b/server/templates/home.html @@ -0,0 +1,180 @@ +{% load static %} + + + + + + + + {% if user.id %} + + {% endif %} + Web Push + + + + +
+
+

Send a push notification

+

+ + + +
+
+ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/server/templates/sw.js b/server/templates/sw.js new file mode 100644 index 0000000..fa8edb9 --- /dev/null +++ b/server/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' + }) + ); +}); \ No newline at end of file diff --git a/server/urls.py b/server/urls.py index be94b6e..4889c6a 100644 --- a/server/urls.py +++ b/server/urls.py @@ -2,10 +2,17 @@ from django.conf import settings from django.conf.urls import include, url from django.conf.urls.static import static from django.contrib import admin +from django.urls import path, include +from .views import home, send_push +from django.views.generic import TemplateView urlpatterns = [ url(r"^", include("djangoldp.urls")), url(r"^admin/", admin.site.urls), + path('', home), + 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')), ] if settings.DEBUG: diff --git a/server/views.py b/server/views.py new file mode 100644 index 0000000..2a434f6 --- /dev/null +++ b/server/views.py @@ -0,0 +1,36 @@ +from django.http.response import HttpResponse +from django.views.decorators.http import require_GET +from django.http.response import JsonResponse, HttpResponse +from django.views.decorators.http import require_GET, require_POST +from django.shortcuts import render, get_object_or_404 +from django.contrib.auth.models import User +from django.views.decorators.csrf import csrf_exempt +from webpush import send_user_notification +from django.conf import settings +import json + +@require_GET +def home(request): + webpush_settings = getattr(settings, 'WEBPUSH_SETTINGS', {}) + vapid_key = webpush_settings.get('VAPID_PUBLIC_KEY') + user = request.user + return render(request, 'home.html', {user: user, 'vapid_key': vapid_key}) + +@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(User, 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"}) \ No newline at end of file diff --git a/settings.yml b/settings.yml index 9c7f322..24d5534 100644 --- a/settings.yml +++ b/settings.yml @@ -1,5 +1,5 @@ dependencies: - + djangoldp_notifications ldppackages: server: @@ -11,12 +11,19 @@ server: default: ENGINE: django.db.backends.postgresql_psycopg2 NAME: djangoldp - USER: postgres - PASSWORD: passw0rd LDP_RDF_CONTEXT: https://cdn.happy-dev.fr/owl/hdcontext.jsonld ROOT_URLCONF: server.urls STATIC_ROOT: static + # STATIC_URL: = '/static/' + # STATICFILES_DIRS: = '/Users/trav/Documents/gitnsurge/autonomic/startinblox/startinblox-startinoff' MEDIA_ROOT: media INSTALLED_APPS: - server - djangoldp_crypto # only needed by decentral1se for #236 + - webpush + + + WEBPUSH_SETTINGS: + VAPID_PUBLIC_KEY: "BIDVJ0sd4Cyycf_aGCxhQ_SmXBneWboI3wGO-Iyj3ofeGkvYyNp5o6W9eTf13YkJSz6NlRwiCHA08m8e82n5WXI" + VAPID_PRIVATE_KEY: "zQ-Apj3yLGvcq-l_YNFMFgNVBxQ5_JYEsUbZA36Yhes" + VAPID_ADMIN_EMAIL: "trav@teafry.me"