mirror of
https://github.com/flibusta-apps/book_bot.git
synced 2025-12-06 15:35:35 +01:00
Use cache-me
This commit is contained in:
@@ -2,7 +2,7 @@ use std::{convert::TryInto, str::FromStr};
|
||||
|
||||
use futures::TryStreamExt;
|
||||
use regex::Regex;
|
||||
use teloxide::{dispatching::UpdateFilterExt, dptree, prelude::*, types::*, adaptors::Throttle};
|
||||
use teloxide::{dispatching::UpdateFilterExt, dptree, prelude::*, types::*, adaptors::{Throttle, CacheMe}};
|
||||
use tokio_util::compat::FuturesAsyncReadCompatExt;
|
||||
|
||||
use crate::bots::{
|
||||
@@ -162,7 +162,7 @@ async fn download_image(
|
||||
|
||||
pub async fn send_annotation_handler<T, Fut>(
|
||||
message: Message,
|
||||
bot: Throttle<Bot>,
|
||||
bot: CacheMe<Throttle<Bot>>,
|
||||
command: AnnotationCommand,
|
||||
annotation_getter: fn(id: u32) -> Fut,
|
||||
) -> BotHandlerInternal
|
||||
@@ -248,7 +248,7 @@ where
|
||||
|
||||
pub async fn annotation_pagination_handler<T, Fut>(
|
||||
cq: CallbackQuery,
|
||||
bot: Throttle<Bot>,
|
||||
bot: CacheMe<Throttle<Bot>>,
|
||||
callback_data: AnnotationCallbackData,
|
||||
annotation_getter: fn(id: u32) -> Fut,
|
||||
) -> BotHandlerInternal
|
||||
@@ -308,7 +308,7 @@ pub fn get_annotations_handler() -> crate::bots::BotHandler {
|
||||
Update::filter_message()
|
||||
.chain(filter_command::<AnnotationCommand>())
|
||||
.endpoint(
|
||||
|message: Message, bot: Throttle<Bot>, command: AnnotationCommand| async move {
|
||||
|message: Message, bot: CacheMe<Throttle<Bot>>, command: AnnotationCommand| async move {
|
||||
match command {
|
||||
AnnotationCommand::Book { .. } => {
|
||||
send_annotation_handler(message, bot, command, get_book_annotation)
|
||||
@@ -332,7 +332,7 @@ pub fn get_annotations_handler() -> crate::bots::BotHandler {
|
||||
.chain(filter_callback_query::<AnnotationCallbackData>())
|
||||
.endpoint(
|
||||
|cq: CallbackQuery,
|
||||
bot: Throttle<Bot>,
|
||||
bot: CacheMe<Throttle<Bot>>,
|
||||
callback_data: AnnotationCallbackData| async move {
|
||||
match callback_data {
|
||||
AnnotationCallbackData::Book { .. } => {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use std::str::FromStr;
|
||||
|
||||
use regex::Regex;
|
||||
use teloxide::{dispatching::UpdateFilterExt, dptree, prelude::*, adaptors::Throttle};
|
||||
use teloxide::{dispatching::UpdateFilterExt, dptree, prelude::*, adaptors::{Throttle, CacheMe}};
|
||||
|
||||
use crate::bots::approved_bot::{
|
||||
services::{
|
||||
@@ -114,7 +114,7 @@ impl GetPaginationCallbackData for BookCallbackData {
|
||||
|
||||
async fn send_book_handler<T, Fut>(
|
||||
message: Message,
|
||||
bot: Throttle<Bot>,
|
||||
bot: CacheMe<Throttle<Bot>>,
|
||||
command: BookCommand,
|
||||
books_getter: fn(id: u32, page: u32, allowed_langs: Vec<String>) -> Fut,
|
||||
) -> crate::bots::BotHandlerInternal
|
||||
@@ -196,7 +196,7 @@ where
|
||||
|
||||
async fn send_pagination_book_handler<T, Fut>(
|
||||
cq: CallbackQuery,
|
||||
bot: Throttle<Bot>,
|
||||
bot: CacheMe<Throttle<Bot>>,
|
||||
callback_data: BookCallbackData,
|
||||
books_getter: fn(id: u32, page: u32, allowed_langs: Vec<String>) -> Fut,
|
||||
) -> crate::bots::BotHandlerInternal
|
||||
@@ -295,7 +295,7 @@ pub fn get_book_handler() -> crate::bots::BotHandler {
|
||||
Update::filter_message()
|
||||
.chain(filter_command::<BookCommand>())
|
||||
.endpoint(
|
||||
|message: Message, bot: Throttle<Bot>, command: BookCommand| async move {
|
||||
|message: Message, bot: CacheMe<Throttle<Bot>>, command: BookCommand| async move {
|
||||
match command {
|
||||
BookCommand::Author { .. } => {
|
||||
send_book_handler(
|
||||
@@ -331,7 +331,7 @@ pub fn get_book_handler() -> crate::bots::BotHandler {
|
||||
.branch(
|
||||
Update::filter_callback_query()
|
||||
.chain(filter_callback_query::<BookCallbackData>())
|
||||
.endpoint(|cq: CallbackQuery, bot: Throttle<Bot>, callback_data: BookCallbackData| async move {
|
||||
.endpoint(|cq: CallbackQuery, bot: CacheMe<Throttle<Bot>>, callback_data: BookCallbackData| async move {
|
||||
match callback_data {
|
||||
BookCallbackData::Author { .. } => send_pagination_book_handler(cq, bot, callback_data, get_author_books).await,
|
||||
BookCallbackData::Translator { .. } => send_pagination_book_handler(cq, bot, callback_data, get_translator_books).await,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use futures::TryStreamExt;
|
||||
use regex::Regex;
|
||||
use teloxide::{dispatching::UpdateFilterExt, dptree, prelude::*, types::*, adaptors::Throttle};
|
||||
use teloxide::{dispatching::UpdateFilterExt, dptree, prelude::*, types::*, adaptors::{Throttle, CacheMe}};
|
||||
use tokio_util::compat::FuturesAsyncReadCompatExt;
|
||||
|
||||
use crate::{
|
||||
@@ -47,7 +47,7 @@ impl CommandParse<Self> for DownloadData {
|
||||
|
||||
async fn _send_cached(
|
||||
message: &Message,
|
||||
bot: &Throttle<Bot>,
|
||||
bot: &CacheMe<Throttle<Bot>>,
|
||||
cached_message: CachedMessage,
|
||||
) -> BotHandlerInternal {
|
||||
match bot
|
||||
@@ -66,7 +66,7 @@ async fn _send_cached(
|
||||
|
||||
async fn send_cached_message(
|
||||
message: Message,
|
||||
bot: Throttle<Bot>,
|
||||
bot: CacheMe<Throttle<Bot>>,
|
||||
download_data: DownloadData,
|
||||
) -> BotHandlerInternal {
|
||||
if let Ok(v) = get_cached_message(&download_data).await {
|
||||
@@ -83,7 +83,7 @@ async fn send_cached_message(
|
||||
|
||||
async fn _send_downloaded_file(
|
||||
message: &Message,
|
||||
bot: Throttle<Bot>,
|
||||
bot: CacheMe<Throttle<Bot>>,
|
||||
downloaded_data: DownloadFile,
|
||||
) -> BotHandlerInternal {
|
||||
let DownloadFile {
|
||||
@@ -113,7 +113,7 @@ async fn _send_downloaded_file(
|
||||
|
||||
async fn send_with_download_from_channel(
|
||||
message: Message,
|
||||
bot: Throttle<Bot>,
|
||||
bot: CacheMe<Throttle<Bot>>,
|
||||
download_data: DownloadData,
|
||||
) -> BotHandlerInternal {
|
||||
match download_file(&download_data).await {
|
||||
@@ -127,7 +127,7 @@ async fn send_with_download_from_channel(
|
||||
|
||||
async fn download_handler(
|
||||
message: Message,
|
||||
bot: Throttle<Bot>,
|
||||
bot: CacheMe<Throttle<Bot>>,
|
||||
cache: BotCache,
|
||||
download_data: DownloadData,
|
||||
) -> BotHandlerInternal {
|
||||
@@ -143,7 +143,7 @@ pub fn get_download_hander() -> crate::bots::BotHandler {
|
||||
.chain(filter_command::<DownloadData>())
|
||||
.endpoint(
|
||||
|message: Message,
|
||||
bot: Throttle<Bot>,
|
||||
bot: CacheMe<Throttle<Bot>>,
|
||||
cache: BotCache,
|
||||
download_data: DownloadData| async move {
|
||||
download_handler(message, bot, cache, download_data).await
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use crate::bots::BotHandlerInternal;
|
||||
|
||||
use teloxide::{prelude::*, utils::command::BotCommands, types::ParseMode, adaptors::Throttle};
|
||||
use teloxide::{prelude::*, utils::command::BotCommands, types::ParseMode, adaptors::{Throttle, CacheMe}};
|
||||
|
||||
#[derive(BotCommands, Clone)]
|
||||
#[command(rename_rule = "lowercase")]
|
||||
@@ -10,7 +10,7 @@ enum HelpCommand {
|
||||
}
|
||||
|
||||
|
||||
pub async fn help_handler(message: Message, bot: Throttle<Bot>) -> BotHandlerInternal {
|
||||
pub async fn help_handler(message: Message, bot: CacheMe<Throttle<Bot>>) -> BotHandlerInternal {
|
||||
let name = message
|
||||
.from()
|
||||
.map(|user| user.first_name.clone())
|
||||
|
||||
@@ -2,7 +2,7 @@ use strum_macros::{Display, EnumIter};
|
||||
use teloxide::{
|
||||
prelude::*,
|
||||
types::{InlineKeyboardButton, InlineKeyboardMarkup},
|
||||
utils::command::BotCommands, adaptors::Throttle,
|
||||
utils::command::BotCommands, adaptors::{Throttle, CacheMe},
|
||||
};
|
||||
|
||||
use crate::bots::{
|
||||
@@ -75,7 +75,7 @@ impl std::str::FromStr for RandomCallbackData {
|
||||
}
|
||||
}
|
||||
|
||||
async fn random_handler(message: Message, bot: Throttle<Bot>) -> crate::bots::BotHandlerInternal {
|
||||
async fn random_handler(message: Message, bot: CacheMe<Throttle<Bot>>) -> crate::bots::BotHandlerInternal {
|
||||
const MESSAGE_TEXT: &str = "Что хотим получить?";
|
||||
|
||||
let keyboard = InlineKeyboardMarkup {
|
||||
@@ -122,7 +122,7 @@ async fn random_handler(message: Message, bot: Throttle<Bot>) -> crate::bots::Bo
|
||||
|
||||
async fn get_random_item_handler_internal<T>(
|
||||
cq: CallbackQuery,
|
||||
bot: Throttle<Bot>,
|
||||
bot: CacheMe<Throttle<Bot>>,
|
||||
item: Result<T, Box<dyn std::error::Error + Send + Sync>>,
|
||||
) -> BotHandlerInternal
|
||||
where
|
||||
@@ -176,7 +176,7 @@ where
|
||||
|
||||
async fn get_random_item_handler<T, Fut>(
|
||||
cq: CallbackQuery,
|
||||
bot: Throttle<Bot>,
|
||||
bot: CacheMe<Throttle<Bot>>,
|
||||
item_getter: fn(allowed_langs: Vec<String>) -> Fut,
|
||||
) -> BotHandlerInternal
|
||||
where
|
||||
@@ -190,7 +190,7 @@ where
|
||||
get_random_item_handler_internal(cq, bot, item).await
|
||||
}
|
||||
|
||||
async fn get_genre_metas_handler(cq: CallbackQuery, bot: Throttle<Bot>) -> BotHandlerInternal {
|
||||
async fn get_genre_metas_handler(cq: CallbackQuery, bot: CacheMe<Throttle<Bot>>) -> BotHandlerInternal {
|
||||
let genre_metas = match book_library::get_genre_metas().await {
|
||||
Ok(v) => v,
|
||||
Err(err) => return Err(err),
|
||||
@@ -243,7 +243,7 @@ async fn get_genre_metas_handler(cq: CallbackQuery, bot: Throttle<Bot>) -> BotHa
|
||||
|
||||
async fn get_genres_by_meta_handler(
|
||||
cq: CallbackQuery,
|
||||
bot: Throttle<Bot>,
|
||||
bot: CacheMe<Throttle<Bot>>,
|
||||
genre_index: u32,
|
||||
) -> BotHandlerInternal {
|
||||
let genre_metas = match book_library::get_genre_metas().await {
|
||||
@@ -323,7 +323,7 @@ async fn get_genres_by_meta_handler(
|
||||
|
||||
async fn get_random_book_by_genre(
|
||||
cq: CallbackQuery,
|
||||
bot: Throttle<Bot>,
|
||||
bot: CacheMe<Throttle<Bot>>,
|
||||
genre_id: u32,
|
||||
) -> BotHandlerInternal {
|
||||
let allowed_langs = get_user_or_default_lang_codes(cq.from.id).await;
|
||||
@@ -350,7 +350,7 @@ pub fn get_random_hander() -> crate::bots::BotHandler {
|
||||
.branch(
|
||||
Update::filter_callback_query()
|
||||
.chain(filter_callback_query::<RandomCallbackData>())
|
||||
.endpoint(|cq: CallbackQuery, callback_data: RandomCallbackData, bot: Throttle<Bot>| async move {
|
||||
.endpoint(|cq: CallbackQuery, callback_data: RandomCallbackData, bot: CacheMe<Throttle<Bot>>| async move {
|
||||
match callback_data {
|
||||
RandomCallbackData::RandomBook => get_random_item_handler(cq, bot, book_library::get_random_book).await,
|
||||
RandomCallbackData::RandomAuthor => get_random_item_handler(cq, bot, book_library::get_random_author).await,
|
||||
|
||||
@@ -4,7 +4,7 @@ use regex::Regex;
|
||||
use strum_macros::EnumIter;
|
||||
use teloxide::{
|
||||
prelude::*,
|
||||
types::{InlineKeyboardButton, InlineKeyboardMarkup}, dispatching::dialogue::GetChatId, adaptors::Throttle,
|
||||
types::{InlineKeyboardButton, InlineKeyboardMarkup}, dispatching::dialogue::GetChatId, adaptors::{Throttle, CacheMe},
|
||||
};
|
||||
|
||||
use crate::bots::{
|
||||
@@ -107,7 +107,7 @@ fn get_query(cq: CallbackQuery) -> Option<String> {
|
||||
|
||||
async fn generic_search_pagination_handler<T, Fut>(
|
||||
cq: CallbackQuery,
|
||||
bot: Throttle<Bot>,
|
||||
bot: CacheMe<Throttle<Bot>>,
|
||||
search_data: SearchCallbackData,
|
||||
items_getter: fn(query: String, page: u32, allowed_langs: Vec<String>) -> Fut,
|
||||
) -> BotHandlerInternal
|
||||
@@ -217,7 +217,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn message_handler(message: Message, bot: Throttle<Bot>) -> BotHandlerInternal {
|
||||
pub async fn message_handler(message: Message, bot: CacheMe<Throttle<Bot>>) -> BotHandlerInternal {
|
||||
let message_text = "Что ищем?";
|
||||
|
||||
let keyboard = InlineKeyboardMarkup {
|
||||
@@ -268,7 +268,7 @@ pub fn get_search_handler() -> crate::bots::BotHandler {
|
||||
).branch(
|
||||
Update::filter_callback_query()
|
||||
.chain(filter_callback_query::<SearchCallbackData>())
|
||||
.endpoint(|cq: CallbackQuery, callback_data: SearchCallbackData, bot: Throttle<Bot>| async move {
|
||||
.endpoint(|cq: CallbackQuery, callback_data: SearchCallbackData, bot: CacheMe<Throttle<Bot>>| async move {
|
||||
match callback_data {
|
||||
SearchCallbackData::SearchBook { .. } => generic_search_pagination_handler(cq, bot, callback_data, search_book).await,
|
||||
SearchCallbackData::SearchAuthors { .. } => generic_search_pagination_handler(cq, bot, callback_data, search_author).await,
|
||||
|
||||
@@ -14,7 +14,7 @@ use regex::Regex;
|
||||
use teloxide::{
|
||||
prelude::*,
|
||||
types::{InlineKeyboardButton, InlineKeyboardMarkup, Me},
|
||||
utils::command::BotCommands, adaptors::Throttle,
|
||||
utils::command::BotCommands, adaptors::{Throttle, CacheMe},
|
||||
};
|
||||
|
||||
#[derive(BotCommands, Clone)]
|
||||
@@ -67,7 +67,7 @@ impl ToString for SettingsCallbackData {
|
||||
}
|
||||
}
|
||||
|
||||
async fn settings_handler(message: Message, bot: Throttle<Bot>) -> BotHandlerInternal {
|
||||
async fn settings_handler(message: Message, bot: CacheMe<Throttle<Bot>>) -> BotHandlerInternal {
|
||||
let keyboard = InlineKeyboardMarkup {
|
||||
inline_keyboard: vec![vec![InlineKeyboardButton {
|
||||
text: "Языки".to_string(),
|
||||
@@ -117,7 +117,7 @@ fn get_lang_keyboard(all_langs: Vec<Lang>, allowed_langs: HashSet<String>) -> In
|
||||
|
||||
async fn settings_callback_handler(
|
||||
cq: CallbackQuery,
|
||||
bot: Throttle<Bot>,
|
||||
bot: CacheMe<Throttle<Bot>>,
|
||||
callback_data: SettingsCallbackData,
|
||||
me: Me,
|
||||
) -> BotHandlerInternal {
|
||||
@@ -219,7 +219,7 @@ pub fn get_settings_handler() -> crate::bots::BotHandler {
|
||||
.chain(filter_callback_query::<SettingsCallbackData>())
|
||||
.endpoint(
|
||||
|cq: CallbackQuery,
|
||||
bot: Throttle<Bot>,
|
||||
bot: CacheMe<Throttle<Bot>>,
|
||||
callback_data: SettingsCallbackData,
|
||||
me: Me| async move {
|
||||
settings_callback_handler(cq, bot, callback_data, me).await
|
||||
|
||||
@@ -3,7 +3,7 @@ use crate::bots::BotHandlerInternal;
|
||||
use teloxide::{
|
||||
prelude::*,
|
||||
types::{InlineKeyboardButton, InlineKeyboardMarkup},
|
||||
utils::command::BotCommands, adaptors::Throttle,
|
||||
utils::command::BotCommands, adaptors::{Throttle, CacheMe},
|
||||
};
|
||||
|
||||
#[derive(BotCommands, Clone)]
|
||||
@@ -12,7 +12,7 @@ enum SupportCommand {
|
||||
Support,
|
||||
}
|
||||
|
||||
pub async fn support_command_handler(message: Message, bot: Throttle<Bot>) -> BotHandlerInternal {
|
||||
pub async fn support_command_handler(message: Message, bot: CacheMe<Throttle<Bot>>) -> BotHandlerInternal {
|
||||
const MESSAGE_TEXT: &str = "
|
||||
[Лицензии](https://github.com/flibusta-apps/book_bot/blob/main/LICENSE.md)
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ use regex::Regex;
|
||||
use teloxide::{
|
||||
prelude::*,
|
||||
types::{InlineKeyboardButton, InlineKeyboardMarkup},
|
||||
utils::command::BotCommands, adaptors::Throttle,
|
||||
utils::command::BotCommands, adaptors::{Throttle, CacheMe},
|
||||
};
|
||||
|
||||
use super::utils::{generic_get_pagination_keyboard, GetPaginationCallbackData};
|
||||
@@ -77,7 +77,7 @@ impl GetPaginationCallbackData for UpdateLogCallbackData {
|
||||
}
|
||||
}
|
||||
|
||||
async fn update_log_command(message: Message, bot: Throttle<Bot>) -> BotHandlerInternal {
|
||||
async fn update_log_command(message: Message, bot: CacheMe<Throttle<Bot>>) -> BotHandlerInternal {
|
||||
let now = Utc::now().date_naive();
|
||||
let d3 = now - Duration::days(3);
|
||||
let d7 = now - Duration::days(7);
|
||||
@@ -134,7 +134,7 @@ async fn update_log_command(message: Message, bot: Throttle<Bot>) -> BotHandlerI
|
||||
|
||||
async fn update_log_pagination_handler(
|
||||
cq: CallbackQuery,
|
||||
bot: Throttle<Bot>,
|
||||
bot: CacheMe<Throttle<Bot>>,
|
||||
update_callback_data: UpdateLogCallbackData,
|
||||
) -> BotHandlerInternal {
|
||||
let message = match cq.message {
|
||||
@@ -223,7 +223,7 @@ pub fn get_update_log_handler() -> crate::bots::BotHandler {
|
||||
.chain(filter_callback_query::<UpdateLogCallbackData>())
|
||||
.endpoint(
|
||||
|cq: CallbackQuery,
|
||||
bot: Throttle<Bot>,
|
||||
bot: CacheMe<Throttle<Bot>>,
|
||||
update_log_data: UpdateLogCallbackData| async move {
|
||||
update_log_pagination_handler(cq, bot, update_log_data).await
|
||||
},
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use teloxide::{prelude::*, adaptors::Throttle};
|
||||
use teloxide::{prelude::*, adaptors::{Throttle, CacheMe}};
|
||||
|
||||
use std::error::Error;
|
||||
|
||||
@@ -10,7 +10,7 @@ pub mod utils;
|
||||
|
||||
pub async fn message_handler(
|
||||
message: Message,
|
||||
bot: Throttle<Bot>,
|
||||
bot: CacheMe<Throttle<Bot>>,
|
||||
) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
let from_user = message.from().unwrap();
|
||||
let text = message.text().unwrap_or("");
|
||||
|
||||
@@ -3,7 +3,7 @@ pub mod bots_manager;
|
||||
|
||||
use std::error::Error;
|
||||
|
||||
use teloxide::{prelude::*, adaptors::Throttle};
|
||||
use teloxide::{prelude::*, adaptors::{Throttle, CacheMe}};
|
||||
|
||||
pub type BotHandlerInternal = Result<(), Box<dyn Error + Send + Sync>>;
|
||||
|
||||
@@ -27,7 +27,7 @@ fn ignore_chat_member_update() -> crate::bots::BotHandler {
|
||||
}
|
||||
|
||||
fn get_pending_handler() -> BotHandler {
|
||||
let handler = |msg: Message, bot: Throttle<Bot>| async move {
|
||||
let handler = |msg: Message, bot: CacheMe<Throttle<Bot>>| async move {
|
||||
let message_text = "
|
||||
Бот зарегистрирован, но не подтвержден администратором! \
|
||||
Подтверждение занимает примерно 12 часов.
|
||||
@@ -44,7 +44,7 @@ fn get_pending_handler() -> BotHandler {
|
||||
}
|
||||
|
||||
fn get_blocked_handler() -> BotHandler {
|
||||
let handler = |msg: Message, bot: Throttle<Bot>| async move {
|
||||
let handler = |msg: Message, bot: CacheMe<Throttle<Bot>>| async move {
|
||||
let message_text = "Бот заблокирован!";
|
||||
|
||||
bot.send_message(msg.chat.id, message_text).await?;
|
||||
|
||||
Reference in New Issue
Block a user