autoconnect_web/
routes.rs

1use actix_web::{web, HttpRequest, HttpResponse};
2use uuid::Uuid;
3
4use autoconnect_settings::AppState;
5use autopush_common::notification::Notification;
6
7use crate::error::ApiError;
8
9/// Handle WebSocket WebPush clients
10pub async fn ws_route(
11    req: HttpRequest,
12    body: web::Payload,
13    app_state: web::Data<AppState>,
14) -> Result<HttpResponse, ApiError> {
15    Ok(autoconnect_ws::ws_handler(req, body, app_state).await?)
16}
17
18/// Deliver a Push notification directly to a connected client
19#[allow(unused_mut)]
20pub async fn push_route(
21    uaid: web::Path<Uuid>,
22    mut notif: web::Json<Notification>,
23    app_state: web::Data<AppState>,
24) -> HttpResponse {
25    trace!(
26        "⏩ in push_route, uaid: {} channel_id: {}",
27        uaid,
28        notif.channel_id,
29    );
30    #[cfg(feature = "reliable_report")]
31    {
32        notif
33            .record_reliability(
34                &app_state.reliability,
35                autopush_common::reliability::ReliabilityState::IntAccepted,
36            )
37            .await;
38        notif
39            .record_reliability(
40                &app_state.reliability,
41                autopush_common::reliability::ReliabilityState::Transmitted,
42            )
43            .await;
44    }
45    // Attempt to send the notification to the UA using WebSocket protocol, or store on failure.
46    // NOTE: Since this clones the notification, there is a potential to
47    // double count the reliability state.
48    let result = app_state
49        .clients
50        .notify(uaid.into_inner(), notif.clone_without_reliability_state())
51        .await;
52    if result.is_ok() {
53        #[cfg(feature = "reliable_report")]
54        notif
55            .record_reliability(
56                &app_state.reliability,
57                autopush_common::reliability::ReliabilityState::Accepted,
58            )
59            .await;
60        HttpResponse::Ok().finish()
61    } else {
62        #[cfg(feature = "reliable_report")]
63        notif
64            .record_reliability(
65                &app_state.reliability,
66                autopush_common::reliability::ReliabilityState::Errored,
67            )
68            .await;
69        HttpResponse::NotFound().body("Client not available")
70    }
71}
72
73/// Notify a connected client to check storage for new notifications
74pub async fn check_storage_route(
75    uaid: web::Path<Uuid>,
76    app_state: web::Data<AppState>,
77) -> HttpResponse {
78    trace!("⏩ check_storage_route, uaid: {}", uaid);
79    let result = app_state.clients.check_storage(uaid.into_inner()).await;
80    if result.is_ok() {
81        HttpResponse::Ok().finish()
82    } else {
83        HttpResponse::NotFound().body("Client not available")
84    }
85}