From 49148320eee537dfb116f12e34bb96fd830b6901 Mon Sep 17 00:00:00 2001 From: trav Date: Thu, 1 Apr 2021 10:24:39 -0400 Subject: [PATCH] push notifs stuff from https://www.digitalocean.com/community/tutorials/how-to-send-web-push-notifications-from-django-applications --- server/__pycache__/__init__.cpython-37.pyc | Bin 0 -> 182 bytes server/__pycache__/admin.cpython-37.pyc | Bin 0 -> 277 bytes server/__pycache__/models.cpython-37.pyc | Bin 0 -> 266 bytes server/__pycache__/urls.cpython-37.pyc | Bin 0 -> 867 bytes server/__pycache__/views.cpython-37.pyc | Bin 0 -> 1491 bytes server/__pycache__/wsgi.cpython-37.pyc | Bin 0 -> 541 bytes .../__pycache__/0001_initial.cpython-37.pyc | Bin 0 -> 1154 bytes .../0002_auto_20210315_1108.cpython-37.pyc | Bin 0 -> 604 bytes .../0003_delete_todo.cpython-37.pyc | Bin 0 -> 518 bytes .../__pycache__/__init__.cpython-37.pyc | Bin 0 -> 193 bytes server/templates/home.html | 180 ++++++++++++++++++ server/templates/sw.js | 18 ++ server/urls.py | 7 + server/views.py | 36 ++++ settings.yml | 13 +- 15 files changed, 251 insertions(+), 3 deletions(-) create mode 100644 server/__pycache__/__init__.cpython-37.pyc create mode 100644 server/__pycache__/admin.cpython-37.pyc create mode 100644 server/__pycache__/models.cpython-37.pyc create mode 100644 server/__pycache__/urls.cpython-37.pyc create mode 100644 server/__pycache__/views.cpython-37.pyc create mode 100644 server/__pycache__/wsgi.cpython-37.pyc create mode 100644 server/migrations/__pycache__/0001_initial.cpython-37.pyc create mode 100644 server/migrations/__pycache__/0002_auto_20210315_1108.cpython-37.pyc create mode 100644 server/migrations/__pycache__/0003_delete_todo.cpython-37.pyc create mode 100644 server/migrations/__pycache__/__init__.cpython-37.pyc create mode 100644 server/templates/home.html create mode 100644 server/templates/sw.js create mode 100644 server/views.py diff --git a/server/__pycache__/__init__.cpython-37.pyc b/server/__pycache__/__init__.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..abe0f438b36ab01fdbe1aa06f69509626fb6dd5c GIT binary patch literal 182 zcmZ?b<>g`kf+mZg1Q7igM8E(ekl_Ht#VkM~g&~+hlhJP_LlH4=qkDD%LM4 zN-WcN$xklLP0cGQ)=$qY$tx}`N>9~KEG@~;%g@bB)-NteEGo&&OUlWwKxXSg#q-nB m^npf|r55SO$7kkcmc+;F6;$5hu*uC&Da}c>16lkTh#3HbwK7}) literal 0 HcmV?d00001 diff --git a/server/__pycache__/admin.cpython-37.pyc b/server/__pycache__/admin.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..907f3d0420ce7bd023365843c453930d39564f92 GIT binary patch literal 277 zcmZ?b<>g`kf+mZg1Ro&%7{q}ACLqHBh>K-_L<&O+V-7ID$m{G+A!(rGSO>lJoOQiZYXmK*rzV zhY04R6zGAq7BK@wS27f_0Vy!?D@H%GIJKx)zoaO!Oy4CxximL5ucTN%J+maQxU?ue zRX?$`BtI`dH#1qkxFoTtBr`85C%*!jtqT>;PfODW8da8Bqz|@Bub}c4hfQvNN@-52 N9V5`fVvv(~7y*nhO;i8? literal 0 HcmV?d00001 diff --git a/server/__pycache__/models.cpython-37.pyc b/server/__pycache__/models.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..cda65b60c49f964a6acc75a410dd47ddb8dd4074 GIT binary patch literal 266 zcmYk0zY4-I5XO^MMa6;-;0tuHSsg?~Ty;=f3W9BumP-Cel8UarjjzFp^YONhQjR7@KHY%9N|iKaan2mFEOjotY-hvFLQIyHntbvl)hq#yvH_Zwq@# ASO5S3 literal 0 HcmV?d00001 diff --git a/server/__pycache__/urls.cpython-37.pyc b/server/__pycache__/urls.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..9244a65bee65c13a42763de5d81087775ef0c4c0 GIT binary patch literal 867 zcmZ8fy>8nu5Edm_mSrh+;{0@I(b+?+u}e|3L4u+{+W=|OK?UL};>DIgN&-naiPy|| zi!ObS4t;=5Ts!3zGW94sKu{7Qzq|YHS3HQKz{2(N4}AY&Th?DAJgz>1xA@85cu_7`isbC2CvW1@WtO6BEDj&0q|%ZxP?w9^QNHczTlgWmm9Q5%UQ)}J;BIw z**$OL#o7{P@Js&+E{t!y4A*GI>&UA~A(zk6x@LKXwNzz%y={MnWsKGC#!+x~=$wq);r+hjEp z(J`LuzDz77@(bY^DkFg9jxHV@$uqZ~@%~z0U0%i*Y5^iPl}+j!?Z4&~Z5X^U Z??67sBP2r-;vSmYG04Xql3m9m{{Ww^4jZ89 zvL;wTnb6xarXoH$X9)}2j{zvf3d(bJQFvTgla#py)uLvbS;}xmUxwCSVTOsOH(5PFH zlw3Iww0ESt&qfEwr{ky5cyw}dc=TkfyI(&%K71SdrL;$575AoG-q> zOH!EB6()CuVO*F;L^n>Bb2c)2`!%s+rp-~r9fSF{BvNWmB;GgkCTniGcg%{0tsHZv zT~MXjiW~x$+$6Vgm(+29cW{SvaR-YJ0qgJSJ$fD&GY*NZ<^!>=cw5rpoi%imK$In1 z#EJ`P*V=Nw2?5?bq`T|I@{9|WluFwCx8dpc)>y&|CDO?NviNL}bILMlL)ZW7$vh0A zVeI?s>baq*^`FJ5Ej;l7EV8L=1AB`YOOa)#{xSG0GxmvjJ1jW?5Yn~HNT$_E;1m2W D1;K*a literal 0 HcmV?d00001 diff --git a/server/__pycache__/wsgi.cpython-37.pyc b/server/__pycache__/wsgi.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..ba8cd316764f7e5419060fcb5ca57f52e421001a GIT binary patch literal 541 zcmYjOu};G<5Vf5qP1_VYAjHB4bf8%gLI@Bu5>yBXLkWo+xpM5vb_!*LA1NchfdwF;7ET4C~Z3OG>@cQu;BlP8x)d~Qd!qF25Im$6diTe_cOOkllVUIUT zKk=8i!Gp4yG%=#yB-BT=xoCYL$ozMdwC8)E_Iam_k_fnH(RKfBk$}wYdnCvmcnncL z_y#|8*~d22)EFiT4S1)ZX7sEJHcF>c&a%`nDS+?re7Y$+%M6<_Gry{6MTb<)SVsF> zmPluF6J<#=7TKTSzuT84VCsIAgmf`0iz5$JnjY?I literal 0 HcmV?d00001 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 0000000000000000000000000000000000000000..1c58d03008ee683cc515c316ab215a6ac1a09c1a GIT binary patch literal 1154 zcmYjQ&5j&35N^A>=YKX6vH^)xU;DuJ=759{ibAqlghV0)o6B4pPgl*1XWjP1c5ilb z1g^sqaDXGP2ws)`1y+;ev(NP{oM-3L2~qrmOG;j zB48@2*__pU&RHvS70;7zB8VaRHG%}$CjBLzr@()V^7K8~L^%(mTCS|{(r6cYwE81x9V(f1Qz-T6rLT{HWIrMS38EwYhErY=> zrn@2OO-N@1;~gh?(#Z^_2l<5LDLjDLj?wEw?Ei7{XSC$29@%^ZkKpw87#{E96`MbR zj|s&mVfgZx{#8h>44A+rTn7H_MV^F-XuK&ai^3y>@z%;(*t_x?@50cbl|spP7$!Gp z7sg?!MU5eCtdfw&Azdh;uS236r9!XK@=b$bEM2(})wPoP`rYRaeb$DkiN8uHb(eXQ zNev`;%={X&mo1^0beYJGX2f|Pa48z)!*GpiTY9|p;W62pw})i!V|>}hAg`A~IdW+- zkWMTVesGs{@Th#5SbDUFh)&!frA~m}|4U`d8P!?bmJ0RCufqUP071Y!4Y2?Sy=pD= z3PX>wMNgJD65ob?Wwf_OS=PFU!$2&Yx1#dl(E&iYMO#bfy6#B;^j<(o#RHd&JTQcF*j2@`UM8cy*E{`2Mw3`^U+1V-yN~P<^o$c7L2+P^qqDjeBuq z2+{sG{1w@P{B+wtx_C_u;|hX*Q(Ty;sZo1Ztfbeju`4XvHqfS)RpGp_Uh0K1xBowX zcDOgoWkIf}=ZgEESDc@pKPeB$&$f5A{n~w6?o-+ZJmE=_FwRfeDVxzeVUw6M&N{uk ucf@`Mi;k#0H?t3foGrUr*u2Hly1qxYKb_NE7<{!a$10hk}A%SSNF}cia@G7wv+u7p^8ldGv z_$9Yg`~nr@Ga5hJ&u^Yzot)&1;@jD~k6$@szr*4f2{q@G_AwP;z&qCS4d?6) zg9zdu3}WQF=zF}0AbHP<*&jN`3hwh>wx)JcE87I<7#TI^l(wRRY{P-kRo?PVjLB{m zNFjk4q%eo9ok@h7oll$w?H>Sc>!EXE zkEWN_1|>eO0esqgs#S|V1?gnp4SgoqF)BTN!NkmF%!{20anj}2pBYIiHegQjjOP|8kIMayV>7KerbkS$e OCu3d&J{#s=#{U3FT&hd} literal 0 HcmV?d00001 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 0000000000000000000000000000000000000000..bdba30a3f8ca643c47ab7d2adb7907f5c8f88dab GIT binary patch literal 518 zcmYjNJx{|h5VeyKN@-bH5fVcNa8iWCfDl5JnA(AlC5s955v5A(Vmk;{VC9GKm%K9Z z7nnHrL*=A9y}R?B-@D6xze~{GPOt9|C?VfISy_bO0^RIm5Jb?LG&HA_ToDn7@PmjD z=rni_^FTy5B-!}EcO;>%+f=14Y^9``_|nQk1Q+OL4+D^#3WB4wqC%@K1c^ydYql`EK5)G=vE1ZzeloYV4Q5a4(A?n?z)^e zO0>1dJ{a{o#puR|P8dfKo!7=m=(fbPkfk~j4|5Uo`kL+n?ckW}oGmVK?zb5)xEKBbX_bh; literal 0 HcmV?d00001 diff --git a/server/migrations/__pycache__/__init__.cpython-37.pyc b/server/migrations/__pycache__/__init__.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..aa88342814b16b0f0edbccab8f96048b3a19d2c6 GIT binary patch literal 193 zcmZ?b<>g`kf+mZg1Q7igM8E(ekl_Ht#VkM~g&~+hlhJP_LlH9~KEG@~;%g@bB)-NteEGo&&OUlWwKxXSg#q-nB y^npf|r55SuW~LV + + + + + + + {% 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"