autoendpoint/routes/
webpush.rs

1use std::str::FromStr;
2
3use crate::error::{ApiErrorKind, ApiResult};
4use crate::extractors::message_id::MessageId;
5use crate::extractors::notification::Notification;
6use crate::extractors::routers::{RouterType, Routers};
7use crate::server::AppState;
8use actix_web::web::Data;
9use actix_web::HttpResponse;
10
11/// Handle the `POST /wpush/{api_version}/{token}` and `POST /wpush/{token}` routes
12/// This is the endpoint for all incoming Push subscription updates.
13pub async fn webpush_route(
14    notification: Notification,
15    routers: Routers,
16    _app_state: Data<AppState>,
17) -> ApiResult<HttpResponse> {
18    sentry::configure_scope(|scope| {
19        scope.set_extra(
20            "uaid",
21            notification.subscription.user.uaid.to_string().into(),
22        );
23    });
24    let router = routers.get(
25        RouterType::from_str(&notification.subscription.user.router_type)
26            .map_err(|_| ApiErrorKind::InvalidRouterType)?,
27    );
28    Ok(router.route_notification(notification).await?.into())
29}
30
31/// Handle the `DELETE /m/{message_id}` route
32pub async fn delete_notification_route(
33    message_id: MessageId,
34    app_state: Data<AppState>,
35) -> ApiResult<HttpResponse> {
36    let sort_key = message_id.sort_key();
37    debug!("Deleting notification with sort-key {}", sort_key);
38    trace!("message_id = {:?}", message_id);
39    app_state
40        .db
41        .remove_message(&message_id.uaid(), &sort_key)
42        .await?;
43
44    Ok(HttpResponse::NoContent().finish())
45}