Moved the check of existence of a master password into master_pass.rs, added buttons for /get_account

This commit is contained in:
2023-05-04 19:03:03 +03:00
parent 17e4f1892c
commit 83aa2c90b1
4 changed files with 44 additions and 21 deletions

View File

@ -1,7 +1,13 @@
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};
use teloxide::{
adaptors::Throttle,
prelude::*,
types::ParseMode,
types::{KeyboardButton, KeyboardMarkup},
};
use tokio::task::spawn_blocking;
async fn get_master_pass(
@ -12,6 +18,7 @@ async fn get_master_pass(
name: String,
master_pass: String,
) -> crate::Result<()> {
let _ = bot.delete_message(msg.chat.id, msg.id).await;
dialogue.exit().await?;
let user_id = msg.from().unwrap().id.0;
let account = Account::find()
@ -52,8 +59,18 @@ pub async fn get_account(
bot: Throttle<Bot>,
msg: Message,
dialogue: MainDialogue,
db: DatabaseConnection,
) -> crate::Result<()> {
bot.send_message(msg.chat.id, "Send account name").await?;
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);
bot.send_message(msg.chat.id, "Send account name")
.reply_markup(markup)
.await?;
dialogue
.update(State::GetAccountName(package_handler(get_account_name)))
.await?;

View File

@ -2,7 +2,7 @@ use crate::{
entity::{master_pass, prelude::*},
handlers::{utils::package_handler, MainDialogue, State},
};
use sea_orm::{prelude::*, QuerySelect};
use sea_orm::prelude::*;
use teloxide::{adaptors::Throttle, prelude::*};
use tokio::task;
@ -15,19 +15,6 @@ async fn get_master_pass(
) -> crate::Result<()> {
dialogue.exit().await?;
let user_id = msg.from().unwrap().id.0;
let exists = MasterPass::find()
.select_only()
.column(master_pass::Column::UserId)
.filter(master_pass::Column::UserId.eq(user_id))
.into_tuple::<u64>()
.one(&db)
.await?
.is_some();
if exists {
bot.send_message(msg.chat.id, "Master password already exists")
.await?;
return Ok(());
}
let model = task::spawn_blocking(move || {
master_pass::ActiveModel::from_unencrypted(user_id, &master_password)
})
@ -41,7 +28,14 @@ pub async fn set_master_pass(
bot: Throttle<Bot>,
msg: Message,
dialogue: MainDialogue,
db: DatabaseConnection,
) -> crate::Result<()> {
let user_id = msg.from().unwrap().id.0;
if MasterPass::exists(user_id, &db).await? {
bot.send_message(msg.chat.id, "Master password already exists")
.await?;
return Ok(());
}
bot.send_message(msg.chat.id, "Send new master password")
.await?;
dialogue