From 0fb3f39c071c20bb0c4e423f8a7ebb1173288e9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Commaille?= Date: Sat, 19 Mar 2022 12:32:42 +0100 Subject: [PATCH] common: Deserialize stable names for unstable features --- crates/ruma-common/src/events/relation.rs | 2 +- crates/ruma-common/src/events/room.rs | 6 ++- crates/ruma-common/src/events/room/avatar.rs | 6 ++- .../events/room/encrypted/relation_serde.rs | 39 ++++++++++++++----- crates/ruma-common/src/events/room/member.rs | 6 ++- .../src/events/room/message/relation_serde.rs | 38 ++++++++++++++---- crates/ruma-common/tests/events/relations.rs | 38 +++++++++++++++++- 7 files changed, 113 insertions(+), 22 deletions(-) diff --git a/crates/ruma-common/src/events/relation.rs b/crates/ruma-common/src/events/relation.rs index c401a12f..817df276 100644 --- a/crates/ruma-common/src/events/relation.rs +++ b/crates/ruma-common/src/events/relation.rs @@ -135,7 +135,7 @@ pub struct Relations { /// Thread relation. #[cfg(feature = "unstable-msc3440")] - #[serde(rename = "io.element.thread")] + #[serde(rename = "io.element.thread", alias = "m.thread")] pub thread: Option, } diff --git a/crates/ruma-common/src/events/room.rs b/crates/ruma-common/src/events/room.rs index 1cf0c2e4..2ead7623 100644 --- a/crates/ruma-common/src/events/room.rs +++ b/crates/ruma-common/src/events/room.rs @@ -73,7 +73,11 @@ pub struct ImageInfo { /// This uses the unstable prefix in /// [MSC2448](https://github.com/matrix-org/matrix-spec-proposals/pull/2448). #[cfg(feature = "unstable-msc2448")] - #[serde(rename = "xyz.amorgan.blurhash", skip_serializing_if = "Option::is_none")] + #[serde( + rename = "xyz.amorgan.blurhash", + alias = "blurhash", + skip_serializing_if = "Option::is_none" + )] pub blurhash: Option, } diff --git a/crates/ruma-common/src/events/room/avatar.rs b/crates/ruma-common/src/events/room/avatar.rs index 9e58bbd9..8975b62b 100644 --- a/crates/ruma-common/src/events/room/avatar.rs +++ b/crates/ruma-common/src/events/room/avatar.rs @@ -91,7 +91,11 @@ pub struct ImageInfo { /// This uses the unstable prefix in /// [MSC2448](https://github.com/matrix-org/matrix-spec-proposals/pull/2448). #[cfg(feature = "unstable-msc2448")] - #[serde(rename = "xyz.amorgan.blurhash", skip_serializing_if = "Option::is_none")] + #[serde( + rename = "xyz.amorgan.blurhash", + alias = "blurhash", + skip_serializing_if = "Option::is_none" + )] pub blurhash: Option, } diff --git a/crates/ruma-common/src/events/room/encrypted/relation_serde.rs b/crates/ruma-common/src/events/room/encrypted/relation_serde.rs index c213bd70..d40b86f9 100644 --- a/crates/ruma-common/src/events/room/encrypted/relation_serde.rs +++ b/crates/ruma-common/src/events/room/encrypted/relation_serde.rs @@ -18,8 +18,10 @@ impl<'de> Deserialize<'de> for Relation { let ev = EventWithRelatesToJsonRepr::deserialize(deserializer)?; #[cfg(feature = "unstable-msc3440")] - if let Some(RelationJsonRepr::Thread(ThreadJsonRepr { event_id, is_falling_back })) = - ev.relates_to.relation + if let Some( + RelationJsonRepr::ThreadStable(ThreadStableJsonRepr { event_id, is_falling_back }) + | RelationJsonRepr::ThreadUnstable(ThreadUnstableJsonRepr { event_id, is_falling_back }), + ) = ev.relates_to.relation { let in_reply_to = ev .relates_to @@ -27,7 +29,6 @@ impl<'de> Deserialize<'de> for Relation { .ok_or_else(|| serde::de::Error::missing_field("m.in_reply_to"))?; return Ok(Relation::Thread(Thread { event_id, in_reply_to, is_falling_back })); } - let rel = if let Some(in_reply_to) = ev.relates_to.in_reply_to { Relation::Reply { in_reply_to } } else if let Some(relation) = ev.relates_to.relation { @@ -40,7 +41,9 @@ impl<'de> Deserialize<'de> for Relation { Relation::Replacement(Replacement { event_id }) } #[cfg(feature = "unstable-msc3440")] - RelationJsonRepr::Thread(_) => unreachable!(), + RelationJsonRepr::ThreadStable(_) | RelationJsonRepr::ThreadUnstable(_) => { + unreachable!() + } // FIXME: Maybe we should log this, though at this point we don't even have // access to the rel_type of the unknown relation. RelationJsonRepr::Unknown => Relation::_Custom, @@ -81,7 +84,7 @@ impl Serialize for Relation { Relation::Thread(Thread { event_id, in_reply_to, is_falling_back }) => { RelatesToJsonRepr { in_reply_to: Some(in_reply_to.clone()), - relation: Some(RelationJsonRepr::Thread(ThreadJsonRepr { + relation: Some(RelationJsonRepr::ThreadUnstable(ThreadUnstableJsonRepr { event_id: event_id.clone(), is_falling_back: *is_falling_back, })), @@ -118,10 +121,23 @@ impl RelatesToJsonRepr { } } -/// A thread relation without the reply fallback. +/// A thread relation without the reply fallback, with stable names. #[derive(Clone, Deserialize, Serialize)] #[cfg(feature = "unstable-msc3440")] -struct ThreadJsonRepr { +struct ThreadStableJsonRepr { + /// The ID of the root message in the thread. + pub event_id: Box, + + /// Whether the `m.in_reply_to` field is a fallback for older clients or a real reply in a + /// thread. + #[serde(default, skip_serializing_if = "ruma_common::serde::is_default")] + pub is_falling_back: bool, +} + +/// A thread relation without the reply fallback, with unstable names. +#[derive(Clone, Deserialize, Serialize)] +#[cfg(feature = "unstable-msc3440")] +struct ThreadUnstableJsonRepr { /// The ID of the root message in the thread. pub event_id: Box, @@ -153,10 +169,15 @@ enum RelationJsonRepr { #[serde(rename = "m.replace")] Replacement(Replacement), - /// An event that belongs to a thread. + /// An event that belongs to a thread, with stable names. + #[cfg(feature = "unstable-msc3440")] + #[serde(rename = "m.thread")] + ThreadStable(ThreadStableJsonRepr), + + /// An event that belongs to a thread, with unstable names. #[cfg(feature = "unstable-msc3440")] #[serde(rename = "io.element.thread")] - Thread(ThreadJsonRepr), + ThreadUnstable(ThreadUnstableJsonRepr), /// An unknown relation type. /// diff --git a/crates/ruma-common/src/events/room/member.rs b/crates/ruma-common/src/events/room/member.rs index 6d5a0e38..e6f39214 100644 --- a/crates/ruma-common/src/events/room/member.rs +++ b/crates/ruma-common/src/events/room/member.rs @@ -76,7 +76,11 @@ pub struct RoomMemberEventContent { /// This uses the unstable prefix in /// [MSC2448](https://github.com/matrix-org/matrix-spec-proposals/pull/2448). #[cfg(feature = "unstable-msc2448")] - #[serde(rename = "xyz.amorgan.blurhash", skip_serializing_if = "Option::is_none")] + #[serde( + rename = "xyz.amorgan.blurhash", + alias = "blurhash", + skip_serializing_if = "Option::is_none" + )] pub blurhash: Option, /// User-supplied text for why their membership has changed. diff --git a/crates/ruma-common/src/events/room/message/relation_serde.rs b/crates/ruma-common/src/events/room/message/relation_serde.rs index 9c66aab3..60285e0f 100644 --- a/crates/ruma-common/src/events/room/message/relation_serde.rs +++ b/crates/ruma-common/src/events/room/message/relation_serde.rs @@ -18,8 +18,10 @@ impl<'de> Deserialize<'de> for Relation { let ev = EventWithRelatesToJsonRepr::deserialize(deserializer)?; #[cfg(feature = "unstable-msc3440")] - if let Some(RelationJsonRepr::Thread(ThreadJsonRepr { event_id, is_falling_back })) = - ev.relates_to.relation + if let Some( + RelationJsonRepr::ThreadStable(ThreadStableJsonRepr { event_id, is_falling_back }) + | RelationJsonRepr::ThreadUnstable(ThreadUnstableJsonRepr { event_id, is_falling_back }), + ) = ev.relates_to.relation { let in_reply_to = ev .relates_to @@ -43,7 +45,9 @@ impl<'de> Deserialize<'de> for Relation { // access to the rel_type of the unknown relation. RelationJsonRepr::Unknown => Relation::_Custom, #[cfg(feature = "unstable-msc3440")] - RelationJsonRepr::Thread(_) => unreachable!(), + RelationJsonRepr::ThreadStable(_) | RelationJsonRepr::ThreadUnstable(_) => { + unreachable!() + } } } else { Relation::_Custom @@ -80,7 +84,7 @@ impl Serialize for Relation { Relation::Thread(Thread { event_id, in_reply_to, is_falling_back }) => { EventWithRelatesToJsonRepr::new(RelatesToJsonRepr { in_reply_to: Some(in_reply_to.clone()), - relation: Some(RelationJsonRepr::Thread(ThreadJsonRepr { + relation: Some(RelationJsonRepr::ThreadUnstable(ThreadUnstableJsonRepr { event_id: event_id.clone(), is_falling_back: *is_falling_back, })), @@ -140,10 +144,15 @@ enum RelationJsonRepr { #[serde(rename = "m.replace")] Replacement(ReplacementJsonRepr), - /// An event that belongs to a thread. + /// An event that belongs to a thread, with unstable names. + #[cfg(feature = "unstable-msc3440")] + #[serde(rename = "m.thread")] + ThreadStable(ThreadStableJsonRepr), + + /// An event that belongs to a thread, with unstable names. #[cfg(feature = "unstable-msc3440")] #[serde(rename = "io.element.thread")] - Thread(ThreadJsonRepr), + ThreadUnstable(ThreadUnstableJsonRepr), /// An unknown relation type. /// @@ -159,10 +168,23 @@ struct ReplacementJsonRepr { event_id: Box, } -/// A thread relation without the reply fallback. +/// A thread relation without the reply fallback, with stable names. #[derive(Clone, Deserialize, Serialize)] #[cfg(feature = "unstable-msc3440")] -struct ThreadJsonRepr { +struct ThreadStableJsonRepr { + /// The ID of the root message in the thread. + event_id: Box, + + /// Whether the `m.in_reply_to` field is a fallback for older clients or a real reply in a + /// thread. + #[serde(default, skip_serializing_if = "ruma_common::serde::is_default")] + is_falling_back: bool, +} + +/// A thread relation without the reply fallback, with unstable names. +#[derive(Clone, Deserialize, Serialize)] +#[cfg(feature = "unstable-msc3440")] +struct ThreadUnstableJsonRepr { /// The ID of the root message in the thread. event_id: Box, diff --git a/crates/ruma-common/tests/events/relations.rs b/crates/ruma-common/tests/events/relations.rs index 598c99d0..96769ff3 100644 --- a/crates/ruma-common/tests/events/relations.rs +++ b/crates/ruma-common/tests/events/relations.rs @@ -255,7 +255,43 @@ fn thread_reply_serialize() { #[test] #[cfg(feature = "unstable-msc3440")] -fn thread_deserialize() { +fn thread_stable_deserialize() { + use ruma_common::events::room::message::Thread; + + let json = json!({ + "msgtype": "m.text", + "body": "", + "m.relates_to": { + "rel_type": "m.thread", + "event_id": "$1598361704261elfgc", + "m.in_reply_to": { + "event_id": "$latesteventid", + }, + }, + }); + + assert_matches!( + from_json_value::(json).unwrap(), + RoomMessageEventContent { + msgtype: MessageType::Text(_), + relates_to: Some(Relation::Thread( + Thread { + event_id, + in_reply_to: InReplyTo { event_id: reply_to_event_id, .. }, + is_falling_back, + .. + }, + )), + .. + } if event_id == "$1598361704261elfgc" + && reply_to_event_id == "$latesteventid" + && !is_falling_back + ); +} + +#[test] +#[cfg(feature = "unstable-msc3440")] +fn thread_unstable_deserialize() { use ruma_common::events::room::message::Thread; let json = json!({