Added delete command and fixed memory deletion for /add_account

This commit is contained in:
StNicolay 2023-05-04 21:35:31 +03:00
parent 332356a95b
commit a2fae8bb89
Signed by: StNicolay
GPG Key ID: 9693D04DCD962B0D
4 changed files with 77 additions and 4 deletions

View File

@ -38,7 +38,8 @@ async fn get_password(
password: String,
) -> crate::Result<()> {
let _ = bot.delete_message(previous.chat.id, previous.id).await;
bot.send_message(msg.chat.id, "Send master password")
let previous = bot
.send_message(msg.chat.id, "Send master password")
.await?;
dialogue
.update(State::GetMasterPass(package_handler(
@ -70,7 +71,7 @@ async fn get_login(
login: String,
) -> crate::Result<()> {
let _ = bot.delete_message(previous.chat.id, previous.id).await;
bot.send_message(msg.chat.id, "Send password").await?;
let previous = bot.send_message(msg.chat.id, "Send password").await?;
dialogue
.update(State::GetPassword(package_handler(
move |bot, msg, db, dialogue, password| {
@ -90,7 +91,7 @@ async fn get_account_name(
name: String,
) -> crate::Result<()> {
let _ = bot.delete_message(previous.chat.id, previous.id).await;
bot.send_message(msg.chat.id, "Send login").await?;
let previous = bot.send_message(msg.chat.id, "Send login").await?;
dialogue
.update(State::GetLogin(package_handler(
move |bot, msg, db, dialogue, login| {

View File

@ -0,0 +1,67 @@
use crate::{
entity::prelude::Account,
handlers::{markups, 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,
previous: Message,
name: String,
_: String,
) -> crate::Result<()> {
let _ = bot.delete_message(previous.chat.id, previous.id).await;
dialogue.exit().await?;
let user_id = msg.from().unwrap().id.0;
Account::delete_by_id((user_id, name)).exec(&db).await?;
bot.send_message(msg.chat.id, "The account is successfully deleted")
.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. Once you send correct master password the account is unrecoverable")
.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 delete(
bot: Throttle<Bot>,
msg: Message,
dialogue: MainDialogue,
db: DatabaseConnection,
) -> crate::Result<()> {
let markup = markups::account_markup(msg.from().unwrap().id.0, &db).await?;
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(())
}

View File

@ -1,5 +1,6 @@
mod add_account;
mod default;
mod delete;
mod get_account;
mod get_accounts;
mod help;
@ -7,6 +8,7 @@ mod set_master_pass;
pub use add_account::add_account;
pub use default::default;
pub use delete::delete;
pub use get_account::get_account;
pub use get_accounts::get_accounts;
pub use help::help;

View File

@ -27,6 +27,8 @@ enum Command {
GetAccounts,
#[command()]
SetMasterPass,
#[command()]
Delete,
}
type MainDialogue = Dialogue<State, InMemStorage<State>>;
@ -70,7 +72,8 @@ pub fn get_dispatcher(
.branch(case![Command::AddAccount].endpoint(commands::add_account))
.branch(case![Command::GetAccount].endpoint(commands::get_account))
.branch(case![Command::GetAccounts].endpoint(commands::get_accounts))
.branch(case![Command::SetMasterPass].endpoint(commands::set_master_pass));
.branch(case![Command::SetMasterPass].endpoint(commands::set_master_pass))
.branch(case![Command::Delete].endpoint(commands::delete));
let message_handler = Update::filter_message()
.map_async(utils::delete_message)