Updated delete callback handler to alter a message if /delete command was used

This commit is contained in:
StNicolay 2023-08-11 15:15:35 +03:00
parent 6691ab12de
commit 06492bf31e
Signed by: StNicolay
GPG Key ID: 9693D04DCD962B0D
5 changed files with 34 additions and 20 deletions

View File

@ -1,3 +1,4 @@
use futures::TryFutureExt;
use tokio::try_join; use tokio::try_join;
use crate::{change_state, prelude::*}; use crate::{change_state, prelude::*};
@ -34,16 +35,16 @@ pub async fn delete(
q: CallbackQuery, q: CallbackQuery,
db: DatabaseConnection, db: DatabaseConnection,
dialogue: MainDialogue, dialogue: MainDialogue,
hash: super::NameHash, (hash, is_command): (super::NameHash, bool),
) -> crate::Result<()> { ) -> 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 user_id = q.from.id.0;
let name = match name_from_hash(&db, user_id, &hash).await? { let name = match name_from_hash(&db, user_id, &hash).await? {
Some(name) => name, Some(name) => name,
None => { None => {
try_join!( try_join!(
bot.send_message(chat_id, "Account wasn't found") bot.send_message(ids.0, "Account wasn't found")
.reply_markup(deletion_markup()) .reply_markup(deletion_markup())
.send(), .send(),
bot.answer_callback_query(q.id).send() bot.answer_callback_query(q.id).send()
@ -53,19 +54,23 @@ pub async fn delete(
} }
}; };
let previous = bot.send_message( let response = async {
chat_id, const TEXT: &str =
"Send master password. Once you send correct master password the account is unrecoverable" "Send master password. Once you send the master password the account is unrecoverable";
).await?;
bot.answer_callback_query(q.id).await?;
change_state!( if is_command {
dialogue, ids.alter_message(&bot, TEXT, None, None).await?
&previous, } else {
(name), let msg = bot.send_message(ids.0, TEXT).await?;
State::GetMasterPass, ids = MessageIds::from(&msg)
get_master_pass };
);
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(()) Ok(())
} }

View File

@ -24,7 +24,7 @@ pub enum CallbackCommand {
Decrypt(NameHash), Decrypt(NameHash),
Hide(NameHash), Hide(NameHash),
Alter(NameHash, AlterableField), Alter(NameHash, AlterableField),
DeleteAccount(NameHash), DeleteAccount { name: NameHash, is_command: bool },
} }
impl CallbackCommand { impl CallbackCommand {
@ -64,7 +64,14 @@ impl FromStr for CallbackCommand {
"an" => Ok(Alter(name_hash, Name)), "an" => Ok(Alter(name_hash, Name)),
"al" => Ok(Alter(name_hash, Login)), "al" => Ok(Alter(name_hash, Login)),
"ap" => Ok(Alter(name_hash, Pass)), "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), _ => Err(InvalidCommand::InvalidParams),
} }
} }

View File

@ -17,7 +17,7 @@ pub async fn delete(bot: Throttle<Bot>, msg: Message, db: DatabaseConnection) ->
return Ok(()); 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") bot.send_message(msg.chat.id, "Choose the account to delete")
.reply_markup(markup) .reply_markup(markup)
.await?; .await?;

View File

@ -62,7 +62,9 @@ fn get_dispatcher(
.branch(case![CallbackCommand::DeleteMessage].endpoint(callbacks::delete_message)) .branch(case![CallbackCommand::DeleteMessage].endpoint(callbacks::delete_message))
.branch(case![CallbackCommand::Get(hash)].endpoint(callbacks::get)) .branch(case![CallbackCommand::Get(hash)].endpoint(callbacks::get))
.branch(case![CallbackCommand::Decrypt(hash)].endpoint(callbacks::decrypt)) .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)); .branch(case![CallbackCommand::Alter(hash, field)].endpoint(callbacks::alter));
let handler = dptree::entry() let handler = dptree::entry()

View File

@ -67,7 +67,7 @@ pub fn account_markup(name: &str, is_encrypted: bool) -> InlineKeyboardMarkup {
("Alter login", "al"), ("Alter login", "al"),
("Alter password", "ap"), ("Alter password", "ap"),
encryption_button, encryption_button,
("Delete account", "delete"), ("Delete account", "delete0"),
] ]
.into_iter() .into_iter()
.map(|(text, command)| make_button(text, command, hash)) .map(|(text, command)| make_button(text, command, hash))