add more events, dry up event filter code

This commit is contained in:
Adrian Malacoda 2018-02-23 00:03:05 -06:00
parent 93ee23c831
commit 72eadd4549
2 changed files with 36 additions and 46 deletions

View File

@ -8,7 +8,11 @@ pub enum Event {
SelfQuit { channel: Channel }, // We quit a channel SelfQuit { channel: Channel }, // We quit a channel
UserJoin { channel: Channel, user: User }, // A user joins a channel UserJoin { channel: Channel, user: User }, // A user joins a channel
UserQuit { channel: Channel, user: User } // A user quits a channel UserQuit { channel: Channel, user: User }, // A user quits a channel
UserKick { channel: Channel, user: User }, // A usre is kicked from a channel
UserBan { channel: Channel, user: User }, // A user is banned from a channel
TopicChange { channel: Channel } // Channel topic is changed
} }
#[derive(Debug)] #[derive(Debug)]

View File

@ -258,80 +258,66 @@ impl EventFilter for AttributeEventFilter {
let mut result = true; let mut result = true;
match &envelope.event { match &envelope.event {
&Event::Message { ref message } => { &Event::Message { ref message } => {
match self.event_type { if let Some(ref event_type) = self.event_type {
Some(ref event_type) => result = result && event_type == "message", result = result && event_type == "message";
None => {}
} }
match self.channel { if let Some(ref channel_name) = self.channel {
Some(ref channel_name) => {
match message.channel { match message.channel {
Some(ref channel) => result = result && channel_name == &channel.name, Some(ref channel) => result = result && channel_name == &channel.name,
None => result = false None => result = false
} }
},
None => {}
} }
match self.username { if let Some(ref username) = self.username {
Some(ref username) => result = result && &message.author.name == username, result = result && &message.author.name == username;
None => {}
} }
}, },
&Event::SelfJoin { ref channel } => { &Event::SelfJoin { ref channel } => {
match self.event_type { if let Some(ref event_type) = self.event_type {
Some(ref event_type) => result = result && event_type == "selfjoin", result = result && event_type == "selfjoin";
None => {}
} }
match self.channel { if let Some(ref channel_name) = self.channel {
Some(ref channel_name) => result = result && channel_name == &channel.name, result = result && channel_name == &channel.name;
None => {}
} }
}, },
&Event::SelfQuit { ref channel } => { &Event::SelfQuit { ref channel } => {
match self.event_type { if let Some(ref event_type) = self.event_type {
Some(ref event_type) => result = result && event_type == "selfquit", result = result && event_type == "selfquit";
None => {}
} }
match self.channel { if let Some(ref channel_name) = self.channel {
Some(ref channel_name) => result = result && channel_name == &channel.name, result = result && channel_name == &channel.name;
None => {}
} }
}, },
&Event::UserJoin { ref channel, ref user } => { &Event::UserJoin { ref channel, ref user } => {
match self.event_type { if let Some(ref event_type) = self.event_type {
Some(ref event_type) => result = result && event_type == "userjoin", result = result && event_type == "userjoin";
None => {}
} }
match self.channel { if let Some(ref channel_name) = self.channel {
Some(ref channel_name) => result = result && channel_name == &channel.name, result = result && channel_name == &channel.name;
None => {}
} }
match self.username { if let Some(ref username) = self.username {
Some(ref username) => result = result && &user.name == username, result = result && &user.name == username;
None => {}
} }
}, },
&Event::UserQuit { ref channel, ref user } => { &Event::UserQuit { ref channel, ref user } => {
match self.event_type { if let Some(ref event_type) = self.event_type {
Some(ref event_type) => result = result && event_type == "userquit", result = result && event_type == "userquit";
None => {}
} }
match self.channel { if let Some(ref channel_name) = self.channel {
Some(ref channel_name) => result = result && channel_name == &channel.name, result = result && channel_name == &channel.name;
None => {}
} }
match self.username { if let Some(ref username) = self.username {
Some(ref username) => result = result && &user.name == username, result = result && &user.name == username;
None => {}
}
} }
},
_ => {}
} }
result result