update to teloxide 17 and add parameter to enable the manga filter

This commit is contained in:
2025-12-17 01:01:41 +00:00
parent 8ca9c85aa0
commit 9c9624b43c
12 changed files with 1373 additions and 836 deletions

4
.gitignore vendored
View File

@@ -2,3 +2,7 @@
/target/ /target/
/allow_users /allow_users
/allow_pole /allow_pole
/anti_manga
/data.db
/polesDB
/polesDBTest

2101
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -4,7 +4,7 @@ version = "0.2.5"
edition = "2018" edition = "2018"
[dependencies] [dependencies]
teloxide = { version = "0.12", features = ["macros", "auto-send"] } teloxide = { version = "0.17", features = ["macros"] }
futures = "0.3.5" futures = "0.3.5"
log = "0.4" log = "0.4"
pretty_env_logger = "0.4.0" pretty_env_logger = "0.4.0"
@@ -15,13 +15,13 @@ frunk = "0.4"
frunk_core = "0.4" frunk_core = "0.4"
once_cell = "1.9.0" once_cell = "1.9.0"
anyhow = "1.0.13" anyhow = "1.0.13"
chrono = "*" chrono = "0.4"
sqlite = "*" sqlite = "0.37"
lazy_static = "1.4.0" lazy_static = "1.4.0"
image = "*" image = "0.25"
turbojpeg = "*" turbojpeg = "1"
phf = { version = "0.11", features = ["macros"] } phf = { version = "0.11", features = ["macros"] }
curl = "*" curl = "0.4"
[dev-dependencies] [dev-dependencies]
mockall = "0.13.1" mockall = "0.13.1"

BIN
polesDB

Binary file not shown.

View File

