First steps
This commit is contained in:
100
src/handlers/commands/add_account.rs
Normal file
100
src/handlers/commands/add_account.rs
Normal file
@ -0,0 +1,100 @@
|
||||
use crate::entity::account;
|
||||
use crate::handlers::{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,
|
||||
name: String,
|
||||
login: String,
|
||||
password: String,
|
||||
master_pass: String,
|
||||
) -> crate::Result<()> {
|
||||
let _ = bot.delete_message(msg.chat.id, msg.id).await;
|
||||
dialogue.exit().await?;
|
||||
let account = account::ActiveModel::from_unencrypted(
|
||||
msg.from().unwrap().id.0,
|
||||
name,
|
||||
&login,
|
||||
&password,
|
||||
&master_pass,
|
||||
)?;
|
||||
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,
|
||||
name: String,
|
||||
login: String,
|
||||
password: String,
|
||||
) -> crate::Result<()> {
|
||||
let _ = bot.delete_message(msg.chat.id, msg.id).await;
|
||||
bot.send_message(msg.chat.id, "Send master password")
|
||||
.await?;
|
||||
dialogue
|
||||
.update(State::GetMasterPass(package_handler(
|
||||
move |bot, msg, db, dialogue, master_pass| {
|
||||
get_master_pass(bot, msg, db, dialogue, name, login, password, master_pass)
|
||||
},
|
||||
)))
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn get_login(
|
||||
bot: Throttle<Bot>,
|
||||
msg: Message,
|
||||
_: DatabaseConnection,
|
||||
dialogue: MainDialogue,
|
||||
name: String,
|
||||
login: String,
|
||||
) -> crate::Result<()> {
|
||||
let _ = bot.delete_message(msg.chat.id, msg.id).await;
|
||||
bot.send_message(msg.chat.id, "Send password").await?;
|
||||
dialogue
|
||||
.update(State::GetPassword(package_handler(
|
||||
move |bot, msg, db, dialogue, password| {
|
||||
get_password(bot, msg, db, dialogue, name, login, password)
|
||||
},
|
||||
)))
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn get_account_name(
|
||||
bot: Throttle<Bot>,
|
||||
msg: Message,
|
||||
_: DatabaseConnection,
|
||||
dialogue: MainDialogue,
|
||||
name: String,
|
||||
) -> crate::Result<()> {
|
||||
let _ = bot.delete_message(msg.chat.id, msg.id).await;
|
||||
bot.send_message(msg.chat.id, "Send login").await?;
|
||||
dialogue
|
||||
.update(State::GetLogin(package_handler(
|
||||
move |bot, msg, db, dialogue, login| get_login(bot, msg, db, dialogue, name, login),
|
||||
)))
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn add_account(
|
||||
bot: Throttle<Bot>,
|
||||
msg: Message,
|
||||
_: DatabaseConnection,
|
||||
dialogue: MainDialogue,
|
||||
) -> crate::Result<()> {
|
||||
bot.send_message(msg.chat.id, "Send account name").await?;
|
||||
dialogue
|
||||
.update(State::GetAccountName(package_handler(get_account_name)))
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
6
src/handlers/commands/default.rs
Normal file
6
src/handlers/commands/default.rs
Normal file
@ -0,0 +1,6 @@
|
||||
use teloxide::{adaptors::Throttle, prelude::*};
|
||||
|
||||
pub async fn default(bot: Throttle<Bot>, msg: Message) -> crate::Result<()> {
|
||||
bot.send_message(msg.chat.id, "Unknown command").await?;
|
||||
Ok(())
|
||||
}
|
61
src/handlers/commands/get_account.rs
Normal file
61
src/handlers/commands/get_account.rs
Normal file
@ -0,0 +1,61 @@
|
||||
use crate::entity::{account, prelude::Account};
|
||||
use sea_orm::prelude::*;
|
||||
use teloxide::{adaptors::Throttle, prelude::*, types::ParseMode};
|
||||
|
||||
use crate::handlers::{utils::package_handler, MainDialogue, State};
|
||||
|
||||
async fn get_master_pass(
|
||||
bot: Throttle<Bot>,
|
||||
msg: Message,
|
||||
db: DatabaseConnection,
|
||||
dialogue: MainDialogue,
|
||||
name: String,
|
||||
master_pass: String,
|
||||
) -> crate::Result<()> {
|
||||
dialogue.exit().await?;
|
||||
let user_id = msg.from().unwrap().id.0;
|
||||
let account = Account::find()
|
||||
.filter(account::Column::UserId.eq(user_id))
|
||||
.filter(account::Column::Name.eq(&name))
|
||||
.one(&db)
|
||||
.await?
|
||||
.unwrap();
|
||||
let (login, password) = account.decrypt(&master_pass)?;
|
||||
let message = format!("Name:\n`{name}`\nLogin:\n`{login}`\nPassword:\n`{password}`");
|
||||
bot.send_message(msg.chat.id, message)
|
||||
.parse_mode(ParseMode::MarkdownV2)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn get_account_name(
|
||||
bot: Throttle<Bot>,
|
||||
msg: Message,
|
||||
_: DatabaseConnection,
|
||||
dialogue: MainDialogue,
|
||||
name: String,
|
||||
) -> crate::Result<()> {
|
||||
let _ = bot.delete_message(msg.chat.id, msg.id).await;
|
||||
bot.send_message(msg.chat.id, "Send master password")
|
||||
.await?;
|
||||
dialogue
|
||||
.update(State::GetMasterPass(package_handler(
|
||||
move |bot, msg, db, dialogue, master_pass| {
|
||||
get_master_pass(bot, msg, db, dialogue, name, master_pass)
|
||||
},
|
||||
)))
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn get_account(
|
||||
bot: Throttle<Bot>,
|
||||
msg: Message,
|
||||
dialogue: MainDialogue,
|
||||
) -> crate::Result<()> {
|
||||
bot.send_message(msg.chat.id, "Send account name").await?;
|
||||
dialogue
|
||||
.update(State::GetAccountName(package_handler(get_account_name)))
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
29
src/handlers/commands/get_accounts.rs
Normal file
29
src/handlers/commands/get_accounts.rs
Normal file
@ -0,0 +1,29 @@
|
||||
use crate::entity::prelude::Account;
|
||||
use sea_orm::prelude::*;
|
||||
use teloxide::{adaptors::Throttle, prelude::*, types::ParseMode};
|
||||
|
||||
pub async fn get_accounts(
|
||||
bot: Throttle<Bot>,
|
||||
msg: Message,
|
||||
db: DatabaseConnection,
|
||||
) -> crate::Result<()> {
|
||||
let user_id = msg.from().unwrap().id.0;
|
||||
let mut account_names = Account::get_names(user_id, &db).await?.into_iter();
|
||||
let mut result = match account_names.next() {
|
||||
Some(name) => format!("Accounts:\n`{name}`"),
|
||||
None => {
|
||||
bot.send_message(msg.chat.id, "No accounts found").await?;
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
for name in account_names {
|
||||
result.reserve(name.len() + 3);
|
||||
result.push_str("\n`");
|
||||
result.push_str(&name);
|
||||
result.push('`')
|
||||
}
|
||||
bot.send_message(msg.chat.id, result)
|
||||
.parse_mode(ParseMode::MarkdownV2)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
8
src/handlers/commands/help.rs
Normal file
8
src/handlers/commands/help.rs
Normal file
@ -0,0 +1,8 @@
|
||||
use crate::handlers::Command;
|
||||
use teloxide::{adaptors::Throttle, prelude::*, utils::command::BotCommands};
|
||||
|
||||
pub async fn help(bot: Throttle<Bot>, msg: Message) -> crate::Result<()> {
|
||||
bot.send_message(msg.chat.id, Command::descriptions().to_string())
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
13
src/handlers/commands/mod.rs
Normal file
13
src/handlers/commands/mod.rs
Normal file
@ -0,0 +1,13 @@
|
||||
mod add_account;
|
||||
mod default;
|
||||
mod get_account;
|
||||
mod get_accounts;
|
||||
mod help;
|
||||
mod set_master_pass;
|
||||
|
||||
pub use add_account::add_account;
|
||||
pub use default::default;
|
||||
pub use get_account::get_account;
|
||||
pub use get_accounts::get_accounts;
|
||||
pub use help::help;
|
||||
pub use set_master_pass::set_master_pass;
|
51
src/handlers/commands/set_master_pass.rs
Normal file
51
src/handlers/commands/set_master_pass.rs
Normal file
@ -0,0 +1,51 @@
|
||||
use crate::{
|
||||
entity::{master_pass, prelude::*},
|
||||
handlers::{utils::package_handler, MainDialogue, State},
|
||||
};
|
||||
use sea_orm::{prelude::*, QuerySelect};
|
||||
use teloxide::{adaptors::Throttle, prelude::*};
|
||||
use tokio::task;
|
||||
|
||||
async fn get_master_pass(
|
||||
bot: Throttle<Bot>,
|
||||
msg: Message,
|
||||
db: DatabaseConnection,
|
||||
dialogue: MainDialogue,
|
||||
master_password: String,
|
||||
) -> 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)
|
||||
})
|
||||
.await??;
|
||||
model.insert(&db).await?;
|
||||
bot.send_message(msg.chat.id, "Success").await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn set_master_pass(
|
||||
bot: Throttle<Bot>,
|
||||
msg: Message,
|
||||
dialogue: MainDialogue,
|
||||
) -> crate::Result<()> {
|
||||
bot.send_message(msg.chat.id, "Send new master password")
|
||||
.await?;
|
||||
dialogue
|
||||
.update(State::GetPassword(package_handler(get_master_pass)))
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
Reference in New Issue
Block a user