diff options
| author | Kit La Touche <kit@transneptune.net> | 2025-07-23 21:54:33 -0400 |
|---|---|---|
| committer | Kit La Touche <kit@transneptune.net> | 2025-07-23 21:54:33 -0400 |
| commit | fb6545e1863ac50cfc9147ec728ca261fd8cac71 (patch) | |
| tree | fb6713fa3682fdb18fe338af3ba64b90b7e333e1 | |
| parent | 411ba4920358aef3f221910150d926a99e34a5ae (diff) | |
Clean up proof of concept a bit
| -rw-r--r-- | src/push/handlers/mod.rs | 26 | ||||
| -rw-r--r-- | ui/routes/+layout.svelte | 26 |
2 files changed, 21 insertions, 31 deletions
diff --git a/src/push/handlers/mod.rs b/src/push/handlers/mod.rs index afa76e0..89e0743 100644 --- a/src/push/handlers/mod.rs +++ b/src/push/handlers/mod.rs @@ -32,35 +32,22 @@ pub async fn unregister() -> String { #[axum::debug_handler] pub async fn echo( - Json(echo): Json<Echo>, + Json(payload): Json<PushPayload>, ) -> Result<(), crate::error::Internal> { - // Look this up from a subscription record? Or get it from the client and trust? - let endpoint = echo.endpoint; - // Get these from client: - let p256dh = echo.keys.p256dh; - let auth = echo.keys.auth; - - println!("endpoint: {:?}", endpoint); - println!("p256dh: {:?}", p256dh); - println!("auth: {:?}", auth); - - // You would likely get this by deserializing a browser `pushSubscription` object. + let endpoint = payload.endpoint; + let p256dh = payload.keys.p256dh; + let auth = payload.keys.auth; let subscription_info = SubscriptionInfo::new(endpoint, p256dh, auth); // This will need to come from the DB eventually. let private_key = String::from(env::var("VAPID_PRIVATE_KEY").unwrap_or_default()); let sig_builder = VapidSignatureBuilder::from_base64(&private_key, &subscription_info)?.build()?; - - // Now add payload and encrypt. let mut builder = WebPushMessageBuilder::new(&subscription_info); // Eventually, this message will be something other than an echo: - let content = echo.msg.as_bytes(); + let content = payload.msg.as_bytes(); builder.set_payload(ContentEncoding::Aes128Gcm, content); builder.set_vapid_signature(sig_builder); - let client = IsahcWebPushClient::new()?; - - // Finally, send the notification! client.send(builder.build()?).await?; // Fix up return type? Ok(()) @@ -74,9 +61,8 @@ pub struct Keys { } #[derive(serde::Deserialize)] -pub struct Echo { +pub struct PushPayload { pub msg: String, pub endpoint: String, - pub expirationTime: Option<String>, pub keys: Keys, } diff --git a/ui/routes/+layout.svelte b/ui/routes/+layout.svelte index a1f8d84..9089c7e 100644 --- a/ui/routes/+layout.svelte +++ b/ui/routes/+layout.svelte @@ -11,15 +11,10 @@ function doSubscribe() { navigator.serviceWorker.ready .then(async(registration) => { - console.debug("Beginning subscription"); const response = await fetch("/api/vapid"); // and if we fail to get it? const vapidPublicKey = await response.text(); - // Chrome doesn't accept the base64-encoded (string) vapidPublicKey yet - // urlBase64ToUint8Array() is defined in /tools.js - // const convertedVapidKey = urlBase64ToUint8Array(vapidPublicKey); const convertedVapidKey = vapidPublicKey; - // Subscribe the user return registration.pushManager.subscribe({ userVisibleOnly: true, applicationServerKey: convertedVapidKey @@ -41,7 +36,7 @@ }).then((subscription) => { return subscription.unsubscribe() .then(function() { - console.log("Unsubscribed", subscription.endpoint); + subscriptionJson = null; return fetch("/api/unregister", { method: "post", headers: { "Content-type": "application/json" }, @@ -51,6 +46,14 @@ }); } + function toggleSub() { + if (subscriptionJson !== null) { + doUnsubscribe(); + } else { + doSubscribe(); + } + } + function notifyMe() { fetch("/api/echo", { method: "post", @@ -67,9 +70,7 @@ return registration.pushManager.getSubscription(); }).then((subscription) => { if (subscription) { - console.debug("Already subscribed", subscription.endpoint); - } else { - doSubscribe(); + subscriptionJson = JSON.parse(JSON.stringify(subscription)); } }); }); @@ -103,9 +104,12 @@ <button onclick="{notifyMe}"> notify </button> - <button onclick="{doUnsubscribe}"> - unsub + <button onclick="{toggleSub}"> + toggle sub </button> + <div> + hasSub: {subscriptionJson !== null} + </div> <div class="trail"> {#if session} <div> |
