Switched to inline buttons when getting the account name, finished decrypt and delete Callback commands

This commit is contained in:
2023-07-29 15:21:40 +03:00
parent 5c14a77f29
commit 0139963459
17 changed files with 198 additions and 216 deletions

View File

@ -1,40 +1,11 @@
use crate::prelude::*;
/// Gets the master password and deletes the account.
/// 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: DatabaseConnection,
dialogue: MainDialogue,
mut ids: MessageIds,
name: String,
_: String,
) -> crate::Result<()> {
dialogue.exit().await?;
pub async fn delete(bot: Throttle<Bot>, msg: Message, db: DatabaseConnection) -> crate::Result<()> {
let user_id = msg.from().ok_or(NoUserInfo)?.id.0;
Account::delete_by_id((user_id, name)).exec(&db).await?;
ids.alter_message(
&bot,
"The account is successfully deleted",
deletion_markup(),
None,
)
.await?;
bot.send_message(msg.chat.id, "Choose the account to delete")
.reply_markup(menu_markup("delete", user_id, &db).await?)
.await?;
Ok(())
}
handler!(
get_account_name(name: String),
"Send master password. Once you send correct master password the account is unrecoverable",
State::GetMasterPass,
get_master_pass
);
ask_name_handler!(
delete,
"Send the name of the account to delete",
get_account_name
);

View File

@ -1,42 +1,15 @@
use crate::prelude::*;
use teloxide::types::ParseMode;
use tokio::task::spawn_blocking;
/// Gets the master password, decryptes the account and sends it to the user with copyable fields
async fn get_master_pass(
pub async fn get_account(
bot: Throttle<Bot>,
msg: Message,
db: DatabaseConnection,
dialogue: MainDialogue,
mut ids: MessageIds,
name: String,
master_pass: String,
) -> crate::Result<()> {
dialogue.exit().await?;
let user_id = msg.from().ok_or(NoUserInfo)?.id.0;
let account = match Account::get(user_id, &name, &db).await? {
Some(account) => account,
None => {
bot.send_message(msg.chat.id, "Account not found")
.reply_markup(deletion_markup())
.await?;
return Ok(());
}
};
let (login, password) = spawn_blocking(move || account.decrypt(&master_pass)).await??;
let text = format!("Name:\n`{name}`\nLogin:\n`{login}`\nPassword:\n`{password}`");
ids.alter_message(&bot, text, deletion_markup(), ParseMode::MarkdownV2)
bot.send_message(msg.chat.id, "Choose the account to get")
.reply_markup(menu_markup("decrypt", user_id, &db).await?)
.await?;
Ok(())
}
handler!(get_account_name(name:String), "Send master password", State::GetMasterPass, get_master_pass);
ask_name_handler!(
get_account,
"Send the name of the account to get",
get_account_name
);

View File

@ -15,7 +15,7 @@ pub async fn menu(bot: Throttle<Bot>, msg: Message, db: DatabaseConnection) -> c
.await?;
}
let markup = spawn_blocking(|| menu_markup_sync(names)).await?;
let markup = spawn_blocking(|| menu_markup_sync("get", names)).await?;
bot.send_message(msg.chat.id, "Choose your account")
.reply_markup(markup)
.await?;