diff --git a/crates/ruma-common/CHANGELOG.md b/crates/ruma-common/CHANGELOG.md index 6587bc02..e9fc781a 100644 --- a/crates/ruma-common/CHANGELOG.md +++ b/crates/ruma-common/CHANGELOG.md @@ -38,6 +38,9 @@ Breaking changes: * Remove `events::reaction::Relation` and use `events::relation::Annotation` instead * Remove `events::key::verification::Relation` and use `events::relation::Reference` instead * Rename `events::relation::Relations` to `BundledRelations` +* 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 + now removed) Improvements: diff --git a/crates/ruma-common/src/events/unsigned.rs b/crates/ruma-common/src/events/unsigned.rs index 4b730ced..dd927bc9 100644 --- a/crates/ruma-common/src/events/unsigned.rs +++ b/crates/ruma-common/src/events/unsigned.rs @@ -143,33 +143,16 @@ impl Default for StateUnsigned { } /// Extra information about a redacted event that is not incorporated into the event's hash. -#[derive(Clone, Debug, Default, Deserialize)] +#[derive(Clone, Debug, Deserialize)] #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] pub struct RedactedUnsigned { /// The event that redacted this event, if any. - #[serde(skip_serializing_if = "Option::is_none")] - pub redacted_because: Option>, + pub redacted_because: Box, } impl RedactedUnsigned { - /// Create a new `RedactedUnsigned` with field set to `None`. - pub fn new() -> Self { - Self::default() - } - - /// Create a new `RedactedUnsigned` with the given redacted because. - pub fn new_because(redacted_because: Box) -> Self { - Self { redacted_because: Some(redacted_because) } - } -} - -impl CanBeEmpty for RedactedUnsigned { - /// Whether this unsigned data is empty (`redacted_because` is `None`). - /// - /// This method is used to determine whether to skip serializing the `unsigned` field in - /// redacted room events. Do not use it to determine whether an incoming `unsigned` field - /// was present - it could still have been present but contained none of the known fields. - fn is_empty(&self) -> bool { - self.redacted_because.is_none() + /// Create a new `RedactedUnsigned` with the given redaction event. + pub fn new(redacted_because: Box) -> Self { + Self { redacted_because } } } diff --git a/crates/ruma-common/tests/events/redacted.rs b/crates/ruma-common/tests/events/redacted.rs index 6eb109cc..20fa4533 100644 --- a/crates/ruma-common/tests/events/redacted.rs +++ b/crates/ruma-common/tests/events/redacted.rs @@ -139,7 +139,6 @@ fn deserialize_redacted_state_event() { ))) => redacted ); assert_eq!(redacted.event_id, "$h29iv0s8:example.com"); - assert!(redacted.unsigned.redacted_because.is_some()); assert_eq!(redacted.content.creator, "@carl:example.com"); } diff --git a/crates/ruma-macros/src/events/event.rs b/crates/ruma-macros/src/events/event.rs index 6fb5c060..83eadea5 100644 --- a/crates/ruma-macros/src/events/event.rs +++ b/crates/ruma-macros/src/events/event.rs @@ -136,7 +136,7 @@ fn expand_deserialize_event( )?; } } - } else if name == "unsigned" { + } else if name == "unsigned" && !var.is_redacted() { if has_prev_content(kind, var) { quote! { let unsigned = unsigned.map(|json| {