common: Deserialize stable names for unstable features

This commit is contained in:
Kévin Commaille 2022-03-19 12:32:42 +01:00 committed by Kévin Commaille
parent 5c2c13145d
commit 0fb3f39c07
7 changed files with 113 additions and 22 deletions

View File

@ -135,7 +135,7 @@ pub struct Relations {
/// Thread relation. /// Thread relation.
#[cfg(feature = "unstable-msc3440")] #[cfg(feature = "unstable-msc3440")]
#[serde(rename = "io.element.thread")] #[serde(rename = "io.element.thread", alias = "m.thread")]
pub thread: Option<BundledThread>, pub thread: Option<BundledThread>,
} }

View File

@ -73,7 +73,11 @@ pub struct ImageInfo {
/// This uses the unstable prefix in /// This uses the unstable prefix in
/// [MSC2448](https://github.com/matrix-org/matrix-spec-proposals/pull/2448). /// [MSC2448](https://github.com/matrix-org/matrix-spec-proposals/pull/2448).
#[cfg(feature = "unstable-msc2448")] #[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<String>, pub blurhash: Option<String>,
} }

View File

@ -91,7 +91,11 @@ pub struct ImageInfo {
/// This uses the unstable prefix in /// This uses the unstable prefix in
/// [MSC2448](https://github.com/matrix-org/matrix-spec-proposals/pull/2448). /// [MSC2448](https://github.com/matrix-org/matrix-spec-proposals/pull/2448).
#[cfg(feature = "unstable-msc2448")] #[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<String>, pub blurhash: Option<String>,
} }

View File

