Add ChatBoostAdded variant to MessageKind enum

also add `boost_added` getter to `Message` struct and `filter_boost_added` filter to `MessageFilterExt`
This commit is contained in:
Andrey Brusnik 2024-07-20 17:48:27 +04:00
parent 02d7a3cd2e
commit 109454ed84
No known key found for this signature in database
GPG key ID: D33232F28CFF442C
2 changed files with 54 additions and 11 deletions

View file

@ -5,15 +5,15 @@ use serde::{Deserialize, Serialize};
use url::Url; use url::Url;
use crate::types::{ use crate::types::{
Animation, Audio, BareChatId, Chat, ChatId, ChatShared, Contact, Dice, Document, Animation, Audio, BareChatId, Chat, ChatBoostAdded, ChatId, ChatShared, Contact, Dice,
ExternalReplyInfo, ForumTopicClosed, ForumTopicCreated, ForumTopicEdited, ForumTopicReopened, Document, ExternalReplyInfo, ForumTopicClosed, ForumTopicCreated, ForumTopicEdited,
Game, GeneralForumTopicHidden, GeneralForumTopicUnhidden, Giveaway, GiveawayCompleted, ForumTopicReopened, Game, GeneralForumTopicHidden, GeneralForumTopicUnhidden, Giveaway,
GiveawayCreated, GiveawayWinners, InlineKeyboardMarkup, Invoice, LinkPreviewOptions, Location, GiveawayCompleted, GiveawayCreated, GiveawayWinners, InlineKeyboardMarkup, Invoice,
MaybeInaccessibleMessage, MessageAutoDeleteTimerChanged, MessageEntity, MessageEntityRef, LinkPreviewOptions, Location, MaybeInaccessibleMessage, MessageAutoDeleteTimerChanged,
MessageId, MessageOrigin, PassportData, PhotoSize, Poll, ProximityAlertTriggered, Sticker, MessageEntity, MessageEntityRef, MessageId, MessageOrigin, PassportData, PhotoSize, Poll,
Story, SuccessfulPayment, TextQuote, ThreadId, True, User, UsersShared, Venue, Video, ProximityAlertTriggered, Sticker, Story, SuccessfulPayment, TextQuote, ThreadId, True, User,
VideoChatEnded, VideoChatParticipantsInvited, VideoChatScheduled, VideoChatStarted, VideoNote, UsersShared, Venue, Video, VideoChatEnded, VideoChatParticipantsInvited, VideoChatScheduled,
Voice, WebAppData, WriteAccessAllowed, VideoChatStarted, VideoNote, Voice, WebAppData, WriteAccessAllowed,
}; };
/// This object represents a message. /// This object represents a message.
@ -84,6 +84,7 @@ pub enum MessageKind {
PassportData(MessagePassportData), PassportData(MessagePassportData),
Dice(MessageDice), Dice(MessageDice),
ProximityAlertTriggered(MessageProximityAlertTriggered), ProximityAlertTriggered(MessageProximityAlertTriggered),
ChatBoostAdded(MessageChatBoostAdded),
ForumTopicCreated(MessageForumTopicCreated), ForumTopicCreated(MessageForumTopicCreated),
ForumTopicEdited(MessageForumTopicEdited), ForumTopicEdited(MessageForumTopicEdited),
ForumTopicClosed(MessageForumTopicClosed), ForumTopicClosed(MessageForumTopicClosed),
@ -553,6 +554,13 @@ pub struct MessageProximityAlertTriggered {
pub proximity_alert_triggered: ProximityAlertTriggered, pub proximity_alert_triggered: ProximityAlertTriggered,
} }
#[serde_with::skip_serializing_none]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct MessageChatBoostAdded {
/// Service message. User boosted the chat.
pub boost_added: ChatBoostAdded,
}
#[serde_with::skip_serializing_none] #[serde_with::skip_serializing_none]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct MessageWriteAccessAllowed { pub struct MessageWriteAccessAllowed {
@ -696,8 +704,8 @@ mod getters {
}; };
use super::{ use super::{
MessageForumTopicClosed, MessageForumTopicCreated, MessageForumTopicEdited, MessageChatBoostAdded, MessageForumTopicClosed, MessageForumTopicCreated,
MessageForumTopicReopened, MessageGeneralForumTopicHidden, MessageForumTopicEdited, MessageForumTopicReopened, MessageGeneralForumTopicHidden,
MessageGeneralForumTopicUnhidden, MessageGiveaway, MessageGiveawayCompleted, MessageGeneralForumTopicUnhidden, MessageGiveaway, MessageGiveawayCompleted,
MessageGiveawayCreated, MessageGiveawayWinners, MessageMessageAutoDeleteTimerChanged, MessageGiveawayCreated, MessageGiveawayWinners, MessageMessageAutoDeleteTimerChanged,
MessageVideoChatEnded, MessageVideoChatScheduled, MessageVideoChatStarted, MessageVideoChatEnded, MessageVideoChatScheduled, MessageVideoChatStarted,
@ -1367,6 +1375,14 @@ mod getters {
} }
} }
#[must_use]
pub fn boost_added(&self) -> Option<&types::ChatBoostAdded> {
match &self.kind {
ChatBoostAdded(MessageChatBoostAdded { boost_added }) => Some(boost_added),
_ => None,
}
}
#[must_use] #[must_use]
pub fn forum_topic_created(&self) -> Option<&types::ForumTopicCreated> { pub fn forum_topic_created(&self) -> Option<&types::ForumTopicCreated> {
match &self.kind { match &self.kind {
@ -2675,4 +2691,30 @@ mod tests {
} }
) )
} }
#[test]
fn chat_boost_added() {
let json = r#"{
"message_id": 28,
"sender_chat": {
"id": -1002236736395,
"title": "Test",
"type": "channel"
},
"chat": {
"id": -1002236736395,
"title": "Test",
"type": "channel"
},
"date": 1721162702,
"boost_added": {
"boost_count": 4
}
}"#;
let message: Message = from_str(json).unwrap();
assert_eq!(
message.boost_added().expect("Failed to get ChatBoostAdded from Message!"),
&ChatBoostAdded { boost_count: 4 }
)
}
} }

View file

@ -114,6 +114,7 @@ define_message_ext! {
(filter_passport_data, Message::passport_data), (filter_passport_data, Message::passport_data),
(filter_dice, Message::dice), (filter_dice, Message::dice),
(filter_proximity_alert_triggered, Message::proximity_alert_triggered), (filter_proximity_alert_triggered, Message::proximity_alert_triggered),
(filter_boost_added, Message::boost_added),
(filter_forum_topic_created, Message::forum_topic_created), (filter_forum_topic_created, Message::forum_topic_created),
(filter_forum_topic_edited, Message::forum_topic_edited), (filter_forum_topic_edited, Message::forum_topic_edited),
(filter_forum_topic_closed, Message::forum_topic_closed), (filter_forum_topic_closed, Message::forum_topic_closed),