Skip to content

Commit

Permalink
Improve filters
Browse files Browse the repository at this point in the history
  • Loading branch information
Desiders committed Apr 28, 2024
1 parent 1c741b8 commit ef3aaec
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 47 deletions.
4 changes: 2 additions & 2 deletions telers/src/filters/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
errors::SessionErrorKind,
extractors::FromContext,
methods::GetMe,
types::{BotCommand, Update, UpdateKind},
types::{BotCommand, Update},
};

use async_trait::async_trait;
Expand Down Expand Up @@ -428,7 +428,7 @@ where
{
#[instrument]
async fn check(&self, bot: &Bot<Client>, update: &Update, context: &Context) -> bool {
let UpdateKind::Message(message) = update.kind() else {
let Some(message) = update.message() else {
return false;
};
let Some(text) = message.text_or_caption() else {
Expand Down
18 changes: 6 additions & 12 deletions telers/src/filters/content_type.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
use super::base::Filter;

use crate::{
client::Bot,
context::Context,
enums::ContentType as ContentTypeEnum,
types::{Update, UpdateKind},
};
use crate::{client::Bot, context::Context, enums::ContentType as ContentTypeEnum, types::Update};

use async_trait::async_trait;

Expand Down Expand Up @@ -53,12 +48,11 @@ impl ContentType {
#[async_trait]
impl<Client> Filter<Client> for ContentType {
async fn check(&self, _bot: &Bot<Client>, update: &Update, _context: &Context) -> bool {
match update.kind() {
UpdateKind::Message(message) => {
self.validate_content_type(ContentTypeEnum::from(message))
}
_ => false,
}
let Some(message) = update.message() else {
return false;
};

self.validate_content_type(ContentTypeEnum::from(message))
}
}

Expand Down
4 changes: 3 additions & 1 deletion telers/src/filters/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,9 @@ impl<'a> Text<'a> {
#[async_trait]
impl<Client> Filter<Client> for Text<'_> {
async fn check(&self, _bot: &Bot<Client>, update: &Update, _context: &Context) -> bool {
update.text().map_or(false, |text| self.validate_text(text))
update
.text_or_caption()
.map_or(false, |text| self.validate_text(text))
}
}

Expand Down
83 changes: 51 additions & 32 deletions telers/src/types/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,23 +92,7 @@ impl Kind {
Some(data) => Some(data),
None => None,
},
Kind::ShippingQuery(ShippingQuery {
invoice_payload, ..
})
| Kind::PreCheckoutQuery(PreCheckoutQuery {
invoice_payload, ..
}) => Some(invoice_payload),
Kind::PollAnswer(_)
| Kind::MyChatMember(_)
| Kind::ChatMember(_)
| Kind::ChatJoinRequest(_)
| Kind::Poll(_)
| Kind::MessageReaction(_)
| Kind::MessageReactionCount(_)
| Kind::ChatBoost(_)
| Kind::RemovedChatBoost(_)
| Kind::BusinessConnection(_)
| Kind::DeletedBusinessMessages(_) => None,
_ => None,
}
}

Expand All @@ -131,21 +115,7 @@ impl Kind {
MaybeInaccessibleMessage::InaccessibleMessage(_) => None,
}
}
Kind::InlineQuery(_)
| Kind::ChosenInlineResult(_)
| Kind::ShippingQuery(_)
| Kind::PreCheckoutQuery(_)
| Kind::PollAnswer(_)
| Kind::MyChatMember(_)
| Kind::ChatMember(_)
| Kind::ChatJoinRequest(_)
| Kind::Poll(_)
| Kind::MessageReaction(_)
| Kind::MessageReactionCount(_)
| Kind::ChatBoost(_)
| Kind::RemovedChatBoost(_)
| Kind::BusinessConnection(_)
| Kind::DeletedBusinessMessages(_) => None,
_ => None,
}
}

Expand Down Expand Up @@ -331,6 +301,45 @@ impl Kind {
| Kind::DeletedBusinessMessages(_) => None,
}
}

#[must_use]
pub const fn business_connection_id(&self) -> Option<&str> {
match self {
Kind::DeletedBusinessMessages(BusinessMessagesDeleted {
business_connection_id,
..
})
| Kind::BusinessConnection(BusinessConnection {
id: business_connection_id,
..
}) => Some(business_connection_id),
Kind::BusinessMessage(message) => message.business_connection_id(),
_ => None,
}
}

#[must_use]
pub const fn message(&self) -> Option<&Message> {
match self {
Kind::Message(message)
| Kind::EditedMessage(message)
| Kind::BusinessMessage(message)
| Kind::EditedBusinessMessage(message)
| Kind::ChannelPost(message)
| Kind::EditedChannelPost(message) => Some(message),
Kind::CallbackQuery(CallbackQuery { message, .. }) => {
let Some(message) = message else {
return None;
};

match message {
MaybeInaccessibleMessage::Message(message) => Some(message),
MaybeInaccessibleMessage::InaccessibleMessage(_) => None,
}
}
_ => None,
}
}
}

impl Default for Kind {
Expand Down Expand Up @@ -522,4 +531,14 @@ impl Update {
pub const fn message_thread_id(&self) -> Option<i64> {
self.kind().message_thread_id()
}

#[must_use]
pub const fn business_connection_id(&self) -> Option<&str> {
self.kind().business_connection_id()
}

#[must_use]
pub const fn message(&self) -> Option<&Message> {
self.kind().message()
}
}

0 comments on commit ef3aaec

Please sign in to comment.