1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
//! Web Push: deliver a notification to a subscribed browser **even when the
//! app's tab is closed** (issue #1392).
//!
//! Autumn already generates an installable PWA (`autumn generate pwa`) and
//! stores an in-app notification feed
//! ([`notifications`](crate::notifications)). This module closes the loop
//! between them: the browser subscribes, the app calls
//! [`WebPush::send`], and the service worker raises a notification on the
//! user's device. The developer writes **zero** lines of crypto — VAPID
//! signing (RFC 8292) and payload encryption (RFC 8291) both happen here.
//!
//! # Quick start
//!
//! 1. Mint a key pair once, offline, and record the private half:
//!
//! ```rust,ignore
//! let key = autumn_web::push::VapidKey::generate();
//! println!("private_key = \"{}\"", key.private_key_base64url());
//! ```
//!
//! 2. Configure it (see [`PushConfig`]):
//!
//! ```toml
//! [push]
//! private_key = "…" # from step 1; keep it secret
//! subject = "mailto:ops@example.com"
//! ```
//!
//! 3. Mount the built-in routes — `autumn generate pwa` does this for you:
//!
//! ```rust,ignore
//! autumn_web::app()
//! .merge(autumn_web::push::router())
//! .run()
//! .await;
//! ```
//!
//! 4. Send:
//!
//! ```rust,ignore
//! use autumn_web::prelude::*;
//!
//! #[post("/builds/{id}/fail")]
//! async fn build_failed(push: WebPush, id: Path<i64>) -> AutumnResult<&'static str> {
//! push.send(
//! owner_id,
//! &PushMessage::new("Build failed", "main is red")
//! .url(format!("/builds/{}", *id)),
//! )
//! .await?;
//! Ok("ok")
//! }
//! ```
//!
//! # Composing with the in-app notification feed (#1148)
//!
//! Push is a *delivery leg*, not a replacement for the feed: the feed is the
//! durable record the user can come back to, and the push is the nudge that
//! brings them back. Write the notification first, then push best-effort —
//! mirroring how [`Notifications::notify_with_push`](crate::notifications::Notifications::notify_with_push) treats its `channels`
//! broadcast:
//!
//! ```rust,ignore
//! use autumn_web::prelude::*;
//! use autumn_web::push::PushMessage;
//!
//! #[post("/posts/{id}/comments")]
//! async fn comment(
//! notifications: Notifications,
//! push: WebPush,
//! id: Path<i64>,
//! ) -> AutumnResult<&'static str> {
//! // 1. The durable record. A failure here IS a failure of the request.
//! notifications
//! .notify(author_id, "comment.created", serde_json::json!({ "post": *id }))
//! .await?;
//!
//! // 2. The nudge. Best-effort: a push service outage, a revoked
//! // permission, or an app with no `[push]` key configured must never
//! // fail the comment that was already written. `PushPrincipal` accepts
//! // the same `i64` recipient the feed uses, so there is no conversion.
//! if let Err(e) = push
//! .send(
//! author_id,
//! &PushMessage::new("New comment", "Someone replied to your post")
//! .url(format!("/posts/{}", *id)),
//! )
//! .await
//! {
//! tracing::warn!(error = %e, "web push failed; the in-app notification still stands");
//! }
//!
//! Ok("ok")
//! }
//! ```
//!
//! The ordering is the point: the feed write is awaited and propagated, the
//! push is awaited and *logged*. A user whose browser has never subscribed
//! gets an empty [`PushDeliveryReport`] rather than an error, so the branch
//! above only fires on a genuine problem.
//!
//! # What is stored, and where
//!
//! A browser subscription is an endpoint URL plus two keys, bound to a
//! principal — see [`PushSubscriptionStore`]. The default backend is the
//! `push_subscriptions` table scaffolded by `autumn generate pwa`, falling
//! back to an in-memory store when no database is configured.
//!
//! # Failure posture
//!
//! - **Never a silent no-op.** Sending with no key configured is
//! [`PushError::NotConfigured`], raised before anything is dispatched, and a
//! key that is present but unusable fails the boot outright.
//! - **A dead device never blocks a live one.** Per-device outcomes are
//! reported through [`PushDeliveryReport`], not raised.
//! - **Stale subscriptions are pruned, transient failures are not.** A
//! `404`/`410` removes the subscription; a `5xx` or a rate limit leaves it
//! in place to retry, because pruning on those would silently unsubscribe
//! every user during an incident.
pub
pub use PushConfig;
pub use ;
pub use ;
pub use DbPushSubscriptionStore;
pub use ;
pub use ;
pub use VapidKey;
/// Errors produced by the Web Push subsystem.