Started simplifying code using macros
This commit is contained in:
parent
1225adefc2
commit
255f29bfad
@ -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<Bot>,
|
||||
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<Bot>,
|
||||
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<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(())
|
||||
}
|
||||
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);
|
||||
|
@ -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<Bot>,
|
||||
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(
|
||||
|
@ -25,23 +25,9 @@ async fn get_master_pass(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Handles /delete_all command
|
||||
pub async fn delete_all(
|
||||
bot: Throttle<Bot>,
|
||||
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
|
||||
);
|
||||
|
@ -53,16 +53,4 @@ async fn get_master_pass(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Handles /export command
|
||||
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(())
|
||||
}
|
||||
handler!(pub export(), "Send the master password to export your accounts", State::GetMasterPass, get_master_pass);
|
||||
|
@ -31,27 +31,7 @@ async fn get_master_pass(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Gets the account name to get
|
||||
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(())
|
||||
}
|
||||
handler!(get_account_name(name:String), "Send master password", State::GetMasterPass, get_master_pass);
|
||||
|
||||
/// Handles /get_account command
|
||||
pub async fn get_account(
|
||||
|
@ -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<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(())
|
||||
}
|
||||
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);
|
||||
|
22
src/macros.rs
Normal file
22
src/macros.rs
Normal 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(())
|
||||
}
|
||||
};
|
||||
}
|
@ -2,6 +2,7 @@ mod callbacks;
|
||||
mod commands;
|
||||
mod default;
|
||||
mod errors;
|
||||
mod macros;
|
||||
mod markups;
|
||||
mod master_password_check;
|
||||
mod models;
|
||||
|
@ -1,6 +1,7 @@
|
||||
pub(crate) use crate::{
|
||||
commands::Command,
|
||||
errors::*,
|
||||
handler,
|
||||
markups::*,
|
||||
models::*,
|
||||
state::State,
|
||||
|
Loading…
Reference in New Issue
Block a user