diff --git a/crates/ruma-common/src/events.rs b/crates/ruma-common/src/events.rs index 1b97dfe1..6a640f7d 100644 --- a/crates/ruma-common/src/events.rs +++ b/crates/ruma-common/src/events.rs @@ -171,7 +171,7 @@ pub use self::{ content::*, enums::*, kinds::*, - relation::BundledRelations, + relation::{BundledMessageLikeRelations, BundledStateRelations}, state_key::EmptyStateKey, unsigned::{MessageLikeUnsigned, RedactedUnsigned, StateUnsigned, UnsignedRoomRedactionEvent}, }; diff --git a/crates/ruma-common/src/events/enums.rs b/crates/ruma-common/src/events/enums.rs index f3a88c38..d03a8ea3 100644 --- a/crates/ruma-common/src/events/enums.rs +++ b/crates/ruma-common/src/events/enums.rs @@ -2,7 +2,7 @@ use ruma_macros::{event_enum, EventEnumFromEvent}; use serde::{de, Deserialize}; use serde_json::value::RawValue as RawJsonValue; -use super::{room::encrypted, BundledRelations}; +use super::room::encrypted; use crate::{ serde::from_raw_json_value, EventId, MilliSecondsSinceUnixEpoch, OwnedRoomId, RoomId, TransactionId, UserId, @@ -184,9 +184,6 @@ impl AnyTimelineEvent { /// Returns this event's `transaction_id` from inside `unsigned`, if there is one. pub fn transaction_id(&self) -> Option<&TransactionId>; - - /// Returns this event's `relations` from inside `unsigned`. - pub fn relations(&self) -> &BundledRelations; } /// Returns this event's `type`. @@ -224,9 +221,6 @@ impl AnySyncTimelineEvent { /// Returns this event's `transaction_id` from inside `unsigned`, if there is one. pub fn transaction_id(&self) -> Option<&TransactionId>; - - /// Returns this event's `relations` from inside `unsigned`, if that field exists. - pub fn relations(&self) -> &BundledRelations; } /// Returns this event's `type`. diff --git a/crates/ruma-common/src/events/relation.rs b/crates/ruma-common/src/events/relation.rs index 7abb53c3..7692009b 100644 --- a/crates/ruma-common/src/events/relation.rs +++ b/crates/ruma-common/src/events/relation.rs @@ -218,12 +218,12 @@ impl ReferenceChunk { } } -/// [Bundled aggregations] of related child events. +/// [Bundled aggregations] of related child events of a message-like event. /// /// [Bundled aggregations]: https://spec.matrix.org/latest/client-server-api/#aggregations #[derive(Clone, Debug, Default, Deserialize, Serialize)] #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] -pub struct BundledRelations { +pub struct BundledMessageLikeRelations { /// Replacement relation. #[serde(rename = "m.replace", skip_serializing_if = "Option::is_none")] pub replace: Option>, @@ -237,8 +237,8 @@ pub struct BundledRelations { pub reference: Option>, } -impl BundledRelations { - /// Creates a new empty `BundledRelations`. +impl BundledMessageLikeRelations { + /// Creates a new empty `BundledMessageLikeRelations`. pub const fn new() -> Self { Self { replace: None, thread: None, reference: None } } @@ -249,6 +249,33 @@ impl BundledRelations { } } +/// [Bundled aggregations] of related child events of a state event. +/// +/// [Bundled aggregations]: https://spec.matrix.org/latest/client-server-api/#aggregations +#[derive(Clone, Debug, Default, Deserialize, Serialize)] +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] +pub struct BundledStateRelations { + /// Thread relation. + #[serde(rename = "m.thread", skip_serializing_if = "Option::is_none")] + pub thread: Option>, + + /// Reference relations. + #[serde(rename = "m.reference", skip_serializing_if = "Option::is_none")] + pub reference: Option>, +} + +impl BundledStateRelations { + /// Creates a new empty `BundledStateRelations`. + pub const fn new() -> Self { + Self { thread: None, reference: None } + } + + /// Returns `true` if all fields are empty. + pub fn is_empty(&self) -> bool { + self.thread.is_none() && self.reference.is_none() + } +} + /// Relation types as defined in `rel_type` of an `m.relates_to` field. #[doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/src/doc/string_enum.md"))] #[derive(Clone, PartialEq, Eq, StringEnum)] diff --git a/crates/ruma-common/src/events/room/member.rs b/crates/ruma-common/src/events/room/member.rs index f7d45ecd..745a956a 100644 --- a/crates/ruma-common/src/events/room/member.rs +++ b/crates/ruma-common/src/events/room/member.rs @@ -10,8 +10,9 @@ use serde::{Deserialize, Serialize}; use crate::{ events::{ - AnyStrippedStateEvent, BundledRelations, EventContent, PossiblyRedactedStateEventContent, - RedactContent, RedactedStateEventContent, StateEventType, + AnyStrippedStateEvent, BundledStateRelations, EventContent, + PossiblyRedactedStateEventContent, RedactContent, RedactedStateEventContent, + StateEventType, }, serde::{CanBeEmpty, Raw, StringEnum}, OwnedMxcUri, OwnedServerName, OwnedServerSigningKeyId, OwnedTransactionId, OwnedUserId, @@ -507,7 +508,7 @@ pub struct RoomMemberUnsigned { /// /// [Bundled aggregations]: https://spec.matrix.org/latest/client-server-api/#aggregations #[serde(rename = "m.relations", default)] - pub relations: BundledRelations, + pub relations: BundledStateRelations, } impl RoomMemberUnsigned { diff --git a/crates/ruma-common/src/events/unsigned.rs b/crates/ruma-common/src/events/unsigned.rs index f8473b65..242842f6 100644 --- a/crates/ruma-common/src/events/unsigned.rs +++ b/crates/ruma-common/src/events/unsigned.rs @@ -2,7 +2,8 @@ use js_int::Int; use serde::Deserialize; use super::{ - relation::BundledRelations, room::redaction::RoomRedactionEventContent, + relation::{BundledMessageLikeRelations, BundledStateRelations}, + room::redaction::RoomRedactionEventContent, PossiblyRedactedStateEventContent, }; use crate::{ @@ -28,7 +29,7 @@ pub struct MessageLikeUnsigned { /// /// [Bundled aggregations]: https://spec.matrix.org/latest/client-server-api/#aggregations #[serde(rename = "m.relations", default)] - pub relations: BundledRelations, + pub relations: BundledMessageLikeRelations, } impl MessageLikeUnsigned { @@ -71,7 +72,7 @@ pub struct StateUnsigned { /// /// [Bundled aggregations]: https://spec.matrix.org/latest/client-server-api/#aggregations #[serde(rename = "m.relations", default)] - pub relations: BundledRelations, + pub relations: BundledStateRelations, } impl StateUnsigned { diff --git a/crates/ruma-macros/src/events/event_enum.rs b/crates/ruma-macros/src/events/event_enum.rs index e4612e82..2b520240 100644 --- a/crates/ruma-macros/src/events/event_enum.rs +++ b/crates/ruma-macros/src/events/event_enum.rs @@ -606,7 +606,6 @@ fn expand_accessor_methods( let maybe_redacted_accessors = maybe_redacted.then(|| { let variants = variants.iter().map(|v| v.match_arm(quote! { Self })); - let variants2 = variants.clone(); quote! { /// Returns this event's `transaction_id` from inside `unsigned`, if there is one. @@ -622,28 +621,6 @@ fn expand_accessor_methods( } } } - - /// Returns this event's `relations` from inside `unsigned`. - pub fn relations(&self) -> &#ruma_common::events::BundledRelations { - static DEFAULT_BUNDLED_RELATIONS: #ruma_common::events::BundledRelations = - #ruma_common::events::BundledRelations::new(); - match self { - #( - #variants2(event) => { - event.as_original().map_or_else( - || &DEFAULT_BUNDLED_RELATIONS, - |ev| &ev.unsigned.relations, - ) - } - )* - Self::_Custom(event) => { - event.as_original().map_or_else( - || &DEFAULT_BUNDLED_RELATIONS, - |ev| &ev.unsigned.relations, - ) - } - } - } } });