diff --git a/src/main.rs b/src/main.rs index 57b3de0..ad7a145 100644 --- a/src/main.rs +++ b/src/main.rs @@ -35,7 +35,7 @@ fn get_dispatcher( .branch(case![Command::GenPassword].endpoint(commands::gen_password)) .branch(case![Command::Cancel].endpoint(commands::cancel)) // This branch filters out the users that don't have a master password set - .branch(master_password_check::get_handler()) + .branch(master_password_check::get_handler::()) .branch(case![Command::Menu].endpoint(commands::menu)) .branch(case![Command::AddAccount].endpoint(commands::add_account)) .branch(case![Command::GetAccount].endpoint(commands::get_account)) @@ -58,8 +58,9 @@ fn get_dispatcher( let callback_handler = Update::filter_callback_query() .filter_map(CallbackCommand::from_query) - .branch(case![CallbackCommand::GetMenu].endpoint(callbacks::get_menu)) .branch(case![CallbackCommand::DeleteMessage].endpoint(callbacks::delete_message)) + .branch(master_password_check::get_handler::()) + .branch(case![CallbackCommand::GetMenu].endpoint(callbacks::get_menu)) .branch(case![CallbackCommand::Get(hash)].endpoint(callbacks::get)) .branch(case![CallbackCommand::Decrypt(hash)].endpoint(callbacks::decrypt)) .branch( diff --git a/src/master_password_check.rs b/src/master_password_check.rs index e9db77b..23eac81 100644 --- a/src/master_password_check.rs +++ b/src/master_password_check.rs @@ -1,6 +1,26 @@ use crate::prelude::*; use std::sync::Arc; -use teloxide::{dispatching::DpHandlerDescription, dptree::Handler}; +use teloxide::{ + dispatching::{dialogue::GetChatId, DpHandlerDescription}, + dptree::Handler, + types::User, +}; + +pub trait GetUserInfo: GetChatId + Clone + Send + Sync + 'static { + fn user(&self) -> Option<&User>; +} + +impl GetUserInfo for Message { + fn user(&self) -> Option<&User> { + self.from() + } +} + +impl GetUserInfo for CallbackQuery { + fn user(&self) -> Option<&User> { + Some(&self.from) + } +} /// A wierd filter that checks for the existance of a master password. /// @@ -9,11 +29,13 @@ use teloxide::{dispatching::DpHandlerDescription, dptree::Handler}; /// Returns None if account exists, Some(None) if there's an account and Some(Some(String)) if an error occures. /// The String represents the error that occured #[inline] -async fn master_pass_exists( - msg: Message, +async fn master_pass_exists( + msg: T, db: DatabaseConnection, ) -> Option>> { - let user_id = match msg.from() { + msg.chat_id()?; + + let user_id = match msg.user() { Some(user) => user.id.0, None => return Some(Some(Arc::new(NoUserInfo))), }; @@ -25,16 +47,16 @@ async fn master_pass_exists( } #[inline] -async fn notify_about_no_master_pass( +async fn notify_about_no_master_pass( bot: Throttle, result: Option>, - msg: Message, + msg: T, ) -> crate::Result<()> { if let Some(error) = result { return Err(error.into()); } bot.send_message( - msg.chat.id, + msg.chat_id().unwrap(), "No master password set. Use /set_master_pass to set it", ) .reply_markup(deletion_markup()) @@ -44,6 +66,7 @@ async fn notify_about_no_master_pass( /// Gets a handler that filters out the messages of users that don't have a master password set #[inline] -pub fn get_handler() -> Handler<'static, DependencyMap, crate::Result<()>, DpHandlerDescription> { - dptree::filter_map_async(master_pass_exists).endpoint(notify_about_no_master_pass) +pub fn get_handler( +) -> Handler<'static, DependencyMap, crate::Result<()>, DpHandlerDescription> { + dptree::filter_map_async(master_pass_exists::).endpoint(notify_about_no_master_pass::) }