autoconnect_web/
routes.rs

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
use actix_web::{web, HttpRequest, HttpResponse};
use uuid::Uuid;

use autoconnect_settings::AppState;
use autopush_common::notification::Notification;

use crate::error::ApiError;

/// Handle WebSocket WebPush clients
pub async fn ws_route(
    req: HttpRequest,
    body: web::Payload,
    app_state: web::Data<AppState>,
) -> Result<HttpResponse, ApiError> {
    Ok(autoconnect_ws::ws_handler(req, body, app_state).await?)
}

/// Deliver a Push notification directly to a connected client
#[allow(unused_mut)]
pub async fn push_route(
    uaid: web::Path<Uuid>,
    mut notif: web::Json<Notification>,
    app_state: web::Data<AppState>,
) -> HttpResponse {
    trace!(
        "⏩ in push_route, uaid: {} channel_id: {}",
        uaid,
        notif.channel_id,
    );
    #[cfg(feature = "reliable_report")]
    {
        notif
            .record_reliability(
                &app_state.reliability,
                autopush_common::reliability::ReliabilityState::IntAccepted,
            )
            .await;
        notif
            .record_reliability(
                &app_state.reliability,
                autopush_common::reliability::ReliabilityState::Transmitted,
            )
            .await;
    }
    // Attempt to send the notification to the UA using WebSocket protocol, or store on failure.
    let result = app_state
        .clients
        .notify(uaid.into_inner(), notif.clone())
        .await;
    if result.is_ok() {
        #[cfg(feature = "reliable_report")]
        notif
            .record_reliability(
                &app_state.reliability,
                autopush_common::reliability::ReliabilityState::Accepted,
            )
            .await;
        HttpResponse::Ok().finish()
    } else {
        HttpResponse::NotFound().body("Client not available")
    }
}

/// Notify a connected client to check storage for new notifications
pub async fn check_storage_route(
    uaid: web::Path<Uuid>,
    app_state: web::Data<AppState>,
) -> HttpResponse {
    trace!("⏩ check_storage_route, uaid: {}", uaid);
    let result = app_state.clients.check_storage(uaid.into_inner()).await;
    if result.is_ok() {
        HttpResponse::Ok().finish()
    } else {
        HttpResponse::NotFound().body("Client not available")
    }
}