pass_manager/src/commands/delete_all.rs
2024-05-13 13:58:56 +03:00

46 lines
1.2 KiB
Rust

use crate::prelude::*;
/// Gets the master password, deletes the accounts and the master password from DB.
/// Although it doesn't use the master password, we get it to be sure that it's the user who used that command
async fn get_master_pass(
bot: Throttle<Bot>,
msg: Message,
db: Pool,
dialogue: MainDialogue,
mut ids: MessageIds,
locale: LocaleRef,
_: String,
) -> crate::Result<()> {
dialogue.exit().await?;
let user_id = msg.from().ok_or(NoUserInfo)?.id.0;
let mut txn = db.begin().await?;
let result = (
Account::delete_all(user_id, &mut *txn).await,
MasterPass::remove(user_id, &mut *txn).await,
);
let text = match result {
(Ok(()), Ok(())) => {
txn.commit().await?;
&locale.everything_was_deleted
}
(Err(err), _) | (_, Err(err)) => {
handle_error(err);
if let Err(err) = txn.rollback().await {
handle_error(err);
}
&locale.something_went_wrong
}
};
ids.alter_message(&bot, text, deletion_markup(locale), None)
.await?;
Ok(())
}
first_handler!(
delete_all,
send_master_pass_to_delete_everything,
State::GetMasterPass,
get_master_pass
);