Started simplifying code using macros

This commit is contained in:
StNicolay 2023-07-16 22:34:11 +03:00
parent 1225adefc2
commit 255f29bfad
Signed by: StNicolay
GPG Key ID: 9693D04DCD962B0D
9 changed files with 63 additions and 191 deletions

View File

@ -2,17 +2,23 @@ use crate::prelude::*;
use tokio::task::spawn_blocking; use tokio::task::spawn_blocking;
/// Gets the name of the master password, encryptes the account and adds it to the DB /// 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( async fn get_master_pass(
bot: Throttle<Bot>, bot: Throttle<Bot>,
msg: Message, msg: Message,
db: DatabaseConnection, db: DatabaseConnection,
dialogue: MainDialogue, dialogue: MainDialogue,
account: DecryptedAccount, name: String,
login: String,
password: String,
master_pass: String, master_pass: String,
) -> crate::Result<()> { ) -> crate::Result<()> {
let user_id = msg.from().ok_or(NoUserInfo)?.id.0; let user_id = msg.from().ok_or(NoUserInfo)?.id.0;
dialogue.exit().await?; 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?; account.insert(&db).await?;
bot.send_message(msg.chat.id, "Success") bot.send_message(msg.chat.id, "Success")
.reply_markup(deletion_markup()) .reply_markup(deletion_markup())
@ -20,83 +26,16 @@ async fn get_master_pass(
Ok(()) Ok(())
} }
/// Gets the password of the account to add handler!(
async fn get_password( get_password(name:String, login: String, password: String),
bot: Throttle<Bot>, "Send master password",
msg: Message, State::GetMasterPass,
_: DatabaseConnection, get_master_pass
dialogue: MainDialogue, );
name: String, handler!(get_login(name: String, login: String),
login: String, "Send password",
password: String, State::GetPassword,
) -> crate::Result<()> { get_password
let previous = bot );
.send_message(msg.chat.id, "Send master password") handler!(get_account_name(name: String), "Send login", State::GetLogin, get_login);
.await?; handler!(pub add_account(), "Send account name", State::GetNewName, get_account_name);
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<Bot>,
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<Bot>,
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<Bot>,
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(())
}

View File

@ -8,6 +8,7 @@ async fn get_master_pass(
db: DatabaseConnection, db: DatabaseConnection,
dialogue: MainDialogue, dialogue: MainDialogue,
name: String, name: String,
_: String,
) -> crate::Result<()> { ) -> crate::Result<()> {
dialogue.exit().await?; dialogue.exit().await?;
let user_id = msg.from().ok_or(NoUserInfo)?.id.0; let user_id = msg.from().ok_or(NoUserInfo)?.id.0;
@ -18,25 +19,12 @@ async fn get_master_pass(
Ok(()) Ok(())
} }
/// Gets the name of the account to delete handler!(
async fn get_account_name( get_account_name(name: String),
bot: Throttle<Bot>, "Send master password. Once you send correct master password the account is unrecoverable",
msg: Message, State::GetMasterPass,
_: DatabaseConnection, get_master_pass
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(())
}
/// Handles /delete command /// Handles /delete command
pub async fn delete( pub async fn delete(

View File

@ -25,23 +25,9 @@ async fn get_master_pass(
Ok(()) Ok(())
} }
/// Handles /delete_all command handler!(
pub async fn delete_all( pub delete_all(),
bot: Throttle<Bot>, "Send master password to delete EVERYTHING.\nTHIS ACTION IS IRREVERSIBLE",
msg: Message, State::GetMasterPass,
dialogue: MainDialogue, get_master_pass
) -> 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(())
}

View File

@ -53,16 +53,4 @@ async fn get_master_pass(
Ok(()) Ok(())
} }
/// Handles /export command handler!(pub export(), "Send the master password to export your accounts", State::GetMasterPass, get_master_pass);
pub async fn export(bot: Throttle<Bot>, 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(())
}

View File

@ -31,27 +31,7 @@ async fn get_master_pass(
Ok(()) Ok(())
} }
/// Gets the account name to get handler!(get_account_name(name:String), "Send master password", State::GetMasterPass, get_master_pass);
async fn get_account_name(
bot: Throttle<Bot>,
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(())
}
/// Handles /get_account command /// Handles /get_account command
pub async fn get_account( pub async fn get_account(

View File

@ -31,8 +31,8 @@ async fn get_master_pass(
msg: Message, msg: Message,
db: DatabaseConnection, db: DatabaseConnection,
dialogue: MainDialogue, dialogue: MainDialogue,
master_pass: String,
user: User, user: User,
master_pass: String,
) -> crate::Result<()> { ) -> crate::Result<()> {
let user_id = msg.from().ok_or(NoUserInfo)?.id.0; let user_id = msg.from().ok_or(NoUserInfo)?.id.0;
let mut failed = Vec::new(); let mut failed = Vec::new();
@ -63,38 +63,5 @@ async fn get_master_pass(
Ok(()) Ok(())
} }
/// Downloads and parses the json and asks for the master password handler!(get_user(user: User), "Send master password", State::GetMasterPass, get_master_pass);
async fn get_document( handler!(pub import(), "Send a json document with the same format as created by /export", State::GetUser, get_user);
bot: Throttle<Bot>,
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<Bot>, 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(())
}

22
src/macros.rs Normal file
View File

@ -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<Bot>,
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(())
}
};
}

View File

@ -2,6 +2,7 @@ mod callbacks;
mod commands; mod commands;
mod default; mod default;
mod errors; mod errors;
mod macros;
mod markups; mod markups;
mod master_password_check; mod master_password_check;
mod models; mod models;

View File

@ -1,6 +1,7 @@
pub(crate) use crate::{ pub(crate) use crate::{
commands::Command, commands::Command,
errors::*, errors::*,
handler,
markups::*, markups::*,
models::*, models::*,
state::State, state::State,