pass_manager/src/handlers/commands/add_account.rs

101 lines
2.7 KiB
Rust
Raw Normal View History

2023-05-03 18:08:14 +00:00
use crate::entity::account;
use crate::handlers::{utils::package_handler, MainDialogue, State};
use sea_orm::prelude::*;
use teloxide::{adaptors::Throttle, prelude::*};
async fn get_master_pass(
bot: Throttle<Bot>,
msg: Message,
db: DatabaseConnection,
dialogue: MainDialogue,
name: String,
login: String,
password: String,
master_pass: String,
) -> crate::Result<()> {
let _ = bot.delete_message(msg.chat.id, msg.id).await;
dialogue.exit().await?;
let account = account::ActiveModel::from_unencrypted(
msg.from().unwrap().id.0,
name,
&login,
&password,
&master_pass,
)?;
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,
name: String,
login: String,
password: String,
) -> crate::Result<()> {
let _ = bot.delete_message(msg.chat.id, msg.id).await;
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, name, login, password, master_pass)
},
)))
.await?;
Ok(())
}
async fn get_login(
bot: Throttle<Bot>,
msg: Message,
_: DatabaseConnection,
dialogue: MainDialogue,
name: String,
login: String,
) -> crate::Result<()> {
let _ = bot.delete_message(msg.chat.id, msg.id).await;
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, name, login, password)
},
)))
.await?;
Ok(())
}
async fn get_account_name(
bot: Throttle<Bot>,
msg: Message,
_: DatabaseConnection,
dialogue: MainDialogue,
name: String,
) -> crate::Result<()> {
let _ = bot.delete_message(msg.chat.id, msg.id).await;
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, name, login),
)))
.await?;
Ok(())
}
pub async fn add_account(
bot: Throttle<Bot>,
msg: Message,
_: DatabaseConnection,
dialogue: MainDialogue,
) -> crate::Result<()> {
bot.send_message(msg.chat.id, "Send account name").await?;
dialogue
.update(State::GetAccountName(package_handler(get_account_name)))
.await?;
Ok(())
}