Added button for deleting the resulting message
This commit is contained in:
		
							
								
								
									
										18
									
								
								src/handlers/callbacks/delete_message.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								src/handlers/callbacks/delete_message.rs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,18 @@
 | 
			
		||||
use teloxide::{adaptors::Throttle, prelude::*};
 | 
			
		||||
 | 
			
		||||
pub async fn run(bot: Throttle<Bot>, q: CallbackQuery) -> crate::Result<()> {
 | 
			
		||||
    match q.message {
 | 
			
		||||
        Some(msg) => {
 | 
			
		||||
            bot.delete_message(msg.chat.id, msg.id).await?;
 | 
			
		||||
            Ok(())
 | 
			
		||||
        }
 | 
			
		||||
        None => Ok(()),
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub fn filter(q: CallbackQuery) -> bool {
 | 
			
		||||
    match q.data.as_deref() {
 | 
			
		||||
        Some("delete_message") => true,
 | 
			
		||||
        _ => false,
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										1
									
								
								src/handlers/callbacks/mod.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								src/handlers/callbacks/mod.rs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1 @@
 | 
			
		||||
pub mod delete_message;
 | 
			
		||||
@@ -1,6 +1,8 @@
 | 
			
		||||
use crate::entity::{account, prelude::Account};
 | 
			
		||||
use crate::errors::NoUserInfo;
 | 
			
		||||
use crate::handlers::{utils::package_handler, MainDialogue, State};
 | 
			
		||||
use crate::{
 | 
			
		||||
    entity::{account, prelude::Account},
 | 
			
		||||
    errors::NoUserInfo,
 | 
			
		||||
    handlers::{markups::deletion_markup, utils::package_handler, MainDialogue, State},
 | 
			
		||||
};
 | 
			
		||||
use sea_orm::prelude::*;
 | 
			
		||||
use teloxide::{adaptors::Throttle, prelude::*};
 | 
			
		||||
use tokio::task::spawn_blocking;
 | 
			
		||||
@@ -24,7 +26,9 @@ async fn get_master_pass(
 | 
			
		||||
    })
 | 
			
		||||
    .await??;
 | 
			
		||||
    account.insert(&db).await?;
 | 
			
		||||
    bot.send_message(msg.chat.id, "Success").await?;
 | 
			
		||||
    bot.send_message(msg.chat.id, "Success")
 | 
			
		||||
        .reply_markup(deletion_markup())
 | 
			
		||||
        .await?;
 | 
			
		||||
    Ok(())
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -95,6 +99,7 @@ async fn get_account_name(
 | 
			
		||||
    let user_id = msg.from().ok_or(NoUserInfo)?.id.0;
 | 
			
		||||
    if Account::exists(user_id, &name, &db).await? {
 | 
			
		||||
        bot.send_message(msg.chat.id, "Account alreay exists")
 | 
			
		||||
            .reply_markup(deletion_markup())
 | 
			
		||||
            .await?;
 | 
			
		||||
        return Ok(());
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -1,6 +1,9 @@
 | 
			
		||||
use crate::handlers::markups::deletion_markup;
 | 
			
		||||
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?;
 | 
			
		||||
    bot.send_message(msg.chat.id, "Unknown command")
 | 
			
		||||
        .reply_markup(deletion_markup())
 | 
			
		||||
        .await?;
 | 
			
		||||
    Ok(())
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,7 +1,11 @@
 | 
			
		||||
use crate::{
 | 
			
		||||
    entity::prelude::Account,
 | 
			
		||||
    errors::NoUserInfo,
 | 
			
		||||
    handlers::{markups, utils::package_handler, MainDialogue, State},
 | 
			
		||||
    handlers::{
 | 
			
		||||
        markups::{self, deletion_markup},
 | 
			
		||||
        utils::package_handler,
 | 
			
		||||
        MainDialogue, State,
 | 
			
		||||
    },
 | 
			
		||||
};
 | 
			
		||||
use sea_orm::prelude::*;
 | 
			
		||||
use teloxide::{adaptors::Throttle, prelude::*};
 | 
			
		||||
@@ -19,6 +23,7 @@ async fn get_master_pass(
 | 
			
		||||
    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(())
 | 
			
		||||
}
 | 
			
		||||
@@ -35,6 +40,7 @@ async fn get_account_name(
 | 
			
		||||
    let user_id = msg.from().ok_or(NoUserInfo)?.id.0;
 | 
			
		||||
    if !Account::exists(user_id, &name, &db).await? {
 | 
			
		||||
        bot.send_message(msg.chat.id, "Account doesn't exists")
 | 
			
		||||
            .reply_markup(deletion_markup())
 | 
			
		||||
            .await?;
 | 
			
		||||
        return Ok(());
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -1,7 +1,7 @@
 | 
			
		||||
use crate::{
 | 
			
		||||
    entity::{account, prelude::*},
 | 
			
		||||
    errors::NoUserInfo,
 | 
			
		||||
    handlers::{utils::package_handler, MainDialogue, State},
 | 
			
		||||
    handlers::{markups::deletion_markup, utils::package_handler, MainDialogue, State},
 | 
			
		||||
};
 | 
			
		||||
use sea_orm::prelude::*;
 | 
			
		||||
use teloxide::{adaptors::Throttle, prelude::*};
 | 
			
		||||
@@ -22,6 +22,7 @@ async fn get_master_pass(
 | 
			
		||||
        .await?;
 | 
			
		||||
    MasterPass::delete_by_id(user_id).exec(&db).await?;
 | 
			
		||||
    bot.send_message(msg.chat.id, "Everything was deleted")
 | 
			
		||||
        .reply_markup(deletion_markup())
 | 
			
		||||
        .await?;
 | 
			
		||||
    Ok(())
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,7 +1,7 @@
 | 
			
		||||
use crate::{
 | 
			
		||||
    entity::prelude::Account,
 | 
			
		||||
    errors::NoUserInfo,
 | 
			
		||||
    handlers::{utils::package_handler, MainDialogue, State},
 | 
			
		||||
    handlers::{markups::deletion_markup, utils::package_handler, MainDialogue, State},
 | 
			
		||||
    models::{DecryptedAccount, User},
 | 
			
		||||
};
 | 
			
		||||
use futures::TryStreamExt;
 | 
			
		||||
@@ -45,7 +45,9 @@ async fn get_master_pass(
 | 
			
		||||
    accounts.sort_unstable_by(|this, other| this.name.cmp(&other.name));
 | 
			
		||||
    let json = to_string_pretty(&User { accounts })?;
 | 
			
		||||
    let file = InputFile::memory(json).file_name("accounts.json");
 | 
			
		||||
    bot.send_document(msg.chat.id, file).await?;
 | 
			
		||||
    bot.send_document(msg.chat.id, file)
 | 
			
		||||
        .reply_markup(deletion_markup())
 | 
			
		||||
        .await?;
 | 
			
		||||
    dialogue.exit().await?;
 | 
			
		||||
    Ok(())
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,7 +1,11 @@
 | 
			
		||||
use crate::{
 | 
			
		||||
    entity::prelude::Account,
 | 
			
		||||
    errors::NoUserInfo,
 | 
			
		||||
    handlers::{markups, utils::package_handler, MainDialogue, State},
 | 
			
		||||
    handlers::{
 | 
			
		||||
        markups::{self, deletion_markup},
 | 
			
		||||
        utils::package_handler,
 | 
			
		||||
        MainDialogue, State,
 | 
			
		||||
    },
 | 
			
		||||
};
 | 
			
		||||
use sea_orm::prelude::*;
 | 
			
		||||
use teloxide::{adaptors::Throttle, prelude::*, types::ParseMode};
 | 
			
		||||
@@ -22,13 +26,16 @@ async fn get_master_pass(
 | 
			
		||||
    let account = match Account::get(user_id, &name, &db).await? {
 | 
			
		||||
        Some(account) => account,
 | 
			
		||||
        None => {
 | 
			
		||||
            bot.send_message(msg.chat.id, "Account not found").await?;
 | 
			
		||||
            bot.send_message(msg.chat.id, "Account not found")
 | 
			
		||||
                .reply_markup(deletion_markup())
 | 
			
		||||
                .await?;
 | 
			
		||||
            return Ok(());
 | 
			
		||||
        }
 | 
			
		||||
    };
 | 
			
		||||
    let (login, password) = spawn_blocking(move || account.decrypt(&master_pass)).await??;
 | 
			
		||||
    let message = format!("Name:\n`{name}`\nLogin:\n`{login}`\nPassword:\n`{password}`");
 | 
			
		||||
    bot.send_message(msg.chat.id, message)
 | 
			
		||||
        .reply_markup(deletion_markup())
 | 
			
		||||
        .parse_mode(ParseMode::MarkdownV2)
 | 
			
		||||
        .await?;
 | 
			
		||||
    Ok(())
 | 
			
		||||
@@ -46,6 +53,7 @@ async fn get_account_name(
 | 
			
		||||
    let user_id = msg.from().ok_or(NoUserInfo)?.id.0;
 | 
			
		||||
    if !Account::exists(user_id, &name, &db).await? {
 | 
			
		||||
        bot.send_message(msg.chat.id, "Account doesn't exists")
 | 
			
		||||
            .reply_markup(deletion_markup())
 | 
			
		||||
            .await?;
 | 
			
		||||
        return Ok(());
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -1,4 +1,4 @@
 | 
			
		||||
use crate::{entity::prelude::Account, errors::NoUserInfo};
 | 
			
		||||
use crate::{entity::prelude::Account, errors::NoUserInfo, handlers::markups::deletion_markup};
 | 
			
		||||
use futures::TryStreamExt;
 | 
			
		||||
use sea_orm::prelude::*;
 | 
			
		||||
use teloxide::{adaptors::Throttle, prelude::*, types::ParseMode};
 | 
			
		||||
@@ -13,7 +13,9 @@ pub async fn get_accounts(
 | 
			
		||||
    let mut result = match account_names.try_next().await? {
 | 
			
		||||
        Some(name) => format!("Accounts:\n`{name}`"),
 | 
			
		||||
        None => {
 | 
			
		||||
            bot.send_message(msg.chat.id, "No accounts found").await?;
 | 
			
		||||
            bot.send_message(msg.chat.id, "No accounts found")
 | 
			
		||||
                .reply_markup(deletion_markup())
 | 
			
		||||
                .await?;
 | 
			
		||||
            return Ok(());
 | 
			
		||||
        }
 | 
			
		||||
    };
 | 
			
		||||
@@ -25,6 +27,7 @@ pub async fn get_accounts(
 | 
			
		||||
        .await?;
 | 
			
		||||
    bot.send_message(msg.chat.id, result)
 | 
			
		||||
        .parse_mode(ParseMode::MarkdownV2)
 | 
			
		||||
        .reply_markup(deletion_markup())
 | 
			
		||||
        .await?;
 | 
			
		||||
    Ok(())
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,8 +1,9 @@
 | 
			
		||||
use crate::handlers::Command;
 | 
			
		||||
use crate::handlers::{markups::deletion_markup, 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())
 | 
			
		||||
        .reply_markup(deletion_markup())
 | 
			
		||||
        .await?;
 | 
			
		||||
    Ok(())
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,6 +1,6 @@
 | 
			
		||||
use crate::{
 | 
			
		||||
    errors::NoUserInfo,
 | 
			
		||||
    handlers::{utils::package_handler, MainDialogue, State},
 | 
			
		||||
    handlers::{markups::deletion_markup, utils::package_handler, MainDialogue, State},
 | 
			
		||||
    models::{DecryptedAccount, User},
 | 
			
		||||
};
 | 
			
		||||
use futures::{stream, StreamExt, TryStreamExt};
 | 
			
		||||
@@ -46,15 +46,17 @@ async fn get_master_pass(
 | 
			
		||||
        Ok(accounts) => accounts.into_inner(),
 | 
			
		||||
        Err(_) => unreachable!(),
 | 
			
		||||
    };
 | 
			
		||||
    if failed.is_empty() {
 | 
			
		||||
        bot.send_message(msg.chat.id, "Success").await?;
 | 
			
		||||
    let message = if failed.is_empty() {
 | 
			
		||||
        "Success".to_owned()
 | 
			
		||||
    } else {
 | 
			
		||||
        let text = format!(
 | 
			
		||||
        format!(
 | 
			
		||||
            "Failed to create the following accounts:\n{}",
 | 
			
		||||
            failed.into_iter().format("\n")
 | 
			
		||||
        );
 | 
			
		||||
        bot.send_message(msg.chat.id, text).await?;
 | 
			
		||||
    }
 | 
			
		||||
        )
 | 
			
		||||
    };
 | 
			
		||||
    bot.send_message(msg.chat.id, message)
 | 
			
		||||
        .reply_markup(deletion_markup())
 | 
			
		||||
        .await?;
 | 
			
		||||
    dialogue.exit().await?;
 | 
			
		||||
    Ok(())
 | 
			
		||||
}
 | 
			
		||||
@@ -71,6 +73,7 @@ async fn get_document(
 | 
			
		||||
        Some(doc) => doc,
 | 
			
		||||
        None => {
 | 
			
		||||
            bot.send_message(msg.chat.id, "You didn't send a file")
 | 
			
		||||
                .reply_markup(deletion_markup())
 | 
			
		||||
                .await?;
 | 
			
		||||
            dialogue.exit().await?;
 | 
			
		||||
            return Ok(());
 | 
			
		||||
@@ -79,7 +82,9 @@ async fn get_document(
 | 
			
		||||
    match document.file_name {
 | 
			
		||||
        Some(ref name) if name.trim().ends_with(".json") => (),
 | 
			
		||||
        _ => {
 | 
			
		||||
            bot.send_message(msg.chat.id, "Invalid file name").await?;
 | 
			
		||||
            bot.send_message(msg.chat.id, "Invalid file name")
 | 
			
		||||
                .reply_markup(deletion_markup())
 | 
			
		||||
                .await?;
 | 
			
		||||
            dialogue.exit().await?;
 | 
			
		||||
            return Ok(());
 | 
			
		||||
        }
 | 
			
		||||
@@ -96,13 +101,14 @@ async fn get_document(
 | 
			
		||||
        Ok(user) => user.accounts,
 | 
			
		||||
        Err(_) => {
 | 
			
		||||
            bot.send_message(msg.chat.id, "Error parsing the json file")
 | 
			
		||||
                .reply_markup(deletion_markup())
 | 
			
		||||
                .await?;
 | 
			
		||||
            dialogue.exit().await?;
 | 
			
		||||
            return Ok(());
 | 
			
		||||
        }
 | 
			
		||||
    };
 | 
			
		||||
    let previous = bot
 | 
			
		||||
        .send_message(msg.chat.id, "Send a new master password")
 | 
			
		||||
        .send_message(msg.chat.id, "Send master password")
 | 
			
		||||
        .await?;
 | 
			
		||||
    dialogue
 | 
			
		||||
        .update(State::GetMasterPass(package_handler(
 | 
			
		||||
 
 | 
			
		||||
@@ -1,7 +1,7 @@
 | 
			
		||||
use crate::{
 | 
			
		||||
    entity::{master_pass, prelude::*},
 | 
			
		||||
    errors::NoUserInfo,
 | 
			
		||||
    handlers::{utils::package_handler, MainDialogue, State},
 | 
			
		||||
    handlers::{markups::deletion_markup, utils::package_handler, MainDialogue, State},
 | 
			
		||||
};
 | 
			
		||||
use sea_orm::prelude::*;
 | 
			
		||||
use teloxide::{adaptors::Throttle, prelude::*};
 | 
			
		||||
@@ -23,7 +23,9 @@ async fn get_master_pass(
 | 
			
		||||
    })
 | 
			
		||||
    .await??;
 | 
			
		||||
    model.insert(&db).await?;
 | 
			
		||||
    bot.send_message(msg.chat.id, "Success").await?;
 | 
			
		||||
    bot.send_message(msg.chat.id, "Success")
 | 
			
		||||
        .reply_markup(deletion_markup())
 | 
			
		||||
        .await?;
 | 
			
		||||
    Ok(())
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -36,6 +38,7 @@ pub async fn set_master_pass(
 | 
			
		||||
    let user_id = msg.from().ok_or(NoUserInfo)?.id.0;
 | 
			
		||||
    if MasterPass::exists(user_id, &db).await? {
 | 
			
		||||
        bot.send_message(msg.chat.id, "Master password already exists")
 | 
			
		||||
            .reply_markup(deletion_markup())
 | 
			
		||||
            .await?;
 | 
			
		||||
        return Ok(());
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -1,7 +1,7 @@
 | 
			
		||||
use crate::entity::prelude::Account;
 | 
			
		||||
use futures::TryStreamExt;
 | 
			
		||||
use sea_orm::prelude::*;
 | 
			
		||||
use teloxide::types::{KeyboardButton, KeyboardMarkup};
 | 
			
		||||
use teloxide::types::{InlineKeyboardButton, InlineKeyboardMarkup, KeyboardButton, KeyboardMarkup};
 | 
			
		||||
 | 
			
		||||
#[inline]
 | 
			
		||||
pub async fn account_markup(
 | 
			
		||||
@@ -19,3 +19,9 @@ pub async fn account_markup(
 | 
			
		||||
        .one_time_keyboard(true);
 | 
			
		||||
    Ok(markup)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[inline]
 | 
			
		||||
pub fn deletion_markup() -> InlineKeyboardMarkup {
 | 
			
		||||
    let button = InlineKeyboardButton::callback("Delete message", "delete_message");
 | 
			
		||||
    InlineKeyboardMarkup::new([[button]])
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,3 +1,4 @@
 | 
			
		||||
mod callbacks;
 | 
			
		||||
mod commands;
 | 
			
		||||
mod markups;
 | 
			
		||||
mod state;
 | 
			
		||||
@@ -96,7 +97,15 @@ pub fn get_dispatcher(
 | 
			
		||||
        .branch(command_handler)
 | 
			
		||||
        .branch(endpoint(commands::default));
 | 
			
		||||
 | 
			
		||||
    Dispatcher::builder(bot, message_handler)
 | 
			
		||||
    let callback_handler = Update::filter_callback_query()
 | 
			
		||||
        .filter(callbacks::delete_message::filter)
 | 
			
		||||
        .endpoint(callbacks::delete_message::run);
 | 
			
		||||
 | 
			
		||||
    let handler = dptree::entry()
 | 
			
		||||
        .branch(message_handler)
 | 
			
		||||
        .branch(callback_handler);
 | 
			
		||||
 | 
			
		||||
    Dispatcher::builder(bot, handler)
 | 
			
		||||
        .dependencies(deps![db, InMemStorage::<State>::new()])
 | 
			
		||||
        .enable_ctrlc_handler()
 | 
			
		||||
        .build()
 | 
			
		||||
 
 | 
			
		||||
@@ -1,8 +1,7 @@
 | 
			
		||||
use crate::{errors::HandlerUsed, handlers::markups::deletion_markup, PinnedFuture};
 | 
			
		||||
use sea_orm::prelude::*;
 | 
			
		||||
use teloxide::{adaptors::Throttle, prelude::*};
 | 
			
		||||
 | 
			
		||||
use crate::{errors::HandlerUsed, PinnedFuture};
 | 
			
		||||
 | 
			
		||||
pub async fn generic<F>(
 | 
			
		||||
    bot: Throttle<Bot>,
 | 
			
		||||
    text: String,
 | 
			
		||||
@@ -23,6 +22,7 @@ where
 | 
			
		||||
    if text == "/cancel" {
 | 
			
		||||
        dialogue.exit().await?;
 | 
			
		||||
        bot.send_message(msg.chat.id, "Successfully cancelled")
 | 
			
		||||
            .reply_markup(deletion_markup())
 | 
			
		||||
            .await?;
 | 
			
		||||
        return Ok(());
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -1,7 +1,7 @@
 | 
			
		||||
use crate::{
 | 
			
		||||
    entity::prelude::MasterPass,
 | 
			
		||||
    errors::{NoMessageText, NoUserInfo},
 | 
			
		||||
    handlers::{MainDialogue, PackagedHandler},
 | 
			
		||||
    handlers::{markups::deletion_markup, MainDialogue, PackagedHandler},
 | 
			
		||||
};
 | 
			
		||||
use sea_orm::prelude::*;
 | 
			
		||||
use teloxide::{adaptors::Throttle, prelude::*};
 | 
			
		||||
@@ -19,11 +19,13 @@ pub async fn check_master_pass<'a>(
 | 
			
		||||
        Ok(Some(true)) => Ok(true),
 | 
			
		||||
        Ok(Some(false)) => {
 | 
			
		||||
            bot.send_message(msg.chat.id, "Wrong master password")
 | 
			
		||||
                .reply_markup(deletion_markup())
 | 
			
		||||
                .await?;
 | 
			
		||||
            Ok(false)
 | 
			
		||||
        }
 | 
			
		||||
        Ok(None) => {
 | 
			
		||||
            bot.send_message(msg.chat.id, "No master password set")
 | 
			
		||||
                .reply_markup(deletion_markup())
 | 
			
		||||
                .await?;
 | 
			
		||||
            Ok(false)
 | 
			
		||||
        }
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user