mirror of
https://github.com/flibusta-apps/book_bot.git
synced 2025-12-06 15:35:35 +01:00
118 lines
4.2 KiB
Rust
118 lines
4.2 KiB
Rust
pub mod modules;
|
|
pub mod services;
|
|
mod tools;
|
|
|
|
use smartstring::alias::String as SmartString;
|
|
|
|
use moka::future::Cache;
|
|
use smallvec::SmallVec;
|
|
use teloxide::{prelude::*, types::BotCommand, adaptors::{Throttle, CacheMe}};
|
|
|
|
use crate::{bots::approved_bot::services::user_settings::create_or_update_user_settings, bots_manager::AppState};
|
|
|
|
use self::{
|
|
modules::{
|
|
annotations::get_annotations_handler, book::get_book_handler,
|
|
download::get_download_hander, help::get_help_handler, random::get_random_hander,
|
|
search::get_search_handler, settings::get_settings_handler, support::get_support_handler,
|
|
update_history::get_update_log_handler,
|
|
},
|
|
services::user_settings::{get_user_or_default_lang_codes, update_user_activity},
|
|
};
|
|
|
|
use super::{ignore_channel_messages, BotCommands, BotHandler, bots_manager::get_manager_handler, ignore_chat_member_update};
|
|
|
|
async fn _update_activity(
|
|
me: teloxide::types::Me,
|
|
user: teloxide::types::User,
|
|
activity_cache: Cache<UserId, ()>,
|
|
user_langs_cache: Cache<UserId, SmallVec<[SmartString; 3]>>,
|
|
) -> Option<()> {
|
|
if activity_cache.contains_key(&user.id) {
|
|
return None;
|
|
}
|
|
|
|
tokio::spawn(async move {
|
|
let mut update_result = update_user_activity(user.id).await;
|
|
|
|
if update_result.is_err() {
|
|
let allowed_langs = get_user_or_default_lang_codes(user.id, user_langs_cache.clone()).await;
|
|
|
|
if create_or_update_user_settings(
|
|
user.id,
|
|
user.last_name.clone().unwrap_or("".to_string()),
|
|
user.first_name.clone(),
|
|
user.username.clone().unwrap_or("".to_string()),
|
|
me.username.clone().unwrap(),
|
|
allowed_langs,
|
|
user_langs_cache,
|
|
).await.is_ok()
|
|
{
|
|
update_result = update_user_activity(user.id).await;
|
|
}
|
|
}
|
|
|
|
if update_result.is_ok() {
|
|
activity_cache.insert(user.id, ()).await;
|
|
}
|
|
});
|
|
|
|
None
|
|
}
|
|
|
|
fn update_user_activity_handler() -> BotHandler {
|
|
dptree::entry()
|
|
.branch(
|
|
Update::filter_callback_query().chain(dptree::filter_map_async(
|
|
|cq: CallbackQuery, bot: CacheMe<Throttle<Bot>>, app_state: AppState| async move {
|
|
_update_activity(bot.get_me().await.unwrap(), cq.from, app_state.user_activity_cache, app_state.user_langs_cache).await
|
|
},
|
|
)),
|
|
)
|
|
.branch(Update::filter_message().chain(dptree::filter_map_async(
|
|
|message: Message, bot: CacheMe<Throttle<Bot>>, app_state: AppState| async move {
|
|
match message.from() {
|
|
Some(user) => _update_activity(bot.get_me().await.unwrap(), user.clone(), app_state.user_activity_cache, app_state.user_langs_cache).await,
|
|
None => None,
|
|
}
|
|
},
|
|
)))
|
|
}
|
|
|
|
pub fn get_approved_handler() -> (BotHandler, BotCommands) {
|
|
(
|
|
dptree::entry()
|
|
.branch(ignore_channel_messages())
|
|
.branch(ignore_chat_member_update())
|
|
.branch(update_user_activity_handler())
|
|
.branch(get_help_handler())
|
|
.branch(get_settings_handler())
|
|
.branch(get_support_handler())
|
|
.branch(get_random_hander())
|
|
.branch(get_download_hander())
|
|
.branch(get_annotations_handler())
|
|
.branch(get_book_handler())
|
|
.branch(get_update_log_handler())
|
|
.branch(get_manager_handler())
|
|
.branch(get_search_handler()),
|
|
Some(vec![
|
|
BotCommand {
|
|
command: String::from("random"),
|
|
description: String::from("🎲 Попытать удачу"),
|
|
},
|
|
BotCommand {
|
|
command: String::from("update_log"),
|
|
description: String::from("🔄 Обновления каталога"),
|
|
},
|
|
BotCommand {
|
|
command: String::from("settings"),
|
|
description: String::from("⚙️ Настройки"),
|
|
},
|
|
BotCommand {
|
|
command: String::from("donate"),
|
|
description: String::from("☕️ Поддержать разработчика"),
|
|
},
|
|
]),
|
|
)
|
|
}
|