ferrisgram/ext/handlers/
chat_member_handler.rs

1use async_trait::async_trait;
2use std::future::Future;
3
4// use crate::ext::filters::ChatMemberFilter;
5use crate::ext::{Context, Handler};
6use crate::types::Update;
7use crate::{error::GroupIteration, error::Result, Bot};
8
9pub struct ChatMemberHandler<F: Future<Output = Result<GroupIteration>> + Send + 'static> {
10    pub callback: fn(Bot, Context) -> F,
11    // pub filter: Box<dyn ChatMemberFilter>,
12    pub allow_channel: bool,
13}
14
15impl<F: Future<Output = Result<GroupIteration>> + Send + 'static> ChatMemberHandler<F> {
16    pub fn new(
17        callback: fn(Bot, Context) -> F,
18        // filter: Box<dyn ChatMemberFilter>,
19    ) -> Box<Self> {
20        Box::new(Self {
21            callback,
22            // filter,
23            allow_channel: false,
24        })
25    }
26}
27
28impl<F: Future<Output = Result<GroupIteration>> + Send + 'static> Clone for ChatMemberHandler<F> {
29    fn clone(&self) -> Self {
30        Self {
31            callback: self.callback,
32            // filter: self.filter.clone(),
33            allow_channel: self.allow_channel,
34        }
35    }
36}
37
38#[async_trait]
39impl<F: Future<Output = Result<GroupIteration>> + Send + 'static> Handler for ChatMemberHandler<F> {
40    async fn check_update(&self, _: &Bot, update: &Update) -> bool {
41        if update.chat_member.is_none() {
42            return false;
43        }
44        let cjr = update.chat_member.as_ref().unwrap();
45        if !self.allow_channel && cjr.chat.r#type == "channel" {
46            return false;
47        }
48        true
49    }
50    async fn handle_update(&self, bot: &Bot, context: &Context) -> Result<GroupIteration> {
51        (self.callback)(bot.clone(), context.clone()).await
52    }
53}