events: Introduce RedactContent trait
This commit is contained in:
parent
18005244c9
commit
c07c3fa6d5
@ -224,9 +224,10 @@ pub fn expand_event_content(
|
||||
quote! {
|
||||
// this is the non redacted event content's impl
|
||||
#[automatically_derived]
|
||||
impl #ident {
|
||||
/// Transforms the full event content into a redacted content according to spec.
|
||||
pub fn redact(self, version: &#ruma_identifiers::RoomVersionId) -> #redacted_ident {
|
||||
impl #ruma_events::RedactContent for #ident {
|
||||
type Redacted = #redacted_ident;
|
||||
|
||||
fn redact(self, version: &#ruma_identifiers::RoomVersionId) -> #redacted_ident {
|
||||
#redacted_ident {
|
||||
#( #redaction_struct_fields: self.#redaction_struct_fields, )*
|
||||
}
|
||||
|
@ -496,7 +496,7 @@ fn expand_redact(
|
||||
match self {
|
||||
#(
|
||||
#self_variants(event) => {
|
||||
let content = event.content.redact(version);
|
||||
let content = #ruma_events::RedactContent::redact(event.content, version);
|
||||
#redaction_variants(#redacted_type {
|
||||
content,
|
||||
#fields
|
||||
@ -504,7 +504,7 @@ fn expand_redact(
|
||||
}
|
||||
)*
|
||||
Self::Custom(event) => {
|
||||
let content = event.content.redact(version);
|
||||
let content = #ruma_events::RedactContent::redact(event.content, version);
|
||||
#redacted_enum::Custom(#redacted_type {
|
||||
content,
|
||||
#fields
|
||||
|
@ -8,7 +8,7 @@ use serde_json::{value::RawValue as RawJsonValue, Value as JsonValue};
|
||||
|
||||
use crate::{
|
||||
EphemeralRoomEventContent, EventContent, GlobalAccountDataEventContent, HasDeserializeFields,
|
||||
MessageEventContent, RedactedEventContent, RedactedMessageEventContent,
|
||||
MessageEventContent, RedactContent, RedactedEventContent, RedactedMessageEventContent,
|
||||
RedactedStateEventContent, RoomAccountDataEventContent, RoomEventContent, StateEventContent,
|
||||
ToDeviceEventContent,
|
||||
};
|
||||
@ -25,9 +25,10 @@ pub struct CustomEventContent {
|
||||
pub data: BTreeMap<String, JsonValue>,
|
||||
}
|
||||
|
||||
impl CustomEventContent {
|
||||
/// Transforms the full event content into a redacted content according to spec.
|
||||
pub fn redact(self, _: &RoomVersionId) -> RedactedCustomEventContent {
|
||||
impl RedactContent for CustomEventContent {
|
||||
type Redacted = RedactedCustomEventContent;
|
||||
|
||||
fn redact(self, _: &RoomVersionId) -> RedactedCustomEventContent {
|
||||
RedactedCustomEventContent { event_type: self.event_type }
|
||||
}
|
||||
}
|
||||
|
@ -118,7 +118,7 @@
|
||||
use std::fmt::Debug;
|
||||
|
||||
use js_int::Int;
|
||||
use ruma_identifiers::EventEncryptionAlgorithm;
|
||||
use ruma_identifiers::{EventEncryptionAlgorithm, RoomVersionId};
|
||||
use ruma_serde::Raw;
|
||||
use serde::{
|
||||
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>;
|
||||
}
|
||||
|
||||
/// 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>
|
||||
pub trait RawExt<T: EventContent> {
|
||||
/// Try to deserialize the JSON as event content
|
||||
|
@ -6,7 +6,8 @@ use serde::{Deserialize, Serialize};
|
||||
use serde_json::value::RawValue as RawJsonValue;
|
||||
|
||||
use crate::{
|
||||
EventContent, HasDeserializeFields, RedactedEventContent, RedactedStateEventContent, StateEvent,
|
||||
EventContent, HasDeserializeFields, RedactContent, RedactedEventContent,
|
||||
RedactedStateEventContent, StateEvent,
|
||||
};
|
||||
|
||||
/// Informs the room about what room aliases it has been given.
|
||||
@ -26,9 +27,12 @@ impl AliasesEventContent {
|
||||
pub fn new(aliases: Vec<RoomAliasId>) -> Self {
|
||||
Self { aliases }
|
||||
}
|
||||
}
|
||||
|
||||
/// Redact an `AliasesEventContent` according to current Matrix spec.
|
||||
pub fn redact(self, version: &RoomVersionId) -> RedactedAliasesEventContent {
|
||||
impl RedactContent for AliasesEventContent {
|
||||
type Redacted = RedactedAliasesEventContent;
|
||||
|
||||
fn redact(self, version: &RoomVersionId) -> RedactedAliasesEventContent {
|
||||
// We compare the long way to avoid pre version 6 behavior if/when
|
||||
// a new room version is introduced.
|
||||
let aliases = match version {
|
||||
|
Loading…
x
Reference in New Issue
Block a user