pass_manager/src/commands/add_account.rs

103 lines
2.8 KiB
Rust
Raw Normal View History

use crate::prelude::*;
use tokio::task::spawn_blocking;
2023-05-03 18:08:14 +00:00
2023-05-09 17:27:58 +00:00
/// Gets the name of the master password, encryptes the account and adds it to the DB
2023-05-03 18:08:14 +00:00
async fn get_master_pass(
bot: Throttle<Bot>,
msg: Message,
db: DatabaseConnection,
dialogue: MainDialogue,
account: DecryptedAccount,
2023-05-03 18:08:14 +00:00
master_pass: String,
) -> crate::Result<()> {
let user_id = msg.from().ok_or(NoUserInfo)?.id.0;
2023-05-03 18:08:14 +00:00
dialogue.exit().await?;
let account = spawn_blocking(move || account.into_account(user_id, &master_pass)).await??;
2023-05-03 18:08:14 +00:00
account.insert(&db).await?;
bot.send_message(msg.chat.id, "Success")
.reply_markup(deletion_markup())
.await?;
2023-05-03 18:08:14 +00:00
Ok(())
}
2023-05-09 17:27:58 +00:00
/// Gets the password of the account to add
2023-05-03 18:08:14 +00:00
async fn get_password(
bot: Throttle<Bot>,
msg: Message,
_: DatabaseConnection,
dialogue: MainDialogue,
name: String,
login: String,
password: String,
) -> crate::Result<()> {
let previous = bot
.send_message(msg.chat.id, "Send master password")
2023-05-03 18:08:14 +00:00
.await?;
let account = DecryptedAccount {
name,
login,
password,
};
2023-05-03 18:08:14 +00:00
dialogue
.update(State::GetMasterPass(Handler::new(
2023-05-03 18:08:14 +00:00
move |bot, msg, db, dialogue, master_pass| {
get_master_pass(bot, msg, db, dialogue, account, master_pass)
2023-05-03 18:08:14 +00:00
},
&previous,
2023-05-03 18:08:14 +00:00
)))
.await?;
Ok(())
}
2023-05-09 17:27:58 +00:00
/// Gets the login of the account to add
2023-05-03 18:08:14 +00:00
async fn get_login(
bot: Throttle<Bot>,
msg: Message,
_: DatabaseConnection,
dialogue: MainDialogue,
name: String,
login: String,
) -> crate::Result<()> {
let previous = bot.send_message(msg.chat.id, "Send password").await?;
2023-05-03 18:08:14 +00:00
dialogue
.update(State::GetPassword(Handler::new(
2023-05-03 18:08:14 +00:00
move |bot, msg, db, dialogue, password| {
get_password(bot, msg, db, dialogue, name, login, password)
2023-05-03 18:08:14 +00:00
},
&previous,
2023-05-03 18:08:14 +00:00
)))
.await?;
Ok(())
}
2023-05-09 17:27:58 +00:00
/// Gets the name of the account to add and checks that there're no accounts with the same name
2023-05-03 18:08:14 +00:00
async fn get_account_name(
bot: Throttle<Bot>,
msg: Message,
2023-05-27 23:21:50 +00:00
_: DatabaseConnection,
2023-05-03 18:08:14 +00:00
dialogue: MainDialogue,
name: String,
) -> crate::Result<()> {
let previous = bot.send_message(msg.chat.id, "Send login").await?;
2023-05-03 18:08:14 +00:00
dialogue
.update(State::GetLogin(Handler::new(
move |bot, msg, db, dialogue, login| get_login(bot, msg, db, dialogue, name, login),
&previous,
2023-05-03 18:08:14 +00:00
)))
.await?;
Ok(())
}
2023-05-09 17:27:58 +00:00
/// Handles /add_account
2023-05-03 18:08:14 +00:00
pub async fn add_account(
bot: Throttle<Bot>,
msg: Message,
dialogue: MainDialogue,
) -> crate::Result<()> {
2023-05-04 17:51:36 +00:00
let previous = bot.send_message(msg.chat.id, "Send account name").await?;
2023-05-03 18:08:14 +00:00
dialogue
.update(State::GetNewName(Handler::new(get_account_name, &previous)))
2023-05-03 18:08:14 +00:00
.await?;
Ok(())
}