Moved Handler and PackagedHandler into state module, converted utils::package_handler into Handler::new

This commit is contained in:
StNicolay 2023-05-23 21:09:28 +03:00
parent a271b72038
commit 58d1cd56ab
Signed by: StNicolay
GPG Key ID: 9693D04DCD962B0D
17 changed files with 81 additions and 88 deletions

View File

@ -1,7 +1,7 @@
use crate::{
entity::prelude::Account,
errors::NoUserInfo,
handlers::{markups::deletion_markup, utils::package_handler, MainDialogue, State},
handlers::{markups::deletion_markup, Handler, MainDialogue, State},
models::DecryptedAccount,
};
use sea_orm::prelude::*;
@ -46,7 +46,7 @@ async fn get_password(
password,
};
dialogue
.update(State::GetMasterPass(package_handler(
.update(State::GetMasterPass(Handler::new(
move |bot, msg, db, dialogue, master_pass| {
get_master_pass(bot, msg, db, dialogue, account, master_pass)
},
@ -67,7 +67,7 @@ async fn get_login(
) -> crate::Result<()> {
let previous = bot.send_message(msg.chat.id, "Send password").await?;
dialogue
.update(State::GetPassword(package_handler(
.update(State::GetPassword(Handler::new(
move |bot, msg, db, dialogue, password| {
get_password(bot, msg, db, dialogue, name, login, password)
},
@ -94,7 +94,7 @@ async fn get_account_name(
}
let previous = bot.send_message(msg.chat.id, "Send login").await?;
dialogue
.update(State::GetLogin(package_handler(
.update(State::GetLogin(Handler::new(
move |bot, msg, db, dialogue, login| get_login(bot, msg, db, dialogue, name, login),
previous,
)))
@ -110,7 +110,7 @@ pub async fn add_account(
) -> crate::Result<()> {
let previous = bot.send_message(msg.chat.id, "Send account name").await?;
dialogue
.update(State::GetAccountName(package_handler(
.update(State::GetAccountName(Handler::new(
get_account_name,
previous,
)))

View File

@ -3,8 +3,7 @@ use crate::{
errors::NoUserInfo,
handlers::{
markups::{self, deletion_markup},
utils::package_handler,
MainDialogue, State,
Handler, MainDialogue, State,
},
};
use sea_orm::prelude::*;
@ -47,7 +46,7 @@ async fn get_account_name(
.send_message(msg.chat.id, "Send master password. Once you send correct master password the account is unrecoverable")
.await?;
dialogue
.update(State::GetMasterPass(package_handler(
.update(State::GetMasterPass(Handler::new(
move |bot, msg, db, dialogue, _| get_master_pass(bot, msg, db, dialogue, name),
previous,
)))
@ -69,7 +68,7 @@ pub async fn delete(
.reply_markup(markup)
.await?;
dialogue
.update(State::GetAccountName(package_handler(
.update(State::GetAccountName(Handler::new(
get_account_name,
previous,
)))

View File

@ -1,7 +1,7 @@
use crate::{
entity::prelude::*,
errors::NoUserInfo,
handlers::{markups::deletion_markup, utils::package_handler, MainDialogue, State},
handlers::{markups::deletion_markup, Handler, MainDialogue, State},
};
use sea_orm::prelude::*;
use teloxide::{adaptors::Throttle, prelude::*};
@ -38,7 +38,7 @@ pub async fn delete_all(
)
.await?;
dialogue
.update(State::GetMasterPass(package_handler(
.update(State::GetMasterPass(Handler::new(
get_master_pass,
previous,
)))

View File

@ -1,7 +1,7 @@
use crate::{
entity::prelude::Account,
errors::NoUserInfo,
handlers::{markups::deletion_markup, utils::package_handler, MainDialogue, State},
handlers::{markups::deletion_markup, Handler, MainDialogue, State},
models::{DecryptedAccount, User},
};
use futures::TryStreamExt;
@ -55,7 +55,7 @@ pub async fn export(bot: Throttle<Bot>, msg: Message, dialogue: MainDialogue) ->
.send_message(msg.chat.id, "Send a master password to export the accounts")
.await?;
dialogue
.update(State::GetMasterPass(package_handler(
.update(State::GetMasterPass(Handler::new(
move |bot, msg, db, dialogue, master_pass| {
get_master_pass(bot, msg, db, dialogue, master_pass)
},

View File

@ -3,8 +3,7 @@ use crate::{
errors::NoUserInfo,
handlers::{
markups::{self, deletion_markup},
utils::package_handler,
MainDialogue, State,
Handler, MainDialogue, State,
},
};
use sea_orm::prelude::*;
@ -59,7 +58,7 @@ async fn get_account_name(
.send_message(msg.chat.id, "Send master password")
.await?;
dialogue
.update(State::GetMasterPass(package_handler(
.update(State::GetMasterPass(Handler::new(
move |bot, msg, db, dialogue, master_pass| {
get_master_pass(bot, msg, db, dialogue, name, master_pass)
},
@ -83,7 +82,7 @@ pub async fn get_account(
.reply_markup(markup)
.await?;
dialogue
.update(State::GetAccountName(package_handler(
.update(State::GetAccountName(Handler::new(
get_account_name,
previous,
)))

View File

@ -1,6 +1,6 @@
use crate::{
errors::NoUserInfo,
handlers::{markups::deletion_markup, utils::package_handler, MainDialogue, State},
handlers::{markups::deletion_markup, Handler, MainDialogue, State},
models::{DecryptedAccount, User},
};
use futures::{future, stream::FuturesUnordered, StreamExt, TryStreamExt};
@ -105,7 +105,7 @@ async fn get_document(
.send_message(msg.chat.id, "Send master password")
.await?;
dialogue
.update(State::GetMasterPass(package_handler(
.update(State::GetMasterPass(Handler::new(
move |bot, msg, db, dialogue, master_pass| {
get_master_pass(bot, msg, db, dialogue, master_pass, accounts)
},
@ -124,7 +124,7 @@ pub async fn import(bot: Throttle<Bot>, msg: Message, dialogue: MainDialogue) ->
)
.await?;
dialogue
.update(State::GetDocument(package_handler(get_document, previous)))
.update(State::GetDocument(Handler::new(get_document, previous)))
.await?;
Ok(())
}

View File

@ -1,7 +1,7 @@
use crate::{
entity::prelude::*,
errors::NoUserInfo,
handlers::{markups::deletion_markup, utils::package_handler, MainDialogue, State},
handlers::{markups::deletion_markup, Handler, MainDialogue, State},
};
use sea_orm::prelude::*;
use teloxide::{adaptors::Throttle, prelude::*};
@ -46,10 +46,7 @@ pub async fn set_master_pass(
.send_message(msg.chat.id, "Send new master password")
.await?;
dialogue
.update(State::GetPassword(package_handler(
get_master_pass,
previous,
)))
.update(State::GetPassword(Handler::new(get_master_pass, previous)))
.await?;
Ok(())
}

View File

@ -6,7 +6,7 @@ mod state;
mod utils;
use sea_orm::prelude::*;
use std::sync::Arc;
use state::{Handler, PackagedHandler};
use teloxide::{
adaptors::{throttle::Limits, Throttle},
dispatching::dialogue::InMemStorage,
@ -14,29 +14,9 @@ use teloxide::{
prelude::*,
utils::command::BotCommands,
};
use tokio::sync::Mutex;
type MainDialogue = Dialogue<State, InMemStorage<State>>;
#[allow(clippy::type_complexity)]
pub struct Handler<T> {
pub handler: Box<
dyn FnOnce(
Throttle<Bot>,
Message,
DatabaseConnection,
MainDialogue,
T,
) -> crate::PinnedFuture<'static, crate::Result<()>>
+ Send
+ Sync,
>,
pub previous: Option<Message>,
}
type PackagedHandler<T> = Arc<Mutex<Option<Handler<T>>>>;
#[derive(BotCommands, Clone, Copy)]
#[command(
rename_rule = "snake_case",

View File

@ -14,7 +14,7 @@ pub async fn generic<F>(
db: DatabaseConnection,
dialogue: crate::handlers::MainDialogue,
check: F,
next: crate::handlers::PackagedHandler<String>,
next: super::PackagedHandler<String>,
) -> crate::Result<()>
where
for<'a> F: FnOnce(

View File

@ -1,17 +1,14 @@
use crate::handlers::{markups::deletion_markup, utils::validate_field, MainDialogue};
use sea_orm::prelude::*;
use teloxide::{adaptors::Throttle, prelude::*};
use crate::handlers::{
markups::deletion_markup, utils::validate_field, MainDialogue, PackagedHandler,
};
/// Function to handle GetAccountName state
pub async fn get_account_name(
bot: Throttle<Bot>,
msg: Message,
db: DatabaseConnection,
dialogue: MainDialogue,
next: PackagedHandler<String>,
next: super::PackagedHandler<String>,
) -> crate::Result<()> {
super::generic::generic(
bot,

View File

@ -1,6 +1,6 @@
use crate::{
errors::HandlerUsed,
handlers::{markups::deletion_markup, utils::delete_optional, MainDialogue, PackagedHandler},
handlers::{markups::deletion_markup, utils::delete_optional, MainDialogue},
};
use sea_orm::prelude::*;
use teloxide::{adaptors::Throttle, prelude::*};
@ -11,7 +11,7 @@ pub async fn get_document(
msg: Message,
db: DatabaseConnection,
dialogue: MainDialogue,
next: PackagedHandler<()>,
next: super::PackagedHandler<()>,
) -> crate::Result<()> {
let handler = next.lock().await.take();
let previous = handler.as_ref().and_then(|h| h.previous.as_ref());

View File

@ -1,17 +1,14 @@
use crate::handlers::{markups::deletion_markup, utils::validate_field, MainDialogue};
use sea_orm::prelude::*;
use teloxide::{adaptors::Throttle, prelude::*};
use crate::handlers::{
markups::deletion_markup, utils::validate_field, MainDialogue, PackagedHandler,
};
/// Function to handle GetLogin state
pub async fn get_login(
bot: Throttle<Bot>,
msg: Message,
db: DatabaseConnection,
dialogue: MainDialogue,
next: PackagedHandler<String>,
next: super::PackagedHandler<String>,
) -> crate::Result<()> {
super::generic::generic(
bot,

View File

@ -1,7 +1,7 @@
use crate::{
entity::prelude::MasterPass,
errors::{NoUserInfo},
handlers::{markups::deletion_markup, MainDialogue, PackagedHandler},
errors::NoUserInfo,
handlers::{markups::deletion_markup, MainDialogue},
};
use sea_orm::prelude::*;
use teloxide::{adaptors::Throttle, prelude::*};
@ -39,7 +39,7 @@ pub async fn get_master_pass(
msg: Message,
db: DatabaseConnection,
dialogue: MainDialogue,
next: PackagedHandler<String>,
next: super::PackagedHandler<String>,
) -> crate::Result<()> {
super::generic::generic(
bot,

View File

@ -1,9 +1,7 @@
use sea_orm::prelude::*;
use teloxide::{adaptors::Throttle, prelude::*};
use crate::handlers::{
markups::deletion_markup, utils::validate_field, MainDialogue, PackagedHandler,
};
use crate::handlers::{markups::deletion_markup, utils::validate_field, MainDialogue};
/// Function to handle GetPassword state
pub async fn get_password(
@ -11,7 +9,7 @@ pub async fn get_password(
msg: Message,
db: DatabaseConnection,
dialogue: MainDialogue,
next: PackagedHandler<String>,
next: super::PackagedHandler<String>,
) -> crate::Result<()> {
super::generic::generic(
bot,

View File

@ -0,0 +1,45 @@
use crate::handlers::MainDialogue;
use sea_orm::prelude::*;
use std::{future::Future, sync::Arc};
use teloxide::{adaptors::Throttle, prelude::*};
use tokio::sync::Mutex;
#[allow(clippy::type_complexity)]
pub struct Handler<T> {
pub handler: Box<
dyn FnOnce(
Throttle<Bot>,
Message,
DatabaseConnection,
MainDialogue,
T,
) -> crate::PinnedFuture<'static, crate::Result<()>>
+ Send
+ Sync,
>,
pub previous: Option<Message>,
}
pub type PackagedHandler<T> = Arc<Mutex<Option<Handler<T>>>>;
impl<T> Handler<T> {
/// Convinience method to convert a simple async function and a previous message into PackagedHandler
#[inline]
pub fn new<H, F>(f: H, previous: impl Into<Option<Message>>) -> PackagedHandler<T>
where
H: FnOnce(Throttle<Bot>, Message, DatabaseConnection, MainDialogue, T) -> F
+ Send
+ Sync
+ 'static,
F: Future<Output = crate::Result<()>> + Send + 'static,
{
let handler = Self {
handler: Box::new(move |bot, msg, db, dialogue, val| {
Box::pin(f(bot, msg, db, dialogue, val))
}),
previous: previous.into(),
};
Arc::new(Mutex::new(Some(handler)))
}
}

View File

@ -6,9 +6,11 @@ mod get_document;
mod get_login;
mod get_master_pass;
mod get_password;
mod handler;
pub use get_account_name::get_account_name;
pub use get_document::get_document;
pub use get_login::get_login;
pub use get_master_pass::get_master_pass;
pub use get_password::get_password;
pub use handler::{Handler, PackagedHandler};

View File

@ -1,26 +1,5 @@
use crate::handlers::{Handler, MainDialogue, PackagedHandler};
use sea_orm::prelude::*;
use std::{borrow::Borrow, future::Future, sync::Arc};
use std::borrow::Borrow;
use teloxide::{adaptors::Throttle, prelude::*};
use tokio::sync::Mutex;
/// Convinience method to convert a simple async function and a previous message into PackagedHandler
#[inline]
pub fn package_handler<H, F, T>(f: H, previous: impl Into<Option<Message>>) -> PackagedHandler<T>
where
H: FnOnce(Throttle<Bot>, Message, DatabaseConnection, MainDialogue, T) -> F
+ Send
+ Sync
+ 'static,
F: Future<Output = crate::Result<()>> + Send + 'static,
{
Arc::new(Mutex::new(Some(Handler {
handler: Box::new(move |bot, msg, db, dialogue, val| {
Box::pin(f(bot, msg, db, dialogue, val))
}),
previous: previous.into(),
})))
}
/// Deletes the message ignoring the errors
#[inline]