Add donation notifications

This commit is contained in:
2023-05-24 21:30:21 +02:00
parent f9723361a0
commit 10b063ac25
6 changed files with 86 additions and 16 deletions

View File

@@ -1,17 +1,18 @@
use futures::TryStreamExt; use futures::TryStreamExt;
use moka::future::Cache;
use regex::Regex; use regex::Regex;
use teloxide::{dispatching::UpdateFilterExt, dptree, prelude::*, types::*, adaptors::{Throttle, CacheMe}}; use teloxide::{dispatching::UpdateFilterExt, dptree, prelude::*, types::*, adaptors::{Throttle, CacheMe}};
use tokio_util::compat::FuturesAsyncReadCompatExt; use tokio_util::compat::FuturesAsyncReadCompatExt;
use crate::{ use crate::{
bots::{ bots::{
approved_bot::services::book_cache::{ approved_bot::services::{book_cache::{
download_file, get_cached_message, download_file, get_cached_message,
types::{CachedMessage, DownloadFile}, types::{CachedMessage, DownloadFile},
}, }, donation_notificatioins::send_donation_notification},
BotHandlerInternal, BotHandlerInternal,
}, },
bots_manager::BotCache, bots_manager::{BotCache, AppState},
}; };
use super::utils::{filter_command, CommandParse}; use super::utils::{filter_command, CommandParse};
@@ -68,14 +69,17 @@ async fn send_cached_message(
message: Message, message: Message,
bot: CacheMe<Throttle<Bot>>, bot: CacheMe<Throttle<Bot>>,
download_data: DownloadData, download_data: DownloadData,
donation_notification_cache: Cache<ChatId, bool>,
) -> BotHandlerInternal { ) -> BotHandlerInternal {
if let Ok(v) = get_cached_message(&download_data).await { if let Ok(v) = get_cached_message(&download_data).await {
if _send_cached(&message, &bot, v).await.is_ok() { if _send_cached(&message, &bot, v).await.is_ok() {
send_donation_notification(bot.clone(), message, donation_notification_cache).await?;
return Ok(()); 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(()) Ok(())
} }
@@ -84,6 +88,7 @@ async fn _send_downloaded_file(
message: &Message, message: &Message,
bot: CacheMe<Throttle<Bot>>, bot: CacheMe<Throttle<Bot>>,
downloaded_data: DownloadFile, downloaded_data: DownloadFile,
donation_notification_cache: Cache<ChatId, bool>,
) -> BotHandlerInternal { ) -> BotHandlerInternal {
let DownloadFile { let DownloadFile {
response, response,
@@ -105,6 +110,8 @@ async fn _send_downloaded_file(
.send() .send()
.await?; .await?;
send_donation_notification(bot, message.clone(), donation_notification_cache).await?;
Ok(()) Ok(())
} }
@@ -112,12 +119,10 @@ async fn send_with_download_from_channel(
message: Message, message: Message,
bot: CacheMe<Throttle<Bot>>, bot: CacheMe<Throttle<Bot>>,
download_data: DownloadData, download_data: DownloadData,
donation_notification_cache: Cache<ChatId, bool>,
) -> BotHandlerInternal { ) -> BotHandlerInternal {
match download_file(&download_data).await { match download_file(&download_data).await {
Ok(v) => match _send_downloaded_file(&message, bot, v).await { Ok(v) => Ok(_send_downloaded_file(&message, bot, v, donation_notification_cache).await?),
Ok(v_2) => Ok(v_2),
Err(err) => Err(err),
},
Err(err) => Err(err), Err(err) => Err(err),
} }
} }
@@ -127,10 +132,11 @@ async fn download_handler(
bot: CacheMe<Throttle<Bot>>, bot: CacheMe<Throttle<Bot>>,
cache: BotCache, cache: BotCache,
download_data: DownloadData, download_data: DownloadData,
donation_notification_cache: Cache<ChatId, bool>,
) -> BotHandlerInternal { ) -> BotHandlerInternal {
match cache { match cache {
BotCache::Original => send_cached_message(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).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, |message: Message,
bot: CacheMe<Throttle<Bot>>, bot: CacheMe<Throttle<Bot>>,
cache: BotCache, cache: BotCache,
download_data: DownloadData| async move { download_data: DownloadData,
download_handler(message, bot, cache, download_data).await app_state: AppState| async move {
download_handler(
message,
bot,
cache,
download_data,
app_state.chat_donation_notifications_cache
).await
}, },
), ),
) )

View File

@@ -12,7 +12,9 @@ enum SupportCommand {
Donate Donate
} }
pub async fn support_command_handler(message: Message, bot: CacheMe<Throttle<Bot>>) -> BotHandlerInternal { pub async fn support_command_handler(
message: Message,
bot: CacheMe<Throttle<Bot>>) -> BotHandlerInternal {
let username = &message.from().unwrap().first_name; let username = &message.from().unwrap().first_name;
let message_text = format!(" let message_text = format!("

View File

@@ -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<Throttle<Bot>>,
message: Message,
donation_notification_cache: Cache<ChatId, bool>,
) -> 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(())
}

View File

@@ -1,3 +1,4 @@
pub mod book_cache; pub mod book_cache;
pub mod book_library; pub mod book_library;
pub mod user_settings; pub mod user_settings;
pub mod donation_notificatioins;

View File

@@ -1,7 +1,7 @@
use moka::future::Cache; use moka::future::Cache;
use serde::Deserialize; use serde::Deserialize;
use serde_json::json; use serde_json::json;
use teloxide::types::UserId; use teloxide::{types::{UserId, ChatId}};
use crate::config; use crate::config;
@@ -113,3 +113,25 @@ pub async fn update_user_activity(
Ok(()) Ok(())
} }
pub async fn is_need_donate_notifications(chat_id: ChatId) -> Result<bool, Box<dyn std::error::Error + Send + Sync>> {
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::<bool>().await?)
}
pub async fn mark_donate_notification_sended(chat_id: ChatId) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
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(())
}

View File

@@ -25,6 +25,7 @@ use self::bot_manager_client::get_bots;
pub struct AppState { pub struct AppState {
pub user_activity_cache: Cache<UserId, bool>, pub user_activity_cache: Cache<UserId, bool>,
pub user_langs_cache: Cache<UserId, Vec<String>>, pub user_langs_cache: Cache<UserId, Vec<String>>,
pub chat_donation_notifications_cache: Cache<ChatId, bool>,
} }
pub struct BotsManager { pub struct BotsManager {
@@ -41,12 +42,16 @@ impl BotsManager {
app_state: AppState { app_state: AppState {
user_activity_cache: Cache::builder() user_activity_cache: Cache::builder()
.time_to_live(Duration::from_secs(5 * 60)) .time_to_live(Duration::from_secs(5 * 60))
.max_capacity(4096) .max_capacity(16384)
.build(), .build(),
user_langs_cache: Cache::builder() user_langs_cache: Cache::builder()
.time_to_live(Duration::from_secs(5 * 60)) .time_to_live(Duration::from_secs(5 * 60))
.max_capacity(4096) .max_capacity(16384)
.build(), .build(),
chat_donation_notifications_cache: Cache::builder()
.time_to_live(Duration::from_secs(24 * 60 * 60))
.max_capacity(32768)
.build()
}, },
next_port: 8000, next_port: 8000,
bot_port_map: HashMap::new(), bot_port_map: HashMap::new(),