mirror of
https://github.com/flibusta-apps/book_bot.git
synced 2025-12-06 15:35:35 +01:00
Refactor annotation module
This commit is contained in:
@@ -145,19 +145,7 @@ impl AnnotationFormat for AuthorAnnotation {
|
|||||||
async fn download_image(
|
async fn download_image(
|
||||||
file: &String,
|
file: &String,
|
||||||
) -> Result<reqwest::Response, Box<dyn std::error::Error + Send + Sync>> {
|
) -> Result<reqwest::Response, Box<dyn std::error::Error + Send + Sync>> {
|
||||||
let response = reqwest::get(file).await;
|
Ok(reqwest::get(file).await?.error_for_status()?)
|
||||||
|
|
||||||
let response = match response {
|
|
||||||
Ok(v) => v,
|
|
||||||
Err(err) => return Err(Box::new(err)),
|
|
||||||
};
|
|
||||||
|
|
||||||
let response = match response.error_for_status() {
|
|
||||||
Ok(v) => v,
|
|
||||||
Err(err) => return Err(Box::new(err)),
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(response)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn send_annotation_handler<T, Fut>(
|
pub async fn send_annotation_handler<T, Fut>(
|
||||||
@@ -175,10 +163,7 @@ where
|
|||||||
AnnotationCommand::Author { id } => id,
|
AnnotationCommand::Author { id } => id,
|
||||||
};
|
};
|
||||||
|
|
||||||
let annotation = match annotation_getter(id).await {
|
let annotation = annotation_getter(id).await?;
|
||||||
Ok(v) => v,
|
|
||||||
Err(err) => return Err(err),
|
|
||||||
};
|
|
||||||
|
|
||||||
if annotation.get_file().is_none() && !annotation.is_normal_text() {
|
if annotation.get_file().is_none() && !annotation.is_normal_text() {
|
||||||
return match bot
|
return match bot
|
||||||
@@ -202,13 +187,11 @@ where
|
|||||||
.into_async_read()
|
.into_async_read()
|
||||||
.compat();
|
.compat();
|
||||||
|
|
||||||
match bot
|
#[allow(unused_must_use)] {
|
||||||
|
bot
|
||||||
.send_photo(message.chat.id, InputFile::read(data))
|
.send_photo(message.chat.id, InputFile::read(data))
|
||||||
.send()
|
.send()
|
||||||
.await
|
.await;
|
||||||
{
|
|
||||||
Ok(_) => (),
|
|
||||||
Err(err) => log::info!("{}", err),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -230,20 +213,18 @@ where
|
|||||||
};
|
};
|
||||||
let keyboard = generic_get_pagination_keyboard(
|
let keyboard = generic_get_pagination_keyboard(
|
||||||
1,
|
1,
|
||||||
chunked_text.len().try_into().unwrap(),
|
chunked_text.len().try_into()?,
|
||||||
callback_data,
|
callback_data,
|
||||||
false,
|
false,
|
||||||
);
|
);
|
||||||
|
|
||||||
match bot
|
bot
|
||||||
.send_message(message.chat.id, current_text)
|
.send_message(message.chat.id, current_text)
|
||||||
.reply_markup(keyboard)
|
.reply_markup(keyboard)
|
||||||
.send()
|
.send()
|
||||||
.await
|
.await?;
|
||||||
{
|
|
||||||
Ok(_) => Ok(()),
|
Ok(())
|
||||||
Err(err) => Err(Box::new(err)),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn annotation_pagination_handler<T, Fut>(
|
pub async fn annotation_pagination_handler<T, Fut>(
|
||||||
@@ -261,10 +242,7 @@ where
|
|||||||
AnnotationCallbackData::Author { id, page } => (id, page),
|
AnnotationCallbackData::Author { id, page } => (id, page),
|
||||||
};
|
};
|
||||||
|
|
||||||
let annotation = match annotation_getter(id).await {
|
let annotation = annotation_getter(id).await?;
|
||||||
Ok(v) => v,
|
|
||||||
Err(err) => return Err(err),
|
|
||||||
};
|
|
||||||
|
|
||||||
let message = match cq.message {
|
let message = match cq.message {
|
||||||
Some(v) => v,
|
Some(v) => v,
|
||||||
@@ -273,7 +251,6 @@ where
|
|||||||
|
|
||||||
let request_page: usize = page.try_into().unwrap();
|
let request_page: usize = page.try_into().unwrap();
|
||||||
|
|
||||||
|
|
||||||
let annotation_text = annotation.get_text();
|
let annotation_text = annotation.get_text();
|
||||||
let chunked_text = split_text_to_chunks(annotation_text, 512);
|
let chunked_text = split_text_to_chunks(annotation_text, 512);
|
||||||
|
|
||||||
@@ -286,20 +263,18 @@ where
|
|||||||
|
|
||||||
let keyboard = generic_get_pagination_keyboard(
|
let keyboard = generic_get_pagination_keyboard(
|
||||||
page,
|
page,
|
||||||
chunked_text.len().try_into().unwrap(),
|
chunked_text.len().try_into()?,
|
||||||
callback_data,
|
callback_data,
|
||||||
false,
|
false,
|
||||||
);
|
);
|
||||||
|
|
||||||
match bot
|
bot
|
||||||
.edit_message_text(message.chat.id, message.id, current_text)
|
.edit_message_text(message.chat.id, message.id, current_text)
|
||||||
.reply_markup(keyboard)
|
.reply_markup(keyboard)
|
||||||
.send()
|
.send()
|
||||||
.await
|
.await?;
|
||||||
{
|
|
||||||
Ok(_) => Ok(()),
|
Ok(())
|
||||||
Err(err) => Err(Box::new(err)),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_annotations_handler() -> crate::bots::BotHandler {
|
pub fn get_annotations_handler() -> crate::bots::BotHandler {
|
||||||
|
|||||||
Reference in New Issue
Block a user