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` * Move `Relation::Thread` and associated types and methods out of `unstable-msc3440`
* Add parameter to `RoomMessageEventContent::make_reply_to` to be thread-aware * Add parameter to `RoomMessageEventContent::make_reply_to` to be thread-aware
* Add `RoomMessageEventContent::make_for_reply` * Add `RoomMessageEventContent::make_for_reply`
* Stabilize support for event replacements (edits)
# 0.10.3 # 0.10.3

View File

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

View File

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

View File

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

View File

@ -469,7 +469,6 @@ pub enum Relation {
}, },
/// An event that replaces another event. /// An event that replaces another event.
#[cfg(feature = "unstable-msc2676")]
Replacement(Replacement), Replacement(Replacement),
/// An event that belongs to a thread. /// 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)] #[derive(Clone, Debug)]
#[cfg(feature = "unstable-msc2676")]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct Replacement { pub struct Replacement {
/// The ID of the event being replaced. /// The ID of the event being replaced.
@ -506,7 +506,6 @@ pub struct Replacement {
pub new_content: Box<RoomMessageEventContent>, pub new_content: Box<RoomMessageEventContent>,
} }
#[cfg(feature = "unstable-msc2676")]
impl Replacement { impl Replacement {
/// Creates a new `Replacement` with the given event ID and new content. /// Creates a new `Replacement` with the given event ID and new content.
pub fn new(event_id: OwnedEventId, new_content: Box<RoomMessageEventContent>) -> Self { pub fn new(event_id: OwnedEventId, new_content: Box<RoomMessageEventContent>) -> Self {

View File

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

View File

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