Added a check for master password existance for callbacks

This commit is contained in:
StNicolay 2023-08-11 16:00:35 +03:00
parent 79fce0a58f
commit 4fbb0d101e
Signed by: StNicolay
GPG Key ID: 9693D04DCD962B0D
2 changed files with 35 additions and 11 deletions

View File

@ -35,7 +35,7 @@ fn get_dispatcher(
.branch(case![Command::GenPassword].endpoint(commands::gen_password)) .branch(case![Command::GenPassword].endpoint(commands::gen_password))
.branch(case![Command::Cancel].endpoint(commands::cancel)) .branch(case![Command::Cancel].endpoint(commands::cancel))
// This branch filters out the users that don't have a master password set // 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::<Message>())
.branch(case![Command::Menu].endpoint(commands::menu)) .branch(case![Command::Menu].endpoint(commands::menu))
.branch(case![Command::AddAccount].endpoint(commands::add_account)) .branch(case![Command::AddAccount].endpoint(commands::add_account))
.branch(case![Command::GetAccount].endpoint(commands::get_account)) .branch(case![Command::GetAccount].endpoint(commands::get_account))
@ -58,8 +58,9 @@ fn get_dispatcher(
let callback_handler = Update::filter_callback_query() let callback_handler = Update::filter_callback_query()
.filter_map(CallbackCommand::from_query) .filter_map(CallbackCommand::from_query)
.branch(case![CallbackCommand::GetMenu].endpoint(callbacks::get_menu))
.branch(case![CallbackCommand::DeleteMessage].endpoint(callbacks::delete_message)) .branch(case![CallbackCommand::DeleteMessage].endpoint(callbacks::delete_message))
.branch(master_password_check::get_handler::<CallbackQuery>())
.branch(case![CallbackCommand::GetMenu].endpoint(callbacks::get_menu))
.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( .branch(

View File

@ -1,6 +1,26 @@
use crate::prelude::*; use crate::prelude::*;
use std::sync::Arc; 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. /// 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. /// 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 /// The String represents the error that occured
#[inline] #[inline]
async fn master_pass_exists( async fn master_pass_exists<T: GetUserInfo>(
msg: Message, msg: T,
db: DatabaseConnection, db: DatabaseConnection,
) -> Option<Option<Arc<dyn std::error::Error + Send + Sync>>> { ) -> Option<Option<Arc<dyn std::error::Error + Send + Sync>>> {
let user_id = match msg.from() { msg.chat_id()?;
let user_id = match msg.user() {
Some(user) => user.id.0, Some(user) => user.id.0,
None => return Some(Some(Arc::new(NoUserInfo))), None => return Some(Some(Arc::new(NoUserInfo))),
}; };
@ -25,16 +47,16 @@ async fn master_pass_exists(
} }
#[inline] #[inline]
async fn notify_about_no_master_pass( async fn notify_about_no_master_pass<T: GetChatId>(
bot: Throttle<Bot>, bot: Throttle<Bot>,
result: Option<Arc<dyn std::error::Error + Send + Sync>>, result: Option<Arc<dyn std::error::Error + Send + Sync>>,
msg: Message, msg: T,
) -> crate::Result<()> { ) -> crate::Result<()> {
if let Some(error) = result { if let Some(error) = result {
return Err(error.into()); return Err(error.into());
} }
bot.send_message( bot.send_message(
msg.chat.id, msg.chat_id().unwrap(),
"No master password set. Use /set_master_pass to set it", "No master password set. Use /set_master_pass to set it",
) )
.reply_markup(deletion_markup()) .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 /// Gets a handler that filters out the messages of users that don't have a master password set
#[inline] #[inline]
pub fn get_handler() -> Handler<'static, DependencyMap, crate::Result<()>, DpHandlerDescription> { pub fn get_handler<T: GetUserInfo>(
dptree::filter_map_async(master_pass_exists).endpoint(notify_about_no_master_pass) ) -> Handler<'static, DependencyMap, crate::Result<()>, DpHandlerDescription> {
dptree::filter_map_async(master_pass_exists::<T>).endpoint(notify_about_no_master_pass::<T>)
} }