From aeaa4af77622ad2f405c36b4d7d00422655c708b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Commaille?= Date: Fri, 25 Nov 2022 19:25:07 +0100 Subject: [PATCH] events: Move relations under events::room::message to events::relation --- crates/ruma-common/CHANGELOG.md | 2 + crates/ruma-common/src/events/relation.rs | 75 +++++++++++++++++++ .../ruma-common/src/events/room/encrypted.rs | 10 +-- crates/ruma-common/src/events/room/message.rs | 72 +----------------- crates/ruma-common/tests/events/audio.rs | 5 +- crates/ruma-common/tests/events/encrypted.rs | 6 +- crates/ruma-common/tests/events/file.rs | 5 +- crates/ruma-common/tests/events/image.rs | 5 +- crates/ruma-common/tests/events/location.rs | 3 +- crates/ruma-common/tests/events/message.rs | 5 +- crates/ruma-common/tests/events/relations.rs | 11 +-- crates/ruma-common/tests/events/video.rs | 5 +- crates/ruma-common/tests/events/voice.rs | 5 +- .../tests/events/without_relation.rs | 5 +- 14 files changed, 108 insertions(+), 106 deletions(-) diff --git a/crates/ruma-common/CHANGELOG.md b/crates/ruma-common/CHANGELOG.md index 85085c46..bc382893 100644 --- a/crates/ruma-common/CHANGELOG.md +++ b/crates/ruma-common/CHANGELOG.md @@ -31,6 +31,8 @@ Breaking changes: * Change the `ignored_users` field of `IgnoredUserListEventContent` to a map of empty structs, to allow eventual fields to be added, as intended by the spec * 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` Improvements: diff --git a/crates/ruma-common/src/events/relation.rs b/crates/ruma-common/src/events/relation.rs index 17496e83..ff368956 100644 --- a/crates/ruma-common/src/events/relation.rs +++ b/crates/ruma-common/src/events/relation.rs @@ -13,6 +13,23 @@ use crate::{ MilliSecondsSinceUnixEpoch, OwnedEventId, OwnedUserId, PrivOwnedStr, }; +/// Information about the event a [rich reply] is replying to. +/// +/// [rich reply]: https://spec.matrix.org/v1.5/client-server-api/#rich-replies +#[derive(Clone, Debug, Deserialize, Serialize)] +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] +pub struct InReplyTo { + /// The event being replied to. + pub event_id: OwnedEventId, +} + +impl InReplyTo { + /// Creates a new `InReplyTo` with the given event ID. + pub fn new(event_id: OwnedEventId) -> Self { + Self { event_id } + } +} + /// Summary of all annotations to an event with the given key and type. #[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize)] #[cfg(feature = "unstable-msc2677")] @@ -106,6 +123,64 @@ impl BundledReplacement { } } +/// The content of a [replacement] relation. +/// +/// [replacement]: https://spec.matrix.org/v1.5/client-server-api/#event-replacements +#[derive(Clone, Debug)] +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] +pub struct Replacement { + /// The ID of the event being replaced. + pub event_id: OwnedEventId, + + /// New content. + pub new_content: C, +} + +impl Replacement { + /// Creates a new `Replacement` with the given event ID and new content. + pub fn new(event_id: OwnedEventId, new_content: C) -> Self { + Self { event_id, new_content } + } +} + +/// The content of a [thread] relation. +/// +/// [thread]: https://spec.matrix.org/v1.5/client-server-api/#threading +#[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 genuine 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: true } + } + + /// 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: false } + } +} + /// A bundled thread. #[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 3b054988..3f3a5d4f 100644 --- a/crates/ruma-common/src/events/room/encrypted.rs +++ b/crates/ruma-common/src/events/room/encrypted.rs @@ -8,8 +8,8 @@ use js_int::UInt; use ruma_macros::EventContent; use serde::{Deserialize, Serialize}; -use super::message::{self, InReplyTo}; -use crate::{OwnedDeviceId, OwnedEventId}; +use super::message; +use crate::{events::relation::InReplyTo, OwnedDeviceId, OwnedEventId}; mod relation_serde; @@ -130,9 +130,9 @@ impl From> for Relation { /// 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. +/// In contrast to [`relation::Replacement`](crate::events::relation::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)] diff --git a/crates/ruma-common/src/events/room/message.rs b/crates/ruma-common/src/events/room/message.rs index 34d8265d..e690d9d9 100644 --- a/crates/ruma-common/src/events/room/message.rs +++ b/crates/ruma-common/src/events/room/message.rs @@ -9,6 +9,7 @@ use serde::{de::DeserializeOwned, Deserialize, Serialize}; use serde_json::Value as JsonValue; use crate::{ + events::relation::{InReplyTo, Replacement, Thread}, serde::{JsonObject, StringEnum}, OwnedEventId, PrivOwnedStr, }; @@ -591,77 +592,6 @@ pub enum Relation { _Custom, } -/// Information about the event a "rich reply" is replying to. -#[derive(Clone, Debug, Deserialize, Serialize)] -#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] -pub struct InReplyTo { - /// The event being replied to. - pub event_id: OwnedEventId, -} - -impl InReplyTo { - /// Creates a new `InReplyTo` with the given event ID. - pub fn new(event_id: OwnedEventId) -> Self { - Self { event_id } - } -} - -/// 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_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] -pub struct Replacement { - /// The ID of the event being replaced. - pub event_id: OwnedEventId, - - /// New content. - pub new_content: C, -} - -impl Replacement { - /// Creates a new `Replacement` with the given event ID and new content. - pub fn new(event_id: OwnedEventId, new_content: C) -> Self { - Self { event_id, new_content } - } -} - -/// The content of a thread relation. -#[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 genuine 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: true } - } - - /// 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: false } - } -} - /// The format for the formatted representation of a message body. #[doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/src/doc/string_enum.md"))] #[derive(Clone, Debug, PartialEq, Eq, StringEnum)] diff --git a/crates/ruma-common/tests/events/audio.rs b/crates/ruma-common/tests/events/audio.rs index 8e01da7a..eec48c3d 100644 --- a/crates/ruma-common/tests/events/audio.rs +++ b/crates/ruma-common/tests/events/audio.rs @@ -11,10 +11,9 @@ use ruma_common::{ audio::{Amplitude, AudioContent, AudioEventContent, Waveform, WaveformError}, file::{EncryptedContentInit, FileContent, FileContentInfo}, message::MessageContent, + relation::InReplyTo, room::{ - message::{ - AudioMessageEventContent, InReplyTo, MessageType, Relation, RoomMessageEventContent, - }, + message::{AudioMessageEventContent, MessageType, Relation, RoomMessageEventContent}, JsonWebKeyInit, MediaSource, }, AnyMessageLikeEvent, MessageLikeEvent, MessageLikeUnsigned, OriginalMessageLikeEvent, diff --git a/crates/ruma-common/tests/events/encrypted.rs b/crates/ruma-common/tests/events/encrypted.rs index 1f512c45..e1f5de4c 100644 --- a/crates/ruma-common/tests/events/encrypted.rs +++ b/crates/ruma-common/tests/events/encrypted.rs @@ -1,12 +1,12 @@ use assert_matches::assert_matches; use ruma_common::{ device_id, event_id, - events::room::{ - encrypted::{ + events::{ + relation::InReplyTo, + room::encrypted::{ EncryptedEventScheme, MegolmV1AesSha2ContentInit, Reference, Relation, Replacement, RoomEncryptedEventContent, Thread, }, - message::InReplyTo, }, }; use serde_json::{from_value as from_json_value, json, to_value as to_json_value}; diff --git a/crates/ruma-common/tests/events/file.rs b/crates/ruma-common/tests/events/file.rs index 01652e1c..62831c31 100644 --- a/crates/ruma-common/tests/events/file.rs +++ b/crates/ruma-common/tests/events/file.rs @@ -8,10 +8,9 @@ use ruma_common::{ events::{ file::{EncryptedContentInit, FileContentInfo, FileEventContent}, message::MessageContent, + relation::InReplyTo, room::{ - message::{ - FileMessageEventContent, InReplyTo, MessageType, Relation, RoomMessageEventContent, - }, + message::{FileMessageEventContent, MessageType, Relation, RoomMessageEventContent}, EncryptedFileInit, JsonWebKeyInit, MediaSource, }, AnyMessageLikeEvent, MessageLikeEvent, MessageLikeUnsigned, OriginalMessageLikeEvent, diff --git a/crates/ruma-common/tests/events/image.rs b/crates/ruma-common/tests/events/image.rs index 8dc5d40f..b7658fb9 100644 --- a/crates/ruma-common/tests/events/image.rs +++ b/crates/ruma-common/tests/events/image.rs @@ -12,10 +12,9 @@ use ruma_common::{ ThumbnailFileContentInfo, }, message::MessageContent, + relation::InReplyTo, room::{ - message::{ - ImageMessageEventContent, InReplyTo, MessageType, Relation, RoomMessageEventContent, - }, + message::{ImageMessageEventContent, MessageType, Relation, RoomMessageEventContent}, JsonWebKeyInit, MediaSource, }, AnyMessageLikeEvent, MessageLikeEvent, MessageLikeUnsigned, OriginalMessageLikeEvent, diff --git a/crates/ruma-common/tests/events/location.rs b/crates/ruma-common/tests/events/location.rs index 9677a063..673c27a7 100644 --- a/crates/ruma-common/tests/events/location.rs +++ b/crates/ruma-common/tests/events/location.rs @@ -8,8 +8,9 @@ use ruma_common::{ events::{ location::{AssetType, LocationContent, LocationEventContent, ZoomLevel, ZoomLevelError}, message::MessageContent, + relation::InReplyTo, room::message::{ - InReplyTo, LocationMessageEventContent, MessageType, Relation, RoomMessageEventContent, + LocationMessageEventContent, MessageType, Relation, RoomMessageEventContent, }, AnyMessageLikeEvent, MessageLikeEvent, MessageLikeUnsigned, OriginalMessageLikeEvent, }, diff --git a/crates/ruma-common/tests/events/message.rs b/crates/ruma-common/tests/events/message.rs index fe67dea0..f02643f1 100644 --- a/crates/ruma-common/tests/events/message.rs +++ b/crates/ruma-common/tests/events/message.rs @@ -9,9 +9,8 @@ use ruma_common::{ emote::EmoteEventContent, message::{MessageContent, MessageEventContent, Text}, notice::NoticeEventContent, - room::message::{ - EmoteMessageEventContent, InReplyTo, MessageType, Relation, RoomMessageEventContent, - }, + relation::InReplyTo, + room::message::{EmoteMessageEventContent, MessageType, Relation, RoomMessageEventContent}, AnyMessageLikeEvent, MessageLikeEvent, MessageLikeUnsigned, OriginalMessageLikeEvent, }, room_id, diff --git a/crates/ruma-common/tests/events/relations.rs b/crates/ruma-common/tests/events/relations.rs index ec7dced6..54623905 100644 --- a/crates/ruma-common/tests/events/relations.rs +++ b/crates/ruma-common/tests/events/relations.rs @@ -2,7 +2,10 @@ use assert_matches::assert_matches; use assign::assign; use ruma_common::{ event_id, - events::room::message::{InReplyTo, MessageType, Relation, RoomMessageEventContent}, + events::{ + relation::{InReplyTo, Replacement, Thread}, + room::message::{MessageType, Relation, RoomMessageEventContent}, + }, }; use serde_json::{from_value as from_json_value, json, to_value as to_json_value}; @@ -67,8 +70,6 @@ fn reply_serialize() { #[test] fn replacement_serialize() { - use ruma_common::events::room::message::Replacement; - let content = assign!( RoomMessageEventContent::text_plain(""), { @@ -148,8 +149,6 @@ fn replacement_deserialize() { #[test] fn thread_plain_serialize() { - use ruma_common::events::room::message::Thread; - let content = assign!( RoomMessageEventContent::text_plain(""), { @@ -200,8 +199,6 @@ fn thread_plain_serialize() { #[test] fn thread_reply_serialize() { - use ruma_common::events::room::message::Thread; - let content = assign!( RoomMessageEventContent::text_plain(""), { diff --git a/crates/ruma-common/tests/events/video.rs b/crates/ruma-common/tests/events/video.rs index 68cc1ec1..bfa4f591 100644 --- a/crates/ruma-common/tests/events/video.rs +++ b/crates/ruma-common/tests/events/video.rs @@ -11,10 +11,9 @@ use ruma_common::{ file::{EncryptedContentInit, FileContent, FileContentInfo}, image::{ThumbnailContent, ThumbnailFileContent, ThumbnailFileContentInfo}, message::MessageContent, + relation::InReplyTo, room::{ - message::{ - InReplyTo, MessageType, Relation, RoomMessageEventContent, VideoMessageEventContent, - }, + message::{MessageType, Relation, RoomMessageEventContent, VideoMessageEventContent}, JsonWebKeyInit, MediaSource, }, video::{VideoContent, VideoEventContent}, diff --git a/crates/ruma-common/tests/events/voice.rs b/crates/ruma-common/tests/events/voice.rs index 3cfad370..8952e082 100644 --- a/crates/ruma-common/tests/events/voice.rs +++ b/crates/ruma-common/tests/events/voice.rs @@ -10,10 +10,9 @@ use ruma_common::{ events::{ audio::AudioContent, file::{FileContent, FileContentInfo}, + relation::InReplyTo, room::{ - message::{ - AudioMessageEventContent, InReplyTo, MessageType, Relation, RoomMessageEventContent, - }, + message::{AudioMessageEventContent, MessageType, Relation, RoomMessageEventContent}, MediaSource, }, voice::{VoiceContent, VoiceEventContent}, diff --git a/crates/ruma-common/tests/events/without_relation.rs b/crates/ruma-common/tests/events/without_relation.rs index 1d828edd..38345d15 100644 --- a/crates/ruma-common/tests/events/without_relation.rs +++ b/crates/ruma-common/tests/events/without_relation.rs @@ -1,7 +1,10 @@ use assert_matches::assert_matches; use ruma_common::{ event_id, - events::room::message::{InReplyTo, MessageType, Relation, RoomMessageEventContent}, + events::{ + relation::InReplyTo, + room::message::{MessageType, Relation, RoomMessageEventContent}, + }, }; use serde_json::{from_value as from_json_value, json, to_value as to_json_value};