Added an early master password check for the commands that rely on its existance
This commit is contained in:
		
							
								
								
									
										32
									
								
								src/handlers/master_password_check.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								src/handlers/master_password_check.rs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,32 @@
 | 
			
		||||
use crate::{entity::prelude::MasterPass, errors::NoUserInfo};
 | 
			
		||||
use sea_orm::prelude::*;
 | 
			
		||||
use teloxide::{adaptors::Throttle, dispatching::DpHandlerDescription, prelude::*};
 | 
			
		||||
 | 
			
		||||
async fn master_pass_exists(msg: Message, db: DatabaseConnection) -> Option<Option<String>> {
 | 
			
		||||
    let user_id = match msg.from() {
 | 
			
		||||
        Some(user) => user.id.0,
 | 
			
		||||
        None => return Some(Some(NoUserInfo.to_string())),
 | 
			
		||||
    };
 | 
			
		||||
    match MasterPass::exists(user_id, &db).await {
 | 
			
		||||
        Ok(true) => None,
 | 
			
		||||
        Ok(false) => Some(None),
 | 
			
		||||
        Err(err) => Some(Some(err.to_string())),
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
async fn notify_about_no_master_pass(
 | 
			
		||||
    bot: Throttle<Bot>,
 | 
			
		||||
    result: Option<String>,
 | 
			
		||||
    msg: Message,
 | 
			
		||||
) -> crate::Result<()> {
 | 
			
		||||
    if let Some(message) = result {
 | 
			
		||||
        return Err(crate::Error::msg(message));
 | 
			
		||||
    }
 | 
			
		||||
    bot.send_message(msg.chat.id, "No master password set")
 | 
			
		||||
        .await?;
 | 
			
		||||
    Ok(())
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub fn get_handler() -> Handler<'static, DependencyMap, crate::Result<()>, DpHandlerDescription> {
 | 
			
		||||
    dptree::filter_map_async(master_pass_exists).endpoint(notify_about_no_master_pass)
 | 
			
		||||
}
 | 
			
		||||
@@ -1,6 +1,7 @@
 | 
			
		||||
mod callbacks;
 | 
			
		||||
mod commands;
 | 
			
		||||
mod markups;
 | 
			
		||||
mod master_password_check;
 | 
			
		||||
mod state;
 | 
			
		||||
mod utils;
 | 
			
		||||
 | 
			
		||||
@@ -75,14 +76,16 @@ pub fn get_dispatcher(
 | 
			
		||||
    db: DatabaseConnection,
 | 
			
		||||
) -> Dispatcher<Throttle<Bot>, crate::Error, teloxide::dispatching::DefaultKey> {
 | 
			
		||||
    use dptree::{case, deps, endpoint};
 | 
			
		||||
 | 
			
		||||
    let bot = Bot::new(token).throttle(Limits::default());
 | 
			
		||||
 | 
			
		||||
    let command_handler = filter_command::<Command, _>()
 | 
			
		||||
        .branch(case![Command::Help].endpoint(commands::help))
 | 
			
		||||
        .branch(case![Command::SetMasterPass].endpoint(commands::set_master_pass))
 | 
			
		||||
        .branch(master_password_check::get_handler())
 | 
			
		||||
        .branch(case![Command::AddAccount].endpoint(commands::add_account))
 | 
			
		||||
        .branch(case![Command::GetAccount].endpoint(commands::get_account))
 | 
			
		||||
        .branch(case![Command::GetAccounts].endpoint(commands::get_accounts))
 | 
			
		||||
        .branch(case![Command::SetMasterPass].endpoint(commands::set_master_pass))
 | 
			
		||||
        .branch(case![Command::Delete].endpoint(commands::delete))
 | 
			
		||||
        .branch(case![Command::DeleteAll].endpoint(commands::delete_all))
 | 
			
		||||
        .branch(case![Command::Export].endpoint(commands::export))
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,5 @@
 | 
			
		||||
use super::{Handler, MainDialogue, PackagedHandler};
 | 
			
		||||
use sea_orm::DatabaseConnection;
 | 
			
		||||
use crate::handlers::{Handler, MainDialogue, PackagedHandler};
 | 
			
		||||
use sea_orm::prelude::*;
 | 
			
		||||
use std::{future::Future, sync::Arc};
 | 
			
		||||
use teloxide::{adaptors::Throttle, prelude::*};
 | 
			
		||||
use tokio::sync::Mutex;
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user