Handle adding new aliases

This commit is contained in:
traxys 2023-08-29 21:43:23 +02:00
parent 1785d25bc6
commit e983079d8e
2 changed files with 41 additions and 1 deletions

View file

@ -641,6 +641,45 @@ async fn add_mail(
}
}
#[derive(Deserialize, Debug)]
struct AddRecipient {
alias: String,
recipient: String,
}
#[tracing::instrument(skip(state))]
async fn add_recipient(
state: State<Arc<AppState>>,
User(user): User,
Form(add): Form<AddRecipient>,
) -> Result<Redirect, Error> {
let can_use_alias = sqlx::query!(
"SELECT COUNT(*) FROM emails WHERE id = $1 AND mail = $2",
user,
add.alias
)
.fetch_one(&state.db)
.await?
.count
.expect("count should not be null")
> 0;
if !can_use_alias {
tracing::error!("User is not authorized to use this alias");
return Err(Error::InternalError);
}
sqlx::query!(
"INSERT INTO alias_recipient (mail, recipient) VALUES ($1, $2) ON CONFLICT DO NOTHING",
add.alias,
add.recipient
)
.execute(&state.db)
.await?;
Ok(Redirect::to("/"))
}
#[tokio::main]
async fn main() -> color_eyre::Result<()> {
color_eyre::install()?;
@ -673,6 +712,7 @@ async fn main() -> color_eyre::Result<()> {
.route("/", get(home))
.route("/mail/delete", post(delete_mail))
.route("/mail/add", post(add_mail))
.route("/alias/recipient/add", post(add_recipient))
.fallback(page_not_found)
.with_state(Arc::new(AppState {
db,