events: Fix new_content serialization in the plain-text part of m.encrypted

This commit is contained in:
Jonas Platte 2021-07-26 13:52:49 +02:00
parent 3101be1f99
commit 6236b024fd
No known key found for this signature in database
GPG Key ID: CC154DE0E30B7C67
2 changed files with 28 additions and 36 deletions

View File

@ -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<MegolmV1AesSha2ContentInit> for MegolmV1AesSha2Content {
}
}
// FIXME: Remove on next breaking change release
impl From<message::Relation> 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 })
}
}
}
}

View File

@ -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<Option<Relation>, 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<Box<MessageEventContent>>,
}
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,
}