Added menu endpoints
This commit is contained in:
parent
64a5435dc3
commit
96c7d2e37e
28
src/callbacks/get_menu.rs
Normal file
28
src/callbacks/get_menu.rs
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
use crate::prelude::*;
|
||||||
|
use tokio::task::spawn_blocking;
|
||||||
|
|
||||||
|
pub async fn get_menu(
|
||||||
|
bot: Throttle<Bot>,
|
||||||
|
q: CallbackQuery,
|
||||||
|
db: DatabaseConnection,
|
||||||
|
) -> crate::Result<()> {
|
||||||
|
let user_id = q.from.id.0;
|
||||||
|
let msg = q.message.as_ref().unwrap();
|
||||||
|
|
||||||
|
let names: Vec<String> = Account::get_names(user_id, &db)
|
||||||
|
.await?
|
||||||
|
.try_collect()
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
if names.is_empty() {
|
||||||
|
bot.edit_message_text(msg.chat.id, msg.id, "You don't have any accounts")
|
||||||
|
.reply_markup(deletion_markup())
|
||||||
|
.await?;
|
||||||
|
}
|
||||||
|
|
||||||
|
let markup = spawn_blocking(|| menu_markup(names)).await?;
|
||||||
|
bot.edit_message_text(msg.chat.id, msg.id, "Choose your account")
|
||||||
|
.reply_markup(markup)
|
||||||
|
.await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
@ -1,8 +1,10 @@
|
|||||||
//! This module consists of endpoints to handle callbacks
|
//! This module consists of endpoints to handle callbacks
|
||||||
|
|
||||||
mod delete_message;
|
mod delete_message;
|
||||||
|
mod get_menu;
|
||||||
|
|
||||||
pub use delete_message::delete_message;
|
pub use delete_message::delete_message;
|
||||||
|
pub use get_menu::get_menu;
|
||||||
|
|
||||||
use crate::errors::InvalidCommand;
|
use crate::errors::InvalidCommand;
|
||||||
use base64::{engine::general_purpose::STANDARD_NO_PAD as B64_ENGINE, Engine as _};
|
use base64::{engine::general_purpose::STANDARD_NO_PAD as B64_ENGINE, Engine as _};
|
||||||
@ -23,7 +25,6 @@ pub enum CallbackCommand {
|
|||||||
DeleteMessage,
|
DeleteMessage,
|
||||||
Get(NameHash),
|
Get(NameHash),
|
||||||
GetMenu,
|
GetMenu,
|
||||||
GetAccounts,
|
|
||||||
Decrypt(NameHash),
|
Decrypt(NameHash),
|
||||||
Hide(NameHash),
|
Hide(NameHash),
|
||||||
Alter(NameHash, AlterableField),
|
Alter(NameHash, AlterableField),
|
||||||
@ -47,7 +48,6 @@ impl FromStr for CallbackCommand {
|
|||||||
match s {
|
match s {
|
||||||
"delete_message" => return Ok(DeleteMessage),
|
"delete_message" => return Ok(DeleteMessage),
|
||||||
"get_menu" => return Ok(GetMenu),
|
"get_menu" => return Ok(GetMenu),
|
||||||
"get_accounts" => return Ok(GetAccounts),
|
|
||||||
_ => (),
|
_ => (),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
23
src/commands/menu.rs
Normal file
23
src/commands/menu.rs
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
use crate::prelude::*;
|
||||||
|
use tokio::task::spawn_blocking;
|
||||||
|
|
||||||
|
pub async fn menu(bot: Throttle<Bot>, msg: Message, db: DatabaseConnection) -> crate::Result<()> {
|
||||||
|
let user_id = msg.from().ok_or(NoUserInfo)?.id.0;
|
||||||
|
|
||||||
|
let names: Vec<String> = Account::get_names(user_id, &db)
|
||||||
|
.await?
|
||||||
|
.try_collect()
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
if names.is_empty() {
|
||||||
|
bot.send_message(msg.chat.id, "You don't have any accounts")
|
||||||
|
.reply_markup(deletion_markup())
|
||||||
|
.await?;
|
||||||
|
}
|
||||||
|
|
||||||
|
let markup = spawn_blocking(|| menu_markup(names)).await?;
|
||||||
|
bot.send_message(msg.chat.id, "Choose your account")
|
||||||
|
.reply_markup(markup)
|
||||||
|
.await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
@ -10,6 +10,7 @@ mod get_account;
|
|||||||
mod get_accounts;
|
mod get_accounts;
|
||||||
mod help;
|
mod help;
|
||||||
mod import;
|
mod import;
|
||||||
|
mod menu;
|
||||||
mod set_master_pass;
|
mod set_master_pass;
|
||||||
mod start;
|
mod start;
|
||||||
|
|
||||||
@ -23,6 +24,7 @@ pub use get_account::get_account;
|
|||||||
pub use get_accounts::get_accounts;
|
pub use get_accounts::get_accounts;
|
||||||
pub use help::help;
|
pub use help::help;
|
||||||
pub use import::import;
|
pub use import::import;
|
||||||
|
pub use menu::menu;
|
||||||
pub use set_master_pass::set_master_pass;
|
pub use set_master_pass::set_master_pass;
|
||||||
pub use start::start;
|
pub use start::start;
|
||||||
|
|
||||||
@ -56,6 +58,8 @@ pub enum Command {
|
|||||||
Import,
|
Import,
|
||||||
#[command(description = "generates 10 secure passwords")]
|
#[command(description = "generates 10 secure passwords")]
|
||||||
GenPassword,
|
GenPassword,
|
||||||
|
#[command(description = "gives you a menu to manage your accounts")]
|
||||||
|
Menu,
|
||||||
#[command(description = "cancels the current action")]
|
#[command(description = "cancels the current action")]
|
||||||
Cancel,
|
Cancel,
|
||||||
}
|
}
|
||||||
|
@ -38,6 +38,7 @@ fn get_dispatcher(
|
|||||||
.branch(case![Command::Cancel].endpoint(commands::cancel))
|
.branch(case![Command::Cancel].endpoint(commands::cancel))
|
||||||
// This branch filters out the users that don't have a master password set
|
// This branch filters out the users that don't have a master password set
|
||||||
.branch(master_password_check::get_handler())
|
.branch(master_password_check::get_handler())
|
||||||
|
.branch(case![Command::Menu].endpoint(commands::menu))
|
||||||
.branch(case![Command::AddAccount].endpoint(commands::add_account))
|
.branch(case![Command::AddAccount].endpoint(commands::add_account))
|
||||||
.branch(case![Command::GetAccount].endpoint(commands::get_account))
|
.branch(case![Command::GetAccount].endpoint(commands::get_account))
|
||||||
.branch(case![Command::GetAccounts].endpoint(commands::get_accounts))
|
.branch(case![Command::GetAccounts].endpoint(commands::get_accounts))
|
||||||
@ -61,6 +62,7 @@ fn get_dispatcher(
|
|||||||
|
|
||||||
let callback_handler = Update::filter_callback_query()
|
let callback_handler = Update::filter_callback_query()
|
||||||
.filter_map(CallbackCommand::from_query)
|
.filter_map(CallbackCommand::from_query)
|
||||||
|
.branch(case![CallbackCommand::GetMenu].endpoint(callbacks::get_menu))
|
||||||
.branch(case![CallbackCommand::DeleteMessage].endpoint(callbacks::delete_message));
|
.branch(case![CallbackCommand::DeleteMessage].endpoint(callbacks::delete_message));
|
||||||
|
|
||||||
let handler = dptree::entry()
|
let handler = dptree::entry()
|
||||||
|
@ -24,6 +24,22 @@ pub async fn account_markup(
|
|||||||
Ok(markup)
|
Ok(markup)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn menu_markup(names: impl IntoIterator<Item = String>) -> InlineKeyboardMarkup {
|
||||||
|
let names = names
|
||||||
|
.into_iter()
|
||||||
|
.map(|name| {
|
||||||
|
let hash = <Sha256 as Digest>::digest(name.as_bytes());
|
||||||
|
let mut data = "get ".to_owned();
|
||||||
|
data.reserve(43);
|
||||||
|
B64_ENGINE.encode_string(hash, &mut data);
|
||||||
|
InlineKeyboardButton::callback(name, data)
|
||||||
|
})
|
||||||
|
.chunks(3);
|
||||||
|
|
||||||
|
InlineKeyboardMarkup::new(&names)
|
||||||
|
}
|
||||||
|
|
||||||
/// Creates a markup with a "Delete message" button.
|
/// Creates a markup with a "Delete message" button.
|
||||||
/// This markup should be added for all messages that won't be deleted afterwards
|
/// This markup should be added for all messages that won't be deleted afterwards
|
||||||
#[inline]
|
#[inline]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user