events: Remove the Redact trait

It has not proven to be useful, being used by no downstream project.
This commit is contained in:
Jonas Platte 2022-12-14 12:37:15 +01:00
parent a5c5b0b137
commit 675e6f43cd
No known key found for this signature in database
GPG Key ID: AAA7A61F696C3E0C
7 changed files with 12 additions and 267 deletions

View File

@ -104,7 +104,6 @@
use serde::{de::IgnoredAny, Deserialize, Serializer}; use serde::{de::IgnoredAny, Deserialize, Serializer};
use self::room::redaction::SyncRoomRedactionEvent;
use crate::{EventEncryptionAlgorithm, RoomVersionId}; use crate::{EventEncryptionAlgorithm, RoomVersionId};
// Needs to be public for trybuild tests // Needs to be public for trybuild tests
@ -177,18 +176,6 @@ pub use self::{
unsigned::{MessageLikeUnsigned, RedactedUnsigned, StateUnsigned, StateUnsignedFromParts}, unsigned::{MessageLikeUnsigned, RedactedUnsigned, StateUnsigned, StateUnsignedFromParts},
}; };
/// Trait to define the behavior of redacting an event.
pub trait Redact {
/// The redacted form of the event.
type Redacted;
/// Transforms `self` into a redacted form (removing most fields) according to the spec.
///
/// A small number of events have room-version specific redaction behavior, so a version has to
/// be specified.
fn redact(self, redaction: SyncRoomRedactionEvent, version: &RoomVersionId) -> Self::Redacted;
}
/// Trait to define the behavior of redact an event's content object. /// Trait to define the behavior of redact an event's content object.
pub trait RedactContent { pub trait RedactContent {
/// The redacted form of the event's content. /// The redacted form of the event's content.
@ -198,8 +185,6 @@ pub trait RedactContent {
/// ///
/// A small number of events have room-version specific redaction behavior, so a version has to /// A small number of events have room-version specific redaction behavior, so a version has to
/// be specified. /// be specified.
///
/// Where applicable, it is preferred to use [`Redact::redact`] on the outer event.
fn redact(self, version: &RoomVersionId) -> Self::Redacted; fn redact(self, version: &RoomVersionId) -> Self::Redacted;
} }

View File

@ -2,13 +2,10 @@ 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::{ use super::{room::encrypted, BundledRelations};
room::{encrypted, redaction::SyncRoomRedactionEvent},
BundledRelations, Redact,
};
use crate::{ use crate::{
serde::from_raw_json_value, EventId, MilliSecondsSinceUnixEpoch, OwnedRoomId, RoomId, serde::from_raw_json_value, EventId, MilliSecondsSinceUnixEpoch, OwnedRoomId, RoomId,
RoomVersionId, TransactionId, UserId, TransactionId, UserId,
}; };
event_enum! { event_enum! {
@ -272,34 +269,6 @@ impl<'de> Deserialize<'de> for AnySyncTimelineEvent {
} }
} }
impl Redact for AnyTimelineEvent {
type Redacted = Self;
/// Redacts `self`, referencing the given event in `unsigned.redacted_because`.
///
/// Does nothing for events that are already redacted.
fn redact(self, redaction: SyncRoomRedactionEvent, version: &RoomVersionId) -> Self {
match self {
Self::MessageLike(ev) => Self::MessageLike(ev.redact(redaction, version)),
Self::State(ev) => Self::State(ev.redact(redaction, version)),
}
}
}
impl Redact for AnySyncTimelineEvent {
type Redacted = Self;
/// Redacts `self`, referencing the given event in `unsigned.redacted_because`.
///
/// Does nothing for events that are already redacted.
fn redact(self, redaction: SyncRoomRedactionEvent, version: &RoomVersionId) -> Self {
match self {
Self::MessageLike(ev) => Self::MessageLike(ev.redact(redaction, version)),
Self::State(ev) => Self::State(ev.redact(redaction, version)),
}
}
}
impl AnyMessageLikeEventContent { impl AnyMessageLikeEventContent {
/// Get a copy of the event's `m.relates_to` field, if any. /// Get a copy of the event's `m.relates_to` field, if any.
/// ///

View File

@ -5,15 +5,15 @@ use serde::{Deserialize, Deserializer, Serialize};
use serde_json::value::RawValue as RawJsonValue; use serde_json::value::RawValue as RawJsonValue;
use super::{ use super::{
room::redaction::SyncRoomRedactionEvent, EphemeralRoomEventContent, EventContent, EphemeralRoomEventContent, EventContent, GlobalAccountDataEventContent,
GlobalAccountDataEventContent, MessageLikeEventContent, MessageLikeEventType, MessageLikeEventContent, MessageLikeEventType, MessageLikeUnsigned, RedactContent,
MessageLikeUnsigned, Redact, RedactContent, RedactedMessageLikeEventContent, RedactedMessageLikeEventContent, RedactedStateEventContent, RedactedUnsigned,
RedactedStateEventContent, RedactedUnsigned, RedactionDeHelper, RoomAccountDataEventContent, RedactionDeHelper, RoomAccountDataEventContent, StateEventContent, StateEventType,
StateEventContent, StateEventType, ToDeviceEventContent, ToDeviceEventContent,
}; };
use crate::{ use crate::{
serde::from_raw_json_value, EventId, MilliSecondsSinceUnixEpoch, OwnedEventId, OwnedRoomId, serde::from_raw_json_value, EventId, MilliSecondsSinceUnixEpoch, OwnedEventId, OwnedRoomId,
OwnedUserId, RoomId, RoomVersionId, UserId, OwnedUserId, RoomId, UserId,
}; };
/// A global account data event. /// A global account data event.
@ -456,22 +456,6 @@ macro_rules! impl_possibly_redacted_event {
$($extra)* $($extra)*
} }
impl<C> Redact for $ty<C>
where
C: $content_trait + RedactContent,
C::Redacted: $redacted_content_trait,
$( C::Redacted: $trait<StateKey = C::StateKey>, )?
{
type Redacted = Self;
fn redact(self, redaction: SyncRoomRedactionEvent, version: &RoomVersionId) -> Self {
match self {
Self::Original(ev) => Self::Redacted(ev.redact(redaction, version)),
Self::Redacted(ev) => Self::Redacted(ev),
}
}
}
impl<'de, C> Deserialize<'de> for $ty<C> impl<'de, C> Deserialize<'de> for $ty<C>
where where
C: $content_trait + RedactContent, C: $content_trait + RedactContent,

View File

@ -8,8 +8,8 @@ use serde_json::value::RawValue as RawJsonValue;
use crate::{ use crate::{
events::{ events::{
EventContent, MessageLikeEventType, MessageLikeUnsigned, Redact, RedactContent, EventContent, MessageLikeEventType, MessageLikeUnsigned, RedactedUnsigned,
RedactedUnsigned, RedactionDeHelper, RedactionDeHelper,
}, },
serde::from_raw_json_value, serde::from_raw_json_value,
EventId, MilliSecondsSinceUnixEpoch, OwnedEventId, OwnedRoomId, OwnedUserId, RoomId, UserId, EventId, MilliSecondsSinceUnixEpoch, OwnedEventId, OwnedRoomId, OwnedUserId, RoomId, UserId,
@ -65,25 +65,6 @@ pub struct OriginalRoomRedactionEvent {
pub unsigned: MessageLikeUnsigned, pub unsigned: MessageLikeUnsigned,
} }
impl Redact for OriginalRoomRedactionEvent {
type Redacted = RedactedRoomRedactionEvent;
fn redact(
self,
redaction: SyncRoomRedactionEvent,
version: &crate::RoomVersionId,
) -> Self::Redacted {
RedactedRoomRedactionEvent {
content: self.content.redact(version),
event_id: self.event_id,
sender: self.sender,
origin_server_ts: self.origin_server_ts,
room_id: self.room_id,
unsigned: RedactedUnsigned::new_because(Box::new(redaction)),
}
}
}
/// Redacted redaction event. /// Redacted redaction event.
#[derive(Clone, Debug, Event)] #[derive(Clone, Debug, Event)]
#[allow(clippy::exhaustive_structs)] #[allow(clippy::exhaustive_structs)]
@ -130,24 +111,6 @@ pub struct OriginalSyncRoomRedactionEvent {
pub unsigned: MessageLikeUnsigned, pub unsigned: MessageLikeUnsigned,
} }
impl Redact for OriginalSyncRoomRedactionEvent {
type Redacted = RedactedSyncRoomRedactionEvent;
fn redact(
self,
redaction: SyncRoomRedactionEvent,
version: &crate::RoomVersionId,
) -> Self::Redacted {
RedactedSyncRoomRedactionEvent {
content: self.content.redact(version),
event_id: self.event_id,
sender: self.sender,
origin_server_ts: self.origin_server_ts,
unsigned: RedactedUnsigned::new_because(Box::new(redaction)),
}
}
}
/// Redacted redaction event without a `room_id`. /// Redacted redaction event without a `room_id`.
#[derive(Clone, Debug, Event)] #[derive(Clone, Debug, Event)]
#[allow(clippy::exhaustive_structs)] #[allow(clippy::exhaustive_structs)]
@ -240,17 +203,6 @@ impl RoomRedactionEvent {
} }
} }
impl Redact for RoomRedactionEvent {
type Redacted = Self;
fn redact(self, redaction: SyncRoomRedactionEvent, version: &crate::RoomVersionId) -> Self {
match self {
Self::Original(ev) => Self::Redacted(ev.redact(redaction, version)),
Self::Redacted(ev) => Self::Redacted(ev),
}
}
}
impl<'de> Deserialize<'de> for RoomRedactionEvent { impl<'de> Deserialize<'de> for RoomRedactionEvent {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where where
@ -326,17 +278,6 @@ impl From<RoomRedactionEvent> for SyncRoomRedactionEvent {
} }
} }
impl Redact for SyncRoomRedactionEvent {
type Redacted = Self;
fn redact(self, redaction: SyncRoomRedactionEvent, version: &crate::RoomVersionId) -> Self {
match self {
Self::Original(ev) => Self::Redacted(ev.redact(redaction, version)),
Self::Redacted(ev) => Self::Redacted(ev),
}
}
}
impl<'de> Deserialize<'de> for SyncRoomRedactionEvent { impl<'de> Deserialize<'de> for SyncRoomRedactionEvent {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where where

View File

@ -1,8 +1,8 @@
//! Implementation of the top level `*Event` derive macro. //! Implementation of the top level `*Event` derive macro.
use proc_macro2::{Span, TokenStream}; use proc_macro2::{Span, TokenStream};
use quote::{format_ident, quote}; use quote::quote;
use syn::{parse_quote, Data, DataStruct, DeriveInput, Field, Fields, FieldsNamed, GenericParam}; use syn::{Data, DataStruct, DeriveInput, Field, Fields, FieldsNamed};
use super::{ use super::{
event_parse::{to_kind_variation, EventKind, EventKindVariation}, event_parse::{to_kind_variation, EventKind, EventKindVariation},
@ -51,12 +51,6 @@ pub fn expand_event(input: DeriveInput) -> syn::Result<TokenStream> {
res.extend(expand_sync_from_into_full(&input, kind, var, &fields, &ruma_common)); res.extend(expand_sync_from_into_full(&input, kind, var, &fields, &ruma_common));
} }
if matches!(kind, EventKind::MessageLike | EventKind::State)
&& matches!(var, EventKindVariation::Original | EventKindVariation::OriginalSync)
{
res.extend(expand_redact_event(&input, kind, var, &fields, &ruma_common));
}
if is_non_stripped_room_event(kind, var) { if is_non_stripped_room_event(kind, var) {
res.extend(expand_eq_ord_event(&input)); res.extend(expand_eq_ord_event(&input));
} }
@ -338,82 +332,6 @@ fn expand_deserialize_event(
}) })
} }
fn expand_redact_event(
input: &DeriveInput,
kind: EventKind,
var: EventKindVariation,
fields: &[Field],
ruma_common: &TokenStream,
) -> syn::Result<TokenStream> {
let redacted_type = kind.to_event_ident(var.to_redacted())?;
let ident = &input.ident;
let mut generics = input.generics.clone();
if generics.params.is_empty() {
return Ok(TokenStream::new());
}
assert_eq!(generics.params.len(), 1, "expected one generic parameter");
let ty_param = match &generics.params[0] {
GenericParam::Type(ty) => ty.ident.clone(),
_ => panic!("expected a type parameter"),
};
let where_clause = generics.make_where_clause();
where_clause.predicates.push(parse_quote! { #ty_param: #ruma_common::events::RedactContent });
let assoc_type_bounds =
(kind == EventKind::State).then(|| quote! { StateKey = #ty_param::StateKey });
let trait_name = format_ident!("Redacted{kind}Content");
let redacted_event_content_bound = quote! {
#ruma_common::events::#trait_name<#assoc_type_bounds>
};
where_clause.predicates.push(parse_quote! {
<#ty_param as #ruma_common::events::RedactContent>::Redacted: #redacted_event_content_bound
});
let (impl_generics, ty_gen, where_clause) = generics.split_for_impl();
let fields = fields.iter().filter_map(|field| {
let ident = field.ident.as_ref().unwrap();
if ident == "content" || ident == "prev_content" {
None
} else if ident == "unsigned" {
Some(quote! {
unsigned: #ruma_common::events::RedactedUnsigned::new_because(
::std::boxed::Box::new(redaction),
)
})
} else {
Some(quote! {
#ident: self.#ident
})
}
});
Ok(quote! {
#[automatically_derived]
impl #impl_generics #ruma_common::events::Redact for #ident #ty_gen #where_clause {
type Redacted = #ruma_common::events::#redacted_type<
<#ty_param as #ruma_common::events::RedactContent>::Redacted,
>;
fn redact(
self,
redaction: #ruma_common::events::room::redaction::SyncRoomRedactionEvent,
version: &#ruma_common::RoomVersionId,
) -> Self::Redacted {
let content = #ruma_common::events::RedactContent::redact(self.content, version);
#ruma_common::events::#redacted_type {
content,
#(#fields),*
}
}
}
})
}
fn expand_sync_from_into_full( fn expand_sync_from_into_full(
input: &DeriveInput, input: &DeriveInput,
kind: EventKind, kind: EventKind,

View File

@ -69,14 +69,6 @@ pub fn expand_event_enums(input: &EventEnumDecl) -> syn::Result<TokenStream> {
expand_event_enum(kind, V::Sync, events, docs, attrs, variants, ruma_common) expand_event_enum(kind, V::Sync, events, docs, attrs, variants, ruma_common)
.unwrap_or_else(syn::Error::into_compile_error), .unwrap_or_else(syn::Error::into_compile_error),
); );
res.extend(
expand_redact(kind, V::None, variants, ruma_common)
.unwrap_or_else(syn::Error::into_compile_error),
);
res.extend(
expand_redact(kind, V::Sync, variants, ruma_common)
.unwrap_or_else(syn::Error::into_compile_error),
);
res.extend( res.extend(
expand_from_full_event(kind, V::None, variants) expand_from_full_event(kind, V::None, variants)
.unwrap_or_else(syn::Error::into_compile_error), .unwrap_or_else(syn::Error::into_compile_error),
@ -427,42 +419,6 @@ fn expand_content_enum(
}) })
} }
fn expand_redact(
kind: EventKind,
var: EventEnumVariation,
variants: &[EventEnumVariant],
ruma_common: &TokenStream,
) -> syn::Result<TokenStream> {
let ident = kind.to_event_enum_ident(var.into())?;
let self_variants = variants.iter().map(|v| v.match_arm(quote! { Self }));
let redacted_variants = variants.iter().map(|v| v.ctor(&ident));
Ok(quote! {
#[automatically_derived]
impl #ruma_common::events::Redact for #ident {
type Redacted = Self;
fn redact(
self,
redaction: #ruma_common::events::room::redaction::SyncRoomRedactionEvent,
version: &#ruma_common::RoomVersionId,
) -> Self {
match self {
#(
#self_variants(event) => #redacted_variants(
#ruma_common::events::Redact::redact(event, redaction, version),
),
)*
Self::_Custom(event) => Self::_Custom(
#ruma_common::events::Redact::redact(event, redaction, version),
)
}
}
}
})
}
fn expand_accessor_methods( fn expand_accessor_methods(
kind: EventKind, kind: EventKind,
var: EventEnumVariation, var: EventEnumVariation,

View File

@ -55,14 +55,6 @@ impl EventKindVariation {
matches!(self, Self::OriginalSync | Self::RedactedSync) matches!(self, Self::OriginalSync | Self::RedactedSync)
} }
pub fn to_redacted(self) -> Self {
match self {
EventKindVariation::Original => EventKindVariation::Redacted,
EventKindVariation::OriginalSync => EventKindVariation::RedactedSync,
_ => panic!("No redacted form of {self:?}"),
}
}
pub fn to_full(self) -> Self { pub fn to_full(self) -> Self {
match self { match self {
EventKindVariation::OriginalSync => EventKindVariation::Original, EventKindVariation::OriginalSync => EventKindVariation::Original,