authbeam/api/
notifications.rsuse crate::database::Database;
use crate::model::DatabaseError;
use databeam::DefaultReturn;
use axum::response::IntoResponse;
use axum::{
extract::{Path, State},
Json,
};
use axum_extra::extract::cookie::CookieJar;
pub async fn delete_request(
jar: CookieJar,
Path(id): Path<String>,
State(database): State<Database>,
) -> impl IntoResponse {
let auth_user = match jar.get("__Secure-Token") {
Some(c) => match database
.get_profile_by_unhashed(c.value_trimmed().to_string())
.await
{
Ok(ua) => ua,
Err(e) => {
return Json(DefaultReturn {
success: false,
message: e.to_string(),
payload: (),
});
}
},
None => {
return Json(DefaultReturn {
success: false,
message: DatabaseError::NotAllowed.to_string(),
payload: (),
});
}
};
if let Err(e) = database.delete_notification(id, auth_user).await {
return Json(DefaultReturn {
success: false,
message: e.to_string(),
payload: (),
});
}
Json(DefaultReturn {
success: true,
message: "Notification deleted".to_string(),
payload: (),
})
}
pub async fn delete_all_request(
jar: CookieJar,
State(database): State<Database>,
) -> impl IntoResponse {
let auth_user = match jar.get("__Secure-Token") {
Some(c) => match database
.get_profile_by_unhashed(c.value_trimmed().to_string())
.await
{
Ok(ua) => ua,
Err(e) => {
return Json(DefaultReturn {
success: false,
message: e.to_string(),
payload: (),
});
}
},
None => {
return Json(DefaultReturn {
success: false,
message: DatabaseError::NotAllowed.to_string(),
payload: (),
});
}
};
if let Err(e) = database
.delete_notifications_by_recipient(auth_user.id.clone(), auth_user)
.await
{
return Json(DefaultReturn {
success: false,
message: e.to_string(),
payload: (),
});
}
Json(DefaultReturn {
success: true,
message: "Notifications cleared!".to_string(),
payload: (),
})
}