events: Move Unsigned type to new OriginalStateEventContent trait

Only original events require it.
This commit is contained in:
Kévin Commaille 2022-12-23 17:20:03 +01:00 committed by Kévin Commaille
parent a0cc916742
commit 9d8c9c477f
8 changed files with 50 additions and 26 deletions

View File

@ -41,6 +41,8 @@ Breaking changes:
* Make the `redacted_because` field in `UnsignedRedacted` non-optional and replace parameterless * Make the `redacted_because` field in `UnsignedRedacted` non-optional and replace parameterless
`new` constructor by one that takes a redaction event (like `new_because` previously, which is `new` constructor by one that takes a redaction event (like `new_because` previously, which is
now removed) now removed)
* Move the `Unsigned` associated type from `StateEventContent` to `OriginalStateEventContent`
* `Redacted*EventContent`s don't have an `unsigned` type anymore
Improvements: Improvements:

View File

@ -3,10 +3,11 @@ use serde_json::value::RawValue as RawJsonValue;
use super::{ use super::{
EphemeralRoomEventContent, EphemeralRoomEventType, EventContent, GlobalAccountDataEventContent, EphemeralRoomEventContent, EphemeralRoomEventType, EventContent, GlobalAccountDataEventContent,
GlobalAccountDataEventType, MessageLikeEventContent, MessageLikeEventType, RedactContent, GlobalAccountDataEventType, MessageLikeEventContent, MessageLikeEventType,
RedactedEventContent, RedactedMessageLikeEventContent, RedactedStateEventContent, OriginalStateEventContent, RedactContent, RedactedEventContent,
RoomAccountDataEventContent, RoomAccountDataEventType, StateEventContent, StateEventType, RedactedMessageLikeEventContent, RedactedStateEventContent, RoomAccountDataEventContent,
StateUnsigned, ToDeviceEventContent, ToDeviceEventType, RoomAccountDataEventType, StateEventContent, StateEventType, StateUnsigned,
ToDeviceEventContent, ToDeviceEventType,
}; };
use crate::RoomVersionId; use crate::RoomVersionId;
@ -68,6 +69,8 @@ impl RedactedMessageLikeEventContent for CustomMessageLikeEventContent {}
custom_room_event_content!(CustomStateEventContent, StateEventType); custom_room_event_content!(CustomStateEventContent, StateEventType);
impl StateEventContent for CustomStateEventContent { impl StateEventContent for CustomStateEventContent {
type StateKey = String; type StateKey = String;
}
impl OriginalStateEventContent for CustomStateEventContent {
type Unsigned = StateUnsigned<Self>; type Unsigned = StateUnsigned<Self>;
} }
impl RedactedStateEventContent for CustomStateEventContent {} impl RedactedStateEventContent for CustomStateEventContent {}

View File

@ -6,7 +6,7 @@ use serde_json::value::RawValue as RawJsonValue;
use crate::serde::{CanBeEmpty, Raw}; use crate::serde::{CanBeEmpty, Raw};
use super::{ use super::{
EphemeralRoomEventType, GlobalAccountDataEventType, MessageLikeEventType, EphemeralRoomEventType, GlobalAccountDataEventType, MessageLikeEventType, RedactContent,
RoomAccountDataEventType, StateEventType, StateUnsignedFromParts, ToDeviceEventType, RoomAccountDataEventType, StateEventType, StateUnsignedFromParts, ToDeviceEventType,
}; };
@ -124,7 +124,10 @@ pub trait RedactedMessageLikeEventContent: MessageLikeEventContent + RedactedEve
pub trait StateEventContent: EventContent<EventType = StateEventType> { pub trait StateEventContent: EventContent<EventType = StateEventType> {
/// The type of the event's `state_key` field. /// The type of the event's `state_key` field.
type StateKey: AsRef<str> + Clone + fmt::Debug + DeserializeOwned + Serialize; type StateKey: AsRef<str> + Clone + fmt::Debug + DeserializeOwned + Serialize;
}
/// Content of a non-redacted state event.
pub trait OriginalStateEventContent: StateEventContent + RedactContent {
/// The type of the event's `unsigned` field. /// The type of the event's `unsigned` field.
type Unsigned: Clone + fmt::Debug + Default + CanBeEmpty + StateUnsignedFromParts; type Unsigned: Clone + fmt::Debug + Default + CanBeEmpty + StateUnsignedFromParts;
} }

View File

