diff --git a/src/commands/add_account.rs b/src/commands/add_account.rs index e6eed26..1634b21 100644 --- a/src/commands/add_account.rs +++ b/src/commands/add_account.rs @@ -2,17 +2,23 @@ use crate::prelude::*; use tokio::task::spawn_blocking; /// Gets the name of the master password, encryptes the account and adds it to the DB +#[allow(clippy::too_many_arguments)] async fn get_master_pass( bot: Throttle, msg: Message, db: DatabaseConnection, dialogue: MainDialogue, - account: DecryptedAccount, + name: String, + login: String, + password: String, master_pass: String, ) -> crate::Result<()> { let user_id = msg.from().ok_or(NoUserInfo)?.id.0; dialogue.exit().await?; - let account = spawn_blocking(move || account.into_account(user_id, &master_pass)).await??; + let account = spawn_blocking(move || { + account::ActiveModel::from_unencrypted(user_id, name, &login, &password, &master_pass) + }) + .await??; account.insert(&db).await?; bot.send_message(msg.chat.id, "Success") .reply_markup(deletion_markup()) @@ -20,83 +26,16 @@ async fn get_master_pass( Ok(()) } -/// Gets the password of the account to add -async fn get_password( - bot: Throttle, - msg: Message, - _: DatabaseConnection, - dialogue: MainDialogue, - name: String, - login: String, - password: String, -) -> crate::Result<()> { - let previous = bot - .send_message(msg.chat.id, "Send master password") - .await?; - let account = DecryptedAccount { - name, - login, - password, - }; - dialogue - .update(State::GetMasterPass(Handler::new( - move |bot, msg, db, dialogue, master_pass| { - get_master_pass(bot, msg, db, dialogue, account, master_pass) - }, - &previous, - ))) - .await?; - Ok(()) -} - -/// Gets the login of the account to add -async fn get_login( - bot: Throttle, - msg: Message, - _: DatabaseConnection, - dialogue: MainDialogue, - name: String, - login: String, -) -> crate::Result<()> { - let previous = bot.send_message(msg.chat.id, "Send password").await?; - dialogue - .update(State::GetPassword(Handler::new( - move |bot, msg, db, dialogue, password| { - get_password(bot, msg, db, dialogue, name, login, password) - }, - &previous, - ))) - .await?; - Ok(()) -} - -/// Gets the name of the account to add and checks that there're no accounts with the same name -async fn get_account_name( - bot: Throttle, - msg: Message, - _: DatabaseConnection, - dialogue: MainDialogue, - name: String, -) -> crate::Result<()> { - let previous = bot.send_message(msg.chat.id, "Send login").await?; - dialogue - .update(State::GetLogin(Handler::new( - move |bot, msg, db, dialogue, login| get_login(bot, msg, db, dialogue, name, login), - &previous, - ))) - .await?; - Ok(()) -} - -/// Handles /add_account -pub async fn add_account( - bot: Throttle, - msg: Message, - dialogue: MainDialogue, -) -> crate::Result<()> { - let previous = bot.send_message(msg.chat.id, "Send account name").await?; - dialogue - .update(State::GetNewName(Handler::new(get_account_name, &previous))) - .await?; - Ok(()) -} +handler!( + get_password(name:String, login: String, password: String), + "Send master password", + State::GetMasterPass, + get_master_pass +); +handler!(get_login(name: String, login: String), + "Send password", + State::GetPassword, + get_password +); +handler!(get_account_name(name: String), "Send login", State::GetLogin, get_login); +handler!(pub add_account(), "Send account name", State::GetNewName, get_account_name); diff --git a/src/commands/delete.rs b/src/commands/delete.rs index 2154ed5..acbcb5d 100644 --- a/src/commands/delete.rs +++ b/src/commands/delete.rs @@ -8,6 +8,7 @@ async fn get_master_pass( db: DatabaseConnection, dialogue: MainDialogue, name: String, + _: String, ) -> crate::Result<()> { dialogue.exit().await?; let user_id = msg.from().ok_or(NoUserInfo)?.id.0; @@ -18,25 +19,12 @@ async fn get_master_pass( Ok(()) } -/// Gets the name of the account to delete -async fn get_account_name( - bot: Throttle, - msg: Message, - _: DatabaseConnection, - dialogue: MainDialogue, - name: String, -) -> crate::Result<()> { - let previous = bot - .send_message(msg.chat.id, "Send master password. Once you send correct master password the account is unrecoverable") - .await?; - dialogue - .update(State::GetMasterPass(Handler::new( - move |bot, msg, db, dialogue, _| get_master_pass(bot, msg, db, dialogue, name), - &previous, - ))) - .await?; - Ok(()) -} +handler!( + get_account_name(name: String), + "Send master password. Once you send correct master password the account is unrecoverable", + State::GetMasterPass, + get_master_pass +); /// Handles /delete command pub async fn delete( diff --git a/src/commands/delete_all.rs b/src/commands/delete_all.rs index 3b79247..bf4c5e0 100644 --- a/src/commands/delete_all.rs +++ b/src/commands/delete_all.rs @@ -25,23 +25,9 @@ async fn get_master_pass( Ok(()) } -/// Handles /delete_all command -pub async fn delete_all( - bot: Throttle, - msg: Message, - dialogue: MainDialogue, -) -> crate::Result<()> { - let previous = bot - .send_message( - msg.chat.id, - "Send master password to delete EVERYTHING.\nTHIS ACTION IS IRREVERSIBLE", - ) - .await?; - dialogue - .update(State::GetMasterPass(Handler::new( - get_master_pass, - &previous, - ))) - .await?; - Ok(()) -} +handler!( + pub delete_all(), + "Send master password to delete EVERYTHING.\nTHIS ACTION IS IRREVERSIBLE", + State::GetMasterPass, + get_master_pass +); diff --git a/src/commands/export.rs b/src/commands/export.rs index f1c659d..4ee67c0 100644 --- a/src/commands/export.rs +++ b/src/commands/export.rs @@ -53,16 +53,4 @@ async fn get_master_pass( Ok(()) } -/// Handles /export command -pub async fn export(bot: Throttle, msg: Message, dialogue: MainDialogue) -> crate::Result<()> { - let previous = bot - .send_message(msg.chat.id, "Send a master password to export the accounts") - .await?; - dialogue - .update(State::GetMasterPass(Handler::new( - get_master_pass, - &previous, - ))) - .await?; - Ok(()) -} +handler!(pub export(), "Send the master password to export your accounts", State::GetMasterPass, get_master_pass); diff --git a/src/commands/get_account.rs b/src/commands/get_account.rs index f5c39d3..bea2de7 100644 --- a/src/commands/get_account.rs +++ b/src/commands/get_account.rs @@ -31,27 +31,7 @@ async fn get_master_pass( Ok(()) } -/// Gets the account name to get -async fn get_account_name( - bot: Throttle, - msg: Message, - _: DatabaseConnection, - dialogue: MainDialogue, - name: String, -) -> crate::Result<()> { - let previous = bot - .send_message(msg.chat.id, "Send master password") - .await?; - dialogue - .update(State::GetMasterPass(Handler::new( - move |bot, msg, db, dialogue, master_pass| { - get_master_pass(bot, msg, db, dialogue, name, master_pass) - }, - &previous, - ))) - .await?; - Ok(()) -} +handler!(get_account_name(name:String), "Send master password", State::GetMasterPass, get_master_pass); /// Handles /get_account command pub async fn get_account( diff --git a/src/commands/import.rs b/src/commands/import.rs index 6a215cc..5942d9c 100644 --- a/src/commands/import.rs +++ b/src/commands/import.rs @@ -31,8 +31,8 @@ async fn get_master_pass( msg: Message, db: DatabaseConnection, dialogue: MainDialogue, - master_pass: String, user: User, + master_pass: String, ) -> crate::Result<()> { let user_id = msg.from().ok_or(NoUserInfo)?.id.0; let mut failed = Vec::new(); @@ -63,38 +63,5 @@ async fn get_master_pass( Ok(()) } -/// Downloads and parses the json and asks for the master password -async fn get_document( - bot: Throttle, - msg: Message, - _: DatabaseConnection, - dialogue: MainDialogue, - user: User, -) -> crate::Result<()> { - let previous = bot - .send_message(msg.chat.id, "Send master password") - .await?; - dialogue - .update(State::GetMasterPass(Handler::new( - move |bot, msg, db, dialogue, master_pass| { - get_master_pass(bot, msg, db, dialogue, master_pass, user) - }, - &previous, - ))) - .await?; - Ok(()) -} - -/// Handles /import command -pub async fn import(bot: Throttle, msg: Message, dialogue: MainDialogue) -> crate::Result<()> { - let previous = bot - .send_message( - msg.chat.id, - "Send a json document with the same format as created by /export", - ) - .await?; - dialogue - .update(State::GetUser(Handler::new(get_document, &previous))) - .await?; - Ok(()) -} +handler!(get_user(user: User), "Send master password", State::GetMasterPass, get_master_pass); +handler!(pub import(), "Send a json document with the same format as created by /export", State::GetUser, get_user); diff --git a/src/macros.rs b/src/macros.rs new file mode 100644 index 0000000..ca30a44 --- /dev/null +++ b/src/macros.rs @@ -0,0 +1,22 @@ +#[macro_export] +macro_rules! handler { + ($v: vis $function_name: ident ($($param: ident: $type: ty),*), $message: literal, $next_state: expr, $next_func: ident) => { + #[inline] + $v async fn $function_name( + bot: Throttle, + msg: Message, + _: DatabaseConnection, + dialogue: MainDialogue, + $($param: $type,)* + ) -> $crate::Result<()> { + let previous = bot.send_message(msg.chat.id, $message).await?; + dialogue + .update($next_state(Handler::new( + move |bot, msg, db, dialogue, param| $next_func(bot, msg, db, dialogue, $($param,)* param), + &previous, + ))) + .await?; + Ok(()) + } + }; +} diff --git a/src/main.rs b/src/main.rs index ea889a7..2634122 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ mod callbacks; mod commands; mod default; mod errors; +mod macros; mod markups; mod master_password_check; mod models; diff --git a/src/prelude.rs b/src/prelude.rs index 5e27f66..03a0344 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -1,6 +1,7 @@ pub(crate) use crate::{ commands::Command, errors::*, + handler, markups::*, models::*, state::State,