events: Add redacted redaction event types

This commit is contained in:
Jonas Platte 2021-10-02 13:44:02 +02:00
parent 3ee4ac08fb
commit c807f9f43d
No known key found for this signature in database
GPG Key ID: 7D261D771D915378
4 changed files with 68 additions and 24 deletions

View File

@ -408,6 +408,10 @@ fn expand_redact_event(
let ident = &input.ident;
let mut generics = input.generics.clone();
if generics.params.is_empty() {
return None;
}
assert_eq!(generics.params.len(), 1, "expected one generic parameter");
let ty_param = match &generics.params[0] {
GenericParam::Type(ty) => ty.ident.clone(),
@ -477,7 +481,7 @@ fn expand_from_into(
let fields: Vec<_> = fields.iter().flat_map(|f| &f.ident).collect();
if let EventKindVariation::Sync | EventKindVariation::RedactedSync = var {
let full_struct = kind.to_event_ident(&var.to_full().unwrap());
let full_struct = kind.to_event_ident(&var.to_full().unwrap()).unwrap();
Some(quote! {
#[automatically_derived]
impl #impl_generics ::std::convert::From<#full_struct #ty_gen>

View File

@ -727,23 +727,6 @@ fn to_event_path(
// There is no need to give a good compiler error as `to_camel_case` is called first.
assert_eq!(&name[..2], "m.");
// Temporary hack
if *kind == EventKind::Message && name == "m.room.redaction" {
if *var == EventKindVariation::Redacted {
return quote! {
#ruma_events::RedactedMessageEvent<
#ruma_events::room::redaction::RedactedRedactionEventContent
>
};
} else if *var == EventKindVariation::RedactedSync {
return quote! {
#ruma_events::RedactedSyncMessageEvent<
#ruma_events::room::redaction::RedactedRedactionEventContent
>
};
}
}
let path: Vec<_> = name[2..].split('.').collect();
let event_str = path.last().unwrap();

View File

@ -133,8 +133,10 @@ impl EventKind {
| (Self::State, V::Stripped)
| (Self::State, V::Initial)
| (Self::Message, V::Redacted)
| (Self::Redaction, V::Redacted)
| (Self::State, V::Redacted)
| (Self::Message, V::RedactedSync)
| (Self::Redaction, V::RedactedSync)
| (Self::State, V::RedactedSync) => Some(format_ident!("{}{}", var, self)),
_ => None,
}
@ -203,6 +205,10 @@ pub fn to_kind_variation(ident: &Ident) -> Option<(EventKind, EventKindVariation
"PresenceEvent" => Some((EventKind::Presence, EventKindVariation::Full)),
"RedactionEvent" => Some((EventKind::Redaction, EventKindVariation::Full)),
"SyncRedactionEvent" => Some((EventKind::Redaction, EventKindVariation::Sync)),
"RedactedRedactionEvent" => Some((EventKind::Redaction, EventKindVariation::Redacted)),
"RedactedSyncRedactionEvent" => {
Some((EventKind::Redaction, EventKindVariation::RedactedSync))
}
"DecryptedOlmV1Event" | "DecryptedMegolmV1Event" => {
Some((EventKind::Decrypted, EventKindVariation::Full))
}

View File

@ -34,16 +34,17 @@ pub struct RedactionEvent {
}
impl Redact for RedactionEvent {
// Temporary hack
type Redacted = crate::RedactedMessageEvent<RedactedRedactionEventContent>;
type Redacted = RedactedRedactionEvent;
fn redact(
self,
redaction: SyncRedactionEvent,
version: &ruma_identifiers::RoomVersionId,
) -> Self::Redacted {
crate::RedactedMessageEvent {
RedactedRedactionEvent {
content: self.content.redact(version),
// There is no released room version where this isn't redacted yet
redacts: None,
event_id: self.event_id,
sender: self.sender,
origin_server_ts: self.origin_server_ts,
@ -53,6 +54,32 @@ impl Redact for RedactionEvent {
}
}
/// Redacted redaction event.
#[derive(Clone, Debug, Event)]
#[allow(clippy::exhaustive_structs)]
pub struct RedactedRedactionEvent {
/// Data specific to the event type.
pub content: RedactedRedactionEventContent,
/// The ID of the event that was redacted.
pub redacts: Option<EventId>,
/// The globally unique event identifier for the user who sent the event.
pub event_id: EventId,
/// The fully-qualified ID of the user who sent this event.
pub sender: UserId,
/// Timestamp in milliseconds on originating homeserver when this event was sent.
pub origin_server_ts: MilliSecondsSinceUnixEpoch,
/// The ID of the room associated with this event.
pub room_id: RoomId,
/// Additional key-value pairs not signed by the homeserver.
pub unsigned: RedactedUnsigned,
}
/// Redaction event without a `room_id`.
#[derive(Clone, Debug, Event)]
#[allow(clippy::exhaustive_structs)]
@ -77,16 +104,17 @@ pub struct SyncRedactionEvent {
}
impl Redact for SyncRedactionEvent {
// Temporary hack
type Redacted = crate::RedactedSyncMessageEvent<RedactedRedactionEventContent>;
type Redacted = RedactedSyncRedactionEvent;
fn redact(
self,
redaction: SyncRedactionEvent,
version: &ruma_identifiers::RoomVersionId,
) -> Self::Redacted {
crate::RedactedSyncMessageEvent {
RedactedSyncRedactionEvent {
content: self.content.redact(version),
// There is no released room version where this isn't redacted yet
redacts: None,
event_id: self.event_id,
sender: self.sender,
origin_server_ts: self.origin_server_ts,
@ -95,6 +123,29 @@ impl Redact for SyncRedactionEvent {
}
}
/// Redacted redaction event without a `room_id`.
#[derive(Clone, Debug, Event)]
#[allow(clippy::exhaustive_structs)]
pub struct RedactedSyncRedactionEvent {
/// Data specific to the event type.
pub content: RedactedRedactionEventContent,
/// The ID of the event that was redacted.
pub redacts: Option<EventId>,
/// The globally unique event identifier for the user who sent the event.
pub event_id: EventId,
/// The fully-qualified ID of the user who sent this event.
pub sender: UserId,
/// Timestamp in milliseconds on originating homeserver when this event was sent.
pub origin_server_ts: MilliSecondsSinceUnixEpoch,
/// Additional key-value pairs not signed by the homeserver.
pub unsigned: RedactedUnsigned,
}
/// A redaction of an event.
#[derive(Clone, Debug, Default, Deserialize, Serialize, EventContent)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]