From 3239868c496ebb5c9b4c7829520a427625d46732 Mon Sep 17 00:00:00 2001 From: Bulat Kurbanov Date: Mon, 7 Aug 2023 20:23:46 +0200 Subject: [PATCH] Fix --- src/bots/approved_bot/modules/download.rs | 164 ++++++++++++++-------- 1 file changed, 108 insertions(+), 56 deletions(-) diff --git a/src/bots/approved_bot/modules/download.rs b/src/bots/approved_bot/modules/download.rs index 380940e..8444a67 100644 --- a/src/bots/approved_bot/modules/download.rs +++ b/src/bots/approved_bot/modules/download.rs @@ -184,6 +184,51 @@ impl FromStr for DownloadArchiveQueryData { } } + +#[derive(Clone)] +pub struct CheckArchiveStatus { + pub task_id: String +} + +impl ToString for CheckArchiveStatus { + fn to_string(&self) -> String { + format!("check_da_{}", self.task_id) + } +} + +impl FromStr for CheckArchiveStatus { + type Err = strum::ParseError; + + fn from_str(s: &str) -> Result { + let re = Regex::new(r"^check_da_(?P\w+)$").unwrap(); + + let caps = re.captures(s); + let caps = match caps { + Some(v) => v, + None => return Err(strum::ParseError::VariantNotFound), + }; + + let task_id: String = caps["task_id"].parse().unwrap(); + + Ok(CheckArchiveStatus { task_id }) + } +} + + +fn get_check_keyboard(task_id: String) -> InlineKeyboardMarkup { + InlineKeyboardMarkup { + inline_keyboard: vec![ + vec![InlineKeyboardButton { + kind: teloxide::types::InlineKeyboardButtonKind::CallbackData( + (CheckArchiveStatus { task_id }).to_string(), + ), + text: String::from("Обновить статус"), + }], + ], + } +} + + async fn _send_cached( message: &Message, bot: &CacheMe>, @@ -414,57 +459,13 @@ async fn send_error_message(bot: CacheMe>, chat_id: ChatId, messag .await; } -async fn download_archive( - cq: CallbackQuery, - download_archive_query_data: DownloadArchiveQueryData, +async fn wait_archive( bot: CacheMe>, + task_id: String, + message: Message, ) -> BotHandlerInternal { - let allowed_langs = get_user_or_default_lang_codes( - cq.from.id, - ).await; - - let (id, file_type, task_type) = match download_archive_query_data { - DownloadArchiveQueryData::Sequence { id, file_type } => (id, file_type, TaskObjectType::Sequence), - DownloadArchiveQueryData::Author { id, file_type } => (id, file_type, TaskObjectType::Author), - DownloadArchiveQueryData::Translator { id, file_type } => (id, file_type, TaskObjectType::Translator), - }; - - let message = cq.message.unwrap(); - - let task = create_task(CreateTaskData { - object_id: id, - object_type: task_type, - file_format: file_type, - allowed_langs, - }).await; - - let mut task = match task { - Ok(v) => v, - Err(err) => { - bot - .edit_message_text(message.chat.id, message.id, "Ошибка! Попробуйте позже :(") - .reply_markup(InlineKeyboardMarkup { - inline_keyboard: vec![], - }) - .send() - .await?; - log::error!("{:?}", err); - return Err(err); - }, - }; - - bot - .edit_message_text(message.chat.id, message.id, "⏳ Подготовка архива...") - .reply_markup(InlineKeyboardMarkup { - inline_keyboard: vec![], - }) - .send() - .await?; - - let mut i = 15 * 60 / 5; - - while task.status == TaskStatus::InProgress && i >= 0 { - task = match get_task(task.id).await { + let task = loop { + let task = match get_task(task_id.clone()).await { Ok(v) => v, Err(err) => { send_error_message(bot, message.chat.id, message.id).await; @@ -473,22 +474,22 @@ async fn download_archive( }, }; + if task.status != TaskStatus::InProgress { + break task; + } + bot .edit_message_text( message.chat.id, message.id, format!("Статус: \n ⏳ {}", task.status_description) ) - .reply_markup(InlineKeyboardMarkup { - inline_keyboard: vec![], - }) + .reply_markup(get_check_keyboard(task.id)) .send() .await?; sleep(Duration::from_secs(5)).await; - - i -= 1; - } + }; if task.status != TaskStatus::Complete { send_error_message(bot, message.chat.id, message.id).await; @@ -514,7 +515,6 @@ async fn download_archive( ).await { Ok(_) => (), Err(err) => { - // send_error_message(bot, message.chat.id, message.id).await; let _ = bot .edit_message_text( message.chat.id, @@ -542,6 +542,51 @@ async fn download_archive( Ok(()) } + +async fn download_archive( + cq: CallbackQuery, + download_archive_query_data: DownloadArchiveQueryData, + bot: CacheMe>, +) -> BotHandlerInternal { + let allowed_langs = get_user_or_default_lang_codes( + cq.from.id, + ).await; + + let (id, file_type, task_type) = match download_archive_query_data { + DownloadArchiveQueryData::Sequence { id, file_type } => (id, file_type, TaskObjectType::Sequence), + DownloadArchiveQueryData::Author { id, file_type } => (id, file_type, TaskObjectType::Author), + DownloadArchiveQueryData::Translator { id, file_type } => (id, file_type, TaskObjectType::Translator), + }; + + let message = cq.message.unwrap(); + + let task = create_task(CreateTaskData { + object_id: id, + object_type: task_type, + file_format: file_type, + allowed_langs, + }).await; + + let task = match task { + Ok(v) => v, + Err(err) => { + send_error_message(bot, message.chat.id, message.id).await; + log::error!("{:?}", err); + return Err(err); + }, + }; + + bot + .edit_message_text(message.chat.id, message.id, "⏳ Подготовка архива...") + .reply_markup(get_check_keyboard(task.id.clone())) + .send() + .await?; + + tokio::spawn(wait_archive(bot, task.id, message)); + + Ok(()) +} + pub fn get_download_hander() -> crate::bots::BotHandler { dptree::entry() .branch( @@ -578,4 +623,11 @@ pub fn get_download_hander() -> crate::bots::BotHandler { .chain(filter_callback_query::()) .endpoint(download_archive) ) + .branch( + Update::filter_callback_query() + .chain(filter_callback_query::()) + .endpoint(|cq: CallbackQuery, status: CheckArchiveStatus, bot: CacheMe>| async move { + wait_archive(bot, status.task_id, cq.message.unwrap()).await + }) + ) }