From c807f9f43d008c93c2940089c7fdaa4d088342b8 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Sat, 2 Oct 2021 13:44:02 +0200 Subject: [PATCH] events: Add redacted redaction event types --- crates/ruma-events-macros/src/event.rs | 6 +- crates/ruma-events-macros/src/event_enum.rs | 17 ------ crates/ruma-events-macros/src/event_parse.rs | 6 ++ crates/ruma-events/src/room/redaction.rs | 63 ++++++++++++++++++-- 4 files changed, 68 insertions(+), 24 deletions(-) diff --git a/crates/ruma-events-macros/src/event.rs b/crates/ruma-events-macros/src/event.rs index 79e398dc..5da80ddd 100644 --- a/crates/ruma-events-macros/src/event.rs +++ b/crates/ruma-events-macros/src/event.rs @@ -408,6 +408,10 @@ fn expand_redact_event( let ident = &input.ident; let mut generics = input.generics.clone(); + if generics.params.is_empty() { + return None; + } + assert_eq!(generics.params.len(), 1, "expected one generic parameter"); let ty_param = match &generics.params[0] { GenericParam::Type(ty) => ty.ident.clone(), @@ -477,7 +481,7 @@ fn expand_from_into( let fields: Vec<_> = fields.iter().flat_map(|f| &f.ident).collect(); if let EventKindVariation::Sync | EventKindVariation::RedactedSync = var { - let full_struct = kind.to_event_ident(&var.to_full().unwrap()); + let full_struct = kind.to_event_ident(&var.to_full().unwrap()).unwrap(); Some(quote! { #[automatically_derived] impl #impl_generics ::std::convert::From<#full_struct #ty_gen> diff --git a/crates/ruma-events-macros/src/event_enum.rs b/crates/ruma-events-macros/src/event_enum.rs index 7c58f707..b5fef96d 100644 --- a/crates/ruma-events-macros/src/event_enum.rs +++ b/crates/ruma-events-macros/src/event_enum.rs @@ -727,23 +727,6 @@ fn to_event_path( // There is no need to give a good compiler error as `to_camel_case` is called first. assert_eq!(&name[..2], "m."); - // Temporary hack - if *kind == EventKind::Message && name == "m.room.redaction" { - if *var == EventKindVariation::Redacted { - return quote! { - #ruma_events::RedactedMessageEvent< - #ruma_events::room::redaction::RedactedRedactionEventContent - > - }; - } else if *var == EventKindVariation::RedactedSync { - return quote! { - #ruma_events::RedactedSyncMessageEvent< - #ruma_events::room::redaction::RedactedRedactionEventContent - > - }; - } - } - let path: Vec<_> = name[2..].split('.').collect(); let event_str = path.last().unwrap(); diff --git a/crates/ruma-events-macros/src/event_parse.rs b/crates/ruma-events-macros/src/event_parse.rs index 00c62cdd..d381d27c 100644 --- a/crates/ruma-events-macros/src/event_parse.rs +++ b/crates/ruma-events-macros/src/event_parse.rs @@ -133,8 +133,10 @@ impl EventKind { | (Self::State, V::Stripped) | (Self::State, V::Initial) | (Self::Message, V::Redacted) + | (Self::Redaction, V::Redacted) | (Self::State, V::Redacted) | (Self::Message, V::RedactedSync) + | (Self::Redaction, V::RedactedSync) | (Self::State, V::RedactedSync) => Some(format_ident!("{}{}", var, self)), _ => None, } @@ -203,6 +205,10 @@ pub fn to_kind_variation(ident: &Ident) -> Option<(EventKind, EventKindVariation "PresenceEvent" => Some((EventKind::Presence, EventKindVariation::Full)), "RedactionEvent" => Some((EventKind::Redaction, EventKindVariation::Full)), "SyncRedactionEvent" => Some((EventKind::Redaction, EventKindVariation::Sync)), + "RedactedRedactionEvent" => Some((EventKind::Redaction, EventKindVariation::Redacted)), + "RedactedSyncRedactionEvent" => { + Some((EventKind::Redaction, EventKindVariation::RedactedSync)) + } "DecryptedOlmV1Event" | "DecryptedMegolmV1Event" => { Some((EventKind::Decrypted, EventKindVariation::Full)) } diff --git a/crates/ruma-events/src/room/redaction.rs b/crates/ruma-events/src/room/redaction.rs index 8a13a6d0..a47d098c 100644 --- a/crates/ruma-events/src/room/redaction.rs +++ b/crates/ruma-events/src/room/redaction.rs @@ -34,16 +34,17 @@ pub struct RedactionEvent { } impl Redact for RedactionEvent { - // Temporary hack - type Redacted = crate::RedactedMessageEvent; + type Redacted = RedactedRedactionEvent; fn redact( self, redaction: SyncRedactionEvent, version: &ruma_identifiers::RoomVersionId, ) -> Self::Redacted { - crate::RedactedMessageEvent { + RedactedRedactionEvent { content: self.content.redact(version), + // There is no released room version where this isn't redacted yet + redacts: None, event_id: self.event_id, sender: self.sender, origin_server_ts: self.origin_server_ts, @@ -53,6 +54,32 @@ impl Redact for RedactionEvent { } } +/// Redacted redaction event. +#[derive(Clone, Debug, Event)] +#[allow(clippy::exhaustive_structs)] +pub struct RedactedRedactionEvent { + /// Data specific to the event type. + pub content: RedactedRedactionEventContent, + + /// The ID of the event that was redacted. + pub redacts: Option, + + /// The globally unique event identifier for the user who sent the event. + pub event_id: EventId, + + /// The fully-qualified ID of the user who sent this event. + pub sender: UserId, + + /// Timestamp in milliseconds on originating homeserver when this event was sent. + pub origin_server_ts: MilliSecondsSinceUnixEpoch, + + /// The ID of the room associated with this event. + pub room_id: RoomId, + + /// Additional key-value pairs not signed by the homeserver. + pub unsigned: RedactedUnsigned, +} + /// Redaction event without a `room_id`. #[derive(Clone, Debug, Event)] #[allow(clippy::exhaustive_structs)] @@ -77,16 +104,17 @@ pub struct SyncRedactionEvent { } impl Redact for SyncRedactionEvent { - // Temporary hack - type Redacted = crate::RedactedSyncMessageEvent; + type Redacted = RedactedSyncRedactionEvent; fn redact( self, redaction: SyncRedactionEvent, version: &ruma_identifiers::RoomVersionId, ) -> Self::Redacted { - crate::RedactedSyncMessageEvent { + RedactedSyncRedactionEvent { content: self.content.redact(version), + // There is no released room version where this isn't redacted yet + redacts: None, event_id: self.event_id, sender: self.sender, origin_server_ts: self.origin_server_ts, @@ -95,6 +123,29 @@ impl Redact for SyncRedactionEvent { } } +/// Redacted redaction event without a `room_id`. +#[derive(Clone, Debug, Event)] +#[allow(clippy::exhaustive_structs)] +pub struct RedactedSyncRedactionEvent { + /// Data specific to the event type. + pub content: RedactedRedactionEventContent, + + /// The ID of the event that was redacted. + pub redacts: Option, + + /// The globally unique event identifier for the user who sent the event. + pub event_id: EventId, + + /// The fully-qualified ID of the user who sent this event. + pub sender: UserId, + + /// Timestamp in milliseconds on originating homeserver when this event was sent. + pub origin_server_ts: MilliSecondsSinceUnixEpoch, + + /// Additional key-value pairs not signed by the homeserver. + pub unsigned: RedactedUnsigned, +} + /// A redaction of an event. #[derive(Clone, Debug, Default, Deserialize, Serialize, EventContent)] #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]