diff --git a/src/bots/approved_bot/modules/download.rs b/src/bots/approved_bot/modules/download.rs index c06a7e9..963f7b8 100644 --- a/src/bots/approved_bot/modules/download.rs +++ b/src/bots/approved_bot/modules/download.rs @@ -1,17 +1,18 @@ use futures::TryStreamExt; +use moka::future::Cache; use regex::Regex; use teloxide::{dispatching::UpdateFilterExt, dptree, prelude::*, types::*, adaptors::{Throttle, CacheMe}}; use tokio_util::compat::FuturesAsyncReadCompatExt; use crate::{ bots::{ - approved_bot::services::book_cache::{ + approved_bot::services::{book_cache::{ download_file, get_cached_message, types::{CachedMessage, DownloadFile}, - }, + }, donation_notificatioins::send_donation_notification}, BotHandlerInternal, }, - bots_manager::BotCache, + bots_manager::{BotCache, AppState}, }; use super::utils::{filter_command, CommandParse}; @@ -68,14 +69,17 @@ async fn send_cached_message( message: Message, bot: CacheMe>, download_data: DownloadData, + donation_notification_cache: Cache, ) -> BotHandlerInternal { if let Ok(v) = get_cached_message(&download_data).await { if _send_cached(&message, &bot, v).await.is_ok() { + send_donation_notification(bot.clone(), message, donation_notification_cache).await?; + return Ok(()); } }; - send_with_download_from_channel(message, bot, download_data).await?; + send_with_download_from_channel(message, bot, download_data, donation_notification_cache).await?; Ok(()) } @@ -84,6 +88,7 @@ async fn _send_downloaded_file( message: &Message, bot: CacheMe>, downloaded_data: DownloadFile, + donation_notification_cache: Cache, ) -> BotHandlerInternal { let DownloadFile { response, @@ -105,6 +110,8 @@ async fn _send_downloaded_file( .send() .await?; + send_donation_notification(bot, message.clone(), donation_notification_cache).await?; + Ok(()) } @@ -112,12 +119,10 @@ async fn send_with_download_from_channel( message: Message, bot: CacheMe>, download_data: DownloadData, + donation_notification_cache: Cache, ) -> BotHandlerInternal { match download_file(&download_data).await { - Ok(v) => match _send_downloaded_file(&message, bot, v).await { - Ok(v_2) => Ok(v_2), - Err(err) => Err(err), - }, + Ok(v) => Ok(_send_downloaded_file(&message, bot, v, donation_notification_cache).await?), Err(err) => Err(err), } } @@ -127,10 +132,11 @@ async fn download_handler( bot: CacheMe>, cache: BotCache, download_data: DownloadData, + donation_notification_cache: Cache, ) -> BotHandlerInternal { match cache { - BotCache::Original => send_cached_message(message, bot, download_data).await, - BotCache::NoCache => send_with_download_from_channel(message, bot, download_data).await, + BotCache::Original => send_cached_message(message, bot, download_data, donation_notification_cache).await, + BotCache::NoCache => send_with_download_from_channel(message, bot, download_data, donation_notification_cache).await, } } @@ -142,8 +148,15 @@ pub fn get_download_hander() -> crate::bots::BotHandler { |message: Message, bot: CacheMe>, cache: BotCache, - download_data: DownloadData| async move { - download_handler(message, bot, cache, download_data).await + download_data: DownloadData, + app_state: AppState| async move { + download_handler( + message, + bot, + cache, + download_data, + app_state.chat_donation_notifications_cache + ).await }, ), ) diff --git a/src/bots/approved_bot/modules/support.rs b/src/bots/approved_bot/modules/support.rs index f7398b5..8f4a822 100644 --- a/src/bots/approved_bot/modules/support.rs +++ b/src/bots/approved_bot/modules/support.rs @@ -12,7 +12,9 @@ enum SupportCommand { Donate } -pub async fn support_command_handler(message: Message, bot: CacheMe>) -> BotHandlerInternal { +pub async fn support_command_handler( + message: Message, + bot: CacheMe>) -> BotHandlerInternal { let username = &message.from().unwrap().first_name; let message_text = format!(" diff --git a/src/bots/approved_bot/services/donation_notificatioins.rs b/src/bots/approved_bot/services/donation_notificatioins.rs new file mode 100644 index 0000000..6aabc4c --- /dev/null +++ b/src/bots/approved_bot/services/donation_notificatioins.rs @@ -0,0 +1,27 @@ +use moka::future::Cache; +use teloxide::{types::{ChatId, Message}, adaptors::{CacheMe, Throttle}, Bot}; + +use crate::bots::{BotHandlerInternal, approved_bot::modules::support::support_command_handler}; + +use super::user_settings::{is_need_donate_notifications, mark_donate_notification_sended}; + + +pub async fn send_donation_notification( + bot: CacheMe>, + message: Message, + donation_notification_cache: Cache, +) -> BotHandlerInternal { + if donation_notification_cache.get(&message.chat.id).is_some() { + return Ok(()); + } else if !is_need_donate_notifications(message.chat.id).await? { + donation_notification_cache.insert(message.chat.id, true).await; + return Ok(()); + } + + donation_notification_cache.insert(message.chat.id, true).await; + mark_donate_notification_sended(message.chat.id).await?; + + support_command_handler(message, bot).await?; + + Ok(()) +} diff --git a/src/bots/approved_bot/services/mod.rs b/src/bots/approved_bot/services/mod.rs index 0869a96..1dbcb4e 100644 --- a/src/bots/approved_bot/services/mod.rs +++ b/src/bots/approved_bot/services/mod.rs @@ -1,3 +1,4 @@ pub mod book_cache; pub mod book_library; pub mod user_settings; +pub mod donation_notificatioins; diff --git a/src/bots/approved_bot/services/user_settings/mod.rs b/src/bots/approved_bot/services/user_settings/mod.rs index 81f0e11..40d4d25 100644 --- a/src/bots/approved_bot/services/user_settings/mod.rs +++ b/src/bots/approved_bot/services/user_settings/mod.rs @@ -1,7 +1,7 @@ use moka::future::Cache; use serde::Deserialize; use serde_json::json; -use teloxide::types::UserId; +use teloxide::{types::{UserId, ChatId}}; use crate::config; @@ -113,3 +113,25 @@ pub async fn update_user_activity( Ok(()) } + +pub async fn is_need_donate_notifications(chat_id: ChatId) -> Result> { + let response = reqwest::Client::new() + .get(format!("{}/donate_notifications/{chat_id}/is_need_send", &config::CONFIG.user_settings_url)) + .header("Authorization", &config::CONFIG.user_settings_api_key) + .send() + .await? + .error_for_status()?; + + Ok(response.json::().await?) +} + +pub async fn mark_donate_notification_sended(chat_id: ChatId) -> Result<(), Box> { + reqwest::Client::new() + .post(format!("{}/donate_notifications/{chat_id}", &config::CONFIG.user_settings_url)) + .header("Authorization", &config::CONFIG.user_settings_api_key) + .send() + .await? + .error_for_status()?; + + Ok(()) +} diff --git a/src/bots_manager/mod.rs b/src/bots_manager/mod.rs index 76d569e..6c9fb79 100644 --- a/src/bots_manager/mod.rs +++ b/src/bots_manager/mod.rs @@ -25,6 +25,7 @@ use self::bot_manager_client::get_bots; pub struct AppState { pub user_activity_cache: Cache, pub user_langs_cache: Cache>, + pub chat_donation_notifications_cache: Cache, } pub struct BotsManager { @@ -41,12 +42,16 @@ impl BotsManager { app_state: AppState { user_activity_cache: Cache::builder() .time_to_live(Duration::from_secs(5 * 60)) - .max_capacity(4096) + .max_capacity(16384) .build(), user_langs_cache: Cache::builder() .time_to_live(Duration::from_secs(5 * 60)) - .max_capacity(4096) + .max_capacity(16384) .build(), + chat_donation_notifications_cache: Cache::builder() + .time_to_live(Duration::from_secs(24 * 60 * 60)) + .max_capacity(32768) + .build() }, next_port: 8000, bot_port_map: HashMap::new(),