pass_manager/src/handlers/commands/add_account.rs

126 lines
3.5 KiB
Rust

use crate::entity::{account, prelude::Account};
use crate::handlers::{utils::package_handler, MainDialogue, State};
use sea_orm::prelude::*;
use teloxide::{adaptors::Throttle, prelude::*};
use tokio::task::spawn_blocking;
async fn get_master_pass(
bot: Throttle<Bot>,
msg: Message,
db: DatabaseConnection,
dialogue: MainDialogue,
previous: Message,
name: String,
login: String,
password: String,
master_pass: String,
) -> crate::Result<()> {
let _ = bot.delete_message(previous.chat.id, previous.id).await;
let user_id = msg.from().unwrap().id.0;
dialogue.exit().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").await?;
Ok(())
}
async fn get_password(
bot: Throttle<Bot>,
msg: Message,
_: DatabaseConnection,
dialogue: MainDialogue,
previous: Message,
name: String,
login: String,
password: String,
) -> crate::Result<()> {
let _ = bot.delete_message(previous.chat.id, previous.id).await;
let previous = bot
.send_message(msg.chat.id, "Send master password")
.await?;
dialogue
.update(State::GetMasterPass(package_handler(
move |bot, msg, db, dialogue, master_pass| {
get_master_pass(
bot,
msg,
db,
dialogue,
previous,
name,
login,
password,
master_pass,
)
},
)))
.await?;
Ok(())
}
async fn get_login(
bot: Throttle<Bot>,
msg: Message,
_: DatabaseConnection,
dialogue: MainDialogue,
previous: Message,
name: String,
login: String,
) -> crate::Result<()> {
let _ = bot.delete_message(previous.chat.id, previous.id).await;
let previous = bot.send_message(msg.chat.id, "Send password").await?;
dialogue
.update(State::GetPassword(package_handler(
move |bot, msg, db, dialogue, password| {
get_password(bot, msg, db, dialogue, previous, name, login, password)
},
)))
.await?;
Ok(())
}
async fn get_account_name(
bot: Throttle<Bot>,
msg: Message,
db: DatabaseConnection,
dialogue: MainDialogue,
previous: Message,
name: String,
) -> crate::Result<()> {
let _ = bot.delete_message(previous.chat.id, previous.id).await;
let user_id = msg.from().unwrap().id.0;
if Account::exists(user_id, &name, &db).await? {
bot.send_message(msg.chat.id, "Account alreay exists")
.await?;
return Ok(());
}
let previous = bot.send_message(msg.chat.id, "Send login").await?;
dialogue
.update(State::GetLogin(package_handler(
move |bot, msg, db, dialogue, login| {
get_login(bot, msg, db, dialogue, previous, name, login)
},
)))
.await?;
Ok(())
}
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::GetAccountName(package_handler(
move |bot, msg, db, dialogue, name| {
get_account_name(bot, msg, db, dialogue, previous, name)
},
)))
.await?;
Ok(())
}