From fcc4b9dfe2442fd97cdae4fa649c078e37e10827 Mon Sep 17 00:00:00 2001 From: Bulat Kurbanov Date: Wed, 7 Jun 2023 17:28:56 +0200 Subject: [PATCH] Add download archive command handler --- src/bots/approved_bot/modules/download.rs | 107 ++++++------------ .../approved_bot/services/book_cache/mod.rs | 10 +- .../services/book_library/formaters.rs | 6 +- .../approved_bot/services/book_library/mod.rs | 27 +++++ 4 files changed, 69 insertions(+), 81 deletions(-) diff --git a/src/bots/approved_bot/modules/download.rs b/src/bots/approved_bot/modules/download.rs index c2c84db..784d2e8 100644 --- a/src/bots/approved_bot/modules/download.rs +++ b/src/bots/approved_bot/modules/download.rs @@ -33,35 +33,6 @@ use crate::{ use super::utils::{filter_command, CommandParse}; -#[derive(Clone)] -pub struct DownloadDataCommand { - pub format: String, - pub id: u32, -} - -impl CommandParse for DownloadDataCommand { - fn parse(s: &str, bot_name: &str) -> Result { - let re = Regex::new(r"^/d_(?P[a-zA-Z0-9]+)_(?P\d+)$").unwrap(); - - let full_bot_name = format!("@{bot_name}"); - let after_replace = s.replace(&full_bot_name, ""); - - let caps = re.captures(&after_replace); - let caps = match caps { - Some(v) => v, - None => return Err(strum::ParseError::VariantNotFound), - }; - - let file_format = &caps["file_format"]; - let book_id: u32 = caps["book_id"].parse().unwrap(); - - Ok(DownloadDataCommand { - format: file_format.to_string(), - id: book_id, - }) - } -} - #[derive(Clone)] pub struct StartDownloadCommand { pub id: u32, @@ -144,10 +115,8 @@ impl ToString for DownloadArchiveCommand { } } -impl FromStr for DownloadArchiveCommand { - type Err = strum::ParseError; - - fn from_str(s: &str) -> Result { +impl CommandParse for DownloadArchiveCommand { + fn parse(s: &str, bot_name: &str) -> Result { let re = Regex::new(r"^/da_(?P[s|a|t])_(?P\d+)$").unwrap(); let caps = re.captures(s); @@ -189,7 +158,7 @@ async fn _send_cached( async fn send_cached_message( message: Message, bot: CacheMe>, - download_data: DownloadDataCommand, + download_data: DownloadQueryData, donation_notification_cache: Cache, need_delete_message: bool, ) -> BotHandlerInternal { @@ -244,7 +213,7 @@ async fn _send_downloaded_file( async fn send_with_download_from_channel( message: Message, bot: CacheMe>, - download_data: DownloadDataCommand, + download_data: DownloadQueryData, donation_notification_cache: Cache, need_delete_message: bool, ) -> BotHandlerInternal { @@ -266,7 +235,7 @@ async fn download_handler( message: Message, bot: CacheMe>, cache: BotCache, - download_data: DownloadDataCommand, + download_data: DownloadQueryData, donation_notification_cache: Cache, need_delete_message: bool, ) -> BotHandlerInternal { @@ -340,29 +309,19 @@ async fn get_download_keyboard_handler( Ok(()) } +async fn get_download_archive_keyboard_handler( + message: Message, bot: CacheMe>, _command: DownloadArchiveCommand +) -> BotHandlerInternal { + bot + .send_message(message.chat.id, "Функция в разработке") + .reply_to_message_id(message.id) + .await?; + + Ok(()) +} + pub fn get_download_hander() -> crate::bots::BotHandler { dptree::entry() - .branch( - Update::filter_message() - .chain(filter_command::()) - .endpoint( - |message: Message, - bot: CacheMe>, - cache: BotCache, - download_data: DownloadDataCommand, - app_state: AppState| async move { - download_handler( - message, - bot, - cache, - download_data, - app_state.chat_donation_notifications_cache, - false, - ) - .await - }, - ), - ) .branch( Update::filter_message() .chain(filter_command::()) @@ -383,23 +342,25 @@ pub fn get_download_hander() -> crate::bots::BotHandler { bot: CacheMe>, cache: BotCache, app_state: AppState| async move { - match download_query_data { - DownloadQueryData::DownloadData { book_id, file_type } => { - download_handler( - cq.message.unwrap(), - bot, - cache, - DownloadDataCommand { - format: file_type, - id: book_id, - }, - app_state.chat_donation_notifications_cache, - true, - ) - .await - } - } + download_handler( + cq.message.unwrap(), + bot, + cache, + download_query_data, + app_state.chat_donation_notifications_cache, + true, + ) + .await }, ), ) + .branch( + Update::filter_message() + .chain(filter_command::()) + .endpoint(| + message: Message, bot: CacheMe>, command: DownloadArchiveCommand + | async move { + get_download_archive_keyboard_handler(message, bot, command).await + }) + ) } diff --git a/src/bots/approved_bot/services/book_cache/mod.rs b/src/bots/approved_bot/services/book_cache/mod.rs index e8715d7..7bfa82c 100644 --- a/src/bots/approved_bot/services/book_cache/mod.rs +++ b/src/bots/approved_bot/services/book_cache/mod.rs @@ -2,7 +2,7 @@ use base64::{engine::general_purpose, Engine}; use reqwest::StatusCode; use std::fmt; -use crate::{bots::approved_bot::modules::download::DownloadDataCommand, config}; +use crate::{config, bots::approved_bot::modules::download::DownloadQueryData}; use self::types::{CachedMessage, DownloadFile}; @@ -22,9 +22,9 @@ impl fmt::Display for DownloadError { impl std::error::Error for DownloadError {} pub async fn get_cached_message( - download_data: &DownloadDataCommand, + download_data: &DownloadQueryData, ) -> Result> { - let DownloadDataCommand { format, id } = download_data; + let DownloadQueryData::DownloadData { book_id: id, file_type: format } = download_data; let client = reqwest::Client::new(); let response = client @@ -47,9 +47,9 @@ pub async fn get_cached_message( } pub async fn download_file( - download_data: &DownloadDataCommand, + download_data: &DownloadQueryData, ) -> Result> { - let DownloadDataCommand { format, id } = download_data; + let DownloadQueryData::DownloadData { book_id: id, file_type: format } = download_data; let response = reqwest::Client::new() .get(format!( diff --git a/src/bots/approved_bot/services/book_library/formaters.rs b/src/bots/approved_bot/services/book_library/formaters.rs index c22b533..1a78541 100644 --- a/src/bots/approved_bot/services/book_library/formaters.rs +++ b/src/bots/approved_bot/services/book_library/formaters.rs @@ -50,7 +50,7 @@ impl FormatTitle for BookAuthor { let command = (DownloadArchiveCommand::Author { id: *id }).to_string(); - format!("👤 {last_name} {first_name} {middle_name}\n{command}") + format!("👤 {last_name} {first_name} {middle_name}\nСкачать все книги архивом: {command}") } } @@ -69,7 +69,7 @@ impl FormatTitle for BookTranslator { let command = (DownloadArchiveCommand::Translator { id: *id }).to_string(); - format!("👤 {last_name} {first_name} {middle_name}\n{command}") + format!("👤 {last_name} {first_name} {middle_name}\nСкачать все книги архивом: {command}") } } @@ -83,7 +83,7 @@ impl FormatTitle for Sequence { let command = (DownloadArchiveCommand::Sequence { id: *id }).to_string(); - format!("📚 {name}\n{command}") + format!("📚 {name}\nСкачать все книги архивом: {command}") } } diff --git a/src/bots/approved_bot/services/book_library/mod.rs b/src/bots/approved_bot/services/book_library/mod.rs index 951fe44..90508cd 100644 --- a/src/bots/approved_bot/services/book_library/mod.rs +++ b/src/bots/approved_bot/services/book_library/mod.rs @@ -215,3 +215,30 @@ pub async fn get_uploaded_books( _make_request("/api/v1/books/", params).await } + +pub async fn _get_author_books_available_types( + id: u32, + allowed_langs: Vec, +) -> Result, Box> { + let params = get_allowed_langs_params(allowed_langs); + + _make_request(format!("/api/v1/authors/{id}/available_types").as_str(), params).await +} + +pub async fn _get_translator_books_available_types( + id: u32, + allowed_langs: Vec, +) -> Result, Box> { + let params = get_allowed_langs_params(allowed_langs); + + _make_request(format!("/api/v1/translators/{id}/available_types").as_str(), params).await +} + +pub async fn _get_sequence_books_available_types( + id: u32, + allowed_langs: Vec +) -> Result, Box> { + let params = get_allowed_langs_params(allowed_langs); + + _make_request(format!("/api/v1/sequences/{id}/available_types").as_str(), params).await +}