update to teloxide 17 and add parameter to enable the manga filter
This commit is contained in:
4
.gitignore
vendored
4
.gitignore
vendored
@@ -2,3 +2,7 @@
|
||||
/target/
|
||||
/allow_users
|
||||
/allow_pole
|
||||
/anti_manga
|
||||
/data.db
|
||||
/polesDB
|
||||
/polesDBTest
|
||||
|
||||
2101
Cargo.lock
generated
2101
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
12
Cargo.toml
12
Cargo.toml
@@ -4,7 +4,7 @@ version = "0.2.5"
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
teloxide = { version = "0.12", features = ["macros", "auto-send"] }
|
||||
teloxide = { version = "0.17", features = ["macros"] }
|
||||
futures = "0.3.5"
|
||||
log = "0.4"
|
||||
pretty_env_logger = "0.4.0"
|
||||
@@ -15,13 +15,13 @@ frunk = "0.4"
|
||||
frunk_core = "0.4"
|
||||
once_cell = "1.9.0"
|
||||
anyhow = "1.0.13"
|
||||
chrono = "*"
|
||||
sqlite = "*"
|
||||
chrono = "0.4"
|
||||
sqlite = "0.37"
|
||||
lazy_static = "1.4.0"
|
||||
image = "*"
|
||||
turbojpeg = "*"
|
||||
image = "0.25"
|
||||
turbojpeg = "1"
|
||||
phf = { version = "0.11", features = ["macros"] }
|
||||
curl = "*"
|
||||
curl = "0.4"
|
||||
|
||||
[dev-dependencies]
|
||||
mockall = "0.13.1"
|
||||
|
||||
@@ -13,7 +13,7 @@ pub fn check_stiker(msg: Message) -> bool {
|
||||
None => return false,
|
||||
};
|
||||
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 {
|
||||
@@ -22,7 +22,7 @@ pub fn check_gif(msg: Message) -> bool {
|
||||
None => return false,
|
||||
};
|
||||
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 {
|
||||
@@ -30,7 +30,7 @@ pub fn check_photo(msg: Message) -> bool {
|
||||
Some(s) => {
|
||||
let db = database::Database::get_database();
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -60,7 +60,7 @@ fn insert_ban_stiker(msg: Message) -> anyhow::Result<()> {
|
||||
Some(s) => {
|
||||
let db = database::Database::get_database();
|
||||
match db.add_media(
|
||||
s.file.unique_id.as_str(),
|
||||
&s.file.unique_id.to_string(),
|
||||
msg.chat.id.to_string().as_str(),
|
||||
database::T_STIKER,
|
||||
) {
|
||||
@@ -77,7 +77,7 @@ fn insert_ban_gif(msg: Message) -> anyhow::Result<()> {
|
||||
Some(s) => {
|
||||
let db = database::Database::get_database();
|
||||
match db.add_media(
|
||||
s.file.unique_id.as_str(),
|
||||
&s.file.unique_id.to_string(),
|
||||
msg.chat.id.to_string().as_str(),
|
||||
database::T_GIF,
|
||||
) {
|
||||
@@ -95,7 +95,7 @@ fn insert_ban_photo(msg: Message) -> anyhow::Result<()> {
|
||||
let db = database::Database::get_database();
|
||||
for p in s {
|
||||
match db.add_media(
|
||||
p.file.unique_id.as_str(),
|
||||
&p.file.unique_id.to_string(),
|
||||
msg.chat.id.to_string().as_str(),
|
||||
database::T_PHOTO,
|
||||
) {
|
||||
@@ -111,7 +111,7 @@ fn insert_ban_photo(msg: Message) -> anyhow::Result<()> {
|
||||
|
||||
async fn is_admin(msg: Message, bot: Bot) -> bool {
|
||||
match bot
|
||||
.get_chat_member(msg.chat.id, msg.from().unwrap().id)
|
||||
.get_chat_member(msg.chat.id, msg.from.unwrap().id)
|
||||
.send()
|
||||
.await
|
||||
{
|
||||
|
||||
@@ -1,17 +1,21 @@
|
||||
use std::collections::LinkedList;
|
||||
use std::sync::RwLock;
|
||||
#[cfg(not(test))]
|
||||
use std::fs;
|
||||
use std::sync::RwLock;
|
||||
|
||||
#[cfg(not(debug_assertions))]
|
||||
const ALLOW_PATH: &str = "/opt/mini_admin_bot/allow_users";
|
||||
#[cfg(not(debug_assertions))]
|
||||
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)]
|
||||
const ALLOW_PATH: &str = "allow_users";
|
||||
#[cfg(debug_assertions)]
|
||||
const POLE_PATH: &str = "allow_pole";
|
||||
#[cfg(debug_assertions)]
|
||||
const MANGA_PATH: &str = "anti_manga";
|
||||
|
||||
#[cfg(not(test))]
|
||||
fn read_ids<'a>(path: &str) -> LinkedList<String> {
|
||||
@@ -37,6 +41,7 @@ fn read_ids<'a>(path: &str) -> LinkedList<String> {
|
||||
pub struct GroupPermissions {
|
||||
aproved_groups: RwLock<LinkedList<String>>,
|
||||
party_groups: RwLock<LinkedList<String>>,
|
||||
manga_filtered_groups: RwLock<LinkedList<String>>,
|
||||
}
|
||||
|
||||
impl GroupPermissions {
|
||||
@@ -44,6 +49,7 @@ impl GroupPermissions {
|
||||
Self {
|
||||
aproved_groups: RwLock::new(read_ids(ALLOW_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 {
|
||||
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]
|
||||
|
||||
@@ -5,11 +5,16 @@ use crate::telegram_utils;
|
||||
pub async fn take_actions(msg: Message, bot: Bot) -> anyhow::Result<()> {
|
||||
_ = 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;
|
||||
let user = match msg.from() {
|
||||
let user = match msg.from {
|
||||
Some(user) => user,
|
||||
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.insert(ChatPermissions::SEND_MESSAGES);
|
||||
let restrict = bot.restrict_chat_member(msg.chat.id, user.id, permissions);
|
||||
|
||||
@@ -75,7 +75,7 @@ pub async fn run() {
|
||||
)
|
||||
.branch(
|
||||
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)),
|
||||
)
|
||||
@@ -154,7 +154,7 @@ fn is_photo(msg: Message) -> bool {
|
||||
}
|
||||
|
||||
fn is_channel_user(msg: Message) -> bool {
|
||||
match msg.from() {
|
||||
match msg.from {
|
||||
Some(u) => u.is_channel(),
|
||||
None => false,
|
||||
}
|
||||
|
||||
@@ -63,7 +63,7 @@ fn check_user_points(msg: &Message, rw: Rewards) -> bool {
|
||||
let data: database::DatabasePole = database::DatabasePole::get_database();
|
||||
let ret = data.check_user_pole(
|
||||
&msg.chat.id.to_string(),
|
||||
&msg.from().unwrap().id.to_string(),
|
||||
&msg.from.clone().unwrap().id.to_string(),
|
||||
&get_actual_day(),
|
||||
);
|
||||
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()) {
|
||||
do_pole(
|
||||
&msg.chat.id.to_string(),
|
||||
&*msg.from().unwrap().id.to_string(),
|
||||
&msg.from.clone().unwrap().id.to_string(),
|
||||
&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()) {
|
||||
do_plata(
|
||||
&msg.chat.id.to_string(),
|
||||
&*msg.from().unwrap().id.to_string(),
|
||||
&msg.from.clone().unwrap().id.to_string(),
|
||||
&get_alias(&msg),
|
||||
);
|
||||
bot.send_message(
|
||||
@@ -112,7 +112,7 @@ pub async fn exe_pole(msg: Message, bot: Bot) -> anyhow::Result<()> {
|
||||
} else if bronce_conditions(msg.clone()) {
|
||||
do_fail(
|
||||
&msg.chat.id.to_string(),
|
||||
&*msg.from().unwrap().id.to_string(),
|
||||
&msg.from.clone().unwrap().id.to_string(),
|
||||
&get_alias(&msg),
|
||||
);
|
||||
bot.send_message(msg.chat.id, format!("{} buen fail", get_alias(&msg)))
|
||||
|
||||
@@ -124,12 +124,12 @@ pub async fn check_image(msg: Message, bot: Bot) -> anyhow::Result<()> {
|
||||
id = match id {
|
||||
Some(i) => {
|
||||
if i.1 > p.width {
|
||||
Some((p.clone().file.id, p.clone().width))
|
||||
Some((p.clone().file.id.to_string(), p.clone().width))
|
||||
} else {
|
||||
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?;
|
||||
let response = match id {
|
||||
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),
|
||||
None => match msg.photo().unwrap().iter().max_by_key(|p| p.width) {
|
||||
Some(f) => bot
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
pub fn get_alias(msg: &teloxide::prelude::Message) -> String {
|
||||
match &msg.from().unwrap().username {
|
||||
Some(alias) => format!("@{}",alias),
|
||||
None => format!("{}",&*msg.from().unwrap().first_name),
|
||||
match &msg.from.clone().unwrap().username {
|
||||
Some(alias) => format!("@{}", alias),
|
||||
None => format!("{}", &*msg.from.clone().unwrap().first_name),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,47 +1,45 @@
|
||||
use chrono::DateTime;
|
||||
use teloxide::types::{
|
||||
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 {
|
||||
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 {
|
||||
title: None,
|
||||
kind: public_chat_kind,
|
||||
description: None,
|
||||
invite_link: None,
|
||||
has_protected_content: None,
|
||||
});
|
||||
|
||||
let chat = Chat {
|
||||
has_aggressive_anti_spam_enabled: false,
|
||||
has_hidden_members: false,
|
||||
id: chat_id,
|
||||
pinned_message: None,
|
||||
photo: None,
|
||||
message_auto_delete_time: None,
|
||||
kind: chat_kind,
|
||||
};
|
||||
|
||||
let media_kind = Text(MediaText {
|
||||
text: text.to_string(),
|
||||
entities: Vec::new(),
|
||||
link_preview_options: None,
|
||||
});
|
||||
|
||||
let message_kind = Common(MessageCommon {
|
||||
from: None,
|
||||
sender_chat: None,
|
||||
author_signature: None,
|
||||
forward: None,
|
||||
reply_to_message: None,
|
||||
edit_date: None,
|
||||
media_kind,
|
||||
reply_markup: None,
|
||||
is_topic_message: false,
|
||||
is_automatic_forward: 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 {
|
||||
chat,
|
||||
@@ -50,5 +48,9 @@ pub fn generate_msg_mock(text: &str) -> Message {
|
||||
kind: message_kind,
|
||||
date: DateTime::from_timestamp_nanos(0),
|
||||
id: MessageId { 0: 0 },
|
||||
from: None,
|
||||
is_topic_message: false,
|
||||
sender_business_bot: None,
|
||||
sender_chat: None,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user