2023-05-27 21:56:58 +03:00

79 lines
2.3 KiB
Rust

use crate::{
entity::prelude::Account,
errors::NoUserInfo,
handlers::{
markups::{self, deletion_markup},
Handler, MainDialogue, State,
},
};
use sea_orm::prelude::*;
use teloxide::{adaptors::Throttle, prelude::*};
/// Gets the master password and deletes the account.
/// Although it doesn't use the master password, we get it to be sure that it's the user who used that command
async fn get_master_pass(
bot: Throttle<Bot>,
msg: Message,
db: DatabaseConnection,
dialogue: MainDialogue,
name: String,
) -> crate::Result<()> {
dialogue.exit().await?;
let user_id = msg.from().ok_or(NoUserInfo)?.id.0;
Account::delete_by_id((user_id, name)).exec(&db).await?;
bot.send_message(msg.chat.id, "The account is successfully deleted")
.reply_markup(deletion_markup())
.await?;
Ok(())
}
/// Gets the name of the account to delete
async fn get_account_name(
bot: Throttle<Bot>,
msg: Message,
db: DatabaseConnection,
dialogue: MainDialogue,
name: String,
) -> crate::Result<()> {
let user_id = msg.from().ok_or(NoUserInfo)?.id.0;
if !Account::exists(user_id, &name, &db).await? {
dialogue.exit().await?;
bot.send_message(msg.chat.id, "Account doesn't exists")
.reply_markup(deletion_markup())
.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?;
dialogue
.update(State::GetMasterPass(Handler::new(
move |bot, msg, db, dialogue, _| get_master_pass(bot, msg, db, dialogue, name),
previous,
)))
.await?;
Ok(())
}
/// Handles /delete command
pub async fn delete(
bot: Throttle<Bot>,
msg: Message,
dialogue: MainDialogue,
db: DatabaseConnection,
) -> crate::Result<()> {
let user_id = msg.from().ok_or(NoUserInfo)?.id.0;
let markup = markups::account_markup(user_id, &db).await?;
let previous = bot
.send_message(msg.chat.id, "Send account name")
.reply_markup(markup)
.await?;
dialogue
.update(State::GetAccountName(Handler::new(
get_account_name,
previous,
)))
.await?;
Ok(())
}