mirror of
https://github.com/teloxide/teloxide.git
synced 2025-03-24 23:57:38 +01:00
Use MessageId
for forwards
This commit is contained in:
parent
70b9510e33
commit
ad10c93923
4 changed files with 59 additions and 10 deletions
|
@ -55,6 +55,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||||
- `DiceEmoji` variant order ([#887][pr887])
|
- `DiceEmoji` variant order ([#887][pr887])
|
||||||
- `Dice::value` now use `u8`, instead of `i32` ([#887][pr887])
|
- `Dice::value` now use `u8`, instead of `i32` ([#887][pr887])
|
||||||
- `Invoice::total_amount`, `LabeledPrice::amount`, `PreCheckoutQuery::total_amount`, `SuccessfulPayment::total_amout` now use `u32`, instead of `i32` ([#887][pr887])
|
- `Invoice::total_amount`, `LabeledPrice::amount`, `PreCheckoutQuery::total_amount`, `SuccessfulPayment::total_amout` now use `u32`, instead of `i32` ([#887][pr887])
|
||||||
|
- `Forward::message_id` and `Message::forward_from_message_id` now use `MessageId` instead of `i32` ([#887][pr887])
|
||||||
|
|
||||||
[pr852]: https://github.com/teloxide/teloxide/pull/853
|
[pr852]: https://github.com/teloxide/teloxide/pull/853
|
||||||
[pr859]: https://github.com/teloxide/teloxide/pull/859
|
[pr859]: https://github.com/teloxide/teloxide/pull/859
|
||||||
|
|
|
@ -386,6 +386,42 @@ pub(crate) mod option_url_from_string {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) mod option_msg_id_as_int {
|
||||||
|
use crate::types::MessageId;
|
||||||
|
|
||||||
|
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
||||||
|
|
||||||
|
pub(crate) fn serialize<S>(this: &Option<MessageId>, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
|
where
|
||||||
|
S: Serializer,
|
||||||
|
{
|
||||||
|
this.map(|MessageId(id)| id).serialize(serializer)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn deserialize<'de, D>(deserializer: D) -> Result<Option<MessageId>, D::Error>
|
||||||
|
where
|
||||||
|
D: Deserializer<'de>,
|
||||||
|
{
|
||||||
|
Option::<i32>::deserialize(deserializer).map(|r| r.map(MessageId))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test() {
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
struct Struct {
|
||||||
|
#[serde(with = "crate::types::option_msg_id_as_int")]
|
||||||
|
id: Option<MessageId>,
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
let json = r#"{"id":123}"#;
|
||||||
|
let id: Struct = serde_json::from_str(json).unwrap();
|
||||||
|
assert_eq!(id.id, Some(MessageId(123)));
|
||||||
|
assert_eq!(serde_json::to_string(&id).unwrap(), json.to_owned());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) fn serialize_reply_to_message_id<S>(
|
pub(crate) fn serialize_reply_to_message_id<S>(
|
||||||
this: &Option<MessageId>,
|
this: &Option<MessageId>,
|
||||||
serializer: S,
|
serializer: S,
|
||||||
|
|
|
@ -9,9 +9,9 @@ use crate::types::{
|
||||||
ForumTopicCreated, ForumTopicEdited, ForumTopicReopened, Game, GeneralForumTopicHidden,
|
ForumTopicCreated, ForumTopicEdited, ForumTopicReopened, Game, GeneralForumTopicHidden,
|
||||||
GeneralForumTopicUnhidden, InlineKeyboardMarkup, Invoice, Location,
|
GeneralForumTopicUnhidden, InlineKeyboardMarkup, Invoice, Location,
|
||||||
MessageAutoDeleteTimerChanged, MessageEntity, MessageEntityRef, MessageId, PassportData,
|
MessageAutoDeleteTimerChanged, MessageEntity, MessageEntityRef, MessageId, PassportData,
|
||||||
PhotoSize, Poll, ProximityAlertTriggered, Sticker, SuccessfulPayment, True, User, Venue, Video,
|
PhotoSize, Poll, ProximityAlertTriggered, Sticker, SuccessfulPayment, ThreadId, True, User,
|
||||||
VideoChatEnded, VideoChatParticipantsInvited, VideoChatScheduled, VideoChatStarted, VideoNote,
|
Venue, Video, VideoChatEnded, VideoChatParticipantsInvited, VideoChatScheduled,
|
||||||
Voice, WebAppData, WriteAccessAllowed, ThreadId,
|
VideoChatStarted, VideoNote, Voice, WebAppData, WriteAccessAllowed,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// This object represents a message.
|
/// This object represents a message.
|
||||||
|
@ -290,8 +290,13 @@ pub struct Forward {
|
||||||
|
|
||||||
/// For messages forwarded from channels, identifier of the original message
|
/// For messages forwarded from channels, identifier of the original message
|
||||||
/// in the channel
|
/// in the channel
|
||||||
#[serde(rename = "forward_from_message_id")]
|
#[serde(
|
||||||
pub message_id: Option<i32>,
|
rename = "forward_from_message_id",
|
||||||
|
with = "crate::types::option_msg_id_as_int",
|
||||||
|
default,
|
||||||
|
skip_serializing_if = "Option::is_none"
|
||||||
|
)]
|
||||||
|
pub message_id: Option<MessageId>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The entity that sent the original message that later was forwarded.
|
/// The entity that sent the original message that later was forwarded.
|
||||||
|
@ -614,10 +619,10 @@ mod getters {
|
||||||
MediaLocation, MediaPhoto, MediaPoll, MediaSticker, MediaText, MediaVenue, MediaVideo,
|
MediaLocation, MediaPhoto, MediaPoll, MediaSticker, MediaText, MediaVenue, MediaVideo,
|
||||||
MediaVideoNote, MediaVoice, Message, MessageChannelChatCreated, MessageCommon,
|
MediaVideoNote, MediaVoice, Message, MessageChannelChatCreated, MessageCommon,
|
||||||
MessageConnectedWebsite, MessageDeleteChatPhoto, MessageDice, MessageEntity,
|
MessageConnectedWebsite, MessageDeleteChatPhoto, MessageDice, MessageEntity,
|
||||||
MessageGroupChatCreated, MessageInvoice, MessageLeftChatMember, MessageNewChatMembers,
|
MessageGroupChatCreated, MessageId, MessageInvoice, MessageLeftChatMember,
|
||||||
MessageNewChatPhoto, MessageNewChatTitle, MessagePassportData, MessagePinned,
|
MessageNewChatMembers, MessageNewChatPhoto, MessageNewChatTitle, MessagePassportData,
|
||||||
MessageProximityAlertTriggered, MessageSuccessfulPayment, MessageSupergroupChatCreated,
|
MessagePinned, MessageProximityAlertTriggered, MessageSuccessfulPayment,
|
||||||
MessageVideoChatParticipantsInvited, PhotoSize, True, User,
|
MessageSupergroupChatCreated, MessageVideoChatParticipantsInvited, PhotoSize, True, User,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Getters for [Message] fields from [telegram docs].
|
/// Getters for [Message] fields from [telegram docs].
|
||||||
|
@ -696,7 +701,7 @@ mod getters {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn forward_from_message_id(&self) -> Option<i32> {
|
pub fn forward_from_message_id(&self) -> Option<MessageId> {
|
||||||
self.forward().and_then(|f| f.message_id)
|
self.forward().and_then(|f| f.message_id)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,6 +5,13 @@ use serde::{Deserialize, Serialize};
|
||||||
#[serde(from = "MessageIdRaw", into = "MessageIdRaw")]
|
#[serde(from = "MessageIdRaw", into = "MessageIdRaw")]
|
||||||
pub struct MessageId(pub i32);
|
pub struct MessageId(pub i32);
|
||||||
|
|
||||||
|
// N.B. we [de]serialize `MessageId` as `{"message_id":n}`, which means that if
|
||||||
|
// you want just an integer, you need to special case it with something
|
||||||
|
// like `serde(with = "crate::types::option_msg_id_as_int")]`
|
||||||
|
//
|
||||||
|
// (we can't change the default format of `MessageId` because it's returned
|
||||||
|
// by some methods and we can't change serialization there)
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
struct MessageIdRaw {
|
struct MessageIdRaw {
|
||||||
message_id: i32,
|
message_id: i32,
|
||||||
|
|
Loading…
Add table
Reference in a new issue