diff --git a/crates/ruma-events/src/room/encrypted.rs b/crates/ruma-events/src/room/encrypted.rs index 1dcf6d5a..d3858bb7 100644 --- a/crates/ruma-events/src/room/encrypted.rs +++ b/crates/ruma-events/src/room/encrypted.rs @@ -10,7 +10,7 @@ use ruma_identifiers::EventId; use serde::{Deserialize, Serialize}; #[cfg(feature = "unstable-pre-spec")] -use crate::{key::verification, reaction, room::message::Replacement}; +use crate::{key::verification, reaction}; use crate::{ room::message::{self, InReplyTo}, MessageEvent, @@ -95,6 +95,19 @@ pub enum Relation { Annotation(Annotation), } +/// The event this relation belongs to replaces another event. +/// +/// In contrast to [`message::Replacement`], this struct doesn't store the new content, since that +/// is part of the encrypted payload for `m.encrypted` events. +#[derive(Clone, Debug, Deserialize, Serialize)] +#[cfg(feature = "unstable-pre-spec")] +#[cfg_attr(docsrs, doc(cfg(feature = "unstable-pre-spec")))] +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] +pub struct Replacement { + /// The ID of the event being replacing. + pub event_id: EventId, +} + /// A reference to another event. #[derive(Clone, Debug, Deserialize, Serialize)] #[cfg(feature = "unstable-pre-spec")] @@ -221,12 +234,15 @@ impl From for MegolmV1AesSha2Content { } } +// FIXME: Remove on next breaking change release impl From for Relation { fn from(rel: message::Relation) -> Self { match rel { message::Relation::Reply { in_reply_to } => Self::Reply { in_reply_to }, #[cfg(feature = "unstable-pre-spec")] - message::Relation::Replacement(re) => Self::Replacement(re), + message::Relation::Replacement(re) => { + Self::Replacement(Replacement { event_id: re.event_id }) + } } } } diff --git a/crates/ruma-events/src/room/encrypted/relation_serde.rs b/crates/ruma-events/src/room/encrypted/relation_serde.rs index 84317611..77f7f7f3 100644 --- a/crates/ruma-events/src/room/encrypted/relation_serde.rs +++ b/crates/ruma-events/src/room/encrypted/relation_serde.rs @@ -1,12 +1,8 @@ -#[cfg(feature = "unstable-pre-spec")] -use ruma_identifiers::EventId; use serde::{ser::SerializeStruct as _, Deserialize, Deserializer, Serialize, Serializer}; #[cfg(feature = "unstable-pre-spec")] use super::{Annotation, Reference, Replacement}; use super::{InReplyTo, Relation}; -#[cfg(feature = "unstable-pre-spec")] -use crate::room::message::MessageEventContent; pub fn deserialize<'de, D>(deserializer: D) -> Result, D::Error> where @@ -22,9 +18,8 @@ where let relation = match relation { RelationJsonRepr::Annotation(a) => Relation::Annotation(a), RelationJsonRepr::Reference(r) => Relation::Reference(r), - RelationJsonRepr::Replacement(ReplacementJsonRepr { event_id }) => { - let new_content = ev.new_content?; - Relation::Replacement(Replacement { event_id, new_content }) + RelationJsonRepr::Replacement(Replacement { event_id }) => { + Relation::Replacement(Replacement { event_id }) } // FIXME: Maybe we should log this, though at this point we don't even have access // to the rel_type of the unknown relation. @@ -64,17 +59,12 @@ where ..Default::default() }), #[cfg(feature = "unstable-pre-spec")] - Relation::Replacement(Replacement { event_id, new_content }) => { - EventWithRelatesToJsonRepr { - relates_to: RelatesToJsonRepr { - relation: Some(RelationJsonRepr::Replacement(ReplacementJsonRepr { - event_id: event_id.clone(), - })), - ..Default::default() - }, - new_content: Some(new_content.clone()), - } - } + Relation::Replacement(r) => EventWithRelatesToJsonRepr { + relates_to: RelatesToJsonRepr { + relation: Some(RelationJsonRepr::Replacement(r.clone())), + ..Default::default() + }, + }, Relation::Reply { in_reply_to } => EventWithRelatesToJsonRepr::new(RelatesToJsonRepr { in_reply_to: Some(in_reply_to.clone()), ..Default::default() @@ -88,19 +78,11 @@ where struct EventWithRelatesToJsonRepr { #[serde(rename = "m.relates_to", default, skip_serializing_if = "RelatesToJsonRepr::is_empty")] relates_to: RelatesToJsonRepr, - - #[cfg(feature = "unstable-pre-spec")] - #[serde(rename = "m.new_content", skip_serializing_if = "Option::is_none")] - new_content: Option>, } impl EventWithRelatesToJsonRepr { fn new(relates_to: RelatesToJsonRepr) -> Self { - Self { - relates_to, - #[cfg(feature = "unstable-pre-spec")] - new_content: None, - } + Self { relates_to } } } @@ -145,7 +127,7 @@ enum RelationJsonRepr { /// An event that replaces another event. #[serde(rename = "m.replace")] - Replacement(ReplacementJsonRepr), + Replacement(Replacement), /// An unknown relation type. /// @@ -154,9 +136,3 @@ enum RelationJsonRepr { #[serde(other)] Unknown, } - -#[derive(Clone, Deserialize, Serialize)] -#[cfg(feature = "unstable-pre-spec")] -struct ReplacementJsonRepr { - event_id: EventId, -}