diff --git a/crates/ruma-common/CHANGELOG.md b/crates/ruma-common/CHANGELOG.md index bc382893..2357317d 100644 --- a/crates/ruma-common/CHANGELOG.md +++ b/crates/ruma-common/CHANGELOG.md @@ -33,6 +33,8 @@ Breaking changes: * Make `SimplePushRule` and associated types generic over the expected type of the `rule_id` * Deduplicate and group relation structs in `events::relation`: * Move relation structs under `events::room::message` to `events::relation` + * Move common relation structs under `events::room::encrypted` to `events::relation` and remove + duplicate types Improvements: diff --git a/crates/ruma-common/src/events/enums.rs b/crates/ruma-common/src/events/enums.rs index d27c9414..d6419935 100644 --- a/crates/ruma-common/src/events/enums.rs +++ b/crates/ruma-common/src/events/enums.rs @@ -8,8 +8,9 @@ use super::{ Redact, Relations, }; use crate::{ - serde::from_raw_json_value, EventId, MilliSecondsSinceUnixEpoch, OwnedRoomId, RoomId, - RoomVersionId, TransactionId, UserId, + events::relation::{Annotation, Reference}, + serde::from_raw_json_value, + EventId, MilliSecondsSinceUnixEpoch, OwnedRoomId, RoomId, RoomVersionId, TransactionId, UserId, }; event_enum! { @@ -326,7 +327,7 @@ impl AnyMessageLikeEventContent { | Self::KeyVerificationMac(KeyVerificationMacEventContent { relates_to, .. }) | Self::KeyVerificationDone(KeyVerificationDoneEventContent { relates_to, .. }) => { let key::verification::Relation { event_id } = relates_to; - Some(encrypted::Relation::Reference(encrypted::Reference { + Some(encrypted::Relation::Reference(Reference { event_id: event_id.clone(), })) } @@ -335,7 +336,7 @@ impl AnyMessageLikeEventContent { use super::reaction; let reaction::Relation { event_id, key } = &ev.relates_to; - Some(encrypted::Relation::Annotation(encrypted::Annotation { + Some(encrypted::Relation::Annotation(Annotation { event_id: event_id.clone(), key: key.clone(), })) @@ -364,9 +365,7 @@ impl AnyMessageLikeEventContent { Self::PollResponse(PollResponseEventContent { relates_to, .. }) | Self::PollEnd(PollEndEventContent { relates_to, .. }) => { let super::poll::ReferenceRelation { event_id } = relates_to; - Some(encrypted::Relation::Reference(encrypted::Reference { - event_id: event_id.clone(), - })) + Some(encrypted::Relation::Reference(Reference { event_id: event_id.clone() })) } #[cfg(feature = "unstable-msc3381")] Self::PollStart(_) => None, diff --git a/crates/ruma-common/src/events/relation.rs b/crates/ruma-common/src/events/relation.rs index ff368956..c1af79aa 100644 --- a/crates/ruma-common/src/events/relation.rs +++ b/crates/ruma-common/src/events/relation.rs @@ -30,6 +30,28 @@ impl InReplyTo { } } +/// An [annotation] for an event. +/// +/// [annotation]: https://github.com/matrix-org/matrix-spec-proposals/pull/2677 +#[derive(Clone, Debug, Deserialize, Serialize)] +#[cfg(feature = "unstable-msc2677")] +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] +pub struct Annotation { + /// The event that is being annotated. + pub event_id: OwnedEventId, + + /// The annotation. + pub key: String, +} + +#[cfg(feature = "unstable-msc2677")] +impl Annotation { + /// Creates a new `Annotation` with the given event ID and key. + pub fn new(event_id: OwnedEventId, key: String) -> Self { + Self { event_id, key } + } +} + /// Summary of all annotations to an event with the given key and type. #[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize)] #[cfg(feature = "unstable-msc2677")] @@ -206,6 +228,23 @@ impl BundledThread { } } +/// A [reference] to another event. +/// +/// [reference]: https://spec.matrix.org/v1.5/client-server-api/#reference-relations +#[derive(Clone, Debug, Deserialize, Serialize)] +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] +pub struct Reference { + /// The ID of the event being referenced. + pub event_id: OwnedEventId, +} + +impl Reference { + /// Creates a new `Reference` with the given event ID. + pub fn new(event_id: OwnedEventId) -> Self { + Self { event_id } + } +} + /// A bundled reference. #[derive(Clone, Debug, Deserialize, Serialize)] #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] diff --git a/crates/ruma-common/src/events/room/encrypted.rs b/crates/ruma-common/src/events/room/encrypted.rs index 3f3a5d4f..d399951b 100644 --- a/crates/ruma-common/src/events/room/encrypted.rs +++ b/crates/ruma-common/src/events/room/encrypted.rs @@ -9,7 +9,12 @@ use ruma_macros::EventContent; use serde::{Deserialize, Serialize}; use super::message; -use crate::{events::relation::InReplyTo, OwnedDeviceId, OwnedEventId}; +#[cfg(feature = "unstable-msc2677")] +use crate::events::relation::Annotation; +use crate::{ + events::relation::{InReplyTo, Reference, Thread}, + OwnedDeviceId, OwnedEventId, +}; mod relation_serde; @@ -149,77 +154,6 @@ impl Replacement { } } -/// A reference to another event. -#[derive(Clone, Debug, Deserialize, Serialize)] -#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] -pub struct Reference { - /// The event we are referencing. - pub event_id: OwnedEventId, -} - -impl Reference { - /// Creates a new `Reference` with the given event ID. - pub fn new(event_id: OwnedEventId) -> Self { - Self { event_id } - } -} - -/// An annotation for an event. -#[derive(Clone, Debug, Deserialize, Serialize)] -#[cfg(feature = "unstable-msc2677")] -#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] -pub struct Annotation { - /// The event that is being annotated. - pub event_id: OwnedEventId, - - /// The annotation. - pub key: String, -} - -#[cfg(feature = "unstable-msc2677")] -impl Annotation { - /// Creates a new `Annotation` with the given event ID and key. - pub fn new(event_id: OwnedEventId, key: String) -> Self { - Self { event_id, key } - } -} - -/// A thread relation for an event. -#[derive(Clone, Debug, Deserialize, Serialize)] -#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] -pub struct Thread { - /// The ID of the root message in the thread. - pub event_id: OwnedEventId, - - /// A reply relation. - /// - /// If this event is a reply and belongs to a thread, this points to the message that is being - /// replied to, and `is_falling_back` must be set to `false`. - /// - /// If this event is not a reply, this is used as a fallback mechanism for clients that do not - /// support threads. This should point to the latest message-like event in the thread and - /// `is_falling_back` must be set to `true`. - pub in_reply_to: InReplyTo, - - /// Whether the `m.in_reply_to` field is a fallback for older clients or a real reply in a - /// thread. - pub is_falling_back: bool, -} - -impl Thread { - /// Convenience method to create a regular `Thread` with the given event ID and latest - /// message-like event ID. - pub fn plain(event_id: OwnedEventId, latest_event_id: OwnedEventId) -> Self { - Self { event_id, in_reply_to: InReplyTo::new(latest_event_id), is_falling_back: false } - } - - /// Convenience method to create a reply `Thread` with the given event ID and replied-to event - /// ID. - pub fn reply(event_id: OwnedEventId, reply_to_event_id: OwnedEventId) -> Self { - Self { event_id, in_reply_to: InReplyTo::new(reply_to_event_id), is_falling_back: true } - } -} - /// The content of an `m.room.encrypted` event using the `m.olm.v1.curve25519-aes-sha2` algorithm. #[derive(Clone, Debug, Serialize, Deserialize)] #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] diff --git a/crates/ruma-common/tests/events/encrypted.rs b/crates/ruma-common/tests/events/encrypted.rs index e1f5de4c..578e7a3a 100644 --- a/crates/ruma-common/tests/events/encrypted.rs +++ b/crates/ruma-common/tests/events/encrypted.rs @@ -2,10 +2,10 @@ use assert_matches::assert_matches; use ruma_common::{ device_id, event_id, events::{ - relation::InReplyTo, + relation::{InReplyTo, Reference, Thread}, room::encrypted::{ - EncryptedEventScheme, MegolmV1AesSha2ContentInit, Reference, Relation, Replacement, - RoomEncryptedEventContent, Thread, + EncryptedEventScheme, MegolmV1AesSha2ContentInit, Relation, Replacement, + RoomEncryptedEventContent, }, }, }; @@ -328,6 +328,7 @@ fn content_thread_serialization() { "m.relates_to": { "rel_type": "m.thread", "event_id": "$thread_root", + "is_falling_back": true, "m.in_reply_to": { "event_id": "$prev_event", }, @@ -387,7 +388,7 @@ fn content_thread_deserialization() { #[test] #[cfg(feature = "unstable-msc2677")] fn content_annotation_serialization() { - use ruma_common::events::room::encrypted::Annotation; + use ruma_common::events::relation::Annotation; let content = RoomEncryptedEventContent::new( encrypted_scheme(),