From e239605b5128bdaf1a9ccf24223b6c698844de5e Mon Sep 17 00:00:00 2001 From: Kit La Touche Date: Wed, 23 Jul 2025 22:08:57 -0400 Subject: 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. --- src/push/handlers/mod.rs | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) (limited to 'src/push') 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, +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, +) -> Result<(), crate::error::Internal> { + push_message(payload.endpoint, payload.keys, &payload.msg).await?; + Ok(()) } -- cgit v1.2.3