diff options
| author | Kit La Touche <kit@transneptune.net> | 2025-07-23 22:08:57 -0400 |
|---|---|---|
| committer | Kit La Touche <kit@transneptune.net> | 2025-07-23 22:08:57 -0400 |
| commit | e239605b5128bdaf1a9ccf24223b6c698844de5e (patch) | |
| tree | e0a5f53d9ae7cf1f6038ac14231a8aa0e90bd6f8 | |
| parent | fb6545e1863ac50cfc9147ec728ca261fd8cac71 (diff) | |
Pull push logic into its own helper function
I don't love that this function has three arguments; eventually, it
should take a user and a message, and look up the endpoint and keys for
that user.
| -rw-r--r-- | src/push/handlers/mod.rs | 30 |
1 files changed, 18 insertions, 12 deletions
diff --git a/src/push/handlers/mod.rs b/src/push/handlers/mod.rs index 89e0743..e4a531b 100644 --- a/src/push/handlers/mod.rs +++ b/src/push/handlers/mod.rs @@ -29,27 +29,33 @@ pub async fn unregister() -> String { String::from("OK") } - -#[axum::debug_handler] -pub async fn echo( - Json(payload): Json<PushPayload>, +async fn push_message( + endpoint: String, + keys: Keys, + message: &String, ) -> Result<(), crate::error::Internal> { - let endpoint = payload.endpoint; - let p256dh = payload.keys.p256dh; - let auth = payload.keys.auth; - let subscription_info = SubscriptionInfo::new(endpoint, p256dh, auth); + let content = message.as_bytes(); - // This will need to come from the DB eventually. + let subscription_info = SubscriptionInfo::new(endpoint, keys.p256dh, keys.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()?; let mut builder = WebPushMessageBuilder::new(&subscription_info); - // Eventually, this message will be something other than an echo: - let content = payload.msg.as_bytes(); builder.set_payload(ContentEncoding::Aes128Gcm, content); builder.set_vapid_signature(sig_builder); let client = IsahcWebPushClient::new()?; client.send(builder.build()?).await?; - // Fix up return type? + + Ok(()) +} + + +#[axum::debug_handler] +pub async fn echo( + Json(payload): Json<PushPayload>, +) -> Result<(), crate::error::Internal> { + push_message(payload.endpoint, payload.keys, &payload.msg).await?; + Ok(()) } |