@ -18,8 +18,10 @@ impl<'de> Deserialize<'de> for Relation {
let ev = EventWithRelatesToJsonRepr::deserialize(deserializer)?; let ev = EventWithRelatesToJsonRepr::deserialize(deserializer)?;
#[cfg(feature = "unstable-msc3440")] #[cfg(feature = "unstable-msc3440")]
if let Some(RelationJsonRepr::Thread(ThreadJsonRepr { event_id, is_falling_back })) = if let Some(
ev.relates_to.relation 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 let in_reply_to = ev
.relates_to .relates_to
@ -27,7 +29,6 @@ impl<'de> Deserialize<'de> for Relation {
.ok_or_else(|| serde::de::Error::missing_field("m.in_reply_to"))?; .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 })); 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 { let rel = if let Some(in_reply_to) = ev.relates_to.in_reply_to {
Relation::Reply { in_reply_to } Relation::Reply { in_reply_to }
} else if let Some(relation) = ev.relates_to.relation { } else if let Some(relation) = ev.relates_to.relation {
@ -40,7 +41,9 @@ impl<'de> Deserialize<'de> for Relation {
Relation::Replacement(Replacement { event_id }) Relation::Replacement(Replacement { event_id })
} }
#[cfg(feature = "unstable-msc3440")] #[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 // FIXME: Maybe we should log this, though at this point we don't even have
// access to the rel_type of the unknown relation. // access to the rel_type of the unknown relation.
RelationJsonRepr::Unknown => Relation::_Custom, RelationJsonRepr::Unknown => Relation::_Custom,
@ -81,7 +84,7 @@ impl Serialize for Relation {
Relation::Thread(Thread { event_id, in_reply_to, is_falling_back }) => { Relation::Thread(Thread { event_id, in_reply_to, is_falling_back }) => {
RelatesToJsonRepr { RelatesToJsonRepr {
in_reply_to: Some(in_reply_to.clone()), in_reply_to: Some(in_reply_to.clone()),
relation: Some(RelationJsonRepr::Thread(ThreadJsonRepr { relation: Some(RelationJsonRepr::ThreadUnstable(ThreadUnstableJsonRepr {
event_id: event_id.clone(), event_id: event_id.clone(),
is_falling_back: *is_falling_back, 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)] #[derive(Clone, Deserialize, Serialize)]
#[cfg(feature = "unstable-msc3440")] #[cfg(feature = "unstable-msc3440")]
struct ThreadJsonRepr { struct ThreadStableJsonRepr {
/// The ID of the root message in the thread.
pub event_id: Box<EventId>,
/// 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. /// The ID of the root message in the thread.
pub event_id: Box<EventId>, pub event_id: Box<EventId>,
@ -153,10 +169,15 @@ enum RelationJsonRepr {
#[serde(rename = "m.replace")] #[serde(rename = "m.replace")]
Replacement(Replacement), 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")] #[cfg(feature = "unstable-msc3440")]
#[serde(rename = "io.element.thread")] #[serde(rename = "io.element.thread")]
Thread(ThreadJsonRepr), ThreadUnstable(ThreadUnstableJsonRepr),
/// An unknown relation type. /// An unknown relation type.
/// ///

View File

@ -76,7 +76,11 @@ pub struct RoomMemberEventContent {
/// This uses the unstable prefix in /// This uses the unstable prefix in
/// [MSC2448](https://github.com/matrix-org/matrix-spec-proposals/pull/2448). /// [MSC2448](https://github.com/matrix-org/matrix-spec-proposals/pull/2448).
#[cfg(feature = "unstable-msc2448")] #[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<String>, pub blurhash: Option<String>,
/// User-supplied text for why their membership has changed. /// User-supplied text for why their membership has changed.

View File

@ -18,8 +18,10 @@ impl<'de> Deserialize<'de> for Relation {
let ev = EventWithRelatesToJsonRepr::deserialize(deserializer)?; let ev = EventWithRelatesToJsonRepr::deserialize(deserializer)?;
#[cfg(feature = "unstable-msc3440")] #[cfg(feature = "unstable-msc3440")]
if let Some(RelationJsonRepr::Thread(ThreadJsonRepr { event_id, is_falling_back })) = if let Some(
ev.relates_to.relation 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 let in_reply_to = ev
.relates_to .relates_to
@ -43,7 +45,9 @@ impl<'de> Deserialize<'de> for Relation {
// access to the rel_type of the unknown relation. // access to the rel_type of the unknown relation.
RelationJsonRepr::Unknown => Relation::_Custom, RelationJsonRepr::Unknown => Relation::_Custom,
#[cfg(feature = "unstable-msc3440")] #[cfg(feature = "unstable-msc3440")]
RelationJsonRepr::Thread(_) => unreachable!(), RelationJsonRepr::ThreadStable(_) | RelationJsonRepr::ThreadUnstable(_) => {
unreachable!()
}
} }
} else { } else {
Relation::_Custom Relation::_Custom
@ -80,7 +84,7 @@ impl Serialize for Relation {
Relation::Thread(Thread { event_id, in_reply_to, is_falling_back }) => { Relation::Thread(Thread { event_id, in_reply_to, is_falling_back }) => {
EventWithRelatesToJsonRepr::new(RelatesToJsonRepr { EventWithRelatesToJsonRepr::new(RelatesToJsonRepr {
in_reply_to: Some(in_reply_to.clone()), in_reply_to: Some(in_reply_to.clone()),
relation: Some(RelationJsonRepr::Thread(ThreadJsonRepr { relation: Some(RelationJsonRepr::ThreadUnstable(ThreadUnstableJsonRepr {
event_id: event_id.clone(), event_id: event_id.clone(),
is_falling_back: *is_falling_back, is_falling_back: *is_falling_back,
})), })),
@ -140,10 +144,15 @@ enum RelationJsonRepr {
#[serde(rename = "m.replace")] #[serde(rename = "m.replace")]
Replacement(ReplacementJsonRepr), 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")] #[cfg(feature = "unstable-msc3440")]
#[serde(rename = "io.element.thread")] #[serde(rename = "io.element.thread")]
Thread(ThreadJsonRepr), ThreadUnstable(ThreadUnstableJsonRepr),
/// An unknown relation type. /// An unknown relation type.
/// ///
@ -159,10 +168,23 @@ struct ReplacementJsonRepr {
event_id: Box<EventId>, event_id: Box<EventId>,
} }
/// A thread relation without the reply fallback. /// A thread relation without the reply fallback, with stable names.
#[derive(Clone, Deserialize, Serialize)] #[derive(Clone, Deserialize, Serialize)]
#[cfg(feature = "unstable-msc3440")] #[cfg(feature = "unstable-msc3440")]
struct ThreadJsonRepr { struct ThreadStableJsonRepr {
/// The ID of the root message in the thread.
event_id: Box<EventId>,
/// 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. /// The ID of the root message in the thread.
event_id: Box<EventId>, event_id: Box<EventId>,

View File

@ -255,7 +255,43 @@ fn thread_reply_serialize() {
#[test] #[test]
#[cfg(feature = "unstable-msc3440")] #[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": "<text msg>",
"m.relates_to": {
"rel_type": "m.thread",
"event_id": "$1598361704261elfgc",
"m.in_reply_to": {
"event_id": "$latesteventid",
},
},
});
assert_matches!(
from_json_value::<RoomMessageEventContent>(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; use ruma_common::events::room::message::Thread;
let json = json!({ let json = json!({