pass_manager/src/handlers/commands/add_account.rs

127 lines
3.6 KiB
Rust
Raw Normal View History

use crate::entity::{account, prelude::Account};
use crate::errors::NoUserInfo;
2023-05-03 18:08:14 +00:00
use crate::handlers::{utils::package_handler, MainDialogue, State};
use sea_orm::prelude::*;
use teloxide::{adaptors::Throttle, prelude::*};
use tokio::task::spawn_blocking;
2023-05-03 18:08:14 +00:00
async fn get_master_pass(
bot: Throttle<Bot>,
msg: Message,
db: DatabaseConnection,
dialogue: MainDialogue,
2023-05-04 17:51:36 +00:00
previous: Message,
2023-05-03 18:08:14 +00:00
name: String,
login: String,
password: String,
master_pass: String,
) -> crate::Result<()> {
2023-05-04 17:51:36 +00:00
let _ = bot.delete_message(previous.chat.id, previous.id).await;
let user_id = msg.from().ok_or(NoUserInfo)?.id.0;
2023-05-03 18:08:14 +00:00
dialogue.exit().await?;
let account = spawn_blocking(move || {
account::ActiveModel::from_unencrypted(user_id, name, &login, &password, &master_pass)
})
.await??;
2023-05-03 18:08:14 +00:00
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,
2023-05-04 17:51:36 +00:00
previous: Message,
2023-05-03 18:08:14 +00:00
name: String,
login: String,
password: String,
) -> crate::Result<()> {
2023-05-04 17:51:36 +00:00
let _ = bot.delete_message(previous.chat.id, previous.id).await;
let previous = bot
.send_message(msg.chat.id, "Send master password")
2023-05-03 18:08:14 +00:00
.await?;
dialogue
.update(State::GetMasterPass(package_handler(
move |bot, msg, db, dialogue, master_pass| {
2023-05-04 17:51:36 +00:00
get_master_pass(
bot,
msg,
db,
dialogue,
previous,
name,
login,
password,
master_pass,
)
2023-05-03 18:08:14 +00:00
},
)))
.await?;
Ok(())
}
async fn get_login(
bot: Throttle<Bot>,
msg: Message,
_: DatabaseConnection,
dialogue: MainDialogue,
2023-05-04 17:51:36 +00:00
previous: Message,
2023-05-03 18:08:14 +00:00
name: String,
login: String,
) -> crate::Result<()> {
2023-05-04 17:51:36 +00:00
let _ = bot.delete_message(previous.chat.id, previous.id).await;
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-04 17:51:36 +00:00
get_password(bot, msg, db, dialogue, previous, name, login, password)
2023-05-03 18:08:14 +00:00
},
)))
.await?;
Ok(())
}
async fn get_account_name(
bot: Throttle<Bot>,
msg: Message,
db: DatabaseConnection,
2023-05-03 18:08:14 +00:00
dialogue: MainDialogue,
2023-05-04 17:51:36 +00:00
previous: Message,
2023-05-03 18:08:14 +00:00
name: String,
) -> crate::Result<()> {
2023-05-04 17:51:36 +00:00
let _ = bot.delete_message(previous.chat.id, previous.id).await;
let user_id = msg.from().ok_or(NoUserInfo)?.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?;
2023-05-03 18:08:14 +00:00
dialogue
.update(State::GetLogin(package_handler(
2023-05-04 17:51:36 +00:00
move |bot, msg, db, dialogue, login| {
get_login(bot, msg, db, dialogue, previous, name, login)
},
2023-05-03 18:08:14 +00:00
)))
.await?;
Ok(())
}
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(
move |bot, msg, db, dialogue, name| {
get_account_name(bot, msg, db, dialogue, previous, name)
},
)))
2023-05-03 18:08:14 +00:00
.await?;
Ok(())
}