@ -6,8 +6,8 @@ use serde_json::value::RawValue as RawJsonValue;
use super::{ use super::{
EphemeralRoomEventContent, EventContent, GlobalAccountDataEventContent, EphemeralRoomEventContent, EventContent, GlobalAccountDataEventContent,
MessageLikeEventContent, MessageLikeEventType, MessageLikeUnsigned, RedactContent, MessageLikeEventContent, MessageLikeEventType, MessageLikeUnsigned, OriginalStateEventContent,
RedactedMessageLikeEventContent, RedactedStateEventContent, RedactedUnsigned, RedactContent, RedactedMessageLikeEventContent, RedactedStateEventContent, RedactedUnsigned,
RedactionDeHelper, RoomAccountDataEventContent, StateEventContent, StateEventType, RedactionDeHelper, RoomAccountDataEventContent, StateEventContent, StateEventType,
ToDeviceEventContent, ToDeviceEventContent,
}; };
@ -229,7 +229,7 @@ where
/// `OriginalStateEvent` implements the comparison traits using only the `event_id` field, a sorted /// `OriginalStateEvent` implements the comparison traits using only the `event_id` field, a sorted
/// list would be sorted lexicographically based on the event's `EventId`. /// list would be sorted lexicographically based on the event's `EventId`.
#[derive(Clone, Debug, Event)] #[derive(Clone, Debug, Event)]
pub struct OriginalStateEvent<C: StateEventContent> { pub struct OriginalStateEvent<C: OriginalStateEventContent> {
/// Data specific to the event type. /// Data specific to the event type.
pub content: C, pub content: C,
@ -260,7 +260,7 @@ pub struct OriginalStateEvent<C: StateEventContent> {
/// `OriginalSyncStateEvent` implements the comparison traits using only the `event_id` field, a /// `OriginalSyncStateEvent` implements the comparison traits using only the `event_id` field, a
/// sorted list would be sorted lexicographically based on the event's `EventId`. /// sorted list would be sorted lexicographically based on the event's `EventId`.
#[derive(Clone, Debug, Event)] #[derive(Clone, Debug, Event)]
pub struct OriginalSyncStateEvent<C: StateEventContent> { pub struct OriginalSyncStateEvent<C: OriginalStateEventContent> {
/// Data specific to the event type. /// Data specific to the event type.
pub content: C, pub content: C,
@ -379,7 +379,7 @@ pub struct RedactedSyncStateEvent<C: RedactedStateEventContent> {
/// would be sorted lexicographically based on the event's `EventId`. /// would be sorted lexicographically based on the event's `EventId`.
#[allow(clippy::exhaustive_enums)] #[allow(clippy::exhaustive_enums)]
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub enum StateEvent<C: StateEventContent + RedactContent> pub enum StateEvent<C: OriginalStateEventContent>
where where
C::Redacted: RedactedStateEventContent, C::Redacted: RedactedStateEventContent,
{ {
@ -396,7 +396,7 @@ where
/// would be sorted lexicographically based on the event's `EventId`. /// would be sorted lexicographically based on the event's `EventId`.
#[allow(clippy::exhaustive_enums)] #[allow(clippy::exhaustive_enums)]
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub enum SyncStateEvent<C: StateEventContent + RedactContent> pub enum SyncStateEvent<C: OriginalStateEventContent>
where where
C::Redacted: RedactedStateEventContent, C::Redacted: RedactedStateEventContent,
{ {
@ -617,7 +617,7 @@ impl_possibly_redacted_event!(
); );
impl_possibly_redacted_event!( impl_possibly_redacted_event!(
StateEvent(StateEventContent, RedactedStateEventContent, StateEventType) StateEvent(OriginalStateEventContent, RedactedStateEventContent, StateEventType)
where where
C::Redacted: StateEventContent<StateKey = C::StateKey>, C::Redacted: StateEventContent<StateKey = C::StateKey>,
{ {
@ -648,7 +648,7 @@ impl_possibly_redacted_event!(
); );
impl_possibly_redacted_event!( impl_possibly_redacted_event!(
SyncStateEvent(StateEventContent, RedactedStateEventContent, StateEventType) SyncStateEvent(OriginalStateEventContent, RedactedStateEventContent, StateEventType)
where where
C::Redacted: StateEventContent<StateKey = C::StateKey>, C::Redacted: StateEventContent<StateKey = C::StateKey>,
{ {
@ -701,4 +701,9 @@ impl_sync_from_full!(
MessageLikeEventContent, MessageLikeEventContent,
RedactedMessageLikeEventContent RedactedMessageLikeEventContent
); );
impl_sync_from_full!(SyncStateEvent, StateEvent, StateEventContent, RedactedStateEventContent); impl_sync_from_full!(
SyncStateEvent,
StateEvent,
OriginalStateEventContent,
RedactedStateEventContent
);

View File

@ -7,7 +7,7 @@ use serde_json::value::RawValue as RawJsonValue;
use crate::{ use crate::{
events::{ events::{
EventContent, RedactContent, RedactedEventContent, RedactedStateEventContent, EventContent, RedactContent, RedactedEventContent, RedactedStateEventContent,
StateEventContent, StateEventType, StateUnsigned, StateEventContent, StateEventType,
}, },
OwnedRoomAliasId, OwnedServerName, RoomVersionId, OwnedRoomAliasId, OwnedServerName, RoomVersionId,
}; };
@ -97,8 +97,6 @@ impl EventContent for RedactedRoomAliasesEventContent {
impl StateEventContent for RedactedRoomAliasesEventContent { impl StateEventContent for RedactedRoomAliasesEventContent {
type StateKey = OwnedServerName; type StateKey = OwnedServerName;
// FIXME: Not actually used
type Unsigned = StateUnsigned<Self>;
} }
impl RedactedStateEventContent for RedactedRoomAliasesEventContent {} impl RedactedStateEventContent for RedactedRoomAliasesEventContent {}

View File

@ -12,8 +12,8 @@ use serde_json::{from_str as from_json_str, value::RawValue as RawJsonValue};
use crate::{ use crate::{
events::{ events::{
AnyStrippedStateEvent, BundledRelations, EventContent, RedactContent, RedactedEventContent, AnyStrippedStateEvent, BundledRelations, EventContent, RedactContent, RedactedEventContent,
RedactedStateEventContent, StateEventContent, StateEventType, StateUnsigned, RedactedStateEventContent, StateEventContent, StateEventType, StateUnsignedFromParts,
StateUnsignedFromParts, StaticEventContent, StaticEventContent,
}, },
serde::{CanBeEmpty, Raw, StringEnum}, serde::{CanBeEmpty, Raw, StringEnum},
OwnedMxcUri, OwnedServerName, OwnedServerSigningKeyId, OwnedTransactionId, OwnedUserId, OwnedMxcUri, OwnedServerName, OwnedServerSigningKeyId, OwnedTransactionId, OwnedUserId,
@ -252,8 +252,6 @@ impl EventContent for RedactedRoomMemberEventContent {
impl StateEventContent for RedactedRoomMemberEventContent { impl StateEventContent for RedactedRoomMemberEventContent {
type StateKey = OwnedUserId; type StateKey = OwnedUserId;
// FIXME: Not actually used
type Unsigned = StateUnsigned<Self>;
} }
impl RedactedStateEventContent for RedactedRoomMemberEventContent {} impl RedactedStateEventContent for RedactedRoomMemberEventContent {}

View File

@ -316,6 +316,7 @@ pub fn expand_event_content(
unsigned_type, unsigned_type,
&aliases, &aliases,
ruma_common, ruma_common,
true,
) )
.unwrap_or_else(syn::Error::into_compile_error); .unwrap_or_else(syn::Error::into_compile_error);
let static_event_content_impl = event_kind let static_event_content_impl = event_kind
@ -407,6 +408,7 @@ fn generate_redacted_event_content<'a>(
unsigned_type, unsigned_type,
aliases, aliases,
ruma_common, ruma_common,
false,
) )
.unwrap_or_else(syn::Error::into_compile_error); .unwrap_or_else(syn::Error::into_compile_error);
@ -584,6 +586,7 @@ fn generate_event_content_impl<'a>(
unsigned_type: Option<TokenStream>, unsigned_type: Option<TokenStream>,
aliases: &[LitStr], aliases: &[LitStr],
ruma_common: &TokenStream, ruma_common: &TokenStream,
is_original: bool,
) -> syn::Result<TokenStream> { ) -> syn::Result<TokenStream> {
let serde = quote! { #ruma_common::exports::serde }; let serde = quote! { #ruma_common::exports::serde };
let serde_json = quote! { #ruma_common::exports::serde_json }; let serde_json = quote! { #ruma_common::exports::serde_json };
@ -667,13 +670,25 @@ fn generate_event_content_impl<'a>(
let state_event_content_impl = (event_kind == Some(EventKind::State)).then(|| { let state_event_content_impl = (event_kind == Some(EventKind::State)).then(|| {
assert!(state_key_type.is_some()); assert!(state_key_type.is_some());
quote! {
type StateKey = #state_key_type;
}
});
let original_state_event_content_impl =
(event_kind == Some(EventKind::State) && is_original).then(|| {
let trait_name = format_ident!("Original{kind}Content");
let unsigned_type = unsigned_type let unsigned_type = unsigned_type
.unwrap_or_else(|| quote! { #ruma_common::events::StateUnsigned<Self> }); .unwrap_or_else(|| quote! { #ruma_common::events::StateUnsigned<Self> });
quote! { quote! {
type StateKey = #state_key_type; #[automatically_derived]
impl #ruma_common::events::#trait_name for #ident {
type Unsigned = #unsigned_type; type Unsigned = #unsigned_type;
} }
}
}); });
quote! { quote! {
@ -681,6 +696,8 @@ fn generate_event_content_impl<'a>(
impl #ruma_common::events::#trait_name for #ident { impl #ruma_common::events::#trait_name for #ident {
#state_event_content_impl #state_event_content_impl
} }
#original_state_event_content_impl
} }
}); });

View File

@ -358,8 +358,6 @@ fn expand_content_enum(
let state_event_content_impl = (kind == EventKind::State).then(|| { let state_event_content_impl = (kind == EventKind::State).then(|| {
quote! { quote! {
type StateKey = String; type StateKey = String;
// FIXME: Not actually used
type Unsigned = #ruma_common::events::StateUnsigned<Self>;
} }
}); });