diff --git a/src/callbacks/delete.rs b/src/callbacks/delete.rs index a27da11..aaa1ec9 100644 --- a/src/callbacks/delete.rs +++ b/src/callbacks/delete.rs @@ -1,3 +1,4 @@ +use futures::TryFutureExt; use tokio::try_join; use crate::{change_state, prelude::*}; @@ -34,16 +35,16 @@ pub async fn delete( q: CallbackQuery, db: DatabaseConnection, dialogue: MainDialogue, - hash: super::NameHash, + (hash, is_command): (super::NameHash, bool), ) -> crate::Result<()> { - let chat_id = q.message.as_ref().unwrap().chat.id; + let mut ids: MessageIds = q.message.as_ref().unwrap().into(); let user_id = q.from.id.0; let name = match name_from_hash(&db, user_id, &hash).await? { Some(name) => name, None => { try_join!( - bot.send_message(chat_id, "Account wasn't found") + bot.send_message(ids.0, "Account wasn't found") .reply_markup(deletion_markup()) .send(), bot.answer_callback_query(q.id).send() @@ -53,19 +54,23 @@ pub async fn delete( } }; - let previous = bot.send_message( - chat_id, - "Send master password. Once you send correct master password the account is unrecoverable" - ).await?; - bot.answer_callback_query(q.id).await?; + let response = async { + const TEXT: &str = + "Send master password. Once you send the master password the account is unrecoverable"; - change_state!( - dialogue, - &previous, - (name), - State::GetMasterPass, - get_master_pass - ); + if is_command { + ids.alter_message(&bot, TEXT, None, None).await? + } else { + let msg = bot.send_message(ids.0, TEXT).await?; + ids = MessageIds::from(&msg) + }; + + Ok::<_, crate::Error>(()) + }; + + try_join!(response, bot.answer_callback_query(q.id).send().err_into())?; + + change_state!(dialogue, ids, (name), State::GetMasterPass, get_master_pass); Ok(()) } diff --git a/src/callbacks/mod.rs b/src/callbacks/mod.rs index 7a39d81..5f5c292 100644 --- a/src/callbacks/mod.rs +++ b/src/callbacks/mod.rs @@ -24,7 +24,7 @@ pub enum CallbackCommand { Decrypt(NameHash), Hide(NameHash), Alter(NameHash, AlterableField), - DeleteAccount(NameHash), + DeleteAccount { name: NameHash, is_command: bool }, } impl CallbackCommand { @@ -64,7 +64,14 @@ impl FromStr for CallbackCommand { "an" => Ok(Alter(name_hash, Name)), "al" => Ok(Alter(name_hash, Login)), "ap" => Ok(Alter(name_hash, Pass)), - "delete" => Ok(DeleteAccount(name_hash)), + "delete0" => Ok(DeleteAccount { + name: name_hash, + is_command: false, + }), + "delete1" => Ok(DeleteAccount { + name: name_hash, + is_command: true, + }), _ => Err(InvalidCommand::InvalidParams), } } diff --git a/src/commands/delete.rs b/src/commands/delete.rs index 77f70be..9027c1f 100644 --- a/src/commands/delete.rs +++ b/src/commands/delete.rs @@ -17,7 +17,7 @@ pub async fn delete(bot: Throttle, msg: Message, db: DatabaseConnection) -> return Ok(()); } - let markup = spawn_blocking(|| menu_markup_sync("delete", names)).await?; + let markup = spawn_blocking(|| menu_markup_sync("delete1", names)).await?; bot.send_message(msg.chat.id, "Choose the account to delete") .reply_markup(markup) .await?; diff --git a/src/main.rs b/src/main.rs index 2a6c4d7..57b3de0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -62,7 +62,9 @@ fn get_dispatcher( .branch(case![CallbackCommand::DeleteMessage].endpoint(callbacks::delete_message)) .branch(case![CallbackCommand::Get(hash)].endpoint(callbacks::get)) .branch(case![CallbackCommand::Decrypt(hash)].endpoint(callbacks::decrypt)) - .branch(case![CallbackCommand::DeleteAccount(hash)].endpoint(callbacks::delete)) + .branch( + case![CallbackCommand::DeleteAccount { name, is_command }].endpoint(callbacks::delete), + ) .branch(case![CallbackCommand::Alter(hash, field)].endpoint(callbacks::alter)); let handler = dptree::entry() diff --git a/src/markups.rs b/src/markups.rs index 106260e..3aa193a 100644 --- a/src/markups.rs +++ b/src/markups.rs @@ -67,7 +67,7 @@ pub fn account_markup(name: &str, is_encrypted: bool) -> InlineKeyboardMarkup { ("Alter login", "al"), ("Alter password", "ap"), encryption_button, - ("Delete account", "delete"), + ("Delete account", "delete0"), ] .into_iter() .map(|(text, command)| make_button(text, command, hash))