Skip to main content

autoconnect_ws_sm/identified/
on_client_msg.rs

1use std::collections::HashMap;
2
3use uuid::Uuid;
4
5use autoconnect_common::{
6    broadcast::Broadcast,
7    protocol::{BroadcastValue, ClientAck, ClientMessage, MessageType, ServerMessage},
8};
9use autopush_common::{
10    delivery_metrics::{DeliveryPath, DeliveryRoute, Outcome, emit_outcome},
11    endpoint::make_endpoint,
12    metric_name::MetricName,
13    metrics::StatsdClientExt,
14    notification::Notification,
15    util::{ms_since_epoch, sec_since_epoch},
16};
17
18use super::WebPushClient;
19use crate::error::{SMError, SMErrorKind};
20
21impl WebPushClient {
22    /// Handle a WebPush `ClientMessage` sent from the user agent over the
23    /// WebSocket for this user
24    pub async fn on_client_msg(
25        &mut self,
26        msg: ClientMessage,
27    ) -> Result<Vec<ServerMessage>, SMError> {
28        match msg {
29            ClientMessage::Hello { .. } => {
30                Err(SMError::invalid_message("Already Hello'd".to_owned()))
31            }
32            ClientMessage::Register { channel_id, key } => {
33                Ok(vec![self.register(channel_id, key).await?])
34            }
35            ClientMessage::Unregister { channel_id, code } => {
36                Ok(vec![self.unregister(channel_id, code).await?])
37            }
38            ClientMessage::BroadcastSubscribe { broadcasts } => Ok(self
39                .broadcast_subscribe(broadcasts)
40                .await?
41                .map_or_else(Vec::new, |smsg| vec![smsg])),
42            ClientMessage::Ack { updates } => self.ack(&updates).await,
43            ClientMessage::Nack { code, .. } => {
44                self.nack(code);
45                Ok(vec![])
46            }
47            ClientMessage::Ping => Ok(vec![self.ping()?]),
48        }
49    }
50
51    /// Register a new Push subscription
52    async fn register(
53        &mut self,
54        channel_id_str: String,
55        key: Option<String>,
56    ) -> Result<ServerMessage, SMError> {
57        let uaid_str = self.uaid.to_string();
58        trace!("WebPushClient:register";
59               "uaid" => &uaid_str,
60               "channel_id" => &channel_id_str,
61               "key" => &key,
62               "message_type" => MessageType::Register.as_ref(),
63        );
64        let channel_id = Uuid::try_parse(&channel_id_str).map_err(|_| {
65            SMError::invalid_message(format!("Invalid channelID: {channel_id_str}"))
66        })?;
67        if channel_id.as_hyphenated().to_string() != channel_id_str {
68            return Err(SMError::invalid_message(format!(
69                "Invalid UUID format, not lower-case/dashed: {channel_id}",
70            )));
71        }
72
73        let (status, push_endpoint) = match self.do_register(&channel_id, key).await {
74            Ok(endpoint) => {
75                let _ = self.app_state.metrics.incr(MetricName::UaCommandRegister);
76                self.stats.registers += 1;
77                (200, endpoint)
78            }
79            Err(SMErrorKind::MakeEndpoint(msg)) => {
80                error!("WebPushClient::register make_endpoint failed: {}", msg);
81                (400, "Failed to generate endpoint".to_owned())
82            }
83            Err(e) => {
84                error!("WebPushClient::register failed: {}", e);
85                (500, "".to_owned())
86            }
87        };
88        Ok(ServerMessage::Register {
89            channel_id,
90            status,
91            push_endpoint,
92        })
93    }
94
95    async fn do_register(
96        &mut self,
97        channel_id: &Uuid,
98        key: Option<String>,
99    ) -> Result<String, SMErrorKind> {
100        if let Some(user) = &self.deferred_add_user {
101            debug!(
102                "💬WebPushClient::register: User not yet registered: {}",
103                &user.uaid
104            );
105            self.app_state.db.add_user(user).await?;
106            self.deferred_add_user = None;
107        }
108
109        let endpoint = make_endpoint(
110            &self.uaid,
111            channel_id,
112            key.as_deref(),
113            &self.app_state.endpoint_url,
114            &self.app_state.fernet,
115        )
116        .map_err(SMErrorKind::MakeEndpoint)?;
117        self.app_state
118            .db
119            .add_channel(&self.uaid, channel_id)
120            .await?;
121        Ok(endpoint)
122    }
123
124    /// Unregister an existing Push subscription
125    async fn unregister(
126        &mut self,
127        channel_id: Uuid,
128        code: Option<u32>,
129    ) -> Result<ServerMessage, SMError> {
130        let uaid_str = self.uaid.to_string();
131        let chid_str = channel_id.to_string();
132        trace!("WebPushClient:unregister";
133               "uaid" => &uaid_str,
134               "channel_id" => &chid_str,
135               "code" => &code,
136               "message_type" => MessageType::Unregister.as_ref(),
137        );
138        // TODO: (copied from previous state machine) unregister should check
139        // the format of channel_id like register does
140
141        let result = self
142            .app_state
143            .db
144            .remove_channel(&self.uaid, &channel_id)
145            .await;
146        let status = match result {
147            Ok(_) => {
148                self.app_state
149                    .metrics
150                    .incr_with_tags(MetricName::UaCommandUnregister)
151                    .with_tag("code", &code.unwrap_or(200).to_string())
152                    .send();
153                self.stats.unregisters += 1;
154                200
155            }
156            Err(e) => {
157                error!("WebPushClient::unregister failed: {}", e);
158                500
159            }
160        };
161        Ok(ServerMessage::Unregister { channel_id, status })
162    }
163
164    /// Subscribe to a new set of Broadcasts
165    async fn broadcast_subscribe(
166        &mut self,
167        broadcasts: HashMap<String, String>,
168    ) -> Result<Option<ServerMessage>, SMError> {
169        trace!("WebPushClient:broadcast_subscribe"; "message_type" => MessageType::BroadcastSubscribe.as_ref());
170        let broadcasts = Broadcast::from_hashmap(broadcasts);
171        let mut response: HashMap<String, BroadcastValue> = HashMap::new();
172
173        let bc = self.app_state.broadcaster.read().await;
174        if let Some(delta) = bc.subscribe_to_broadcasts(&mut self.broadcast_subs, &broadcasts) {
175            response.extend(Broadcast::vec_into_hashmap(delta));
176        };
177        let missing = bc.missing_broadcasts(&broadcasts);
178        if !missing.is_empty() {
179            response.insert(
180                "errors".to_owned(),
181                BroadcastValue::Nested(Broadcast::vec_into_hashmap(missing)),
182            );
183        }
184
185        Ok((!response.is_empty()).then_some(ServerMessage::Broadcast {
186            broadcasts: response,
187        }))
188    }
189
190    /// Acknowledge receipt of one or more Push Notifications
191    async fn ack(&mut self, updates: &[ClientAck]) -> Result<Vec<ServerMessage>, SMError> {
192        trace!("✅ WebPushClient:ack"; "message_type" => MessageType::Ack.as_ref());
193        let _ = self.app_state.metrics.incr(MetricName::UaCommandAck);
194        let ack_ms = ms_since_epoch();
195
196        for notif in updates {
197            let key = notif.version.clone();
198            // Check the map of unacked "direct" (unstored) notifications.
199            if let Some(acked_notification) = self.ack_state.unacked_direct_notifs.remove(&key) {
200                debug!("✅ Ack (Direct)";
201                       "channel_id" => notif.channel_id.as_hyphenated().to_string(),
202                       "version" => &notif.version
203                );
204                self.record_delivered(&acked_notification, DeliveryRoute::Direct, ack_ms);
205                self.stats.direct_acked += 1;
206                continue;
207            };
208            // TTL=0 notifications are only tracked to record their delivery
209            if let Some(acked_notification) = self.ack_state.unacked_ttl0_notifs.remove(&key) {
210                debug!("✅ Ack (Direct, TTL=0)";
211                       "channel_id" => notif.channel_id.as_hyphenated().to_string(),
212                       "version" => &notif.version
213                );
214                self.record_delivered(&acked_notification, DeliveryRoute::Direct, ack_ms);
215                self.stats.direct_acked += 1;
216                continue;
217            };
218
219            // Now, check the map of stored notifications
220            #[allow(unused_mut)]
221            if let Some(mut acked_notification) = self.ack_state.unacked_stored_notifs.remove(&key)
222            {
223                debug!(
224                    "✅ Ack (Stored)";
225                       "channel_id" => notif.channel_id.as_hyphenated().to_string(),
226                       "version" => &notif.version,
227                       "message_type" => MessageType::Ack.as_ref()
228                );
229                // Some storage engines may set this to "".
230                let is_topic = acked_notification
231                    .topic
232                    .as_ref()
233                    .map(|t| !t.is_empty())
234                    .unwrap_or(false);
235                debug!("✅ Ack notif: {:?}", &acked_notification);
236                // Only force delete Topic messages, since they don't have a timestamp.
237                // Other messages persist in the database, to be, eventually, cleaned up by their
238                // TTL. We will need to update the `CurrentTimestamp` field for the channel
239                // record. Use that field to set the baseline timestamp for when to pull messages
240                // in the future.
241                if is_topic {
242                    let chid_message_id = &acked_notification.chidmessageid();
243                    debug!(
244                        "✅🗑 WebPushClient:ack removing Stored, sort_key: {}, version: {}",
245                        &chid_message_id, &acked_notification.version
246                    );
247                    self.app_state
248                        .db
249                        .remove_message(&self.uaid, chid_message_id)
250                        .await?;
251                    // Recorded once removed: until then it may be read again
252                    self.record_delivered(&acked_notification, DeliveryRoute::Stored, ack_ms);
253                    // NOTE: timestamp messages may still be in state of flux: they're not fully
254                    // ack'd (removed/unable to be resurrected) until increment_storage is called,
255                    // so their delivery (and reliability) is recorded there
256                    #[cfg(feature = "reliable_report")]
257                    acked_notification
258                        .record_reliability(&self.app_state.reliability, notif.reliability_state())
259                        .await;
260                } else {
261                    self.ack_state
262                        .acked_stored_timestamp_notifs
263                        .push(super::AckedNotification {
264                            notif: acked_notification,
265                            ack_ms,
266                        });
267                }
268                self.stats.stored_acked += 1;
269                continue;
270            };
271        }
272
273        if self.ack_state.unacked_notifs() {
274            // Wait for the Client to Ack all notifications before further
275            // processing
276            Ok(vec![])
277        } else {
278            self.post_process_all_acked().await
279        }
280    }
281
282    /// Record the delivery latency and outcome of an ACK'd notification.
283    ///
284    /// Any ACK code counts as delivered: 101 (decryption error) and 102 (not
285    /// delivered) describe the client's processing after it received the message.
286    pub(super) fn record_delivered(&self, notif: &Notification, route: DeliveryRoute, ack_ms: u64) {
287        // `timestamp` is persisted (in seconds) for stored messages.
288        let stored_ms = notif.timestamp * 1000;
289        let received_ms = match route {
290            DeliveryRoute::Direct => notif.received_ms.unwrap_or(stored_ms),
291            DeliveryRoute::Stored => stored_ms,
292        };
293        let delivery_metrics = &self.app_state.delivery_metrics;
294        delivery_metrics.observe_delivery(DeliveryPath::Ws, route, received_ms, ack_ms);
295        if route == DeliveryRoute::Stored {
296            delivery_metrics.observe_stored_delivery(self.connected_at, received_ms, ack_ms);
297        }
298        emit_outcome(
299            &self.app_state.metrics,
300            DeliveryPath::Ws,
301            Outcome::Delivered,
302        );
303    }
304
305    /// Negative Acknowledgement (a Client error occurred) of one or more Push
306    /// Notifications
307    fn nack(&mut self, code: Option<i32>) {
308        trace!("WebPushClient:nack"; "message_type" => MessageType::Nack.as_ref());
309        // only metric codes expected from the client (or 0)
310        let code = code
311            .and_then(|code| (301..=303).contains(&code).then_some(code))
312            .unwrap_or(0);
313        self.app_state
314            .metrics
315            .incr_with_tags(MetricName::UaCommandNack)
316            .with_tag("code", &code.to_string())
317            .send();
318        self.stats.nacks += 1;
319    }
320
321    /// Handle a WebPush Ping
322    ///
323    /// Note this is the WebPush Protocol level's Ping: this differs from the
324    /// lower level WebSocket Ping frame (handled by the `webpush_ws` handler).
325    fn ping(&mut self) -> Result<ServerMessage, SMError> {
326        trace!("WebPushClient:ping"; "message_type" => MessageType::Ping.as_ref());
327        // TODO: why is this 45 vs the comment describing a minute? and 45
328        // should be a setting
329        // Clients shouldn't ping > than once per minute or we disconnect them
330        if sec_since_epoch() - self.last_ping >= 45 {
331            trace!("🏓WebPushClient Got a WebPush Ping, sending WebPush Pong");
332            self.last_ping = sec_since_epoch();
333            Ok(ServerMessage::Ping)
334        } else {
335            Err(SMErrorKind::ExcessivePing.into())
336        }
337    }
338
339    /// Post process the Client succesfully Ack'ing all Push Notifications it's
340    /// been sent.
341    ///
342    /// Notifications are read in small batches (approximately 10). We wait for
343    /// the Client to Ack every Notification in that batch (invoking this
344    /// method) before proceeding to read the next batch (or potential other
345    /// actions such as `reset_uaid`).
346    async fn post_process_all_acked(&mut self) -> Result<Vec<ServerMessage>, SMError> {
347        trace!("▶️ WebPushClient:post_process_all_acked"; "message_type" => MessageType::Notification.as_ref());
348        let flags = &self.flags;
349        if flags.check_storage {
350            if flags.increment_storage {
351                debug!(
352                    "▶️ WebPushClient:post_process_all_acked check_storage && increment_storage";
353                    "message_type" => MessageType::Notification.as_ref()
354                );
355                self.increment_storage().await?;
356            }
357
358            debug!("▶️ WebPushClient:post_process_all_acked check_storage"; "message_type" => MessageType::Notification.as_ref());
359            let smsgs = self.check_storage_loop().await?;
360            if !smsgs.is_empty() {
361                debug_assert!(self.flags.check_storage);
362                // More outgoing notifications: send them out and go back to
363                // waiting for the Client to Ack them all before further
364                // processing
365                return Ok(smsgs);
366            }
367            // Otherwise check_storage is finished
368            debug_assert!(!self.flags.check_storage);
369            debug_assert!(!self.flags.increment_storage);
370        }
371
372        // All Ack'd and finished checking/incrementing storage
373        debug_assert!(!self.ack_state.unacked_notifs());
374        let flags = &self.flags;
375        if flags.old_record_version {
376            debug!("▶️ WebPushClient:post_process_all_acked; resetting uaid"; "message_type" => MessageType::Notification.as_ref());
377            self.app_state
378                .metrics
379                .incr_with_tags(MetricName::UaExpiration)
380                .with_tag("reason", "old_record_version")
381                .send();
382            self.app_state.db.remove_user(&self.uaid).await?;
383            Err(SMErrorKind::UaidReset.into())
384        } else {
385            Ok(vec![])
386        }
387    }
388}