Handle adding new aliases
This commit is contained in:
parent
1785d25bc6
commit
e983079d8e
2 changed files with 41 additions and 1 deletions
40
src/main.rs
40
src/main.rs
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -140,6 +140,7 @@
|
|||
<form action="/alias/recipient/add"
|
||||
method="post"
|
||||
id="aliasRecptAddForm{{ loop.index }}">
|
||||
<input type="hidden" name="alias" value="{{ alias.mail }}" />
|
||||
<div class="form-floating mb-3">
|
||||
<input type="email"
|
||||
class="form-control"
|
||||
|
|
@ -151,7 +152,6 @@
|
|||
</form>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<input type="hidden" name="alias" value="{{ alias.mail }}" />
|
||||
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
|
||||
<button type="submit"
|
||||
class="btn btn-primary"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue