events: Introduce Redact trait

This commit is contained in:
Jonas Platte 2021-05-15 13:26:05 +02:00
parent c07c3fa6d5
commit 702b619417
No known key found for this signature in database
GPG Key ID: 7D261D771D915378
4 changed files with 18 additions and 5 deletions

View File

@ -486,9 +486,10 @@ fn expand_redact(
Some(quote! {
#[automatically_derived]
impl #ident {
/// Redacts `Self` given a valid `Redaction[Sync]Event`.
pub fn redact(
impl #ruma_events::Redact for #ident {
type Redacted = #redacted_enum;
fn redact(
self,
redaction: #ruma_events::room::redaction::SyncRedactionEvent,
version: &#ruma_identifiers::RoomVersionId,

View File

@ -4,7 +4,7 @@ use ruma_identifiers::{EventId, RoomId, RoomVersionId, UserId};
use serde::{de, Serialize};
use serde_json::value::RawValue as RawJsonValue;
use crate::{from_raw_json_value, room::redaction::SyncRedactionEvent, EventDeHelper};
use crate::{from_raw_json_value, room::redaction::SyncRedactionEvent, EventDeHelper, Redact};
event_enum! {
/// Any global account data event.

View File

@ -275,6 +275,18 @@ 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 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: SyncRedactionEvent, version: &RoomVersionId) -> Self::Redacted;
}
/// Trait to define the behavior of redact an event's content object.
pub trait RedactContent {
/// The redacted form of the event's content.

View File

@ -10,7 +10,7 @@ use ruma_events::{
redaction::{RedactionEventContent, SyncRedactionEvent},
},
AnyMessageEvent, AnyRedactedMessageEvent, AnyRedactedSyncMessageEvent,
AnyRedactedSyncStateEvent, AnyRoomEvent, AnySyncRoomEvent, RedactedMessageEvent,
AnyRedactedSyncStateEvent, AnyRoomEvent, AnySyncRoomEvent, Redact, RedactedMessageEvent,
RedactedSyncMessageEvent, RedactedSyncStateEvent, RedactedUnsigned, Unsigned,
};
use ruma_identifiers::{event_id, room_id, user_id, RoomVersionId};