2023-06-29 14:33:26 +00:00
|
|
|
use crate::prelude::*;
|
2023-07-25 21:52:12 +00:00
|
|
|
use base64::{engine::general_purpose::STANDARD_NO_PAD as B64_ENGINE, Engine as _};
|
2023-07-25 14:44:12 +00:00
|
|
|
use itertools::Itertools;
|
|
|
|
use sha2::{Digest, Sha256};
|
2023-07-29 12:21:40 +00:00
|
|
|
use teloxide::types::{InlineKeyboardButton, InlineKeyboardMarkup};
|
2023-07-25 21:52:12 +00:00
|
|
|
use tokio::task::spawn_blocking;
|
2023-05-04 18:15:41 +00:00
|
|
|
|
|
|
|
#[inline]
|
2023-07-29 12:21:40 +00:00
|
|
|
pub fn menu_markup_sync(
|
|
|
|
command: &str,
|
|
|
|
names: impl IntoIterator<Item = String>,
|
|
|
|
) -> InlineKeyboardMarkup {
|
2023-07-25 16:09:34 +00:00
|
|
|
let names = names
|
|
|
|
.into_iter()
|
|
|
|
.map(|name| {
|
|
|
|
let hash = <Sha256 as Digest>::digest(name.as_bytes());
|
2023-07-29 12:21:40 +00:00
|
|
|
let mut data = command.to_owned();
|
|
|
|
data.reserve(44);
|
|
|
|
data.push(' ');
|
2023-07-25 16:09:34 +00:00
|
|
|
B64_ENGINE.encode_string(hash, &mut data);
|
|
|
|
InlineKeyboardButton::callback(name, data)
|
|
|
|
})
|
|
|
|
.chunks(3);
|
|
|
|
|
|
|
|
InlineKeyboardMarkup::new(&names)
|
|
|
|
}
|
|
|
|
|
2023-07-25 21:52:12 +00:00
|
|
|
#[inline]
|
|
|
|
pub async fn menu_markup(
|
2023-11-16 18:51:46 +00:00
|
|
|
command: impl Into<String> + Send,
|
2023-07-25 21:52:12 +00:00
|
|
|
user_id: u64,
|
|
|
|
db: &DatabaseConnection,
|
|
|
|
) -> crate::Result<InlineKeyboardMarkup> {
|
2023-07-29 12:21:40 +00:00
|
|
|
let command: String = command.into();
|
2023-07-25 21:52:12 +00:00
|
|
|
let names: Vec<String> = Account::get_names(user_id, db).await?.try_collect().await?;
|
|
|
|
|
2023-07-29 12:21:40 +00:00
|
|
|
spawn_blocking(move || menu_markup_sync(&command, names))
|
2023-07-25 21:52:12 +00:00
|
|
|
.await
|
|
|
|
.map_err(Into::into)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn make_button(text: &str, command: &str, hash: &str) -> InlineKeyboardButton {
|
|
|
|
let mut data = command.to_owned();
|
|
|
|
data.reserve(44);
|
|
|
|
data.push(' ');
|
|
|
|
data.push_str(hash);
|
|
|
|
InlineKeyboardButton::callback(text, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn account_markup(name: &str, is_encrypted: bool) -> InlineKeyboardMarkup {
|
|
|
|
let mut hash = [0; 43];
|
|
|
|
B64_ENGINE
|
|
|
|
.encode_slice(<Sha256 as Digest>::digest(name), &mut hash)
|
|
|
|
.unwrap();
|
|
|
|
let hash = std::str::from_utf8(&hash).unwrap();
|
|
|
|
|
2023-07-29 12:21:40 +00:00
|
|
|
let encryption_button = if is_encrypted {
|
|
|
|
("Decrypt", "decrypt")
|
|
|
|
} else {
|
|
|
|
("Hide", "get")
|
|
|
|
};
|
|
|
|
|
|
|
|
let main_buttons = [
|
2023-07-25 21:52:12 +00:00
|
|
|
("Alter name", "an"),
|
|
|
|
("Alter login", "al"),
|
|
|
|
("Alter password", "ap"),
|
2023-07-29 12:21:40 +00:00
|
|
|
encryption_button,
|
2023-08-11 12:15:35 +00:00
|
|
|
("Delete account", "delete0"),
|
2023-07-25 21:52:12 +00:00
|
|
|
]
|
2023-07-29 12:21:40 +00:00
|
|
|
.into_iter()
|
|
|
|
.map(|(text, command)| make_button(text, command, hash))
|
|
|
|
.chunks(3);
|
2023-07-25 21:52:12 +00:00
|
|
|
|
|
|
|
let menu_button = InlineKeyboardButton::callback("Back to the menu", "get_menu");
|
|
|
|
|
2023-07-29 12:21:40 +00:00
|
|
|
InlineKeyboardMarkup::new(&main_buttons).append_row([menu_button])
|
2023-07-25 21:52:12 +00:00
|
|
|
}
|
|
|
|
|
2023-05-09 17:27:58 +00:00
|
|
|
/// Creates a markup with a "Delete message" button.
|
|
|
|
/// This markup should be added for all messages that won't be deleted afterwards
|
2023-05-06 18:02:32 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn deletion_markup() -> InlineKeyboardMarkup {
|
|
|
|
let button = InlineKeyboardButton::callback("Delete message", "delete_message");
|
|
|
|
InlineKeyboardMarkup::new([[button]])
|
|
|
|
}
|