mirror of
https://github.com/flibusta-apps/book_bot.git
synced 2025-12-06 15:35:35 +01:00
Add user activity update
This commit is contained in:
8
Cargo.lock
generated
8
Cargo.lock
generated
@@ -973,9 +973,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "object"
|
name = "object"
|
||||||
version = "0.30.0"
|
version = "0.30.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "239da7f290cfa979f43f85a8efeee9a8a76d0827c356d37f9d3d7254d6b537fb"
|
checksum = "8d864c91689fdc196779b98dba0aceac6118594c2df6ee5d943eb6a8df4d107a"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"memchr",
|
"memchr",
|
||||||
]
|
]
|
||||||
@@ -1725,9 +1725,9 @@ checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tokio"
|
name = "tokio"
|
||||||
version = "1.23.0"
|
version = "1.24.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "eab6d665857cc6ca78d6e80303a02cea7a7851e85dfbd77cbdc09bd129f1ef46"
|
checksum = "7125661431c26622a80ca5051a2f936c9a678318e0351007b0cc313143024e5c"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"autocfg",
|
"autocfg",
|
||||||
"bytes",
|
"bytes",
|
||||||
|
|||||||
@@ -4,19 +4,64 @@ mod tools;
|
|||||||
|
|
||||||
use teloxide::{prelude::*, types::BotCommand};
|
use teloxide::{prelude::*, types::BotCommand};
|
||||||
|
|
||||||
use self::modules::{
|
use self::{
|
||||||
annotations::get_annotations_handler, book::get_book_handler, download::get_download_hander,
|
modules::{
|
||||||
help::get_help_handler, random::get_random_hander, search::get_search_hanlder,
|
annotations::get_annotations_handler, book::get_book_handler,
|
||||||
settings::get_settings_handler, support::get_support_handler,
|
download::get_download_hander, help::get_help_handler, random::get_random_hander,
|
||||||
update_history::get_update_log_handler,
|
search::get_search_hanlder, 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::{BotCommands, BotHandler, ignore_channel_messages};
|
use super::{ignore_channel_messages, BotCommands, BotHandler};
|
||||||
|
|
||||||
|
async fn _update_activity(me: teloxide::types::Me, user: teloxide::types::User) -> Option<()> {
|
||||||
|
tokio::spawn(async move {
|
||||||
|
let allowed_langs = get_user_or_default_lang_codes(user.id).await;
|
||||||
|
|
||||||
|
match update_user_activity(
|
||||||
|
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,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(_) => (),
|
||||||
|
Err(err) => log::warn!("{}", err),
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
fn update_user_activity_handler() -> BotHandler {
|
||||||
|
dptree::entry()
|
||||||
|
.branch(
|
||||||
|
Update::filter_callback_query().chain(dptree::filter_map_async(
|
||||||
|
|cq: CallbackQuery, me: teloxide::types::Me| async move {
|
||||||
|
_update_activity(me, cq.from).await
|
||||||
|
},
|
||||||
|
))
|
||||||
|
)
|
||||||
|
.branch(Update::filter_message().chain(dptree::filter_map_async(
|
||||||
|
|message: Message, me: teloxide::types::Me| async move {
|
||||||
|
match message.from() {
|
||||||
|
Some(user) => _update_activity(me, user.clone()).await,
|
||||||
|
None => None,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)))
|
||||||
|
}
|
||||||
|
|
||||||
pub fn get_approved_handler() -> (BotHandler, BotCommands) {
|
pub fn get_approved_handler() -> (BotHandler, BotCommands) {
|
||||||
(
|
(
|
||||||
dptree::entry()
|
dptree::entry()
|
||||||
.branch(ignore_channel_messages())
|
.branch(ignore_channel_messages())
|
||||||
|
.branch(update_user_activity_handler())
|
||||||
.branch(get_help_handler())
|
.branch(get_help_handler())
|
||||||
.branch(get_settings_handler())
|
.branch(get_settings_handler())
|
||||||
.branch(get_support_handler())
|
.branch(get_support_handler())
|
||||||
|
|||||||
@@ -202,8 +202,6 @@ where
|
|||||||
.into_async_read()
|
.into_async_read()
|
||||||
.compat();
|
.compat();
|
||||||
|
|
||||||
log::info!("{}", file);
|
|
||||||
|
|
||||||
match bot
|
match bot
|
||||||
.send_photo(message.chat.id, InputFile::read(data))
|
.send_photo(message.chat.id, InputFile::read(data))
|
||||||
.send()
|
.send()
|
||||||
|
|||||||
@@ -84,8 +84,6 @@ pub async fn download_file(
|
|||||||
|
|
||||||
let headers = response.headers();
|
let headers = response.headers();
|
||||||
|
|
||||||
log::info!("Download headers: {:?}", headers);
|
|
||||||
|
|
||||||
let filename = headers
|
let filename = headers
|
||||||
.get("content-disposition")
|
.get("content-disposition")
|
||||||
.unwrap()
|
.unwrap()
|
||||||
|
|||||||
@@ -124,3 +124,39 @@ pub async fn get_langs() -> Result<Vec<Lang>, Box<dyn std::error::Error + Send +
|
|||||||
Err(err) => Err(Box::new(err)),
|
Err(err) => Err(Box::new(err)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn update_user_activity(
|
||||||
|
user_id: UserId,
|
||||||
|
last_name: String,
|
||||||
|
first_name: String,
|
||||||
|
username: String,
|
||||||
|
source: String,
|
||||||
|
allowed_langs: Vec<String>,
|
||||||
|
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||||
|
let body = json!({
|
||||||
|
"user_id": user_id,
|
||||||
|
"last_name": last_name,
|
||||||
|
"first_name": first_name,
|
||||||
|
"username": username,
|
||||||
|
"source": source,
|
||||||
|
"allowed_langs": allowed_langs
|
||||||
|
});
|
||||||
|
|
||||||
|
let client = reqwest::Client::new();
|
||||||
|
let response = client
|
||||||
|
.post(format!("{}/users/{user_id}/update_activity", &config::CONFIG.user_settings_url))
|
||||||
|
.header("Authorization", &config::CONFIG.user_settings_api_key)
|
||||||
|
.body(body.to_string())
|
||||||
|
.send()
|
||||||
|
.await;
|
||||||
|
|
||||||
|
let response = match response {
|
||||||
|
Ok(v) => v,
|
||||||
|
Err(err) => return Err(Box::new(err)),
|
||||||
|
};
|
||||||
|
|
||||||
|
match response.error_for_status() {
|
||||||
|
Ok(_) => Ok(()),
|
||||||
|
Err(err) => Err(Box::new(err)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -72,7 +72,7 @@ impl BotsManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn start_bot(&mut self, bot_data: &BotData) {
|
async fn start_bot(&mut self, bot_data: &BotData) -> bool {
|
||||||
let bot = Bot::new(bot_data.token.clone())
|
let bot = Bot::new(bot_data.token.clone())
|
||||||
.set_api_url(config::CONFIG.telegram_bot_api.clone())
|
.set_api_url(config::CONFIG.telegram_bot_api.clone())
|
||||||
.auto_send();
|
.auto_send();
|
||||||
@@ -95,9 +95,16 @@ impl BotsManager {
|
|||||||
port
|
port
|
||||||
);
|
);
|
||||||
|
|
||||||
let listener = webhooks::axum(bot.clone(), webhooks::Options::new(addr, url))
|
let listener_result = webhooks::axum(bot.clone(), webhooks::Options::new(addr, url)).await;
|
||||||
.await
|
|
||||||
.expect("Couldn't setup webhook");
|
let listener = match listener_result {
|
||||||
|
Ok(v) => v,
|
||||||
|
Err(err) => {
|
||||||
|
log::warn!("{}", err);
|
||||||
|
|
||||||
|
return false;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
let (handler, commands) = crate::bots::get_bot_handler(bot_data.status);
|
let (handler, commands) = crate::bots::get_bot_handler(bot_data.status);
|
||||||
|
|
||||||
@@ -126,6 +133,8 @@ impl BotsManager {
|
|||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn stop_bot(&mut self, bot_id: u32) {
|
async fn stop_bot(&mut self, bot_id: u32) {
|
||||||
@@ -151,20 +160,31 @@ impl BotsManager {
|
|||||||
self.next_port += 1;
|
self.next_port += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
match self.bot_status_and_cache_map.get(&bot_data.id) {
|
let result = match self.bot_status_and_cache_map.get(&bot_data.id) {
|
||||||
Some(v) => {
|
Some(v) => {
|
||||||
|
let mut update_result = true;
|
||||||
|
|
||||||
if *v != (bot_data.status, bot_data.cache) {
|
if *v != (bot_data.status, bot_data.cache) {
|
||||||
self.bot_status_and_cache_map
|
self.bot_status_and_cache_map
|
||||||
.insert(bot_data.id, (bot_data.status, bot_data.cache));
|
.insert(bot_data.id, (bot_data.status, bot_data.cache));
|
||||||
self.stop_bot(bot_data.id).await;
|
self.stop_bot(bot_data.id).await;
|
||||||
self.start_bot(bot_data).await;
|
|
||||||
|
update_result = self.start_bot(bot_data).await;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
update_result
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
self.bot_status_and_cache_map
|
self.bot_status_and_cache_map
|
||||||
.insert(bot_data.id, (bot_data.status, bot_data.cache));
|
.insert(bot_data.id, (bot_data.status, bot_data.cache));
|
||||||
self.start_bot(bot_data).await;
|
|
||||||
|
self.start_bot(bot_data).await
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if !result {
|
||||||
|
self.bot_status_and_cache_map.remove(&bot_data.id);
|
||||||
|
self.bot_shutdown_token_map.remove(&bot_data.id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user