authbeam/api/
notifications.rs

1use crate::database::Database;
2use crate::model::DatabaseError;
3use databeam::prelude::DefaultReturn;
4
5use axum::response::IntoResponse;
6use axum::{
7    extract::{Path, State},
8    Json,
9};
10use axum_extra::extract::cookie::CookieJar;
11
12/// Delete a notification
13pub async fn delete_request(
14    jar: CookieJar,
15    Path(id): Path<String>,
16    State(database): State<Database>,
17) -> impl IntoResponse {
18    // get user from token
19    let auth_user = match jar.get("__Secure-Token") {
20        Some(c) => match database.get_profile_by_unhashed(c.value_trimmed()).await {
21            Ok(ua) => ua,
22            Err(e) => return Json(e.to_json()),
23        },
24        None => return Json(DatabaseError::NotAllowed.to_json()),
25    };
26
27    // return
28    if let Err(e) = database.delete_notification(&id, auth_user).await {
29        return Json(e.to_json());
30    }
31
32    Json(DefaultReturn {
33        success: true,
34        message: "Notification deleted".to_string(),
35        payload: (),
36    })
37}
38
39/// Delete the current user's notifications
40pub async fn delete_all_request(
41    jar: CookieJar,
42    State(database): State<Database>,
43) -> impl IntoResponse {
44    // get user from token
45    let auth_user = match jar.get("__Secure-Token") {
46        Some(c) => match database.get_profile_by_unhashed(c.value_trimmed()).await {
47            Ok(ua) => ua,
48            Err(e) => return Json(e.to_json()),
49        },
50        None => return Json(DatabaseError::NotAllowed.to_json()),
51    };
52
53    // return
54    if let Err(e) = database
55        .delete_notifications_by_recipient(&auth_user.id.clone(), auth_user)
56        .await
57    {
58        return Json(e.to_json());
59    }
60
61    Json(DefaultReturn {
62        success: true,
63        message: "Notifications cleared!".to_string(),
64        payload: (),
65    })
66}