2023-05-05 13:45:56 +00:00
|
|
|
use crate::entity::{account, prelude::Account};
|
2023-05-06 17:20:33 +00:00
|
|
|
use crate::errors::NoUserInfo;
|
2023-05-03 18:08:14 +00:00
|
|
|
use crate::handlers::{utils::package_handler, MainDialogue, State};
|
|
|
|
use sea_orm::prelude::*;
|
|
|
|
use teloxide::{adaptors::Throttle, prelude::*};
|
2023-05-04 15:27:19 +00:00
|
|
|
use tokio::task::spawn_blocking;
|
2023-05-03 18:08:14 +00:00
|
|
|
|
|
|
|
async fn get_master_pass(
|
|
|
|
bot: Throttle<Bot>,
|
|
|
|
msg: Message,
|
|
|
|
db: DatabaseConnection,
|
|
|
|
dialogue: MainDialogue,
|
2023-05-04 17:51:36 +00:00
|
|
|
previous: Message,
|
2023-05-03 18:08:14 +00:00
|
|
|
name: String,
|
|
|
|
login: String,
|
|
|
|
password: String,
|
|
|
|
master_pass: String,
|
|
|
|
) -> crate::Result<()> {
|
2023-05-04 17:51:36 +00:00
|
|
|
let _ = bot.delete_message(previous.chat.id, previous.id).await;
|
2023-05-06 17:20:33 +00:00
|
|
|
let user_id = msg.from().ok_or(NoUserInfo)?.id.0;
|
2023-05-03 18:08:14 +00:00
|
|
|
dialogue.exit().await?;
|
2023-05-04 15:27:19 +00:00
|
|
|
let account = spawn_blocking(move || {
|
|
|
|
account::ActiveModel::from_unencrypted(user_id, name, &login, &password, &master_pass)
|
|
|
|
})
|
|
|
|
.await??;
|
2023-05-03 18:08:14 +00:00
|
|
|
account.insert(&db).await?;
|
|
|
|
bot.send_message(msg.chat.id, "Success").await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn get_password(
|
|
|
|
bot: Throttle<Bot>,
|
|
|
|
msg: Message,
|
|
|
|
_: DatabaseConnection,
|
|
|
|
dialogue: MainDialogue,
|
2023-05-04 17:51:36 +00:00
|
|
|
previous: Message,
|
2023-05-03 18:08:14 +00:00
|
|
|
name: String,
|
|
|
|
login: String,
|
|
|
|
password: String,
|
|
|
|
) -> crate::Result<()> {
|
2023-05-04 17:51:36 +00:00
|
|
|
let _ = bot.delete_message(previous.chat.id, previous.id).await;
|
2023-05-04 18:35:31 +00:00
|
|
|
let previous = bot
|
|
|
|
.send_message(msg.chat.id, "Send master password")
|
2023-05-03 18:08:14 +00:00
|
|
|
.await?;
|
|
|
|
dialogue
|
|
|
|
.update(State::GetMasterPass(package_handler(
|
|
|
|
move |bot, msg, db, dialogue, master_pass| {
|
2023-05-04 17:51:36 +00:00
|
|
|
get_master_pass(
|
|
|
|
bot,
|
|
|
|
msg,
|
|
|
|
db,
|
|
|
|
dialogue,
|
|
|
|
previous,
|
|
|
|
name,
|
|
|
|
login,
|
|
|
|
password,
|
|
|
|
master_pass,
|
|
|
|
)
|
2023-05-03 18:08:14 +00:00
|
|
|
},
|
|
|
|
)))
|
|
|
|
.await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn get_login(
|
|
|
|
bot: Throttle<Bot>,
|
|
|
|
msg: Message,
|
|
|
|
_: DatabaseConnection,
|
|
|
|
dialogue: MainDialogue,
|
2023-05-04 17:51:36 +00:00
|
|
|
previous: Message,
|
2023-05-03 18:08:14 +00:00
|
|
|
name: String,
|
|
|
|
login: String,
|
|
|
|
) -> crate::Result<()> {
|
2023-05-04 17:51:36 +00:00
|
|
|
let _ = bot.delete_message(previous.chat.id, previous.id).await;
|
2023-05-04 18:35:31 +00:00
|
|
|
let previous = bot.send_message(msg.chat.id, "Send password").await?;
|
2023-05-03 18:08:14 +00:00
|
|
|
dialogue
|
|
|
|
.update(State::GetPassword(package_handler(
|
|
|
|
move |bot, msg, db, dialogue, password| {
|
2023-05-04 17:51:36 +00:00
|
|
|
get_password(bot, msg, db, dialogue, previous, name, login, password)
|
2023-05-03 18:08:14 +00:00
|
|
|
},
|
|
|
|
)))
|
|
|
|
.await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn get_account_name(
|
|
|
|
bot: Throttle<Bot>,
|
|
|
|
msg: Message,
|
2023-05-05 13:45:56 +00:00
|
|
|
db: DatabaseConnection,
|
2023-05-03 18:08:14 +00:00
|
|
|
dialogue: MainDialogue,
|
2023-05-04 17:51:36 +00:00
|
|
|
previous: Message,
|
2023-05-03 18:08:14 +00:00
|
|
|
name: String,
|
|
|
|
) -> crate::Result<()> {
|
2023-05-04 17:51:36 +00:00
|
|
|
let _ = bot.delete_message(previous.chat.id, previous.id).await;
|
2023-05-06 17:20:33 +00:00
|
|
|
let user_id = msg.from().ok_or(NoUserInfo)?.id.0;
|
2023-05-05 13:45:56 +00:00
|
|
|
if Account::exists(user_id, &name, &db).await? {
|
|
|
|
bot.send_message(msg.chat.id, "Account alreay exists")
|
|
|
|
.await?;
|
|
|
|
return Ok(());
|
|
|
|
}
|
2023-05-04 18:35:31 +00:00
|
|
|
let previous = bot.send_message(msg.chat.id, "Send login").await?;
|
2023-05-03 18:08:14 +00:00
|
|
|
dialogue
|
|
|
|
.update(State::GetLogin(package_handler(
|
2023-05-04 17:51:36 +00:00
|
|
|
move |bot, msg, db, dialogue, login| {
|
|
|
|
get_login(bot, msg, db, dialogue, previous, name, login)
|
|
|
|
},
|
2023-05-03 18:08:14 +00:00
|
|
|
)))
|
|
|
|
.await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn add_account(
|
|
|
|
bot: Throttle<Bot>,
|
|
|
|
msg: Message,
|
|
|
|
dialogue: MainDialogue,
|
|
|
|
) -> crate::Result<()> {
|
2023-05-04 17:51:36 +00:00
|
|
|
let previous = bot.send_message(msg.chat.id, "Send account name").await?;
|
2023-05-03 18:08:14 +00:00
|
|
|
dialogue
|
2023-05-04 17:51:36 +00:00
|
|
|
.update(State::GetAccountName(package_handler(
|
|
|
|
move |bot, msg, db, dialogue, name| {
|
|
|
|
get_account_name(bot, msg, db, dialogue, previous, name)
|
|
|
|
},
|
|
|
|
)))
|
2023-05-03 18:08:14 +00:00
|
|
|
.await?;
|
|
|
|
Ok(())
|
|
|
|
}
|