From c2646235105d38fb78e1dcecb0629e6cd9fdbe67 Mon Sep 17 00:00:00 2001 From: StNicolay Date: Sat, 6 May 2023 21:02:32 +0300 Subject: [PATCH] Added button for deleting the resulting message --- src/handlers/callbacks/delete_message.rs | 18 ++++++++++++++++++ src/handlers/callbacks/mod.rs | 1 + src/handlers/commands/add_account.rs | 13 +++++++++---- src/handlers/commands/default.rs | 5 ++++- src/handlers/commands/delete.rs | 8 +++++++- src/handlers/commands/delete_all.rs | 3 ++- src/handlers/commands/export.rs | 6 ++++-- src/handlers/commands/get_account.rs | 12 ++++++++++-- src/handlers/commands/get_accounts.rs | 7 +++++-- src/handlers/commands/help.rs | 3 ++- src/handlers/commands/import.rs | 24 +++++++++++++++--------- src/handlers/commands/set_master_pass.rs | 7 +++++-- src/handlers/markups.rs | 8 +++++++- src/handlers/mod.rs | 11 ++++++++++- src/handlers/state/generic.rs | 4 ++-- src/handlers/state/get_master_pass.rs | 4 +++- 16 files changed, 104 insertions(+), 30 deletions(-) create mode 100644 src/handlers/callbacks/delete_message.rs create mode 100644 src/handlers/callbacks/mod.rs diff --git a/src/handlers/callbacks/delete_message.rs b/src/handlers/callbacks/delete_message.rs new file mode 100644 index 0000000..4afbf44 --- /dev/null +++ b/src/handlers/callbacks/delete_message.rs @@ -0,0 +1,18 @@ +use teloxide::{adaptors::Throttle, prelude::*}; + +pub async fn run(bot: Throttle, q: CallbackQuery) -> crate::Result<()> { + match q.message { + Some(msg) => { + bot.delete_message(msg.chat.id, msg.id).await?; + Ok(()) + } + None => Ok(()), + } +} + +pub fn filter(q: CallbackQuery) -> bool { + match q.data.as_deref() { + Some("delete_message") => true, + _ => false, + } +} diff --git a/src/handlers/callbacks/mod.rs b/src/handlers/callbacks/mod.rs new file mode 100644 index 0000000..dec9066 --- /dev/null +++ b/src/handlers/callbacks/mod.rs @@ -0,0 +1 @@ +pub mod delete_message; diff --git a/src/handlers/commands/add_account.rs b/src/handlers/commands/add_account.rs index 253deb2..fad8597 100644 --- a/src/handlers/commands/add_account.rs +++ b/src/handlers/commands/add_account.rs @@ -1,6 +1,8 @@ -use crate::entity::{account, prelude::Account}; -use crate::errors::NoUserInfo; -use crate::handlers::{utils::package_handler, MainDialogue, State}; +use crate::{ + entity::{account, prelude::Account}, + errors::NoUserInfo, + handlers::{markups::deletion_markup, utils::package_handler, MainDialogue, State}, +}; use sea_orm::prelude::*; use teloxide::{adaptors::Throttle, prelude::*}; use tokio::task::spawn_blocking; @@ -24,7 +26,9 @@ async fn get_master_pass( }) .await??; account.insert(&db).await?; - bot.send_message(msg.chat.id, "Success").await?; + bot.send_message(msg.chat.id, "Success") + .reply_markup(deletion_markup()) + .await?; Ok(()) } @@ -95,6 +99,7 @@ async fn get_account_name( 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(()); } diff --git a/src/handlers/commands/default.rs b/src/handlers/commands/default.rs index 47721e0..179ca34 100644 --- a/src/handlers/commands/default.rs +++ b/src/handlers/commands/default.rs @@ -1,6 +1,9 @@ +use crate::handlers::markups::deletion_markup; use teloxide::{adaptors::Throttle, prelude::*}; pub async fn default(bot: Throttle, msg: Message) -> crate::Result<()> { - bot.send_message(msg.chat.id, "Unknown command").await?; + bot.send_message(msg.chat.id, "Unknown command") + .reply_markup(deletion_markup()) + .await?; Ok(()) } diff --git a/src/handlers/commands/delete.rs b/src/handlers/commands/delete.rs index 0ebc39d..c1f51bb 100644 --- a/src/handlers/commands/delete.rs +++ b/src/handlers/commands/delete.rs @@ -1,7 +1,11 @@ use crate::{ entity::prelude::Account, errors::NoUserInfo, - handlers::{markups, utils::package_handler, MainDialogue, State}, + handlers::{ + markups::{self, deletion_markup}, + utils::package_handler, + MainDialogue, State, + }, }; use sea_orm::prelude::*; use teloxide::{adaptors::Throttle, prelude::*}; @@ -19,6 +23,7 @@ async fn get_master_pass( let user_id = msg.from().ok_or(NoUserInfo)?.id.0; Account::delete_by_id((user_id, name)).exec(&db).await?; bot.send_message(msg.chat.id, "The account is successfully deleted") + .reply_markup(deletion_markup()) .await?; Ok(()) } @@ -35,6 +40,7 @@ async fn get_account_name( 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 doesn't exists") + .reply_markup(deletion_markup()) .await?; return Ok(()); } diff --git a/src/handlers/commands/delete_all.rs b/src/handlers/commands/delete_all.rs index cc81e56..c1b4c4f 100644 --- a/src/handlers/commands/delete_all.rs +++ b/src/handlers/commands/delete_all.rs @@ -1,7 +1,7 @@ use crate::{ entity::{account, prelude::*}, errors::NoUserInfo, - handlers::{utils::package_handler, MainDialogue, State}, + handlers::{markups::deletion_markup, utils::package_handler, MainDialogue, State}, }; use sea_orm::prelude::*; use teloxide::{adaptors::Throttle, prelude::*}; @@ -22,6 +22,7 @@ async fn get_master_pass( .await?; MasterPass::delete_by_id(user_id).exec(&db).await?; bot.send_message(msg.chat.id, "Everything was deleted") + .reply_markup(deletion_markup()) .await?; Ok(()) } diff --git a/src/handlers/commands/export.rs b/src/handlers/commands/export.rs index 86c3bc4..d709f5d 100644 --- a/src/handlers/commands/export.rs +++ b/src/handlers/commands/export.rs @@ -1,7 +1,7 @@ use crate::{ entity::prelude::Account, errors::NoUserInfo, - handlers::{utils::package_handler, MainDialogue, State}, + handlers::{markups::deletion_markup, utils::package_handler, MainDialogue, State}, models::{DecryptedAccount, User}, }; use futures::TryStreamExt; @@ -45,7 +45,9 @@ async fn get_master_pass( accounts.sort_unstable_by(|this, other| this.name.cmp(&other.name)); let json = to_string_pretty(&User { accounts })?; let file = InputFile::memory(json).file_name("accounts.json"); - bot.send_document(msg.chat.id, file).await?; + bot.send_document(msg.chat.id, file) + .reply_markup(deletion_markup()) + .await?; dialogue.exit().await?; Ok(()) } diff --git a/src/handlers/commands/get_account.rs b/src/handlers/commands/get_account.rs index 23d10ba..6d003fa 100644 --- a/src/handlers/commands/get_account.rs +++ b/src/handlers/commands/get_account.rs @@ -1,7 +1,11 @@ use crate::{ entity::prelude::Account, errors::NoUserInfo, - handlers::{markups, utils::package_handler, MainDialogue, State}, + handlers::{ + markups::{self, deletion_markup}, + utils::package_handler, + MainDialogue, State, + }, }; use sea_orm::prelude::*; use teloxide::{adaptors::Throttle, prelude::*, types::ParseMode}; @@ -22,13 +26,16 @@ async fn get_master_pass( let account = match Account::get(user_id, &name, &db).await? { Some(account) => account, None => { - bot.send_message(msg.chat.id, "Account not found").await?; + bot.send_message(msg.chat.id, "Account not found") + .reply_markup(deletion_markup()) + .await?; return Ok(()); } }; let (login, password) = spawn_blocking(move || account.decrypt(&master_pass)).await??; let message = format!("Name:\n`{name}`\nLogin:\n`{login}`\nPassword:\n`{password}`"); bot.send_message(msg.chat.id, message) + .reply_markup(deletion_markup()) .parse_mode(ParseMode::MarkdownV2) .await?; Ok(()) @@ -46,6 +53,7 @@ async fn get_account_name( 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 doesn't exists") + .reply_markup(deletion_markup()) .await?; return Ok(()); } diff --git a/src/handlers/commands/get_accounts.rs b/src/handlers/commands/get_accounts.rs index d8f286d..3ab0258 100644 --- a/src/handlers/commands/get_accounts.rs +++ b/src/handlers/commands/get_accounts.rs @@ -1,4 +1,4 @@ -use crate::{entity::prelude::Account, errors::NoUserInfo}; +use crate::{entity::prelude::Account, errors::NoUserInfo, handlers::markups::deletion_markup}; use futures::TryStreamExt; use sea_orm::prelude::*; use teloxide::{adaptors::Throttle, prelude::*, types::ParseMode}; @@ -13,7 +13,9 @@ pub async fn get_accounts( let mut result = match account_names.try_next().await? { Some(name) => format!("Accounts:\n`{name}`"), None => { - bot.send_message(msg.chat.id, "No accounts found").await?; + bot.send_message(msg.chat.id, "No accounts found") + .reply_markup(deletion_markup()) + .await?; return Ok(()); } }; @@ -25,6 +27,7 @@ pub async fn get_accounts( .await?; bot.send_message(msg.chat.id, result) .parse_mode(ParseMode::MarkdownV2) + .reply_markup(deletion_markup()) .await?; Ok(()) } diff --git a/src/handlers/commands/help.rs b/src/handlers/commands/help.rs index 606c8b1..7ffc1e6 100644 --- a/src/handlers/commands/help.rs +++ b/src/handlers/commands/help.rs @@ -1,8 +1,9 @@ -use crate::handlers::Command; +use crate::handlers::{markups::deletion_markup, Command}; use teloxide::{adaptors::Throttle, prelude::*, utils::command::BotCommands}; pub async fn help(bot: Throttle, msg: Message) -> crate::Result<()> { bot.send_message(msg.chat.id, Command::descriptions().to_string()) + .reply_markup(deletion_markup()) .await?; Ok(()) } diff --git a/src/handlers/commands/import.rs b/src/handlers/commands/import.rs index 2b42b72..7a98be1 100644 --- a/src/handlers/commands/import.rs +++ b/src/handlers/commands/import.rs @@ -1,6 +1,6 @@ use crate::{ errors::NoUserInfo, - handlers::{utils::package_handler, MainDialogue, State}, + handlers::{markups::deletion_markup, utils::package_handler, MainDialogue, State}, models::{DecryptedAccount, User}, }; use futures::{stream, StreamExt, TryStreamExt}; @@ -46,15 +46,17 @@ async fn get_master_pass( Ok(accounts) => accounts.into_inner(), Err(_) => unreachable!(), }; - if failed.is_empty() { - bot.send_message(msg.chat.id, "Success").await?; + let message = if failed.is_empty() { + "Success".to_owned() } else { - let text = format!( + format!( "Failed to create the following accounts:\n{}", failed.into_iter().format("\n") - ); - bot.send_message(msg.chat.id, text).await?; - } + ) + }; + bot.send_message(msg.chat.id, message) + .reply_markup(deletion_markup()) + .await?; dialogue.exit().await?; Ok(()) } @@ -71,6 +73,7 @@ async fn get_document( Some(doc) => doc, None => { bot.send_message(msg.chat.id, "You didn't send a file") + .reply_markup(deletion_markup()) .await?; dialogue.exit().await?; return Ok(()); @@ -79,7 +82,9 @@ async fn get_document( match document.file_name { Some(ref name) if name.trim().ends_with(".json") => (), _ => { - bot.send_message(msg.chat.id, "Invalid file name").await?; + bot.send_message(msg.chat.id, "Invalid file name") + .reply_markup(deletion_markup()) + .await?; dialogue.exit().await?; return Ok(()); } @@ -96,13 +101,14 @@ async fn get_document( Ok(user) => user.accounts, Err(_) => { bot.send_message(msg.chat.id, "Error parsing the json file") + .reply_markup(deletion_markup()) .await?; dialogue.exit().await?; return Ok(()); } }; let previous = bot - .send_message(msg.chat.id, "Send a new master password") + .send_message(msg.chat.id, "Send master password") .await?; dialogue .update(State::GetMasterPass(package_handler( diff --git a/src/handlers/commands/set_master_pass.rs b/src/handlers/commands/set_master_pass.rs index 016536d..b2c4529 100644 --- a/src/handlers/commands/set_master_pass.rs +++ b/src/handlers/commands/set_master_pass.rs @@ -1,7 +1,7 @@ use crate::{ entity::{master_pass, prelude::*}, errors::NoUserInfo, - handlers::{utils::package_handler, MainDialogue, State}, + handlers::{markups::deletion_markup, utils::package_handler, MainDialogue, State}, }; use sea_orm::prelude::*; use teloxide::{adaptors::Throttle, prelude::*}; @@ -23,7 +23,9 @@ async fn get_master_pass( }) .await??; model.insert(&db).await?; - bot.send_message(msg.chat.id, "Success").await?; + bot.send_message(msg.chat.id, "Success") + .reply_markup(deletion_markup()) + .await?; Ok(()) } @@ -36,6 +38,7 @@ pub async fn set_master_pass( let user_id = msg.from().ok_or(NoUserInfo)?.id.0; if MasterPass::exists(user_id, &db).await? { bot.send_message(msg.chat.id, "Master password already exists") + .reply_markup(deletion_markup()) .await?; return Ok(()); } diff --git a/src/handlers/markups.rs b/src/handlers/markups.rs index e42b350..40e4483 100644 --- a/src/handlers/markups.rs +++ b/src/handlers/markups.rs @@ -1,7 +1,7 @@ use crate::entity::prelude::Account; use futures::TryStreamExt; use sea_orm::prelude::*; -use teloxide::types::{KeyboardButton, KeyboardMarkup}; +use teloxide::types::{InlineKeyboardButton, InlineKeyboardMarkup, KeyboardButton, KeyboardMarkup}; #[inline] pub async fn account_markup( @@ -19,3 +19,9 @@ pub async fn account_markup( .one_time_keyboard(true); Ok(markup) } + +#[inline] +pub fn deletion_markup() -> InlineKeyboardMarkup { + let button = InlineKeyboardButton::callback("Delete message", "delete_message"); + InlineKeyboardMarkup::new([[button]]) +} diff --git a/src/handlers/mod.rs b/src/handlers/mod.rs index eb72e43..0fed066 100644 --- a/src/handlers/mod.rs +++ b/src/handlers/mod.rs @@ -1,3 +1,4 @@ +mod callbacks; mod commands; mod markups; mod state; @@ -96,7 +97,15 @@ pub fn get_dispatcher( .branch(command_handler) .branch(endpoint(commands::default)); - Dispatcher::builder(bot, message_handler) + let callback_handler = Update::filter_callback_query() + .filter(callbacks::delete_message::filter) + .endpoint(callbacks::delete_message::run); + + let handler = dptree::entry() + .branch(message_handler) + .branch(callback_handler); + + Dispatcher::builder(bot, handler) .dependencies(deps![db, InMemStorage::::new()]) .enable_ctrlc_handler() .build() diff --git a/src/handlers/state/generic.rs b/src/handlers/state/generic.rs index 23f196b..516f55d 100644 --- a/src/handlers/state/generic.rs +++ b/src/handlers/state/generic.rs @@ -1,8 +1,7 @@ +use crate::{errors::HandlerUsed, handlers::markups::deletion_markup, PinnedFuture}; use sea_orm::prelude::*; use teloxide::{adaptors::Throttle, prelude::*}; -use crate::{errors::HandlerUsed, PinnedFuture}; - pub async fn generic( bot: Throttle, text: String, @@ -23,6 +22,7 @@ where if text == "/cancel" { dialogue.exit().await?; bot.send_message(msg.chat.id, "Successfully cancelled") + .reply_markup(deletion_markup()) .await?; return Ok(()); } diff --git a/src/handlers/state/get_master_pass.rs b/src/handlers/state/get_master_pass.rs index c4c93d8..6266354 100644 --- a/src/handlers/state/get_master_pass.rs +++ b/src/handlers/state/get_master_pass.rs @@ -1,7 +1,7 @@ use crate::{ entity::prelude::MasterPass, errors::{NoMessageText, NoUserInfo}, - handlers::{MainDialogue, PackagedHandler}, + handlers::{markups::deletion_markup, MainDialogue, PackagedHandler}, }; use sea_orm::prelude::*; use teloxide::{adaptors::Throttle, prelude::*}; @@ -19,11 +19,13 @@ pub async fn check_master_pass<'a>( Ok(Some(true)) => Ok(true), Ok(Some(false)) => { bot.send_message(msg.chat.id, "Wrong master password") + .reply_markup(deletion_markup()) .await?; Ok(false) } Ok(None) => { bot.send_message(msg.chat.id, "No master password set") + .reply_markup(deletion_markup()) .await?; Ok(false) }