Added message deletion

This commit is contained in:
StNicolay 2023-05-04 20:51:36 +03:00
parent e129934aac
commit 89d3ea1bf7
Signed by: StNicolay
GPG Key ID: 9693D04DCD962B0D
6 changed files with 60 additions and 22 deletions

View File

@ -1,7 +1,7 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.11.2
use chacha20poly1305::{aead::Aead, AeadCore, ChaCha20Poly1305, KeyInit};
use futures::{Stream, StreamExt};
use futures::{Stream, TryStreamExt};
use pbkdf2::pbkdf2_hmac_array;
use rand::{rngs::OsRng, RngCore};
use sea_orm::{prelude::*, ActiveValue::Set, QuerySelect};
@ -99,8 +99,7 @@ impl Entity {
.filter(Column::UserId.eq(user_id))
.into_tuple()
.stream(db)
.await?
.map(|item| item.map_err(Into::into));
Ok(result)
.await?;
Ok(result.map_err(Into::into))
}
}

View File

@ -9,13 +9,14 @@ async fn get_master_pass(
msg: Message,
db: DatabaseConnection,
dialogue: MainDialogue,
previous: Message,
name: String,
login: String,
password: String,
master_pass: String,
) -> crate::Result<()> {
let _ = bot.delete_message(previous.chat.id, previous.id).await;
let user_id = msg.from().unwrap().id.0;
let _ = bot.delete_message(msg.chat.id, msg.id).await;
dialogue.exit().await?;
let account = spawn_blocking(move || {
account::ActiveModel::from_unencrypted(user_id, name, &login, &password, &master_pass)
@ -31,17 +32,28 @@ async fn get_password(
msg: Message,
_: DatabaseConnection,
dialogue: MainDialogue,
previous: Message,
name: String,
login: String,
password: String,
) -> crate::Result<()> {
let _ = bot.delete_message(msg.chat.id, msg.id).await;
let _ = bot.delete_message(previous.chat.id, previous.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)
get_master_pass(
bot,
msg,
db,
dialogue,
previous,
name,
login,
password,
master_pass,
)
},
)))
.await?;
@ -53,15 +65,16 @@ async fn get_login(
msg: Message,
_: DatabaseConnection,
dialogue: MainDialogue,
previous: Message,
name: String,
login: String,
) -> crate::Result<()> {
let _ = bot.delete_message(msg.chat.id, msg.id).await;
let _ = bot.delete_message(previous.chat.id, previous.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)
get_password(bot, msg, db, dialogue, previous, name, login, password)
},
)))
.await?;
@ -73,13 +86,16 @@ async fn get_account_name(
msg: Message,
_: DatabaseConnection,
dialogue: MainDialogue,
previous: Message,
name: String,
) -> crate::Result<()> {
let _ = bot.delete_message(msg.chat.id, msg.id).await;
let _ = bot.delete_message(previous.chat.id, previous.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),
move |bot, msg, db, dialogue, login| {
get_login(bot, msg, db, dialogue, previous, name, login)
},
)))
.await?;
Ok(())
@ -88,12 +104,15 @@ async fn get_account_name(
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?;
let previous = bot.send_message(msg.chat.id, "Send account name").await?;
dialogue
.update(State::GetAccountName(package_handler(get_account_name)))
.update(State::GetAccountName(package_handler(
move |bot, msg, db, dialogue, name| {
get_account_name(bot, msg, db, dialogue, previous, name)
},
)))
.await?;
Ok(())
}

View File

@ -15,10 +15,11 @@ async fn get_master_pass(
msg: Message,
db: DatabaseConnection,
dialogue: MainDialogue,
previous: Message,
name: String,
master_pass: String,
) -> crate::Result<()> {
let _ = bot.delete_message(msg.chat.id, msg.id).await;
let _ = bot.delete_message(previous.chat.id, previous.id).await;
dialogue.exit().await?;
let user_id = msg.from().unwrap().id.0;
let account = Account::find()
@ -40,15 +41,17 @@ async fn get_account_name(
msg: Message,
_: DatabaseConnection,
dialogue: MainDialogue,
previous: Message,
name: String,
) -> crate::Result<()> {
let _ = bot.delete_message(msg.chat.id, msg.id).await;
bot.send_message(msg.chat.id, "Send master password")
let _ = bot.delete_message(previous.chat.id, previous.id).await;
let previous = 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)
get_master_pass(bot, msg, db, dialogue, previous, name, master_pass)
},
)))
.await?;
@ -68,11 +71,16 @@ pub async fn get_account(
.try_collect()
.await?;
let markup = KeyboardMarkup::new(account_names).resize_keyboard(true);
bot.send_message(msg.chat.id, "Send account name")
let previous = bot
.send_message(msg.chat.id, "Send account name")
.reply_markup(markup)
.await?;
dialogue
.update(State::GetAccountName(package_handler(get_account_name)))
.update(State::GetAccountName(package_handler(
move |bot, msg, db, dialogue, name| {
get_account_name(bot, msg, db, dialogue, previous, name)
},
)))
.await?;
Ok(())
}

View File

@ -11,8 +11,10 @@ async fn get_master_pass(
msg: Message,
db: DatabaseConnection,
dialogue: MainDialogue,
previous: Message,
master_password: String,
) -> crate::Result<()> {
let _ = bot.delete_message(previous.chat.id, previous.id).await;
dialogue.exit().await?;
let user_id = msg.from().unwrap().id.0;
let model = task::spawn_blocking(move || {
@ -36,10 +38,15 @@ pub async fn set_master_pass(
.await?;
return Ok(());
}
bot.send_message(msg.chat.id, "Send new master password")
let previous = bot
.send_message(msg.chat.id, "Send new master password")
.await?;
dialogue
.update(State::GetPassword(package_handler(get_master_pass)))
.update(State::GetPassword(package_handler(
move |bot, msg, db, dialogue, master_pass| {
get_master_pass(bot, msg, db, dialogue, previous, master_pass)
},
)))
.await?;
Ok(())
}

View File

@ -72,6 +72,7 @@ pub fn get_dispatcher(
.branch(case![Command::SetMasterPass].endpoint(commands::set_master_pass));
let message_handler = Update::filter_message()
.map_async(utils::delete_message)
.enter_dialogue::<Update, InMemStorage<State>, State>()
.branch(command_handler)
.branch(case![State::GetAccountName(next)].endpoint(state::get_account_name))

View File

@ -17,3 +17,7 @@ where
move |bot, msg, db, dialogue, val| Box::pin(f(bot, msg, db, dialogue, val)),
))))
}
pub async fn delete_message(bot: Throttle<Bot>, msg: Message) {
let _ = bot.delete_message(msg.chat.id, msg.id).await;
}