Now checking that account exists before getting/deleting it, added export command

This commit is contained in:
2023-05-05 16:45:56 +03:00
parent b5e003e1d7
commit 957bcfb952
15 changed files with 193 additions and 39 deletions

View File

@ -1,4 +1,4 @@
use crate::entity::account;
use crate::entity::{account, prelude::Account};
use crate::handlers::{utils::package_handler, MainDialogue, State};
use sea_orm::prelude::*;
use teloxide::{adaptors::Throttle, prelude::*};
@ -85,12 +85,18 @@ async fn get_login(
async fn get_account_name(
bot: Throttle<Bot>,
msg: Message,
_: DatabaseConnection,
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(

View File

@ -25,12 +25,18 @@ async fn get_master_pass(
async fn get_account_name(
bot: Throttle<Bot>,
msg: Message,
_: DatabaseConnection,
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 doesn't exists")
.await?;
return Ok(());
}
let previous = bot
.send_message(msg.chat.id, "Send master password. Once you send correct master password the account is unrecoverable")
.await?;

View File

@ -0,0 +1,58 @@
use crate::{
decrypted_account::DecryptedAccount,
entity::prelude::Account,
handlers::{utils::package_handler, MainDialogue, State},
};
use futures::TryStreamExt;
use sea_orm::DatabaseConnection;
use serde_json::{json, to_string_pretty};
use std::sync::Arc;
use teloxide::{adaptors::Throttle, prelude::*, types::InputFile};
use tokio::task::JoinSet;
async fn get_master_pass(
bot: Throttle<Bot>,
msg: Message,
db: DatabaseConnection,
dialogue: MainDialogue,
previous: Message,
master_pass: String,
) -> crate::Result<()> {
let _ = bot.delete_message(previous.chat.id, previous.id).await;
let master_pass: Arc<str> = master_pass.into();
let user_id = msg.from().unwrap().id.0;
let mut join_set = JoinSet::new();
let mut accounts = Vec::new();
Account::get_all(user_id, &db)
.await?
.try_for_each(|account| {
let master_pass = Arc::clone(&master_pass);
join_set.spawn_blocking(move || DecryptedAccount::from_account(account, &master_pass));
async { crate::Result::Ok(()) }
})
.await?;
drop(master_pass);
while let Some(account) = join_set.join_next().await.transpose()?.transpose()? {
accounts.push(account)
}
accounts.sort_by(|this, other| this.name.cmp(&other.name));
let json = to_string_pretty(&json!({ "accounts": accounts }))?;
let file = InputFile::memory(json).file_name("accounts.json");
bot.send_document(msg.chat.id, file).await?;
dialogue.exit().await?;
Ok(())
}
pub async fn export(bot: Throttle<Bot>, msg: Message, dialogue: MainDialogue) -> crate::Result<()> {
let previous = bot
.send_message(msg.chat.id, "Send a master password to export the accounts")
.await?;
dialogue
.update(State::GetMasterPass(package_handler(
move |bot, msg, db, dialogue, master_pass| {
get_master_pass(bot, msg, db, dialogue, previous, master_pass)
},
)))
.await?;
Ok(())
}

View File

@ -35,12 +35,18 @@ async fn get_master_pass(
async fn get_account_name(
bot: Throttle<Bot>,
msg: Message,
_: DatabaseConnection,
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 doesn't exists")
.await?;
return Ok(());
}
let previous = bot
.send_message(msg.chat.id, "Send master password")
.await?;

View File

@ -9,7 +9,7 @@ pub async fn get_accounts(
db: DatabaseConnection,
) -> crate::Result<()> {
let user_id = msg.from().unwrap().id.0;
let mut account_names = Account::get_names(user_id, &db).await?;
let mut account_names = Account::get_names(user_id, &db, true).await?;
let mut result = match account_names.try_next().await? {
Some(name) => format!("Accounts:\n`{name}`"),
None => {

View File

@ -0,0 +1,6 @@
use crate::handlers::MainDialogue;
use teloxide::{adaptors::Throttle, prelude::*};
pub async fn import(bot: Throttle<Bot>, msg: Message, dialogue: MainDialogue) -> crate::Result<()> {
Ok(())
}

View File

@ -2,16 +2,20 @@ mod add_account;
mod default;
mod delete;
mod delete_all;
mod export;
mod get_account;
mod get_accounts;
mod help;
mod import;
mod set_master_pass;
pub use add_account::add_account;
pub use default::default;
pub use delete::delete;
pub use delete_all::delete_all;
pub use export::export;
pub use get_account::get_account;
pub use get_accounts::get_accounts;
pub use help::help;
pub use import::import;
pub use set_master_pass::set_master_pass;