Skip to content

Commit

Permalink
add: mail api
Browse files Browse the repository at this point in the history
TODO: mail frontend stuff
  • Loading branch information
trisuaso committed Nov 22, 2024
1 parent 1979746 commit 73a7604
Show file tree
Hide file tree
Showing 4 changed files with 475 additions and 2 deletions.
105 changes: 105 additions & 0 deletions crates/authbeam/src/api/mail.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
use crate::database::Database;
use crate::model::{DatabaseError, MailCreate};
use databeam::DefaultReturn;

use axum::response::IntoResponse;
use axum::{
extract::{Path, State},
Json,
};
use axum_extra::extract::cookie::CookieJar;

/// Create a mail
pub async fn create_request(
jar: CookieJar,
State(database): State<Database>,
Json(props): Json<MailCreate>,
) -> impl IntoResponse {
// get user from token
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,
});
}
},
None => {
return Json(DefaultReturn {
success: false,
message: DatabaseError::NotAllowed.to_string(),
payload: None,
});
}
};

// return
let mail = match database.create_mail(props, auth_user.id.clone()).await {
Ok(m) => m,
Err(e) => {
return Json(DefaultReturn {
success: false,
message: e.to_string(),
payload: None,
})
}
};

Json(DefaultReturn {
success: true,
message: "Mail exists".to_string(),
payload: Some(mail),
})
}

/// Delete a mail
pub async fn delete_request(
jar: CookieJar,
Path(id): Path<String>,
State(database): State<Database>,
) -> impl IntoResponse {
// get user from token
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: (),
});
}
};

// return
if let Err(e) = database.delete_mail(id, auth_user).await {
return Json(DefaultReturn {
success: false,
message: e.to_string(),
payload: (),
});
}

Json(DefaultReturn {
success: true,
message: "Mail deleted".to_string(),
payload: (),
})
}
3 changes: 3 additions & 0 deletions crates/authbeam/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use axum::{
pub mod general;
pub mod ipbans;
pub mod ipblocks;
pub mod mail;
pub mod me;
pub mod notifications;
pub mod profile;
Expand Down Expand Up @@ -70,6 +71,8 @@ pub fn routes(database: Database) -> Router {
.route("/profile/:id/avatar", get(profile::avatar_request))
.route("/profile/:id", delete(profile::delete_request))
.route("/profile/:id", get(profile::get_request))
// mail
.route("/mail/:id", delete(notifications::delete_request))
// notifications
.route("/notifications/:id", delete(notifications::delete_request))
.route(
Expand Down
Loading

0 comments on commit 73a7604

Please sign in to comment.