From a2fae8bb89f407af70cda1e8694bed8c0e930249 Mon Sep 17 00:00:00 2001 From: StNicolay Date: Thu, 4 May 2023 21:35:31 +0300 Subject: [PATCH] Added delete command and fixed memory deletion for /add_account --- src/handlers/commands/add_account.rs | 7 +-- src/handlers/commands/delete.rs | 67 ++++++++++++++++++++++++++++ src/handlers/commands/mod.rs | 2 + src/handlers/mod.rs | 5 ++- 4 files changed, 77 insertions(+), 4 deletions(-) create mode 100644 src/handlers/commands/delete.rs diff --git a/src/handlers/commands/add_account.rs b/src/handlers/commands/add_account.rs index 22c9728..c4d9627 100644 --- a/src/handlers/commands/add_account.rs +++ b/src/handlers/commands/add_account.rs @@ -38,7 +38,8 @@ async fn get_password( password: String, ) -> crate::Result<()> { let _ = bot.delete_message(previous.chat.id, previous.id).await; - bot.send_message(msg.chat.id, "Send master password") + let previous = bot + .send_message(msg.chat.id, "Send master password") .await?; dialogue .update(State::GetMasterPass(package_handler( @@ -70,7 +71,7 @@ async fn get_login( login: String, ) -> crate::Result<()> { let _ = bot.delete_message(previous.chat.id, previous.id).await; - bot.send_message(msg.chat.id, "Send password").await?; + let previous = bot.send_message(msg.chat.id, "Send password").await?; dialogue .update(State::GetPassword(package_handler( move |bot, msg, db, dialogue, password| { @@ -90,7 +91,7 @@ async fn get_account_name( name: String, ) -> crate::Result<()> { let _ = bot.delete_message(previous.chat.id, previous.id).await; - bot.send_message(msg.chat.id, "Send login").await?; + let previous = bot.send_message(msg.chat.id, "Send login").await?; dialogue .update(State::GetLogin(package_handler( move |bot, msg, db, dialogue, login| { diff --git a/src/handlers/commands/delete.rs b/src/handlers/commands/delete.rs new file mode 100644 index 0000000..3c9049c --- /dev/null +++ b/src/handlers/commands/delete.rs @@ -0,0 +1,67 @@ +use crate::{ + entity::prelude::Account, + handlers::{markups, utils::package_handler, MainDialogue, State}, +}; +use sea_orm::prelude::*; +use teloxide::{adaptors::Throttle, prelude::*}; + +async fn get_master_pass( + bot: Throttle, + msg: Message, + db: DatabaseConnection, + dialogue: MainDialogue, + previous: Message, + name: String, + _: String, +) -> crate::Result<()> { + let _ = bot.delete_message(previous.chat.id, previous.id).await; + dialogue.exit().await?; + let user_id = msg.from().unwrap().id.0; + Account::delete_by_id((user_id, name)).exec(&db).await?; + bot.send_message(msg.chat.id, "The account is successfully deleted") + .await?; + Ok(()) +} + +async fn get_account_name( + bot: Throttle, + msg: Message, + _: DatabaseConnection, + dialogue: MainDialogue, + previous: Message, + name: String, +) -> crate::Result<()> { + let _ = bot.delete_message(previous.chat.id, previous.id).await; + let previous = bot + .send_message(msg.chat.id, "Send master password. Once you send correct master password the account is unrecoverable") + .await?; + dialogue + .update(State::GetMasterPass(package_handler( + move |bot, msg, db, dialogue, master_pass| { + get_master_pass(bot, msg, db, dialogue, previous, name, master_pass) + }, + ))) + .await?; + Ok(()) +} + +pub async fn delete( + bot: Throttle, + msg: Message, + dialogue: MainDialogue, + db: DatabaseConnection, +) -> crate::Result<()> { + let markup = markups::account_markup(msg.from().unwrap().id.0, &db).await?; + let previous = bot + .send_message(msg.chat.id, "Send account name") + .reply_markup(markup) + .await?; + dialogue + .update(State::GetAccountName(package_handler( + move |bot, msg, db, dialogue, name| { + get_account_name(bot, msg, db, dialogue, previous, name) + }, + ))) + .await?; + Ok(()) +} diff --git a/src/handlers/commands/mod.rs b/src/handlers/commands/mod.rs index 185863c..5f53c42 100644 --- a/src/handlers/commands/mod.rs +++ b/src/handlers/commands/mod.rs @@ -1,5 +1,6 @@ mod add_account; mod default; +mod delete; mod get_account; mod get_accounts; mod help; @@ -7,6 +8,7 @@ mod set_master_pass; pub use add_account::add_account; pub use default::default; +pub use delete::delete; pub use get_account::get_account; pub use get_accounts::get_accounts; pub use help::help; diff --git a/src/handlers/mod.rs b/src/handlers/mod.rs index 031f774..8791dfd 100644 --- a/src/handlers/mod.rs +++ b/src/handlers/mod.rs @@ -27,6 +27,8 @@ enum Command { GetAccounts, #[command()] SetMasterPass, + #[command()] + Delete, } type MainDialogue = Dialogue>; @@ -70,7 +72,8 @@ pub fn get_dispatcher( .branch(case![Command::AddAccount].endpoint(commands::add_account)) .branch(case![Command::GetAccount].endpoint(commands::get_account)) .branch(case![Command::GetAccounts].endpoint(commands::get_accounts)) - .branch(case![Command::SetMasterPass].endpoint(commands::set_master_pass)); + .branch(case![Command::SetMasterPass].endpoint(commands::set_master_pass)) + .branch(case![Command::Delete].endpoint(commands::delete)); let message_handler = Update::filter_message() .map_async(utils::delete_message)