Moved dispatching into a sepparate module
This commit is contained in:
10
src/dispatching/default.rs
Normal file
10
src/dispatching/default.rs
Normal file
@@ -0,0 +1,10 @@
|
||||
use crate::prelude::*;
|
||||
|
||||
/// Handles the messages which weren't matched by any commands or states
|
||||
#[inline]
|
||||
pub async fn default(bot: Throttle<Bot>, msg: Message, locale: LocaleRef) -> crate::Result<()> {
|
||||
bot.send_message(msg.chat.id, &locale.unknown_command_use_help)
|
||||
.reply_markup(deletion_markup(locale))
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
7
src/dispatching/delete_mesage_handler.rs
Normal file
7
src/dispatching/delete_mesage_handler.rs
Normal file
@@ -0,0 +1,7 @@
|
||||
use crate::prelude::*;
|
||||
|
||||
/// Deletes the message ignoring the errors
|
||||
#[inline]
|
||||
pub async fn delete_message(bot: Throttle<Bot>, msg: Message) {
|
||||
let _ = bot.delete_message(msg.chat.id, msg.id).await;
|
||||
}
|
||||
54
src/dispatching/filter_user_info.rs
Normal file
54
src/dispatching/filter_user_info.rs
Normal file
@@ -0,0 +1,54 @@
|
||||
use crate::prelude::*;
|
||||
use teloxide::{dispatching::DpHandlerDescription, dptree::Handler};
|
||||
|
||||
#[inline]
|
||||
#[allow(clippy::needless_pass_by_value)]
|
||||
fn has_no_user_info(msg: Message) -> bool {
|
||||
msg.from().is_none()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
async fn notify_about_no_user_info(
|
||||
bot: Throttle<Bot>,
|
||||
msg: Message,
|
||||
state: State,
|
||||
locale: LocaleRef,
|
||||
) -> crate::Result<()> {
|
||||
let text = &locale.couldnt_get_user_info_send_again;
|
||||
|
||||
match state {
|
||||
State::Start => {
|
||||
bot.send_message(msg.chat.id, text)
|
||||
.reply_markup(deletion_markup(locale))
|
||||
.await?;
|
||||
}
|
||||
State::GetNewName(handler)
|
||||
| State::GetMasterPass(handler)
|
||||
| State::GetNewMasterPass(handler)
|
||||
| State::GetLogin(handler)
|
||||
| State::GetPassword(handler) => {
|
||||
handler
|
||||
.lock()
|
||||
.await
|
||||
.previous
|
||||
.alter_message(&bot, text, None, None)
|
||||
.await?;
|
||||
}
|
||||
State::GetUser(handler) => {
|
||||
handler
|
||||
.lock()
|
||||
.await
|
||||
.previous
|
||||
.alter_message(&bot, text, None, None)
|
||||
.await?;
|
||||
}
|
||||
};
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Gets a handler that filters out the messages without user information
|
||||
#[inline]
|
||||
pub fn get_handler() -> Handler<'static, DependencyMap, crate::Result<()>, DpHandlerDescription> {
|
||||
dptree::filter(has_no_user_info).endpoint(notify_about_no_user_info)
|
||||
}
|
||||
52
src/dispatching/master_password_check.rs
Normal file
52
src/dispatching/master_password_check.rs
Normal file
@@ -0,0 +1,52 @@
|
||||
use crate::prelude::*;
|
||||
use std::sync::Arc;
|
||||
use teloxide::{
|
||||
dispatching::{dialogue::GetChatId, DpHandlerDescription},
|
||||
dptree::Handler,
|
||||
};
|
||||
|
||||
type DynError = Arc<dyn std::error::Error + Send + Sync>;
|
||||
|
||||
/// Filters out the messages from users without master passwords
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// Returns None if account exists, Some(None) if there's an account and Some(Some(DynError)) if an error occures.
|
||||
/// The String represents the error that occured
|
||||
#[inline]
|
||||
async fn master_pass_exists(update: Update, db: Pool) -> Option<Option<DynError>> {
|
||||
let user_id = match update.user() {
|
||||
Some(user) => user.id.0,
|
||||
None => return Some(Some(Arc::new(NoUserInfo))),
|
||||
};
|
||||
match MasterPass::exists(user_id, &db).await {
|
||||
Ok(true) => None,
|
||||
Ok(false) => Some(None),
|
||||
Err(err) => Some(Some(Arc::new(err))),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
async fn notify_about_no_master_pass(
|
||||
bot: Throttle<Bot>,
|
||||
locale: LocaleRef,
|
||||
result: Option<DynError>,
|
||||
update: Update,
|
||||
) -> crate::Result<()> {
|
||||
if let Some(error) = result {
|
||||
return Err(error.into());
|
||||
}
|
||||
bot.send_message(
|
||||
update.chat_id().unwrap(),
|
||||
&locale.master_password_is_not_set,
|
||||
)
|
||||
.reply_markup(deletion_markup(locale))
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Gets a handler that filters out the messages of users that don't have a master password set
|
||||
#[inline]
|
||||
pub fn get_handler() -> Handler<'static, DependencyMap, crate::Result<()>, DpHandlerDescription> {
|
||||
dptree::filter_map_async(master_pass_exists).endpoint(notify_about_no_master_pass)
|
||||
}
|
||||
Reference in New Issue
Block a user