pass_manager/src/markups.rs

98 lines
3.1 KiB
Rust
Raw Normal View History

use crate::prelude::*;
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};
use teloxide::types::{InlineKeyboardButton, InlineKeyboardMarkup};
use tokio::task::spawn_blocking;
2023-05-04 18:15:41 +00:00
#[inline]
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());
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)
}
#[inline]
pub async fn menu_markup(
2023-11-16 18:51:46 +00:00
command: impl Into<String> + Send,
user_id: u64,
2024-02-03 13:23:39 +00:00
db: &Pool,
) -> crate::Result<InlineKeyboardMarkup> {
let command: String = command.into();
2023-11-25 16:29:06 +00:00
let names: Vec<String> = Account::get_names(user_id, db).try_collect().await?;
spawn_blocking(move || menu_markup_sync(&command, names))
.await
.map_err(Into::into)
}
#[inline]
2024-04-16 13:02:48 +00:00
fn make_button(text: &str, command: &str, param: &str) -> InlineKeyboardButton {
let mut data = command.to_owned();
2024-04-16 13:02:48 +00:00
data.reserve(param.len() + 1);
data.push(' ');
2024-04-16 13:02:48 +00:00
data.push_str(param);
InlineKeyboardButton::callback(text, data)
}
#[inline]
2024-04-16 13:02:48 +00:00
pub fn account_markup(name: &str, is_encrypted: bool, locale: LocaleRef) -> 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();
let encryption_button = if is_encrypted {
2024-04-16 13:02:48 +00:00
(locale.decrypt_button.as_ref(), "decrypt")
} else {
2024-04-16 13:02:48 +00:00
(locale.hide_button.as_ref(), "get")
};
let main_buttons = [
2024-04-16 13:02:48 +00:00
(locale.change_name_button.as_ref(), "an"),
(locale.change_login_button.as_ref(), "al"),
(locale.change_password_button.as_ref(), "ap"),
encryption_button,
2024-04-16 13:02:48 +00:00
(locale.delete_account_button.as_ref(), "delete0"),
]
.into_iter()
.map(|(text, command)| make_button(text, command, hash))
2024-04-16 15:06:11 +00:00
.chunks(2);
2024-04-16 13:02:48 +00:00
let menu_button = InlineKeyboardButton::callback(locale.menu_button.as_ref(), "get_menu");
InlineKeyboardMarkup::new(&main_buttons).append_row([menu_button])
}
2024-04-16 13:02:48 +00:00
#[inline]
pub fn language_markup() -> InlineKeyboardMarkup {
let languages = [("🇷🇺 Русский", "ru"), ("🇬🇧 English", "en")]
.into_iter()
.map(|(text, param)| make_button(text, "change_locale", param))
2024-04-16 15:06:11 +00:00
.chunks(2);
2024-04-16 13:02:48 +00:00
InlineKeyboardMarkup::new(&languages)
}
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
#[inline]
2024-04-16 13:02:48 +00:00
pub fn deletion_markup(locale: LocaleRef) -> InlineKeyboardMarkup {
let button =
InlineKeyboardButton::callback(locale.delete_message_button.as_ref(), "delete_message");
InlineKeyboardMarkup::new([[button]])
}