From d7b943dc8c8f485c975730d6054e32e67db36c91 Mon Sep 17 00:00:00 2001 From: retoor Date: Sat, 8 Feb 2025 17:31:03 +0100 Subject: [PATCH] Changes. --- src/snek/app.py | 3 +++ src/snek/static/push.js | 30 +++++++++++++++++++++++++++++ src/snek/static/service-worker.js | 30 +++++++++++++++++++++++++++++ src/snek/templates/app.html | 6 ++++-- src/snek/templates/search-user.html | 8 ++++++++ src/snek/view/search_user.py | 21 ++++++++++++++++++++ 6 files changed, 96 insertions(+), 2 deletions(-) create mode 100644 src/snek/static/push.js create mode 100644 src/snek/static/service-worker.js create mode 100644 src/snek/templates/search-user.html create mode 100644 src/snek/view/search_user.py diff --git a/src/snek/app.py b/src/snek/app.py index 8cfed8f..6d0d1e6 100644 --- a/src/snek/app.py +++ b/src/snek/app.py @@ -27,6 +27,7 @@ from snek.view.rpc import RPCView from snek.view.status import StatusView from snek.view.web import WebView from snek.view.upload import UploadView +from snek.view.search_user import SearchUserView SESSION_KEY = b"c79a0c5fda4b424189c427d28c9f7c34" @@ -83,6 +84,8 @@ class Application(BaseApplication): self.router.add_view("/register.json", RegisterView) self.router.add_view("/drive.bin", UploadView) self.router.add_view("/drive.bin/{uid}", UploadView) + self.router.add_view("/search-user.html", SearchUserView) + self.router.add_view("/search-user.json", SearchUserView) self.router.add_get("/http-get", self.handle_http_get) self.router.add_get("/http-photo", self.handle_http_photo) self.router.add_get("/rpc.ws", RPCView) diff --git a/src/snek/static/push.js b/src/snek/static/push.js new file mode 100644 index 0000000..806a51e --- /dev/null +++ b/src/snek/static/push.js @@ -0,0 +1,30 @@ +this.onpush = (event) => { + console.log(event.data); + // From here we can write the data to IndexedDB, send it to any open + // windows, display a notification, etc. + }; + + navigator.serviceWorker + .register("service-worker.js") + .then((serviceWorkerRegistration) => { + serviceWorkerRegistration.pushManager.subscribe().then( + (pushSubscription) => { + const subscriptionObject = { + endpoint: pushSubscription.endpoint, + keys: { + p256dh: pushSubscription.getKey('p256dh'), + auth: pushSubscription.getKey('auth'), + }, + encoding: PushManager.supportedContentEncodings, + /* other app-specific data, such as user identity */ + }; + console.log(pushSubscription.endpoint, pushSubscription, subscriptionObject); + // The push subscription details needed by the application + // server are now available, and can be sent to it using, + // for example, the fetch() API. + }, + (error) => { + console.error(error); + }, + ); + }); diff --git a/src/snek/static/service-worker.js b/src/snek/static/service-worker.js new file mode 100644 index 0000000..dfdc493 --- /dev/null +++ b/src/snek/static/service-worker.js @@ -0,0 +1,30 @@ +self.addEventListener("install", (event) => { + console.log("Service worker installed"); +}); + +self.addEventListener("push", (event) => { + if (!(self.Notification && self.Notification.permission === "granted")) { + return; + } + console.log("Received a push message", event); + + const data = event.data?.json() ?? {}; + const title = data.title || "Something Has Happened"; + const message = + data.message || "Here's something you might want to check out."; + const icon = "images/new-notification.png"; + + event.waitUntil(self.registration.showNotification(title, { + body: message, + tag: "simple-push-demo-notification", + icon, + })); + +}); + +self.addEventListener("notificationclick", (event) => { + console.log("Notification click Received.", event); + event.notification.close(); + event.waitUntil(clients.openWindow( + "https://snek.molodetz.nl",)); +}); diff --git a/src/snek/templates/app.html b/src/snek/templates/app.html index 2ff66eb..fe38358 100644 --- a/src/snek/templates/app.html +++ b/src/snek/templates/app.html @@ -24,6 +24,7 @@