@@ -13,7 +13,7 @@ pub fn check_stiker(msg: Message) -> bool {
None => return false, None => return false,
}; };
let db = database::Database::get_database(); let db = database::Database::get_database();
db.media_is_banned(stiker.unique_id.as_str(), database::T_STIKER) db.media_is_banned(&stiker.unique_id.to_string(), database::T_STIKER)
} }
pub fn check_gif(msg: Message) -> bool { pub fn check_gif(msg: Message) -> bool {
@@ -22,7 +22,7 @@ pub fn check_gif(msg: Message) -> bool {
None => return false, None => return false,
}; };
let db = database::Database::get_database(); let db = database::Database::get_database();
db.media_is_banned(gif.unique_id.as_str(), database::T_GIF) db.media_is_banned(&gif.unique_id.to_string(), database::T_GIF)
} }
pub fn check_photo(msg: Message) -> bool { pub fn check_photo(msg: Message) -> bool {
@@ -30,7 +30,7 @@ pub fn check_photo(msg: Message) -> bool {
Some(s) => { Some(s) => {
let db = database::Database::get_database(); let db = database::Database::get_database();
for p in s { for p in s {
if db.media_is_banned(p.file.unique_id.as_str(), database::T_PHOTO) { if db.media_is_banned(&p.file.unique_id.to_string(), database::T_PHOTO) {
return true; return true;
} }
} }
@@ -60,7 +60,7 @@ fn insert_ban_stiker(msg: Message) -> anyhow::Result<()> {
Some(s) => { Some(s) => {
let db = database::Database::get_database(); let db = database::Database::get_database();
match db.add_media( match db.add_media(
s.file.unique_id.as_str(), &s.file.unique_id.to_string(),
msg.chat.id.to_string().as_str(), msg.chat.id.to_string().as_str(),
database::T_STIKER, database::T_STIKER,
) { ) {
@@ -77,7 +77,7 @@ fn insert_ban_gif(msg: Message) -> anyhow::Result<()> {
Some(s) => { Some(s) => {
let db = database::Database::get_database(); let db = database::Database::get_database();
match db.add_media( match db.add_media(
s.file.unique_id.as_str(), &s.file.unique_id.to_string(),
msg.chat.id.to_string().as_str(), msg.chat.id.to_string().as_str(),
database::T_GIF, database::T_GIF,
) { ) {
@@ -95,7 +95,7 @@ fn insert_ban_photo(msg: Message) -> anyhow::Result<()> {
let db = database::Database::get_database(); let db = database::Database::get_database();
for p in s { for p in s {
match db.add_media( match db.add_media(
p.file.unique_id.as_str(), &p.file.unique_id.to_string(),
msg.chat.id.to_string().as_str(), msg.chat.id.to_string().as_str(),
database::T_PHOTO, database::T_PHOTO,
) { ) {
@@ -111,7 +111,7 @@ fn insert_ban_photo(msg: Message) -> anyhow::Result<()> {
async fn is_admin(msg: Message, bot: Bot) -> bool { async fn is_admin(msg: Message, bot: Bot) -> bool {
match bot match bot
.get_chat_member(msg.chat.id, msg.from().unwrap().id) .get_chat_member(msg.chat.id, msg.from.unwrap().id)
.send() .send()
.await .await
{ {

View File

@@ -1,17 +1,21 @@
use std::collections::LinkedList; use std::collections::LinkedList;
use std::sync::RwLock;
#[cfg(not(test))] #[cfg(not(test))]
use std::fs; use std::fs;
use std::sync::RwLock;
#[cfg(not(debug_assertions))] #[cfg(not(debug_assertions))]
const ALLOW_PATH: &str = "/opt/mini_admin_bot/allow_users"; const ALLOW_PATH: &str = "/opt/mini_admin_bot/allow_users";
#[cfg(not(debug_assertions))] #[cfg(not(debug_assertions))]
const POLE_PATH: &str = "/opt/mini_admin_bot/allow_pole"; const POLE_PATH: &str = "/opt/mini_admin_bot/allow_pole";
#[cfg(not(debug_assertions))]
const MANGA_PATH: &str = "/opt/mini_admin_bot/anti_manga";
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
const ALLOW_PATH: &str = "allow_users"; const ALLOW_PATH: &str = "allow_users";
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
const POLE_PATH: &str = "allow_pole"; const POLE_PATH: &str = "allow_pole";
#[cfg(debug_assertions)]
const MANGA_PATH: &str = "anti_manga";
#[cfg(not(test))] #[cfg(not(test))]
fn read_ids<'a>(path: &str) -> LinkedList<String> { fn read_ids<'a>(path: &str) -> LinkedList<String> {
@@ -37,6 +41,7 @@ fn read_ids<'a>(path: &str) -> LinkedList<String> {
pub struct GroupPermissions { pub struct GroupPermissions {
aproved_groups: RwLock<LinkedList<String>>, aproved_groups: RwLock<LinkedList<String>>,
party_groups: RwLock<LinkedList<String>>, party_groups: RwLock<LinkedList<String>>,
manga_filtered_groups: RwLock<LinkedList<String>>,
} }
impl GroupPermissions { impl GroupPermissions {
@@ -44,6 +49,7 @@ impl GroupPermissions {
Self { Self {
aproved_groups: RwLock::new(read_ids(ALLOW_PATH)), aproved_groups: RwLock::new(read_ids(ALLOW_PATH)),
party_groups: RwLock::new(read_ids(POLE_PATH)), party_groups: RwLock::new(read_ids(POLE_PATH)),
manga_filtered_groups: RwLock::new(read_ids(MANGA_PATH)),
} }
} }
@@ -54,6 +60,10 @@ impl GroupPermissions {
pub fn compar_party(&self, id: &String) -> bool { pub fn compar_party(&self, id: &String) -> bool {
self.party_groups.read().unwrap().contains(id) self.party_groups.read().unwrap().contains(id)
} }
pub fn compar_manga_filtered(&self, id: &String) -> bool {
self.manga_filtered_groups.read().unwrap().contains(id)
}
} }
#[test] #[test]

View File

@@ -5,11 +5,16 @@ use crate::telegram_utils;
pub async fn take_actions(msg: Message, bot: Bot) -> anyhow::Result<()> { pub async fn take_actions(msg: Message, bot: Bot) -> anyhow::Result<()> {
_ = bot.delete_message(msg.chat.id, msg.id).await; _ = bot.delete_message(msg.chat.id, msg.id).await;
_ = bot.send_message(msg.chat.id, format!("Los mensajes con ficheros ejecutables no están permitidos\nAlerta para el usuario {}.\nSi consideras que ha sido un error ponte en contacto con un administrador", telegram_utils::get_alias(&msg))).await; _ = bot.send_message(msg.chat.id, format!("Los mensajes con ficheros ejecutables no están permitidos\nAlerta para el usuario {}.\nSi consideras que ha sido un error ponte en contacto con un administrador", telegram_utils::get_alias(&msg))).await;
let user = match msg.from() { let user = match msg.from {
Some(user) => user, Some(user) => user,
None => return Ok(()), None => return Ok(()),
}; };
let mut permissions = msg.chat.permissions().unwrap_or(ChatPermissions::empty()); let mut permissions = bot
.get_chat(msg.chat.id)
.await
.unwrap()
.permissions()
.unwrap_or(ChatPermissions::empty());
permissions.remove(ChatPermissions::SEND_OTHER_MESSAGES); permissions.remove(ChatPermissions::SEND_OTHER_MESSAGES);
permissions.insert(ChatPermissions::SEND_MESSAGES); permissions.insert(ChatPermissions::SEND_MESSAGES);
let restrict = bot.restrict_chat_member(msg.chat.id, user.id, permissions); let restrict = bot.restrict_chat_member(msg.chat.id, user.id, permissions);

View File

@@ -75,7 +75,7 @@ pub async fn run() {
) )
.branch( .branch(
dptree::filter(move |msg: Message| { dptree::filter(move |msg: Message| {
is_photo(msg.clone()) && p2.compar_party(&msg.chat.id.to_string()) is_photo(msg.clone()) && p2.compar_manga_filtered(&msg.chat.id.to_string())
}) })
.endpoint(|msg: Message, bot: Bot| spoiler_mangas::check_image(msg, bot)), .endpoint(|msg: Message, bot: Bot| spoiler_mangas::check_image(msg, bot)),
) )
@@ -154,7 +154,7 @@ fn is_photo(msg: Message) -> bool {
} }
fn is_channel_user(msg: Message) -> bool { fn is_channel_user(msg: Message) -> bool {
match msg.from() { match msg.from {
Some(u) => u.is_channel(), Some(u) => u.is_channel(),
None => false, None => false,
} }

View File

@@ -63,7 +63,7 @@ fn check_user_points(msg: &Message, rw: Rewards) -> bool {
let data: database::DatabasePole = database::DatabasePole::get_database(); let data: database::DatabasePole = database::DatabasePole::get_database();
let ret = data.check_user_pole( let ret = data.check_user_pole(
&msg.chat.id.to_string(), &msg.chat.id.to_string(),
&msg.from().unwrap().id.to_string(), &msg.from.clone().unwrap().id.to_string(),
&get_actual_day(), &get_actual_day(),
); );
check_group_points(msg, rw) && (ret == 0) check_group_points(msg, rw) && (ret == 0)
@@ -93,7 +93,7 @@ pub async fn exe_pole(msg: Message, bot: Bot) -> anyhow::Result<()> {
if pole_conditions(msg.clone()) { if pole_conditions(msg.clone()) {
do_pole( do_pole(
&msg.chat.id.to_string(), &msg.chat.id.to_string(),
&*msg.from().unwrap().id.to_string(), &msg.from.clone().unwrap().id.to_string(),
&get_alias(&msg), &get_alias(&msg),
); );
bot.send_message(msg.chat.id, format!("{} ha hecho la pole", get_alias(&msg))) bot.send_message(msg.chat.id, format!("{} ha hecho la pole", get_alias(&msg)))
@@ -101,7 +101,7 @@ pub async fn exe_pole(msg: Message, bot: Bot) -> anyhow::Result<()> {
} else if plata_conditions(msg.clone()) { } else if plata_conditions(msg.clone()) {
do_plata( do_plata(
&msg.chat.id.to_string(), &msg.chat.id.to_string(),
&*msg.from().unwrap().id.to_string(), &msg.from.clone().unwrap().id.to_string(),
&get_alias(&msg), &get_alias(&msg),
); );
bot.send_message( bot.send_message(
@@ -112,7 +112,7 @@ pub async fn exe_pole(msg: Message, bot: Bot) -> anyhow::Result<()> {
} else if bronce_conditions(msg.clone()) { } else if bronce_conditions(msg.clone()) {
do_fail( do_fail(
&msg.chat.id.to_string(), &msg.chat.id.to_string(),
&*msg.from().unwrap().id.to_string(), &msg.from.clone().unwrap().id.to_string(),
&get_alias(&msg), &get_alias(&msg),
); );
bot.send_message(msg.chat.id, format!("{} buen fail", get_alias(&msg))) bot.send_message(msg.chat.id, format!("{} buen fail", get_alias(&msg)))

View File

@@ -124,12 +124,12 @@ pub async fn check_image(msg: Message, bot: Bot) -> anyhow::Result<()> {
id = match id { id = match id {
Some(i) => { Some(i) => {
if i.1 > p.width { if i.1 > p.width {
Some((p.clone().file.id, p.clone().width)) Some((p.clone().file.id.to_string(), p.clone().width))
} else { } else {
Some(i) Some(i)
} }
} }
None => Some((p.clone().file.id, p.clone().width)), None => Some((p.clone().file.id.to_string(), p.clone().width)),
} }
} }
} }
@@ -140,7 +140,10 @@ pub async fn check_image(msg: Message, bot: Bot) -> anyhow::Result<()> {
bot.delete_message(msg.chat.id, msg.id).await?; bot.delete_message(msg.chat.id, msg.id).await?;
let response = match id { let response = match id {
Some(i) => bot Some(i) => bot
.send_photo(msg.chat.id, teloxide::types::InputFile::file_id(i.0)) .send_photo(
msg.chat.id,
teloxide::types::InputFile::file_id(teloxide::types::FileId(i.0)),
)
.has_spoiler(true), .has_spoiler(true),
None => match msg.photo().unwrap().iter().max_by_key(|p| p.width) { None => match msg.photo().unwrap().iter().max_by_key(|p| p.width) {
Some(f) => bot Some(f) => bot

View File

@@ -1,6 +1,6 @@
pub fn get_alias(msg: &teloxide::prelude::Message) -> String { pub fn get_alias(msg: &teloxide::prelude::Message) -> String {
match &msg.from().unwrap().username { match &msg.from.clone().unwrap().username {
Some(alias) => format!("@{}",alias), Some(alias) => format!("@{}", alias),
None => format!("{}",&*msg.from().unwrap().first_name), None => format!("{}", &*msg.from.clone().unwrap().first_name),
} }
} }

View File

@@ -1,47 +1,45 @@
use chrono::DateTime; use chrono::DateTime;
use teloxide::types::{ use teloxide::types::{
Chat, ChatId, ChatKind, ChatPublic, MediaKind::Text, MediaText, Message, MessageCommon, Chat, ChatId, ChatKind, ChatPublic, MediaKind::Text, MediaText, Message, MessageCommon,
MessageId, MessageKind::Common, PublicChatGroup, PublicChatKind::Group, MessageId, MessageKind::Common, PublicChatKind::Group,
}; };
pub fn generate_msg_mock(text: &str) -> Message { pub fn generate_msg_mock(text: &str) -> Message {
let chat_id = ChatId { 0: 0 }; let chat_id = ChatId { 0: 0 };
let public_chat_kind = Group(PublicChatGroup { permissions: None }); let public_chat_kind = Group;
let chat_kind = ChatKind::Public(ChatPublic { let chat_kind = ChatKind::Public(ChatPublic {
title: None, title: None,
kind: public_chat_kind, kind: public_chat_kind,
description: None,
invite_link: None,
has_protected_content: None,
}); });
let chat = Chat { let chat = Chat {
has_aggressive_anti_spam_enabled: false,
has_hidden_members: false,
id: chat_id, id: chat_id,
pinned_message: None,
photo: None,
message_auto_delete_time: None,
kind: chat_kind, kind: chat_kind,
}; };
let media_kind = Text(MediaText { let media_kind = Text(MediaText {
text: text.to_string(), text: text.to_string(),
entities: Vec::new(), entities: Vec::new(),
link_preview_options: None,
}); });
let message_kind = Common(MessageCommon { let message_kind = Common(MessageCommon {
from: None,
sender_chat: None,
author_signature: None, author_signature: None,
forward: None,
reply_to_message: None, reply_to_message: None,
edit_date: None, edit_date: None,
media_kind, media_kind,
reply_markup: None, reply_markup: None,
is_topic_message: false,
is_automatic_forward: false, is_automatic_forward: false,
has_protected_content: false, has_protected_content: false,
paid_star_count: None,
effect_id: None,
forward_origin: None,
external_reply: None,
quote: None,
reply_to_story: None,
sender_boost_count: None,
business_connection_id: None,
is_from_offline: false,
}); });
Message { Message {
chat, chat,
@@ -50,5 +48,9 @@ pub fn generate_msg_mock(text: &str) -> Message {
kind: message_kind, kind: message_kind,
date: DateTime::from_timestamp_nanos(0), date: DateTime::from_timestamp_nanos(0),
id: MessageId { 0: 0 }, id: MessageId { 0: 0 },
from: None,
is_topic_message: false,
sender_business_bot: None,
sender_chat: None,
} }
} }