This commit is contained in:
2023-08-07 20:23:46 +02:00
parent 0d4941229f
commit 3239868c49

View File

@@ -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<Self, Self::Err> {
let re = Regex::new(r"^check_da_(?P<task_id>\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( async fn _send_cached(
message: &Message, message: &Message,
bot: &CacheMe<Throttle<Bot>>, bot: &CacheMe<Throttle<Bot>>,
@@ -414,57 +459,13 @@ async fn send_error_message(bot: CacheMe<Throttle<Bot>>, chat_id: ChatId, messag
.await; .await;
} }
async fn download_archive( async fn wait_archive(
cq: CallbackQuery,
download_archive_query_data: DownloadArchiveQueryData,
bot: CacheMe<Throttle<Bot>>, bot: CacheMe<Throttle<Bot>>,
task_id: String,
message: Message,
) -> BotHandlerInternal { ) -> BotHandlerInternal {
let allowed_langs = get_user_or_default_lang_codes( let task = loop {
cq.from.id, let task = match get_task(task_id.clone()).await {
).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 {
Ok(v) => v, Ok(v) => v,
Err(err) => { Err(err) => {
send_error_message(bot, message.chat.id, message.id).await; 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 bot
.edit_message_text( .edit_message_text(
message.chat.id, message.chat.id,
message.id, message.id,
format!("Статус: \n{}", task.status_description) format!("Статус: \n{}", task.status_description)
) )
.reply_markup(InlineKeyboardMarkup { .reply_markup(get_check_keyboard(task.id))
inline_keyboard: vec![],
})
.send() .send()
.await?; .await?;
sleep(Duration::from_secs(5)).await; sleep(Duration::from_secs(5)).await;
};
i -= 1;
}
if task.status != TaskStatus::Complete { if task.status != TaskStatus::Complete {
send_error_message(bot, message.chat.id, message.id).await; send_error_message(bot, message.chat.id, message.id).await;
@@ -514,7 +515,6 @@ async fn download_archive(
).await { ).await {
Ok(_) => (), Ok(_) => (),
Err(err) => { Err(err) => {
// send_error_message(bot, message.chat.id, message.id).await;
let _ = bot let _ = bot
.edit_message_text( .edit_message_text(
message.chat.id, message.chat.id,
@@ -542,6 +542,51 @@ async fn download_archive(
Ok(()) Ok(())
} }
async fn download_archive(
cq: CallbackQuery,
download_archive_query_data: DownloadArchiveQueryData,
bot: CacheMe<Throttle<Bot>>,
) -> 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 { pub fn get_download_hander() -> crate::bots::BotHandler {
dptree::entry() dptree::entry()
.branch( .branch(
@@ -578,4 +623,11 @@ pub fn get_download_hander() -> crate::bots::BotHandler {
.chain(filter_callback_query::<DownloadArchiveQueryData>()) .chain(filter_callback_query::<DownloadArchiveQueryData>())
.endpoint(download_archive) .endpoint(download_archive)
) )
.branch(
Update::filter_callback_query()
.chain(filter_callback_query::<CheckArchiveStatus>())
.endpoint(|cq: CallbackQuery, status: CheckArchiveStatus, bot: CacheMe<Throttle<Bot>>| async move {
wait_archive(bot, status.task_id, cq.message.unwrap()).await
})
)
} }