summaryrefslogtreecommitdiff
path: root/src/push/publisher.rs
diff options
context:
space:
mode:
authorOwen Jacobson <owen@grimoire.ca>2025-12-09 15:13:21 -0500
committerOwen Jacobson <owen@grimoire.ca>2025-12-17 15:48:20 -0500
commit3c697f5fb1b8dbad46eac8fa299ed7cebfb36159 (patch)
treeb7854fb23d1e104f928acfe3bba75ea3b74b83d9 /src/push/publisher.rs
parent41a5a0f7e13bf5a82aaef59e34eb68f0fe7fa7f5 (diff)
Factor push message publication out to its own helper component.
The `Publisher` component handles the details of web push delivery. Callers must provide the subscription set, the current signer, and the message, while the publisher handles encoding and communication with web push endpoints. To facilitate testing, `Publisher` implements `Publish`, which is a new trait with the same interface. Components that might publish web push messages should rely on the trait where possible. The test suite now constructs an app with a dummy `Publish` impl, which captures push messages for examination. Note that the testing implementation of `Publish` is hand-crafted, and presently only acts to record the arguments it receives. The other alternative was to use a mocking library, such as `mockit`, and while I've used that approach before, I'm not super comfortable with the complexity in this situation. I think we can maintain a more reasonable testing `Publish` impl by hand, at least for now, and we can revisit that decision later if need be. Tests for the `ping` endpoint have been migrated to this endpoint.
Diffstat (limited to 'src/push/publisher.rs')
-rw-r--r--src/push/publisher.rs83
1 files changed, 83 insertions, 0 deletions
diff --git a/src/push/publisher.rs b/src/push/publisher.rs
new file mode 100644
index 0000000..4092724
--- /dev/null
+++ b/src/push/publisher.rs
@@ -0,0 +1,83 @@
+use futures::future::join_all;
+use itertools::Itertools as _;
+use serde::Serialize;
+use web_push::{
+ ContentEncoding, IsahcWebPushClient, PartialVapidSignatureBuilder, SubscriptionInfo,
+ WebPushClient, WebPushError, WebPushMessage, WebPushMessageBuilder,
+};
+
+use crate::error::failed::{Failed, ResultExt as _};
+
+pub trait Publish {
+ fn publish<M>(
+ &self,
+ message: M,
+ signer: PartialVapidSignatureBuilder,
+ subscriptions: impl IntoIterator<Item = SubscriptionInfo> + Send,
+ ) -> impl Future<Output = Result<Vec<(SubscriptionInfo, WebPushError)>, Failed>> + Send
+ where
+ M: Serialize + Send + 'static;
+}
+
+#[derive(Clone)]
+pub struct Publisher {
+ client: IsahcWebPushClient,
+}
+
+impl Publisher {
+ pub fn new() -> Result<Self, WebPushError> {
+ let client = IsahcWebPushClient::new()?;
+ Ok(Self { client })
+ }
+
+ fn prepare_message(
+ payload: &[u8],
+ signer: &PartialVapidSignatureBuilder,
+ subscription: &SubscriptionInfo,
+ ) -> Result<WebPushMessage, Failed> {
+ let signature = signer
+ .clone()
+ .add_sub_info(subscription)
+ .build()
+ .fail("Failed to build VAPID signature")?;
+
+ let mut message = WebPushMessageBuilder::new(subscription);
+ message.set_payload(ContentEncoding::Aes128Gcm, payload);
+ message.set_vapid_signature(signature);
+ let message = message.build().fail("Failed to build push message")?;
+
+ Ok(message)
+ }
+}
+
+impl Publish for Publisher {
+ async fn publish<M>(
+ &self,
+ message: M,
+ signer: PartialVapidSignatureBuilder,
+ subscriptions: impl IntoIterator<Item = SubscriptionInfo> + Send,
+ ) -> Result<Vec<(SubscriptionInfo, WebPushError)>, Failed>
+ where
+ M: Serialize + Send + 'static,
+ {
+ let payload = serde_json::to_vec_pretty(&message)
+ .fail("Failed to encode web push message to JSON")?;
+
+ let messages: Vec<_> = subscriptions
+ .into_iter()
+ .map(|sub| Self::prepare_message(&payload, &signer, &sub).map(|message| (sub, message)))
+ .try_collect()?;
+
+ let deliveries = messages
+ .into_iter()
+ .map(async |(sub, message)| (sub, self.client.send(message).await));
+
+ let failures = join_all(deliveries)
+ .await
+ .into_iter()
+ .filter_map(|(sub, result)| result.err().map(|err| (sub, err)))
+ .collect();
+
+ Ok(failures)
+ }
+}