2023-06-29 14:33:26 +00:00
|
|
|
use crate::prelude::*;
|
2023-06-01 11:54:00 +00:00
|
|
|
use std::sync::Arc;
|
2023-08-11 13:00:35 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2023-05-07 11:24:58 +00:00
|
|
|
|
2023-05-09 17:27:58 +00:00
|
|
|
/// A wierd filter that checks for the existance of a master password.
|
|
|
|
///
|
|
|
|
/// # Returns
|
|
|
|
///
|
|
|
|
/// 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
|
2023-08-07 10:38:46 +00:00
|
|
|
#[inline]
|
2023-08-11 13:00:35 +00:00
|
|
|
async fn master_pass_exists<T: GetUserInfo>(
|
|
|
|
msg: T,
|
2023-06-01 11:54:00 +00:00
|
|
|
db: DatabaseConnection,
|
|
|
|
) -> Option<Option<Arc<dyn std::error::Error + Send + Sync>>> {
|
2023-08-11 13:00:35 +00:00
|
|
|
msg.chat_id()?;
|
|
|
|
|
|
|
|
let user_id = match msg.user() {
|
2023-05-07 11:24:58 +00:00
|
|
|
Some(user) => user.id.0,
|
2023-06-01 11:54:00 +00:00
|
|
|
None => return Some(Some(Arc::new(NoUserInfo))),
|
2023-05-07 11:24:58 +00:00
|
|
|
};
|
|
|
|
match MasterPass::exists(user_id, &db).await {
|
|
|
|
Ok(true) => None,
|
|
|
|
Ok(false) => Some(None),
|
2023-06-01 11:54:00 +00:00
|
|
|
Err(err) => Some(Some(Arc::new(err))),
|
2023-05-07 11:24:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-07 10:38:46 +00:00
|
|
|
#[inline]
|
2023-08-11 13:00:35 +00:00
|
|
|
async fn notify_about_no_master_pass<T: GetChatId>(
|
2023-05-07 11:24:58 +00:00
|
|
|
bot: Throttle<Bot>,
|
2023-06-01 11:54:00 +00:00
|
|
|
result: Option<Arc<dyn std::error::Error + Send + Sync>>,
|
2023-08-11 13:00:35 +00:00
|
|
|
msg: T,
|
2023-05-07 11:24:58 +00:00
|
|
|
) -> crate::Result<()> {
|
2023-06-01 11:54:00 +00:00
|
|
|
if let Some(error) = result {
|
|
|
|
return Err(error.into());
|
2023-05-07 11:24:58 +00:00
|
|
|
}
|
2023-07-29 12:59:11 +00:00
|
|
|
bot.send_message(
|
2023-08-11 13:00:35 +00:00
|
|
|
msg.chat_id().unwrap(),
|
2023-07-29 12:59:11 +00:00
|
|
|
"No master password set. Use /set_master_pass to set it",
|
|
|
|
)
|
|
|
|
.reply_markup(deletion_markup())
|
|
|
|
.await?;
|
2023-05-07 11:24:58 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-05-09 17:27:58 +00:00
|
|
|
/// Gets a handler that filters out the messages of users that don't have a master password set
|
2023-05-15 16:17:52 +00:00
|
|
|
#[inline]
|
2023-08-11 13:00:35 +00:00
|
|
|
pub fn get_handler<T: GetUserInfo>(
|
|
|
|
) -> Handler<'static, DependencyMap, crate::Result<()>, DpHandlerDescription> {
|
|
|
|
dptree::filter_map_async(master_pass_exists::<T>).endpoint(notify_about_no_master_pass::<T>)
|
2023-05-07 11:24:58 +00:00
|
|
|
}
|