Added an early master password check for the commands that rely on its existance

This commit is contained in:
StNicolay 2023-05-07 14:24:58 +03:00
parent ab6e4d9233
commit 8d68cb5b2b
Signed by: StNicolay
GPG Key ID: 9693D04DCD962B0D
3 changed files with 38 additions and 3 deletions

View 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)
}

View File

@ -1,6 +1,7 @@
mod callbacks; mod callbacks;
mod commands; mod commands;
mod markups; mod markups;
mod master_password_check;
mod state; mod state;
mod utils; mod utils;
@ -75,14 +76,16 @@ pub fn get_dispatcher(
db: DatabaseConnection, db: DatabaseConnection,
) -> Dispatcher<Throttle<Bot>, crate::Error, teloxide::dispatching::DefaultKey> { ) -> Dispatcher<Throttle<Bot>, crate::Error, teloxide::dispatching::DefaultKey> {
use dptree::{case, deps, endpoint}; use dptree::{case, deps, endpoint};
let bot = Bot::new(token).throttle(Limits::default()); let bot = Bot::new(token).throttle(Limits::default());
let command_handler = filter_command::<Command, _>() let command_handler = filter_command::<Command, _>()
.branch(case![Command::Help].endpoint(commands::help)) .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::AddAccount].endpoint(commands::add_account))
.branch(case![Command::GetAccount].endpoint(commands::get_account)) .branch(case![Command::GetAccount].endpoint(commands::get_account))
.branch(case![Command::GetAccounts].endpoint(commands::get_accounts)) .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::Delete].endpoint(commands::delete))
.branch(case![Command::DeleteAll].endpoint(commands::delete_all)) .branch(case![Command::DeleteAll].endpoint(commands::delete_all))
.branch(case![Command::Export].endpoint(commands::export)) .branch(case![Command::Export].endpoint(commands::export))

View File

@ -1,5 +1,5 @@
use super::{Handler, MainDialogue, PackagedHandler}; use crate::handlers::{Handler, MainDialogue, PackagedHandler};
use sea_orm::DatabaseConnection; use sea_orm::prelude::*;
use std::{future::Future, sync::Arc}; use std::{future::Future, sync::Arc};
use teloxide::{adaptors::Throttle, prelude::*}; use teloxide::{adaptors::Throttle, prelude::*};
use tokio::sync::Mutex; use tokio::sync::Mutex;