Added not filter.

Signed-off-by: Pavel Kirilin <win10@list.ru>
This commit is contained in:
2023-12-03 11:33:42 +01:00
parent b68f32d11f
commit 07c6ff69e0
2 changed files with 21 additions and 3 deletions

View File

@ -45,6 +45,13 @@ pub struct MessageDirectionFilter(pub MessageDirection);
#[derive(Clone)] #[derive(Clone)]
pub struct TextFilter<'a>(pub &'a [&'a str], pub TextMatchMethod); pub struct TextFilter<'a>(pub &'a [&'a str], pub TextMatchMethod);
/// Filters text by predicate.
#[derive(Clone)]
pub struct ReverseTextFilter<'a>(pub &'a [&'a str], pub TextMatchMethod);
#[derive(Clone)]
pub struct NotFilter<T: Filter + Clone>(pub T);
#[derive(Clone)] #[derive(Clone)]
pub struct OnlyFromId(pub i64); pub struct OnlyFromId(pub i64);
@ -155,3 +162,9 @@ impl<'a> Filter for UpdateTypeFilter<'a> {
Ok(false) Ok(false)
} }
} }
impl<T: Filter + Clone> Filter for NotFilter<T> {
fn filter(&self, update: &Update) -> anyhow::Result<bool> {
Ok(!self.0.filter(update)?)
}
}

View File

@ -11,8 +11,8 @@ use super::{
filters::{ filters::{
filtered_handler::FilteredHandler, filtered_handler::FilteredHandler,
message_fitlers::{ message_fitlers::{
ExcludedChatsFilter, MessageDirection, MessageDirectionFilter, OnlyFromId, RegexFilter, ExcludedChatsFilter, MessageDirection, MessageDirectionFilter, NotFilter, OnlyFromId,
SilentFilter, TextFilter, TextMatchMethod, UpdateType, UpdateTypeFilter, RegexFilter, SilentFilter, TextFilter, TextMatchMethod, UpdateType, UpdateTypeFilter,
}, },
}, },
handlers::{ handlers::{
@ -163,7 +163,12 @@ async fn run(args: BotConfig, client: Client) -> anyhow::Result<()> {
.add_filter(ExcludedChatsFilter(args.excluded_chats.clone())) .add_filter(ExcludedChatsFilter(args.excluded_chats.clone()))
.add_filter(UpdateTypeFilter(&[UpdateType::New])) .add_filter(UpdateTypeFilter(&[UpdateType::New]))
.add_filter(SilentFilter) .add_filter(SilentFilter)
.add_filter(TextFilter(&[".c"], TextMatchMethod::StartsWith)), .add_filter(TextFilter(&[".c"], TextMatchMethod::StartsWith))
.add_filter(NotFilter(TextFilter(
&[".cid"],
TextMatchMethod::StartsWith,
))),
// .add_filter(TextFilter(&[".cid"], TextMatchMethod::)),
// Notify all. // Notify all.
FilteredHandler::new(NotifyAll) FilteredHandler::new(NotifyAll)
.add_filter(ExcludedChatsFilter(args.excluded_chats.clone())) .add_filter(ExcludedChatsFilter(args.excluded_chats.clone()))