summaryrefslogtreecommitdiff
path: root/src/push/handlers/register.rs
blob: 201928b69fb9916ab472abbef97aa2f488481a06 (plain)
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
use axum::extract::{Json, State};
use web_push::SubscriptionInfo;

use crate::{app::App, error::Internal, push::Id, token::extract::Identity};

#[derive(serde::Deserialize)]
pub struct Request {
    endpoint: String,
    p256dh: String,
    auth: String,
}

#[derive(serde::Serialize)]
pub struct Response {
    id: Id,
}

pub async fn handler(
    State(app): State<App>,
    identity: Identity,
    Json(request): Json<Request>,
) -> Result<Json<Response>, Internal> {
    let subscription = request.into();

    let id = app.push().register(&identity.user, &subscription).await?;

    Ok(Json(Response { id }))
}

impl From<Request> for SubscriptionInfo {
    fn from(request: Request) -> Self {
        let Request {
            endpoint,
            p256dh,
            auth,
        } = request;
        let info = Self::new(endpoint, p256dh, auth);
        info
    }
}