Add download archive keyboard generator

This commit is contained in:
2023-06-07 18:27:10 +02:00
parent fcc4b9dfe2
commit 32077a02bb
2 changed files with 69 additions and 8 deletions

View File

@@ -21,8 +21,8 @@ use crate::{
download_file, get_cached_message, download_file, get_cached_message,
types::{CachedMessage, DownloadFile}, types::{CachedMessage, DownloadFile},
}, },
book_library::get_book, book_library::{get_book, get_author_books_available_types, get_translator_books_available_types, get_sequence_books_available_types},
donation_notificatioins::send_donation_notification, donation_notificatioins::send_donation_notification, user_settings::get_user_or_default_lang_codes,
}, },
tools::filter_callback_query, tools::filter_callback_query,
}, },
@@ -136,6 +136,23 @@ impl CommandParse<Self> for DownloadArchiveCommand {
} }
} }
#[derive(Clone, EnumIter)]
pub enum DownloadArchiveQueryData {
Sequence { id: u32, file_type: String },
Author { id: u32, file_type: String },
Translator { id: u32, file_type: String }
}
impl ToString for DownloadArchiveQueryData {
fn to_string(&self) -> String {
match self {
DownloadArchiveQueryData::Sequence { id, file_type } => format!("da_s_{id}_{file_type}"),
DownloadArchiveQueryData::Author { id, file_type } => format!("da_a_{id}_{file_type}"),
DownloadArchiveQueryData::Translator { id, file_type } => format!("da_t_{id}_{file_type}"),
}
}
}
async fn _send_cached( async fn _send_cached(
message: &Message, message: &Message,
bot: &CacheMe<Throttle<Bot>>, bot: &CacheMe<Throttle<Bot>>,
@@ -310,10 +327,54 @@ async fn get_download_keyboard_handler(
} }
async fn get_download_archive_keyboard_handler( async fn get_download_archive_keyboard_handler(
message: Message, bot: CacheMe<Throttle<Bot>>, _command: DownloadArchiveCommand message: Message,
bot: CacheMe<Throttle<Bot>>,
command: DownloadArchiveCommand,
user_langs_cache: Cache<UserId, Vec<String>>
) -> BotHandlerInternal { ) -> BotHandlerInternal {
let allowed_langs = get_user_or_default_lang_codes(
message.from().unwrap().id,
user_langs_cache
).await;
let available_types = match command {
DownloadArchiveCommand::Sequence { id } => get_sequence_books_available_types(id, allowed_langs).await,
DownloadArchiveCommand::Author { id } => get_author_books_available_types(id, allowed_langs).await,
DownloadArchiveCommand::Translator { id } => get_translator_books_available_types(id, allowed_langs).await,
};
let available_types = match available_types {
Ok(v) => v,
Err(err) => return Err(err),
};
let keyboard = InlineKeyboardMarkup {
inline_keyboard:
available_types.iter()
.filter(|file_type| !file_type.contains("fb2"))
.map(|file_type| {
let callback_data: String = match command {
DownloadArchiveCommand::Sequence { id } => DownloadArchiveQueryData::Sequence {
id, file_type: file_type.to_string()
}.to_string(),
DownloadArchiveCommand::Author { id } => DownloadArchiveQueryData::Author {
id, file_type: file_type.to_string()
}.to_string(),
DownloadArchiveCommand::Translator { id } => DownloadArchiveQueryData::Translator {
id, file_type: file_type.to_string()
}.to_string(),
};
vec![InlineKeyboardButton {
text: file_type.to_string(),
kind: InlineKeyboardButtonKind::CallbackData(callback_data)
}]
}).collect()
};
bot bot
.send_message(message.chat.id, "Функция в разработке") .send_message(message.chat.id, "Функция в разработке")
.reply_markup(keyboard)
.reply_to_message_id(message.id) .reply_to_message_id(message.id)
.await?; .await?;
@@ -358,9 +419,9 @@ pub fn get_download_hander() -> crate::bots::BotHandler {
Update::filter_message() Update::filter_message()
.chain(filter_command::<DownloadArchiveCommand>()) .chain(filter_command::<DownloadArchiveCommand>())
.endpoint(| .endpoint(|
message: Message, bot: CacheMe<Throttle<Bot>>, command: DownloadArchiveCommand message: Message, bot: CacheMe<Throttle<Bot>>, command: DownloadArchiveCommand, app_state: AppState
| async move { | async move {
get_download_archive_keyboard_handler(message, bot, command).await get_download_archive_keyboard_handler(message, bot, command, app_state.user_langs_cache).await
}) })
) )
} }

View File

@@ -216,7 +216,7 @@ pub async fn get_uploaded_books(
_make_request("/api/v1/books/", params).await _make_request("/api/v1/books/", params).await
} }
pub async fn _get_author_books_available_types( pub async fn get_author_books_available_types(
id: u32, id: u32,
allowed_langs: Vec<String>, allowed_langs: Vec<String>,
) -> Result<Vec<String>, Box<dyn std::error::Error + Send + Sync>> { ) -> Result<Vec<String>, Box<dyn std::error::Error + Send + Sync>> {
@@ -225,7 +225,7 @@ pub async fn _get_author_books_available_types(
_make_request(format!("/api/v1/authors/{id}/available_types").as_str(), params).await _make_request(format!("/api/v1/authors/{id}/available_types").as_str(), params).await
} }
pub async fn _get_translator_books_available_types( pub async fn get_translator_books_available_types(
id: u32, id: u32,
allowed_langs: Vec<String>, allowed_langs: Vec<String>,
) -> Result<Vec<String>, Box<dyn std::error::Error + Send + Sync>> { ) -> Result<Vec<String>, Box<dyn std::error::Error + Send + Sync>> {
@@ -234,7 +234,7 @@ pub async fn _get_translator_books_available_types(
_make_request(format!("/api/v1/translators/{id}/available_types").as_str(), params).await _make_request(format!("/api/v1/translators/{id}/available_types").as_str(), params).await
} }
pub async fn _get_sequence_books_available_types( pub async fn get_sequence_books_available_types(
id: u32, id: u32,
allowed_langs: Vec<String> allowed_langs: Vec<String>
) -> Result<Vec<String>, Box<dyn std::error::Error + Send + Sync>> { ) -> Result<Vec<String>, Box<dyn std::error::Error + Send + Sync>> {