Switched to inline buttons when getting the account name, finished decrypt and delete Callback commands

This commit is contained in:
2023-07-29 15:21:40 +03:00
parent 5c14a77f29
commit 0139963459
17 changed files with 198 additions and 216 deletions

View File

@@ -2,36 +2,21 @@ use crate::prelude::*;
use base64::{engine::general_purpose::STANDARD_NO_PAD as B64_ENGINE, Engine as _};
use itertools::Itertools;
use sha2::{Digest, Sha256};
use teloxide::types::{InlineKeyboardButton, InlineKeyboardMarkup, KeyboardButton, KeyboardMarkup};
use teloxide::types::{InlineKeyboardButton, InlineKeyboardMarkup};
use tokio::task::spawn_blocking;
/// Creates a markup of all user's account names
#[inline]
pub async fn account_list_markup(
user_id: u64,
db: &DatabaseConnection,
) -> crate::Result<KeyboardMarkup> {
let account_names: Vec<Vec<KeyboardButton>> = Account::get_names(user_id, db)
.await?
.map_ok(KeyboardButton::new)
.try_chunks(3)
.map_err(|err| err.1)
.try_collect()
.await?;
let markup = KeyboardMarkup::new(account_names)
.resize_keyboard(true)
.one_time_keyboard(true);
Ok(markup)
}
#[inline]
pub fn menu_markup_sync(names: impl IntoIterator<Item = String>) -> InlineKeyboardMarkup {
pub fn menu_markup_sync(
command: &str,
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);
let mut data = command.to_owned();
data.reserve(44);
data.push(' ');
B64_ENGINE.encode_string(hash, &mut data);
InlineKeyboardButton::callback(name, data)
})
@@ -42,12 +27,14 @@ pub fn menu_markup_sync(names: impl IntoIterator<Item = String>) -> InlineKeyboa
#[inline]
pub async fn menu_markup(
command: impl Into<String>,
user_id: u64,
db: &DatabaseConnection,
) -> crate::Result<InlineKeyboardMarkup> {
let command: String = command.into();
let names: Vec<String> = Account::get_names(user_id, db).await?.try_collect().await?;
spawn_blocking(|| menu_markup_sync(names))
spawn_blocking(move || menu_markup_sync(&command, names))
.await
.map_err(Into::into)
}
@@ -69,26 +56,26 @@ pub fn account_markup(name: &str, is_encrypted: bool) -> InlineKeyboardMarkup {
.unwrap();
let hash = std::str::from_utf8(&hash).unwrap();
let alter_buttons = [
let encryption_button = if is_encrypted {
("Decrypt", "decrypt")
} else {
("Hide", "get")
};
let main_buttons = [
("Alter name", "an"),
("Alter login", "al"),
("Alter password", "ap"),
encryption_button,
("Delete account", "delete"),
]
.map(|(text, command)| make_button(text, command, hash));
let mut second_raw = Vec::new();
if is_encrypted {
second_raw.push(make_button("Decrypt", "decrypt", hash))
} else {
second_raw.push(make_button("Hide", "hide", hash));
}
second_raw.push(make_button("Delete account", "delete", hash));
.into_iter()
.map(|(text, command)| make_button(text, command, hash))
.chunks(3);
let menu_button = InlineKeyboardButton::callback("Back to the menu", "get_menu");
InlineKeyboardMarkup::new([alter_buttons])
.append_row(second_raw)
.append_row([menu_button])
InlineKeyboardMarkup::new(&main_buttons).append_row([menu_button])
}
/// Creates a markup with a "Delete message" button.