events: Split bundled relations into two types

One for message-like events, one for state events.
This commit is contained in:
Jonas Platte 2023-03-15 14:43:24 +01:00
parent 0599eb1226
commit 19d44489c3
No known key found for this signature in database
GPG Key ID: AAA7A61F696C3E0C
6 changed files with 41 additions and 41 deletions

View File

@ -171,7 +171,7 @@ pub use self::{
content::*, content::*,
enums::*, enums::*,
kinds::*, kinds::*,
relation::BundledRelations, relation::{BundledMessageLikeRelations, BundledStateRelations},
state_key::EmptyStateKey, state_key::EmptyStateKey,
unsigned::{MessageLikeUnsigned, RedactedUnsigned, StateUnsigned, UnsignedRoomRedactionEvent}, unsigned::{MessageLikeUnsigned, RedactedUnsigned, StateUnsigned, UnsignedRoomRedactionEvent},
}; };

View File

@ -2,7 +2,7 @@ use ruma_macros::{event_enum, EventEnumFromEvent};
use serde::{de, Deserialize}; use serde::{de, Deserialize};
use serde_json::value::RawValue as RawJsonValue; use serde_json::value::RawValue as RawJsonValue;
use super::{room::encrypted, BundledRelations}; use super::room::encrypted;
use crate::{ use crate::{
serde::from_raw_json_value, EventId, MilliSecondsSinceUnixEpoch, OwnedRoomId, RoomId, serde::from_raw_json_value, EventId, MilliSecondsSinceUnixEpoch, OwnedRoomId, RoomId,
TransactionId, UserId, TransactionId, UserId,
@ -184,9 +184,6 @@ impl AnyTimelineEvent {
/// Returns this event's `transaction_id` from inside `unsigned`, if there is one. /// Returns this event's `transaction_id` from inside `unsigned`, if there is one.
pub fn transaction_id(&self) -> Option<&TransactionId>; 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`. /// Returns this event's `type`.
@ -224,9 +221,6 @@ impl AnySyncTimelineEvent {
/// Returns this event's `transaction_id` from inside `unsigned`, if there is one. /// Returns this event's `transaction_id` from inside `unsigned`, if there is one.
pub fn transaction_id(&self) -> Option<&TransactionId>; 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`. /// Returns this event's `type`.

View File

@ -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 /// [Bundled aggregations]: https://spec.matrix.org/latest/client-server-api/#aggregations
#[derive(Clone, Debug, Default, Deserialize, Serialize)] #[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct BundledRelations { pub struct BundledMessageLikeRelations {
/// Replacement relation. /// Replacement relation.
#[serde(rename = "m.replace", skip_serializing_if = "Option::is_none")] #[serde(rename = "m.replace", skip_serializing_if = "Option::is_none")]
pub replace: Option<Box<BundledReplacement>>, pub replace: Option<Box<BundledReplacement>>,
@ -237,8 +237,8 @@ pub struct BundledRelations {
pub reference: Option<Box<ReferenceChunk>>, pub reference: Option<Box<ReferenceChunk>>,
} }
impl BundledRelations { impl BundledMessageLikeRelations {
/// Creates a new empty `BundledRelations`. /// Creates a new empty `BundledMessageLikeRelations`.
pub const fn new() -> Self { pub const fn new() -> Self {
Self { replace: None, thread: None, reference: None } 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<Box<BundledThread>>,
/// Reference relations.
#[serde(rename = "m.reference", skip_serializing_if = "Option::is_none")]
pub reference: Option<Box<ReferenceChunk>>,
}
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. /// 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"))] #[doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/src/doc/string_enum.md"))]
#[derive(Clone, PartialEq, Eq, StringEnum)] #[derive(Clone, PartialEq, Eq, StringEnum)]

View File

@ -10,8 +10,9 @@ use serde::{Deserialize, Serialize};
use crate::{ use crate::{
events::{ events::{
AnyStrippedStateEvent, BundledRelations, EventContent, PossiblyRedactedStateEventContent, AnyStrippedStateEvent, BundledStateRelations, EventContent,
RedactContent, RedactedStateEventContent, StateEventType, PossiblyRedactedStateEventContent, RedactContent, RedactedStateEventContent,
StateEventType,
}, },
serde::{CanBeEmpty, Raw, StringEnum}, serde::{CanBeEmpty, Raw, StringEnum},
OwnedMxcUri, OwnedServerName, OwnedServerSigningKeyId, OwnedTransactionId, OwnedUserId, OwnedMxcUri, OwnedServerName, OwnedServerSigningKeyId, OwnedTransactionId, OwnedUserId,
@ -507,7 +508,7 @@ pub struct RoomMemberUnsigned {
/// ///
/// [Bundled aggregations]: https://spec.matrix.org/latest/client-server-api/#aggregations /// [Bundled aggregations]: https://spec.matrix.org/latest/client-server-api/#aggregations
#[serde(rename = "m.relations", default)] #[serde(rename = "m.relations", default)]
pub relations: BundledRelations, pub relations: BundledStateRelations,
} }
impl RoomMemberUnsigned { impl RoomMemberUnsigned {

View File

@ -2,7 +2,8 @@ use js_int::Int;
use serde::Deserialize; use serde::Deserialize;
use super::{ use super::{
relation::BundledRelations, room::redaction::RoomRedactionEventContent, relation::{BundledMessageLikeRelations, BundledStateRelations},
room::redaction::RoomRedactionEventContent,
PossiblyRedactedStateEventContent, PossiblyRedactedStateEventContent,
}; };
use crate::{ use crate::{
@ -28,7 +29,7 @@ pub struct MessageLikeUnsigned {
/// ///
/// [Bundled aggregations]: https://spec.matrix.org/latest/client-server-api/#aggregations /// [Bundled aggregations]: https://spec.matrix.org/latest/client-server-api/#aggregations
#[serde(rename = "m.relations", default)] #[serde(rename = "m.relations", default)]
pub relations: BundledRelations, pub relations: BundledMessageLikeRelations,
} }
impl MessageLikeUnsigned { impl MessageLikeUnsigned {
@ -71,7 +72,7 @@ pub struct StateUnsigned<C: PossiblyRedactedStateEventContent> {
/// ///
/// [Bundled aggregations]: https://spec.matrix.org/latest/client-server-api/#aggregations /// [Bundled aggregations]: https://spec.matrix.org/latest/client-server-api/#aggregations
#[serde(rename = "m.relations", default)] #[serde(rename = "m.relations", default)]
pub relations: BundledRelations, pub relations: BundledStateRelations,
} }
impl<C: PossiblyRedactedStateEventContent> StateUnsigned<C> { impl<C: PossiblyRedactedStateEventContent> StateUnsigned<C> {

View File

@ -606,7 +606,6 @@ fn expand_accessor_methods(
let maybe_redacted_accessors = maybe_redacted.then(|| { let maybe_redacted_accessors = maybe_redacted.then(|| {
let variants = variants.iter().map(|v| v.match_arm(quote! { Self })); let variants = variants.iter().map(|v| v.match_arm(quote! { Self }));
let variants2 = variants.clone();
quote! { quote! {
/// Returns this event's `transaction_id` from inside `unsigned`, if there is one. /// 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,
)
}
}
}
} }
}); });