2023-06-01 11:42:35 +00:00
|
|
|
use crate::errors::NoUserInfo;
|
|
|
|
use entity::prelude::*;
|
|
|
|
use sea_orm::DatabaseConnection;
|
2023-05-07 11:24:58 +00:00
|
|
|
use teloxide::{adaptors::Throttle, dispatching::DpHandlerDescription, prelude::*};
|
|
|
|
|
2023-05-07 13:37:56 +00:00
|
|
|
use super::markups::deletion_markup;
|
|
|
|
|
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-05-07 11:24:58 +00:00
|
|
|
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")
|
2023-05-07 13:37:56 +00:00
|
|
|
.reply_markup(deletion_markup())
|
2023-05-07 11:24:58 +00:00
|
|
|
.await?;
|
|
|
|
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-05-07 11:24:58 +00:00
|
|
|
pub fn get_handler() -> Handler<'static, DependencyMap, crate::Result<()>, DpHandlerDescription> {
|
|
|
|
dptree::filter_map_async(master_pass_exists).endpoint(notify_about_no_master_pass)
|
|
|
|
}
|