From 072596a79717833668cc47c84dcd53c1ea7599c7 Mon Sep 17 00:00:00 2001 From: StNicolay Date: Sat, 12 Aug 2023 17:37:27 +0300 Subject: [PATCH] added a filter for messages without user information --- src/filter_user_info.rs | 51 +++++++++++++++++++++++++++++++++++++++++ src/main.rs | 3 +++ src/state/handler.rs | 2 +- 3 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 src/filter_user_info.rs diff --git a/src/filter_user_info.rs b/src/filter_user_info.rs new file mode 100644 index 0000000..d4e15a1 --- /dev/null +++ b/src/filter_user_info.rs @@ -0,0 +1,51 @@ +use crate::prelude::*; +use teloxide::{dispatching::DpHandlerDescription, dptree::Handler}; + +#[inline] +fn has_no_user_info(msg: Message) -> bool { + msg.from().is_none() +} + +#[inline] +async fn notify_about_no_user_info( + bot: Throttle, + msg: Message, + state: State, +) -> crate::Result<()> { + use State::*; + const TEXT: &str = "Invalid message. Couldn't get the user information. Send the message again"; + + match state { + Start => { + bot.send_message(msg.chat.id, TEXT) + .reply_markup(deletion_markup()) + .await?; + } + GetNewName(handler) + | GetMasterPass(handler) + | GetNewMasterPass(handler) + | GetLogin(handler) + | GetPassword(handler) => { + let mut handler = handler.lock().await; + handler + .previous + .alter_message(&bot, TEXT, None, None) + .await?; + } + GetUser(handler) => { + let mut handler = handler.lock().await; + handler + .previous + .alter_message(&bot, TEXT, None, None) + .await?; + } + }; + + Ok(()) +} + +/// Gets a handler that filters out the messages without user information +#[inline] +pub fn get_handler() -> Handler<'static, DependencyMap, crate::Result<()>, DpHandlerDescription> { + dptree::filter(has_no_user_info).endpoint(notify_about_no_user_info) +} diff --git a/src/main.rs b/src/main.rs index ad7a145..1eaaeb4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ mod callbacks; mod commands; mod default; mod errors; +mod filter_user_info; mod macros; mod markups; mod master_password_check; @@ -47,6 +48,8 @@ fn get_dispatcher( let message_handler = Update::filter_message() .map_async(utils::delete_message) + // Filters out the messages without user information + .branch(filter_user_info::get_handler()) .branch(case![State::GetNewName(next)].endpoint(state::get_new_name)) .branch(case![State::GetMasterPass(next)].endpoint(state::get_master_pass)) .branch(case![State::GetNewMasterPass(next)].endpoint(state::get_new_master_pass)) diff --git a/src/state/handler.rs b/src/state/handler.rs index 6c1f897..36d0bf0 100644 --- a/src/state/handler.rs +++ b/src/state/handler.rs @@ -70,7 +70,7 @@ type DynHanlder = Box< + Send, >; -pub struct Handler { +pub struct Handler { pub func: Option>, pub previous: MessageIds,