events: Introduce RedactContent trait

This commit is contained in:
Jonas Platte 2021-05-15 13:23:20 +02:00
parent 18005244c9
commit c07c3fa6d5
No known key found for this signature in database
GPG Key ID: 7D261D771D915378
5 changed files with 33 additions and 13 deletions

View File

@ -224,9 +224,10 @@ pub fn expand_event_content(
quote! { quote! {
// this is the non redacted event content's impl // this is the non redacted event content's impl
#[automatically_derived] #[automatically_derived]
impl #ident { impl #ruma_events::RedactContent for #ident {
/// Transforms the full event content into a redacted content according to spec. type Redacted = #redacted_ident;
pub fn redact(self, version: &#ruma_identifiers::RoomVersionId) -> #redacted_ident {
fn redact(self, version: &#ruma_identifiers::RoomVersionId) -> #redacted_ident {
#redacted_ident { #redacted_ident {
#( #redaction_struct_fields: self.#redaction_struct_fields, )* #( #redaction_struct_fields: self.#redaction_struct_fields, )*
} }

View File

@ -496,7 +496,7 @@ fn expand_redact(
match self { match self {
#( #(
#self_variants(event) => { #self_variants(event) => {
let content = event.content.redact(version); let content = #ruma_events::RedactContent::redact(event.content, version);
#redaction_variants(#redacted_type { #redaction_variants(#redacted_type {
content, content,
#fields #fields
@ -504,7 +504,7 @@ fn expand_redact(
} }
)* )*
Self::Custom(event) => { Self::Custom(event) => {
let content = event.content.redact(version); let content = #ruma_events::RedactContent::redact(event.content, version);
#redacted_enum::Custom(#redacted_type { #redacted_enum::Custom(#redacted_type {
content, content,
#fields #fields

View File

@ -8,7 +8,7 @@ use serde_json::{value::RawValue as RawJsonValue, Value as JsonValue};
use crate::{ use crate::{
EphemeralRoomEventContent, EventContent, GlobalAccountDataEventContent, HasDeserializeFields, EphemeralRoomEventContent, EventContent, GlobalAccountDataEventContent, HasDeserializeFields,
MessageEventContent, RedactedEventContent, RedactedMessageEventContent, MessageEventContent, RedactContent, RedactedEventContent, RedactedMessageEventContent,
RedactedStateEventContent, RoomAccountDataEventContent, RoomEventContent, StateEventContent, RedactedStateEventContent, RoomAccountDataEventContent, RoomEventContent, StateEventContent,
ToDeviceEventContent, ToDeviceEventContent,
}; };
@ -25,9 +25,10 @@ pub struct CustomEventContent {
pub data: BTreeMap<String, JsonValue>, pub data: BTreeMap<String, JsonValue>,
} }
impl CustomEventContent { impl RedactContent for CustomEventContent {
/// Transforms the full event content into a redacted content according to spec. type Redacted = RedactedCustomEventContent;
pub fn redact(self, _: &RoomVersionId) -> RedactedCustomEventContent {
fn redact(self, _: &RoomVersionId) -> RedactedCustomEventContent {
RedactedCustomEventContent { event_type: self.event_type } RedactedCustomEventContent { event_type: self.event_type }
} }
} }

View File

@ -118,7 +118,7 @@
use std::fmt::Debug; use std::fmt::Debug;
use js_int::Int; use js_int::Int;
use ruma_identifiers::EventEncryptionAlgorithm; use ruma_identifiers::{EventEncryptionAlgorithm, RoomVersionId};
use ruma_serde::Raw; use ruma_serde::Raw;
use serde::{ use serde::{
de::{self, IgnoredAny}, de::{self, IgnoredAny},
@ -275,6 +275,20 @@ pub trait EventContent: Sized + Serialize {
fn from_parts(event_type: &str, content: Box<RawJsonValue>) -> Result<Self, serde_json::Error>; fn from_parts(event_type: &str, content: Box<RawJsonValue>) -> Result<Self, serde_json::Error>;
} }
/// Trait to define the behavior of redact an event's content object.
pub trait RedactContent {
/// The redacted form of the event's content.
type Redacted;
/// Transform `self` into a redacted form (removing most or all fields) according to the spec.
///
/// A small number of events have room-version specific redaction behavior, so a version has to
/// be specified.
///
/// Where applicable, it is prefered to use [`Redact::redact`] on the outer event.
fn redact(self, version: &RoomVersionId) -> Self::Redacted;
}
/// Extension trait for Raw<EventContent> /// Extension trait for Raw<EventContent>
pub trait RawExt<T: EventContent> { pub trait RawExt<T: EventContent> {
/// Try to deserialize the JSON as event content /// Try to deserialize the JSON as event content

View File

@ -6,7 +6,8 @@ use serde::{Deserialize, Serialize};
use serde_json::value::RawValue as RawJsonValue; use serde_json::value::RawValue as RawJsonValue;
use crate::{ use crate::{
EventContent, HasDeserializeFields, RedactedEventContent, RedactedStateEventContent, StateEvent, EventContent, HasDeserializeFields, RedactContent, RedactedEventContent,
RedactedStateEventContent, StateEvent,
}; };
/// Informs the room about what room aliases it has been given. /// Informs the room about what room aliases it has been given.
@ -26,9 +27,12 @@ impl AliasesEventContent {
pub fn new(aliases: Vec<RoomAliasId>) -> Self { pub fn new(aliases: Vec<RoomAliasId>) -> Self {
Self { aliases } Self { aliases }
} }
}
/// Redact an `AliasesEventContent` according to current Matrix spec. impl RedactContent for AliasesEventContent {
pub fn redact(self, version: &RoomVersionId) -> RedactedAliasesEventContent { type Redacted = RedactedAliasesEventContent;
fn redact(self, version: &RoomVersionId) -> RedactedAliasesEventContent {
// We compare the long way to avoid pre version 6 behavior if/when // We compare the long way to avoid pre version 6 behavior if/when
// a new room version is introduced. // a new room version is introduced.
let aliases = match version { let aliases = match version {