events: Stabilize support for event replacements

This commit is contained in:
Kévin Commaille 2022-10-01 16:29:39 +02:00 committed by Kévin Commaille
parent b695dee787
commit 4e2dac30be
7 changed files with 19 additions and 45 deletions

View File

@ -17,6 +17,7 @@ Improvements:
* Move `Relation::Thread` and associated types and methods out of `unstable-msc3440`
* Add parameter to `RoomMessageEventContent::make_reply_to` to be thread-aware
* Add `RoomMessageEventContent::make_for_reply`
* Stabilize support for event replacements (edits)
# 0.10.3

View File

@ -8,14 +8,10 @@ use js_int::UInt;
use serde::{Deserialize, Serialize};
use super::AnyMessageLikeEvent;
#[cfg(any(feature = "unstable-msc2676", feature = "unstable-msc2677"))]
use crate::MilliSecondsSinceUnixEpoch;
use crate::{
serde::{Raw, StringEnum},
PrivOwnedStr,
MilliSecondsSinceUnixEpoch, OwnedEventId, OwnedUserId, PrivOwnedStr,
};
#[cfg(feature = "unstable-msc2676")]
use crate::{OwnedEventId, OwnedUserId};
/// Summary of all annotations to an event with the given key and type.
#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize)]
@ -87,7 +83,6 @@ impl AnnotationChunk {
/// A bundled replacement.
#[derive(Clone, Debug, Deserialize, Serialize)]
#[cfg(feature = "unstable-msc2676")]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct BundledReplacement {
/// The ID of the replacing event.
@ -97,15 +92,17 @@ pub struct BundledReplacement {
pub sender: OwnedUserId,
/// Timestamp in milliseconds on originating homeserver when the latest replacement was sent.
#[serde(skip_serializing_if = "Option::is_none")]
pub origin_server_ts: Option<MilliSecondsSinceUnixEpoch>,
pub origin_server_ts: MilliSecondsSinceUnixEpoch,
}
#[cfg(feature = "unstable-msc2676")]
impl BundledReplacement {
/// Creates a new `BundledReplacement` with the given event ID and sender.
pub fn new(event_id: OwnedEventId, sender: OwnedUserId) -> Self {
Self { event_id, sender, origin_server_ts: None }
/// Creates a new `BundledReplacement` with the given event ID, sender and timestamp.
pub fn new(
event_id: OwnedEventId,
sender: OwnedUserId,
origin_server_ts: MilliSecondsSinceUnixEpoch,
) -> Self {
Self { event_id, sender, origin_server_ts }
}
}
@ -146,7 +143,6 @@ pub struct Relations {
pub annotation: Option<AnnotationChunk>,
/// Replacement relation.
#[cfg(feature = "unstable-msc2676")]
#[serde(rename = "m.replace")]
pub replace: Option<BundledReplacement>,
@ -173,7 +169,6 @@ pub enum RelationType {
Annotation,
/// `m.replace`, a replacement.
#[cfg(feature = "unstable-msc2676")]
#[ruma_enum(rename = "m.replace")]
Replacement,

View File

@ -91,7 +91,6 @@ pub enum Relation {
},
/// An event that replaces another event.
#[cfg(feature = "unstable-msc2676")]
Replacement(Replacement),
/// A reference to another event.
@ -112,7 +111,6 @@ 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-msc2676")]
message::Relation::Replacement(re) => {
Self::Replacement(Replacement { event_id: re.event_id })
}
@ -126,13 +124,14 @@ impl From<message::Relation> for Relation {
}
}
/// The event this relation belongs to replaces another event.
/// The event this relation belongs to [replaces another event].
///
/// In contrast to [`message::Replacement`](super::message::Replacement), this struct doesn't
/// store the new content, since that is part of the encrypted content of an `m.room.encrypted`
/// events.
///
/// [replaces another event]: https://spec.matrix.org/v1.4/client-server-api/#event-replacements
#[derive(Clone, Debug, Deserialize, Serialize)]
#[cfg(feature = "unstable-msc2676")]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct Replacement {
/// The ID of the event being replacing.

View File

@ -2,9 +2,7 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer};
#[cfg(feature = "unstable-msc2677")]
use super::Annotation;
#[cfg(feature = "unstable-msc2676")]
use super::Replacement;
use super::{InReplyTo, Reference, Relation, Thread};
use super::{InReplyTo, Reference, Relation, Replacement, Thread};
use crate::OwnedEventId;
impl<'de> Deserialize<'de> for Relation {
@ -32,7 +30,6 @@ impl<'de> Deserialize<'de> for Relation {
#[cfg(feature = "unstable-msc2677")]
RelationJsonRepr::Annotation(a) => Relation::Annotation(a),
RelationJsonRepr::Reference(r) => Relation::Reference(r),
#[cfg(feature = "unstable-msc2676")]
RelationJsonRepr::Replacement(Replacement { event_id }) => {
Relation::Replacement(Replacement { event_id })
}
@ -67,7 +64,6 @@ impl Serialize for Relation {
relation: Some(RelationJsonRepr::Reference(r.clone())),
..Default::default()
},
#[cfg(feature = "unstable-msc2676")]
Relation::Replacement(r) => RelatesToJsonRepr {
relation: Some(RelationJsonRepr::Replacement(r.clone())),
..Default::default()
@ -157,7 +153,6 @@ enum RelationJsonRepr {
Reference(Reference),
/// An event that replaces another event.
#[cfg(feature = "unstable-msc2676")]
#[serde(rename = "m.replace")]
Replacement(Replacement),

View File

@ -469,7 +469,6 @@ pub enum Relation {
},
/// An event that replaces another event.
#[cfg(feature = "unstable-msc2676")]
Replacement(Replacement),
/// An event that belongs to a thread.
@ -494,9 +493,10 @@ impl InReplyTo {
}
}
/// The event this relation belongs to replaces another event.
/// The event this relation belongs to [replaces another event].
///
/// [replaces another event]: https://spec.matrix.org/v1.4/client-server-api/#event-replacements
#[derive(Clone, Debug)]
#[cfg(feature = "unstable-msc2676")]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct Replacement {
/// The ID of the event being replaced.
@ -506,7 +506,6 @@ pub struct Replacement {
pub new_content: Box<RoomMessageEventContent>,
}
#[cfg(feature = "unstable-msc2676")]
impl Replacement {
/// Creates a new `Replacement` with the given event ID and new content.
pub fn new(event_id: OwnedEventId, new_content: Box<RoomMessageEventContent>) -> Self {

View File

@ -1,10 +1,6 @@
use serde::{Deserialize, Deserializer, Serialize, Serializer};
#[cfg(feature = "unstable-msc2676")]
use super::Replacement;
#[cfg(feature = "unstable-msc2676")]
use super::RoomMessageEventContent;
use super::{InReplyTo, Relation, Thread};
use super::{InReplyTo, Relation, Replacement, RoomMessageEventContent, Thread};
use crate::OwnedEventId;
impl<'de> Deserialize<'de> for Relation {
@ -30,7 +26,6 @@ impl<'de> Deserialize<'de> for Relation {
Relation::Reply { in_reply_to }
} else if let Some(relation) = ev.relates_to.relation {
match relation {
#[cfg(feature = "unstable-msc2676")]
RelationJsonRepr::Replacement(ReplacementJsonRepr { event_id }) => {
let new_content = ev
.new_content
@ -63,7 +58,6 @@ impl Serialize for Relation {
in_reply_to: Some(in_reply_to.clone()),
..Default::default()
}),
#[cfg(feature = "unstable-msc2676")]
Relation::Replacement(Replacement { event_id, new_content }) => {
EventWithRelatesToJsonRepr {
relates_to: RelatesToJsonRepr {
@ -97,18 +91,13 @@ struct EventWithRelatesToJsonRepr {
#[serde(rename = "m.relates_to", default, skip_serializing_if = "RelatesToJsonRepr::is_empty")]
relates_to: RelatesToJsonRepr,
#[cfg(feature = "unstable-msc2676")]
#[serde(rename = "m.new_content", skip_serializing_if = "Option::is_none")]
new_content: Option<Box<RoomMessageEventContent>>,
}
impl EventWithRelatesToJsonRepr {
fn new(relates_to: RelatesToJsonRepr) -> Self {
Self {
relates_to,
#[cfg(feature = "unstable-msc2676")]
new_content: None,
}
Self { relates_to, new_content: None }
}
}
@ -134,7 +123,6 @@ impl RelatesToJsonRepr {
#[serde(tag = "rel_type")]
enum RelationJsonRepr {
/// An event that replaces another event.
#[cfg(feature = "unstable-msc2676")]
#[serde(rename = "m.replace")]
Replacement(ReplacementJsonRepr),
@ -155,7 +143,6 @@ enum RelationJsonRepr {
}
#[derive(Clone, Deserialize, Serialize)]
#[cfg(feature = "unstable-msc2676")]
struct ReplacementJsonRepr {
event_id: OwnedEventId,
}

View File

@ -66,7 +66,6 @@ fn reply_serialize() {
}
#[test]
#[cfg(feature = "unstable-msc2676")]
fn replacement_serialize() {
use ruma_common::events::room::message::Replacement;
@ -120,7 +119,6 @@ fn replacement_serialize() {
}
#[test]
#[cfg(feature = "unstable-msc2676")]
fn replacement_deserialize() {
let json = json!({
"msgtype": "m.text",