Added update types filtering.
This commit is contained in:
		@ -18,6 +18,14 @@ pub enum TextMatchMethod {
 | 
			
		||||
    IMatches,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[allow(dead_code)]
 | 
			
		||||
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
 | 
			
		||||
pub enum UpdateType {
 | 
			
		||||
    New,
 | 
			
		||||
    Edited,
 | 
			
		||||
    Deleted,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// This filter is used to filter out
 | 
			
		||||
/// that marked as silent.
 | 
			
		||||
#[derive(Clone)]
 | 
			
		||||
@ -40,6 +48,10 @@ pub struct TextFilter<'a>(pub &'a [&'a str], pub TextMatchMethod);
 | 
			
		||||
#[derive(Clone)]
 | 
			
		||||
pub struct RegexFilter(pub Regex);
 | 
			
		||||
 | 
			
		||||
/// Filters using provided regex.
 | 
			
		||||
#[derive(Clone)]
 | 
			
		||||
pub struct UpdateTypeFilter<'a>(pub &'a [UpdateType]);
 | 
			
		||||
 | 
			
		||||
impl Filter for ExcludedChatsFilter {
 | 
			
		||||
    fn filter(&self, update: &Update) -> anyhow::Result<bool> {
 | 
			
		||||
        let a = match update {
 | 
			
		||||
@ -102,3 +114,19 @@ impl Filter for RegexFilter {
 | 
			
		||||
        Ok(self.0.is_match(message.text()))
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl<'a> Filter for UpdateTypeFilter<'a> {
 | 
			
		||||
    fn filter(&self, update: &Update) -> anyhow::Result<bool> {
 | 
			
		||||
        for update_type in self.0 {
 | 
			
		||||
            match (update_type, update) {
 | 
			
		||||
                (UpdateType::New, Update::NewMessage(_))
 | 
			
		||||
                | (UpdateType::Edited, Update::MessageEdited(_))
 | 
			
		||||
                | (UpdateType::Deleted, Update::MessageDeleted(_)) => {
 | 
			
		||||
                    return Ok(true);
 | 
			
		||||
                }
 | 
			
		||||
                _ => continue,
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        Ok(false)
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -12,7 +12,7 @@ use super::{
 | 
			
		||||
        filtered_handler::FilteredHandler,
 | 
			
		||||
        message_fitlers::{
 | 
			
		||||
            ExcludedChatsFilter, MessageDirection, MessageDirectionFilter, RegexFilter,
 | 
			
		||||
            SilentFilter, TextFilter, TextMatchMethod,
 | 
			
		||||
            SilentFilter, TextFilter, TextMatchMethod, UpdateType, UpdateTypeFilter,
 | 
			
		||||
        },
 | 
			
		||||
    },
 | 
			
		||||
    handlers::{
 | 
			
		||||
@ -98,9 +98,10 @@ async fn run(args: BotConfig, client: Client) -> anyhow::Result<()> {
 | 
			
		||||
        FilteredHandler::new(Help).add_filter(TextFilter(&[".h"], TextMatchMethod::IMatches)),
 | 
			
		||||
        // Greeting my fellow humans.
 | 
			
		||||
        FilteredHandler::new(Greeter)
 | 
			
		||||
            .add_filter(ExcludedChatsFilter(vec![me.id()]))
 | 
			
		||||
            .add_filter(SilentFilter)
 | 
			
		||||
            .add_filter(UpdateTypeFilter(&[UpdateType::New]))
 | 
			
		||||
            .add_filter(MessageDirectionFilter(MessageDirection::Incoming))
 | 
			
		||||
            .add_filter(SilentFilter)
 | 
			
		||||
            .add_filter(ExcludedChatsFilter(vec![me.id()]))
 | 
			
		||||
            .add_filter(TextFilter(&["привет"], TextMatchMethod::IStartsWith))
 | 
			
		||||
            .add_filter(ExcludedChatsFilter(args.excluded_chats)),
 | 
			
		||||
        // Getting chat id.
 | 
			
		||||
@ -108,26 +109,31 @@ async fn run(args: BotConfig, client: Client) -> anyhow::Result<()> {
 | 
			
		||||
            .add_filter(TextFilter(&[".cid"], TextMatchMethod::IMatches)),
 | 
			
		||||
        // Make бля fun again.
 | 
			
		||||
        FilteredHandler::new(Blyaficator)
 | 
			
		||||
            .add_filter(UpdateTypeFilter(&[UpdateType::New]))
 | 
			
		||||
            .add_filter(SilentFilter)
 | 
			
		||||
            .add_filter(TextFilter(&[".bl"], TextMatchMethod::IStartsWith)),
 | 
			
		||||
        // Handler for converting currecies.
 | 
			
		||||
        FilteredHandler::new(CurrencyConverter::new()?)
 | 
			
		||||
            .add_filter(UpdateTypeFilter(&[UpdateType::New]))
 | 
			
		||||
            .add_filter(SilentFilter)
 | 
			
		||||
            .add_filter(ExcludedChatsFilter(args.currency_excluded_chats))
 | 
			
		||||
            .add_filter(CurrencyTextFilter),
 | 
			
		||||
        // Simlpe rotator.
 | 
			
		||||
        FilteredHandler::new(Rotator)
 | 
			
		||||
            .add_filter(UpdateTypeFilter(&[UpdateType::New]))
 | 
			
		||||
            .add_filter(SilentFilter)
 | 
			
		||||
            .add_filter(TextFilter(&[".rl"], TextMatchMethod::IStartsWith)),
 | 
			
		||||
        // Weather forecast.
 | 
			
		||||
        FilteredHandler::new(WeatherForecaster::new()?)
 | 
			
		||||
            .add_filter(UpdateTypeFilter(&[UpdateType::New]))
 | 
			
		||||
            .add_filter(SilentFilter)
 | 
			
		||||
            .add_filter(TextFilter(&[".w"], TextMatchMethod::IStartsWith)),
 | 
			
		||||
        // Smiley repeator.
 | 
			
		||||
        FilteredHandler::new(Repeator)
 | 
			
		||||
            .add_filter(ExcludedChatsFilter(vec![me.id()]))
 | 
			
		||||
            .add_filter(UpdateTypeFilter(&[UpdateType::New]))
 | 
			
		||||
            .add_filter(MessageDirectionFilter(MessageDirection::Incoming))
 | 
			
		||||
            .add_filter(SilentFilter)
 | 
			
		||||
            .add_filter(ExcludedChatsFilter(vec![me.id()]))
 | 
			
		||||
            .add_filter(RegexFilter(Regex::new("^[)0]+$")?)),
 | 
			
		||||
    ];
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user