mirror of
https://github.com/teloxide/teloxide.git
synced 2024-12-22 14:35:36 +01:00
Merge pull request #830 from teloxide/fix-topic-deserialization
Fix deserialization of messages from topics
This commit is contained in:
commit
5bae7f7cd1
4 changed files with 73 additions and 2 deletions
|
@ -11,10 +11,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||
|
||||
- `Update::user` now handles channel posts, chat member changes and chat join request updates correctly ([#835][pr835])
|
||||
- In cases when `teloxide` can't deserialize an update, error now includes the full json value ([#826][pr826])
|
||||
|
||||
- Deserialization of topic messages ([#830][pr830])
|
||||
|
||||
[pr835]: https://github.com/teloxide/teloxide/pull/835
|
||||
[pr826]: https://github.com/teloxide/teloxide/pull/826
|
||||
[pr830]: https://github.com/teloxide/teloxide/pull/830
|
||||
|
||||
## 0.9.0 - 2023-01-17
|
||||
|
||||
|
|
|
@ -461,6 +461,13 @@ pub(crate) mod serde_rgb {
|
|||
{
|
||||
Ok(from_u32(v))
|
||||
}
|
||||
|
||||
fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E>
|
||||
where
|
||||
E: serde::de::Error,
|
||||
{
|
||||
self.visit_u32(v.try_into().map_err(|_| E::custom("rgb value doesn't fit u32"))?)
|
||||
}
|
||||
}
|
||||
d.deserialize_u32(V)
|
||||
}
|
||||
|
@ -481,5 +488,16 @@ pub(crate) mod serde_rgb {
|
|||
}
|
||||
|
||||
#[test]
|
||||
fn json() {}
|
||||
fn json() {
|
||||
#[derive(Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
||||
struct Struct {
|
||||
#[serde(with = "self")]
|
||||
color: [u8; 3],
|
||||
}
|
||||
|
||||
let json = format!(r#"{{"color":{}}}"#, 0x00AABBCC);
|
||||
let Struct { color } = serde_json::from_str(&json).unwrap();
|
||||
|
||||
assert_eq!(color, [0xAA, 0xBB, 0xCC])
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,3 +19,20 @@ pub struct ForumTopicCreated {
|
|||
// FIXME: CustomEmojiId
|
||||
pub icon_custom_emoji_id: Option<String>,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::types::ForumTopicCreated;
|
||||
|
||||
#[test]
|
||||
fn deserialization() {
|
||||
let json =
|
||||
r#"{"icon_color":9367192,"icon_custom_emoji_id":"5312536423851630001","name":"???"}"#;
|
||||
|
||||
let event = serde_json::from_str::<ForumTopicCreated>(json).unwrap();
|
||||
|
||||
assert_eq!(event.name, "???");
|
||||
assert_eq!(event.icon_color, [0x8E, 0xEE, 0x98]);
|
||||
assert_eq!(event.icon_custom_emoji_id.as_deref(), Some("5312536423851630001"));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -26,6 +26,7 @@ pub struct Message {
|
|||
/// Unique identifier of a message thread to which the message belongs; for
|
||||
/// supergroups only.
|
||||
// FIXME: MessageThreadId or such
|
||||
#[serde(rename = "message_thread_id")]
|
||||
pub thread_id: Option<i32>,
|
||||
|
||||
/// Date the message was sent in Unix time.
|
||||
|
@ -116,6 +117,8 @@ pub struct MessageCommon {
|
|||
pub reply_markup: Option<InlineKeyboardMarkup>,
|
||||
|
||||
/// `true`, if the message is sent to a forum topic.
|
||||
// FIXME: `is_topic_message` is included even in service messages, like ForumTopicCreated.
|
||||
// more this to `Message`
|
||||
#[serde(default)]
|
||||
pub is_topic_message: bool,
|
||||
|
||||
|
@ -1837,4 +1840,36 @@ mod tests {
|
|||
assert!(!entities.is_empty());
|
||||
assert_eq!(entities[0].kind().clone(), MessageEntityKind::Url);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn topic_created() {
|
||||
let json = r#"{
|
||||
"chat":{"id":-1001847508954,"is_forum":true,"title":"twest","type":"supergroup"},
|
||||
"date":1675229139,
|
||||
"forum_topic_created":{
|
||||
"icon_color":9367192,
|
||||
"icon_custom_emoji_id":"5312536423851630001",
|
||||
"name":"???"
|
||||
},
|
||||
"from":{
|
||||
"first_name":"вафель'",
|
||||
"id":1253681278,
|
||||
"is_bot":false,
|
||||
"language_code":"en",
|
||||
"username":"wafflelapkin"
|
||||
},
|
||||
"is_topic_message":true,
|
||||
"message_id":4,
|
||||
"message_thread_id":4
|
||||
}"#;
|
||||
|
||||
let _: Message = serde_json::from_str(json).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn topic_message() {
|
||||
let json = r#"{"chat":{"id":-1001847508954,"is_forum":true,"title":"twest","type":"supergroup"},"date":1675229140,"from":{"first_name":"вафель'","id":1253681278,"is_bot":false,"language_code":"en","username":"wafflelapkin"},"is_topic_message":true,"message_id":5,"message_thread_id":4,"reply_to_message":{"chat":{"id":-1001847508954,"is_forum":true,"title":"twest","type":"supergroup"},"date":1675229139,"forum_topic_created":{"icon_color":9367192,"icon_custom_emoji_id":"5312536423851630001","name":"???"},"from":{"first_name":"вафель'","id":1253681278,"is_bot":false,"language_code":"en","username":"wafflelapkin"},"is_topic_message":true,"message_id":4,"message_thread_id":4},"text":"blah"}"#;
|
||||
|
||||
let _: Message = serde_json::from_str(json).unwrap();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue