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,
|
2024-02-03 13:23:39 +00:00
|
|
|
db: &Pool,
|
2023-07-25 21:52:12 +00:00
|
|
|
) -> crate::Result<InlineKeyboardMarkup> {
|
2023-07-29 12:21:40 +00:00
|
|
|
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?;
|
2023-07-25 21:52:12 +00:00
|
|
|
|
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]
|
2024-04-16 13:02:48 +00:00
|
|
|
fn make_button(text: &str, command: &str, param: &str) -> InlineKeyboardButton {
|
2023-07-25 21:52:12 +00:00
|
|
|
let mut data = command.to_owned();
|
2024-04-16 13:02:48 +00:00
|
|
|
data.reserve(param.len() + 1);
|
2023-07-25 21:52:12 +00:00
|
|
|
data.push(' ');
|
2024-04-16 13:02:48 +00:00
|
|
|
data.push_str(param);
|
2023-07-25 21:52:12 +00:00
|
|
|
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 {
|
2023-07-25 21:52:12 +00:00
|
|
|
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 {
|
2024-04-16 13:02:48 +00:00
|
|
|
(locale.decrypt_button.as_ref(), "decrypt")
|
2023-07-29 12:21:40 +00:00
|
|
|
} else {
|
2024-04-16 13:02:48 +00:00
|
|
|
(locale.hide_button.as_ref(), "get")
|
2023-07-29 12:21:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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"),
|
|
|
|
(locale.delete_account_button.as_ref(), "delete0"),
|
2024-04-16 15:13:08 +00:00
|
|
|
encryption_button,
|
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))
|
2024-04-16 15:06:11 +00:00
|
|
|
.chunks(2);
|
2023-07-25 21:52:12 +00:00
|
|
|
|
2024-04-16 13:02:48 +00:00
|
|
|
let menu_button = InlineKeyboardButton::callback(locale.menu_button.as_ref(), "get_menu");
|
2023-07-25 21:52:12 +00:00
|
|
|
|
2023-07-29 12:21:40 +00:00
|
|
|
InlineKeyboardMarkup::new(&main_buttons).append_row([menu_button])
|
2023-07-25 21:52:12 +00:00
|
|
|
}
|
|
|
|
|
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
|
2023-05-06 18:02:32 +00:00
|
|
|
#[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");
|
2023-05-06 18:02:32 +00:00
|
|
|
InlineKeyboardMarkup::new([[button]])
|
|
|
|
}
|