2023-05-04 20:51:36 +03:00

87 lines
2.6 KiB
Rust

use crate::entity::{account, prelude::Account};
use crate::handlers::{utils::package_handler, MainDialogue, State};
use futures::TryStreamExt;
use sea_orm::prelude::*;
use teloxide::{
adaptors::Throttle,
prelude::*,
types::ParseMode,
types::{KeyboardButton, KeyboardMarkup},
};
use tokio::task::spawn_blocking;
async fn get_master_pass(
bot: Throttle<Bot>,
msg: Message,
db: DatabaseConnection,
dialogue: MainDialogue,
previous: Message,
name: String,
master_pass: String,
) -> crate::Result<()> {
let _ = bot.delete_message(previous.chat.id, previous.id).await;
dialogue.exit().await?;
let user_id = msg.from().unwrap().id.0;
let account = Account::find()
.filter(account::Column::UserId.eq(user_id))
.filter(account::Column::Name.eq(&name))
.one(&db)
.await?
.unwrap();
let (login, password) = spawn_blocking(move || account.decrypt(&master_pass)).await??;
let message = format!("Name:\n`{name}`\nLogin:\n`{login}`\nPassword:\n`{password}`");
bot.send_message(msg.chat.id, message)
.parse_mode(ParseMode::MarkdownV2)
.await?;
Ok(())
}
async fn get_account_name(
bot: Throttle<Bot>,
msg: Message,
_: DatabaseConnection,
dialogue: MainDialogue,
previous: Message,
name: 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, master_pass)
},
)))
.await?;
Ok(())
}
pub async fn get_account(
bot: Throttle<Bot>,
msg: Message,
dialogue: MainDialogue,
db: DatabaseConnection,
) -> crate::Result<()> {
let account_names: Vec<Vec<KeyboardButton>> = Account::get_names(msg.from().unwrap().id.0, &db)
.await?
.map_ok(|account| KeyboardButton::new(account))
.try_chunks(3)
.try_collect()
.await?;
let markup = KeyboardMarkup::new(account_names).resize_keyboard(true);
let previous = bot
.send_message(msg.chat.id, "Send account name")
.reply_markup(markup)
.await?;
dialogue
.update(State::GetAccountName(package_handler(
move |bot, msg, db, dialogue, name| {
get_account_name(bot, msg, db, dialogue, previous, name)
},
)))
.await?;
Ok(())
}