2023-05-06 18:02:32 +00:00
|
|
|
use crate::{
|
2023-05-07 10:20:11 +00:00
|
|
|
entity::prelude::Account,
|
2023-05-06 18:02:32 +00:00
|
|
|
errors::NoUserInfo,
|
|
|
|
handlers::{markups::deletion_markup, utils::package_handler, MainDialogue, State},
|
2023-05-07 10:20:11 +00:00
|
|
|
models::DecryptedAccount,
|
2023-05-06 18:02:32 +00:00
|
|
|
};
|
2023-05-03 18:08:14 +00:00
|
|
|
use sea_orm::prelude::*;
|
|
|
|
use teloxide::{adaptors::Throttle, prelude::*};
|
2023-05-04 15:27:19 +00:00
|
|
|
use tokio::task::spawn_blocking;
|
2023-05-03 18:08:14 +00:00
|
|
|
|
2023-05-09 17:27:58 +00:00
|
|
|
/// Gets the name of the master password, encryptes the account and adds it to the DB
|
2023-05-03 18:08:14 +00:00
|
|
|
async fn get_master_pass(
|
|
|
|
bot: Throttle<Bot>,
|
|
|
|
msg: Message,
|
|
|
|
db: DatabaseConnection,
|
|
|
|
dialogue: MainDialogue,
|
2023-05-07 10:20:11 +00:00
|
|
|
account: DecryptedAccount,
|
2023-05-03 18:08:14 +00:00
|
|
|
master_pass: String,
|
|
|
|
) -> crate::Result<()> {
|
2023-05-06 17:20:33 +00:00
|
|
|
let user_id = msg.from().ok_or(NoUserInfo)?.id.0;
|
2023-05-03 18:08:14 +00:00
|
|
|
dialogue.exit().await?;
|
2023-05-07 10:20:11 +00:00
|
|
|
let account = spawn_blocking(move || account.into_account(user_id, &master_pass)).await??;
|
2023-05-03 18:08:14 +00:00
|
|
|
account.insert(&db).await?;
|
2023-05-06 18:02:32 +00:00
|
|
|
bot.send_message(msg.chat.id, "Success")
|
|
|
|
.reply_markup(deletion_markup())
|
|
|
|
.await?;
|
2023-05-03 18:08:14 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-05-09 17:27:58 +00:00
|
|
|
/// Gets the password of the account to add
|
2023-05-03 18:08:14 +00:00
|
|
|
async fn get_password(
|
|
|
|
bot: Throttle<Bot>,
|
|
|
|
msg: Message,
|
|
|
|
_: DatabaseConnection,
|
|
|
|
dialogue: MainDialogue,
|
|
|
|
name: String,
|
|
|
|
login: String,
|
|
|
|
password: String,
|
|
|
|
) -> crate::Result<()> {
|
2023-05-04 18:35:31 +00:00
|
|
|
let previous = bot
|
|
|
|
.send_message(msg.chat.id, "Send master password")
|
2023-05-03 18:08:14 +00:00
|
|
|
.await?;
|
2023-05-07 10:20:11 +00:00
|
|
|
let account = DecryptedAccount {
|
|
|
|
name,
|
|
|
|
login,
|
|
|
|
password,
|
|
|
|
};
|
2023-05-03 18:08:14 +00:00
|
|
|
dialogue
|
|
|
|
.update(State::GetMasterPass(package_handler(
|
|
|
|
move |bot, msg, db, dialogue, master_pass| {
|
2023-05-07 10:20:11 +00:00
|
|
|
get_master_pass(bot, msg, db, dialogue, account, master_pass)
|
2023-05-03 18:08:14 +00:00
|
|
|
},
|
2023-05-07 10:20:11 +00:00
|
|
|
previous,
|
2023-05-03 18:08:14 +00:00
|
|
|
)))
|
|
|
|
.await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-05-09 17:27:58 +00:00
|
|
|
/// Gets the login of the account to add
|
2023-05-03 18:08:14 +00:00
|
|
|
async fn get_login(
|
|
|
|
bot: Throttle<Bot>,
|
|
|
|
msg: Message,
|
|
|
|
_: DatabaseConnection,
|
|
|
|
dialogue: MainDialogue,
|
|
|
|
name: String,
|
|
|
|
login: String,
|
|
|
|
) -> crate::Result<()> {
|
2023-05-04 18:35:31 +00:00
|
|
|
let previous = bot.send_message(msg.chat.id, "Send password").await?;
|
2023-05-03 18:08:14 +00:00
|
|
|
dialogue
|
|
|
|
.update(State::GetPassword(package_handler(
|
|
|
|
move |bot, msg, db, dialogue, password| {
|
2023-05-07 10:20:11 +00:00
|
|
|
get_password(bot, msg, db, dialogue, name, login, password)
|
2023-05-03 18:08:14 +00:00
|
|
|
},
|
2023-05-07 10:20:11 +00:00
|
|
|
previous,
|
2023-05-03 18:08:14 +00:00
|
|
|
)))
|
|
|
|
.await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-05-09 17:27:58 +00:00
|
|
|
/// Gets the name of the account to add and checks that there're no accounts with the same name
|
2023-05-03 18:08:14 +00:00
|
|
|
async fn get_account_name(
|
|
|
|
bot: Throttle<Bot>,
|
|
|
|
msg: Message,
|
2023-05-05 13:45:56 +00:00
|
|
|
db: DatabaseConnection,
|
2023-05-03 18:08:14 +00:00
|
|
|
dialogue: MainDialogue,
|
|
|
|
name: String,
|
|
|
|
) -> crate::Result<()> {
|
2023-05-06 17:20:33 +00:00
|
|
|
let user_id = msg.from().ok_or(NoUserInfo)?.id.0;
|
2023-05-05 13:45:56 +00:00
|
|
|
if Account::exists(user_id, &name, &db).await? {
|
|
|
|
bot.send_message(msg.chat.id, "Account alreay exists")
|
2023-05-06 18:02:32 +00:00
|
|
|
.reply_markup(deletion_markup())
|
2023-05-05 13:45:56 +00:00
|
|
|
.await?;
|
|
|
|
return Ok(());
|
|
|
|
}
|
2023-05-04 18:35:31 +00:00
|
|
|
let previous = bot.send_message(msg.chat.id, "Send login").await?;
|
2023-05-03 18:08:14 +00:00
|
|
|
dialogue
|
|
|
|
.update(State::GetLogin(package_handler(
|
2023-05-07 10:20:11 +00:00
|
|
|
move |bot, msg, db, dialogue, login| get_login(bot, msg, db, dialogue, name, login),
|
|
|
|
previous,
|
2023-05-03 18:08:14 +00:00
|
|
|
)))
|
|
|
|
.await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-05-09 17:27:58 +00:00
|
|
|
/// Handles /add_account
|
2023-05-03 18:08:14 +00:00
|
|
|
pub async fn add_account(
|
|
|
|
bot: Throttle<Bot>,
|
|
|
|
msg: Message,
|
|
|
|
dialogue: MainDialogue,
|
|
|
|
) -> crate::Result<()> {
|
2023-05-04 17:51:36 +00:00
|
|
|
let previous = bot.send_message(msg.chat.id, "Send account name").await?;
|
2023-05-03 18:08:14 +00:00
|
|
|
dialogue
|
2023-05-04 17:51:36 +00:00
|
|
|
.update(State::GetAccountName(package_handler(
|
2023-05-07 10:20:11 +00:00
|
|
|
get_account_name,
|
|
|
|
previous,
|
2023-05-04 17:51:36 +00:00
|
|
|
)))
|
2023-05-03 18:08:14 +00:00
|
|
|
.await?;
|
|
|
|
Ok(())
|
|
|
|
}
|