From 605db18b0aa610049fd0ae3d0e51fb0a4390a430 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D1=8B=D1=80=D1=86=D0=B5=D0=B2=20=D0=92=D0=B0=D0=B4?= =?UTF-8?q?=D0=B8=D0=BC=20=D0=98=D0=B3=D0=BE=D1=80=D0=B5=D0=B2=D0=B8=D1=87?= Date: Sat, 16 Dec 2023 14:03:30 +0300 Subject: [PATCH] Add missing Message::filter_ functions --- crates/teloxide-core/src/types/message.rs | 164 ++++++++++++++++-- crates/teloxide/src/dispatching/filter_ext.rs | 36 ++++ 2 files changed, 182 insertions(+), 18 deletions(-) diff --git a/crates/teloxide-core/src/types/message.rs b/crates/teloxide-core/src/types/message.rs index d9910d05..53768796 100644 --- a/crates/teloxide-core/src/types/message.rs +++ b/crates/teloxide-core/src/types/message.rs @@ -663,6 +663,14 @@ mod getters { MessageSupergroupChatCreated, MessageVideoChatParticipantsInvited, PhotoSize, True, User, }; + use super::{ + MessageForumTopicClosed, MessageForumTopicCreated, MessageForumTopicEdited, + MessageForumTopicReopened, MessageGeneralForumTopicHidden, + MessageGeneralForumTopicUnhidden, MessageMessageAutoDeleteTimerChanged, + MessageVideoChatEnded, MessageVideoChatScheduled, MessageVideoChatStarted, + MessageWebAppData, MessageWriteAccessAllowed, + }; + /// Getters for [Message] fields from [telegram docs]. /// /// [Message]: crate::types::Message @@ -1090,73 +1098,85 @@ mod getters { // TODO: OK, `Option` is weird, can we do something with it? // mb smt like `is_delete_chat_photo(&self) -> bool`? #[must_use] - pub fn delete_chat_photo(&self) -> Option { + pub fn delete_chat_photo(&self) -> Option<&True> { match &self.kind { DeleteChatPhoto(MessageDeleteChatPhoto { delete_chat_photo }) => { - Some(*delete_chat_photo) + Some(delete_chat_photo) } _ => None, } } #[must_use] - pub fn group_chat_created(&self) -> Option { + pub fn group_chat_created(&self) -> Option<&True> { match &self.kind { GroupChatCreated(MessageGroupChatCreated { group_chat_created }) => { - Some(*group_chat_created) + Some(group_chat_created) } _ => None, } } #[must_use] - pub fn super_group_chat_created(&self) -> Option { + pub fn super_group_chat_created(&self) -> Option<&True> { match &self.kind { SupergroupChatCreated(MessageSupergroupChatCreated { supergroup_chat_created }) => { - Some(*supergroup_chat_created) + Some(supergroup_chat_created) } _ => None, } } #[must_use] - pub fn channel_chat_created(&self) -> Option { + pub fn channel_chat_created(&self) -> Option<&True> { match &self.kind { ChannelChatCreated(MessageChannelChatCreated { channel_chat_created }) => { - Some(*channel_chat_created) + Some(channel_chat_created) } _ => None, } } #[must_use] - pub fn chat_migration(&self) -> Option { + pub fn message_auto_delete_timer_changed( + &self, + ) -> Option<&types::MessageAutoDeleteTimerChanged> { + match &self.kind { + MessageAutoDeleteTimerChanged(MessageMessageAutoDeleteTimerChanged { + message_auto_delete_timer_changed, + }) => Some(message_auto_delete_timer_changed), + _ => None, + } + } + + #[must_use] + pub fn chat_migration(&self) -> Option<&types::ChatMigration> { match &self.kind { Common(MessageCommon { media_kind: MediaKind::Migration(chat_migration), .. - }) => Some(*chat_migration), + }) => Some(chat_migration), _ => None, } } #[must_use] - pub fn migrate_to_chat_id(&self) -> Option { + pub fn migrate_to_chat_id(&self) -> Option<&ChatId> { match &self.kind { Common(MessageCommon { media_kind: MediaKind::Migration(ChatMigration::To { chat_id }), .. - }) => Some(*chat_id), + }) => Some(chat_id), _ => None, } } #[must_use] - pub fn migrate_from_chat_id(&self) -> Option { + pub fn migrate_from_chat_id(&self) -> Option<&ChatId> { match &self.kind { Common(MessageCommon { media_kind: MediaKind::Migration(ChatMigration::From { chat_id }), .. - }) => Some(*chat_id), + }) => Some(chat_id), _ => None, } } @@ -1197,6 +1217,16 @@ mod getters { } } + #[must_use] + pub fn write_access_allowed(&self) -> Option<&types::WriteAccessAllowed> { + match &self.kind { + WriteAccessAllowed(MessageWriteAccessAllowed { write_access_allowed }) => { + Some(write_access_allowed) + } + _ => None, + } + } + #[must_use] pub fn passport_data(&self) -> Option<&types::PassportData> { match &self.kind { @@ -1223,6 +1253,96 @@ mod getters { } } + #[must_use] + pub fn forum_topic_created(&self) -> Option<&types::ForumTopicCreated> { + match &self.kind { + ForumTopicCreated(MessageForumTopicCreated { forum_topic_created }) => { + Some(forum_topic_created) + } + _ => None, + } + } + + #[must_use] + pub fn forum_topic_edited(&self) -> Option<&types::ForumTopicEdited> { + match &self.kind { + ForumTopicEdited(MessageForumTopicEdited { forum_topic_edited }) => { + Some(forum_topic_edited) + } + _ => None, + } + } + + #[must_use] + pub fn forum_topic_closed(&self) -> Option<&types::ForumTopicClosed> { + match &self.kind { + ForumTopicClosed(MessageForumTopicClosed { forum_topic_closed }) => { + Some(forum_topic_closed) + } + _ => None, + } + } + + #[must_use] + pub fn forum_topic_reopened(&self) -> Option<&types::ForumTopicReopened> { + match &self.kind { + ForumTopicReopened(MessageForumTopicReopened { forum_topic_reopened }) => { + Some(forum_topic_reopened) + } + _ => None, + } + } + + #[must_use] + pub fn general_forum_topic_hidden(&self) -> Option<&types::GeneralForumTopicHidden> { + match &self.kind { + GeneralForumTopicHidden(MessageGeneralForumTopicHidden { + general_forum_topic_hidden, + }) => Some(general_forum_topic_hidden), + _ => None, + } + } + + #[must_use] + pub fn general_forum_topic_unhidden(&self) -> Option<&types::GeneralForumTopicUnhidden> { + match &self.kind { + GeneralForumTopicUnhidden(MessageGeneralForumTopicUnhidden { + general_forum_topic_unhidden, + }) => Some(general_forum_topic_unhidden), + _ => None, + } + } + + #[must_use] + pub fn video_chat_scheduled(&self) -> Option<&types::VideoChatScheduled> { + match &self.kind { + VideoChatScheduled(MessageVideoChatScheduled { video_chat_scheduled }) => { + Some(video_chat_scheduled) + } + _ => None, + } + } + + #[must_use] + pub fn video_chat_started(&self) -> Option<&types::VideoChatStarted> { + match &self.kind { + VideoChatStarted(MessageVideoChatStarted { video_chat_started }) => { + Some(video_chat_started) + } + _ => None, + } + } + + #[must_use] + pub fn video_chat_ended(&self) -> Option<&types::VideoChatEnded> { + match &self.kind { + VideoChatEnded(MessageVideoChatEnded { video_chat_ended }) => { + Some(video_chat_ended) + } + _ => None, + } + } + #[must_use] pub fn video_chat_participants_invited( &self, @@ -1235,6 +1355,14 @@ mod getters { } } + #[must_use] + pub fn web_app_data(&self) -> Option<&types::WebAppData> { + match &self.kind { + WebAppData(MessageWebAppData { web_app_data }) => Some(web_app_data), + _ => None, + } + } + #[must_use] pub fn reply_markup(&self) -> Option<&types::InlineKeyboardMarkup> { match &self.kind { @@ -1783,8 +1911,8 @@ mod tests { let message: Message = from_str(json).unwrap(); assert_eq!(message.chat.id, old); - assert_eq!(message.chat_migration(), Some(ChatMigration::To { chat_id: new })); - assert_eq!(message.migrate_to_chat_id(), Some(new)); + assert_eq!(message.chat_migration(), Some(&ChatMigration::To { chat_id: new })); + assert_eq!(message.migrate_to_chat_id(), Some(&new)); // The user who initialized the migration assert!(message.from().is_some()); @@ -1794,8 +1922,8 @@ mod tests { let message: Message = from_str(json).unwrap(); assert_eq!(message.chat.id, new); - assert_eq!(message.chat_migration(), Some(ChatMigration::From { chat_id: old })); - assert_eq!(message.migrate_from_chat_id(), Some(old)); + assert_eq!(message.chat_migration(), Some(&ChatMigration::From { chat_id: old })); + assert_eq!(message.migrate_from_chat_id(), Some(&old)); // Anonymous bot assert!(message.from().is_some()); diff --git a/crates/teloxide/src/dispatching/filter_ext.rs b/crates/teloxide/src/dispatching/filter_ext.rs index f6453184..57df7198 100644 --- a/crates/teloxide/src/dispatching/filter_ext.rs +++ b/crates/teloxide/src/dispatching/filter_ext.rs @@ -68,22 +68,58 @@ macro_rules! define_message_ext { // May be expanded in the future. define_message_ext! { + // MessageCommon (filter_from, Message::from), + // MediaKind variants of the MessageKind::Common (filter_animation, Message::animation), (filter_audio, Message::audio), (filter_contact, Message::contact), (filter_document, Message::document), + (filter_game, Message::game), + (filter_venue, Message::venue), (filter_location, Message::location), (filter_photo, Message::photo), (filter_poll, Message::poll), (filter_sticker, Message::sticker), (filter_text, Message::text), + (filter_video, Message::video), + (filter_video_note, Message::video_note), + (filter_voice, Message::voice), + (filter_migration, Message::chat_migration), + (filter_migration_from, Message::migrate_from_chat_id), + (filter_migration_to, Message::migrate_to_chat_id), + (filter_reply_to_message, Message::reply_to_message), (filter_forward_from, Message::forward_from), + // Rest variants of a MessageKind (filter_new_chat_members, Message::new_chat_members), (filter_left_chat_member, Message::left_chat_member), + (filter_new_chat_title, Message::new_chat_title), + (filter_new_chat_photo, Message::new_chat_photo), + (filter_delete_chat_photo, Message::delete_chat_photo), + (filter_group_chat_created, Message::group_chat_created), + (filter_supergroup_chat_created, Message::super_group_chat_created), + (filter_channel_chat_created, Message::channel_chat_created), + (filter_message_auto_delete_timer_changed, Message::message_auto_delete_timer_changed), (filter_pinned, Message::pinned_message), + (filter_invoice, Message::invoice), + (filter_successful_payment, Message::successful_payment), + (filter_connected_website, Message::connected_website), + (filter_write_access_allowed, Message::write_access_allowed), + (filter_passport_data, Message::passport_data), (filter_dice, Message::dice), + (filter_proximity_alert_triggered, Message::proximity_alert_triggered), + (filter_forum_topic_created, Message::forum_topic_created), + (filter_forum_topic_edited, Message::forum_topic_edited), + (filter_forum_topic_closed, Message::forum_topic_closed), + (filter_forum_topic_reopened, Message::forum_topic_reopened), + (filter_general_forum_topic_hidden, Message::general_forum_topic_hidden), + (filter_general_forum_topic_unhidden, Message::general_forum_topic_unhidden), + (filter_video_chat_scheduled, Message::video_chat_scheduled), + (filter_video_chat_started, Message::video_chat_started), + (filter_video_chat_ended, Message::video_chat_ended), + (filter_video_chat_participants_invited, Message::video_chat_participants_invited), + (filter_web_app_data, Message::web_app_data), } macro_rules! define_update_ext {