use crate::{ entity::prelude::Account, errors::NoUserInfo, handlers::{markups::deletion_markup, utils::package_handler, MainDialogue, State}, models::DecryptedAccount, }; use sea_orm::prelude::*; use teloxide::{adaptors::Throttle, prelude::*}; use tokio::task::spawn_blocking; /// Gets the name of the master password, encryptes the account and adds it to the DB async fn get_master_pass( bot: Throttle, msg: Message, db: DatabaseConnection, dialogue: MainDialogue, account: DecryptedAccount, master_pass: String, ) -> crate::Result<()> { let user_id = msg.from().ok_or(NoUserInfo)?.id.0; dialogue.exit().await?; let account = spawn_blocking(move || account.into_account(user_id, &master_pass)).await??; account.insert(&db).await?; bot.send_message(msg.chat.id, "Success") .reply_markup(deletion_markup()) .await?; Ok(()) } /// Gets the password of the account to add async fn get_password( bot: Throttle, msg: Message, _: DatabaseConnection, dialogue: MainDialogue, name: String, login: String, password: String, ) -> crate::Result<()> { let previous = bot .send_message(msg.chat.id, "Send master password") .await?; let account = DecryptedAccount { name, login, password, }; dialogue .update(State::GetMasterPass(package_handler( move |bot, msg, db, dialogue, master_pass| { get_master_pass(bot, msg, db, dialogue, account, master_pass) }, previous, ))) .await?; Ok(()) } /// Gets the login of the account to add async fn get_login( bot: Throttle, msg: Message, _: DatabaseConnection, dialogue: MainDialogue, name: String, login: String, ) -> crate::Result<()> { let previous = bot.send_message(msg.chat.id, "Send password").await?; dialogue .update(State::GetPassword(package_handler( move |bot, msg, db, dialogue, password| { get_password(bot, msg, db, dialogue, name, login, password) }, previous, ))) .await?; Ok(()) } /// Gets the name of the account to add and checks that there're no accounts with the same name async fn get_account_name( bot: Throttle, msg: Message, db: DatabaseConnection, dialogue: MainDialogue, name: String, ) -> crate::Result<()> { let user_id = msg.from().ok_or(NoUserInfo)?.id.0; if Account::exists(user_id, &name, &db).await? { bot.send_message(msg.chat.id, "Account alreay exists") .reply_markup(deletion_markup()) .await?; return Ok(()); } let previous = bot.send_message(msg.chat.id, "Send login").await?; dialogue .update(State::GetLogin(package_handler( move |bot, msg, db, dialogue, login| get_login(bot, msg, db, dialogue, name, login), previous, ))) .await?; Ok(()) } /// Handles /add_account pub async fn add_account( bot: Throttle, msg: Message, dialogue: MainDialogue, ) -> crate::Result<()> { let previous = bot.send_message(msg.chat.id, "Send account name").await?; dialogue .update(State::GetAccountName(package_handler( get_account_name, previous, ))) .await?; Ok(()) }