events: Merge RedactedSyncUnsigned and RedactedUnsigned

This commit is contained in:
Jonas Platte 2021-05-15 13:12:13 +02:00
parent 824a6db048
commit 18005244c9
No known key found for this signature in database
GPG Key ID: 7D261D771D915378
6 changed files with 35 additions and 164 deletions

View File

@ -369,27 +369,14 @@ fn expand_from_into(
let fields: Vec<_> = fields.iter().flat_map(|f| &f.ident).collect();
let fields_without_unsigned: Vec<_> =
fields.iter().filter(|id| id.to_string().as_str() != "unsigned").collect();
let (into, into_full_event) = if var.is_redacted() {
(quote! { unsigned: unsigned.into(), }, quote! { unsigned: unsigned.into_full(room_id), })
} else if kind == &EventKind::Ephemeral {
(TokenStream::new(), TokenStream::new())
} else {
(quote! { unsigned, }, quote! { unsigned, })
};
if let EventKindVariation::Sync | EventKindVariation::RedactedSync = var {
let full_struct = kind.to_event_ident(&var.to_full_variation());
Some(quote! {
#[automatically_derived]
impl #impl_generics From<#full_struct #ty_gen> for #ident #ty_gen #where_clause {
fn from(event: #full_struct #ty_gen) -> Self {
let #full_struct {
#( #fields, )* ..
} = event;
Self { #( #fields_without_unsigned, )* #into }
let #full_struct { #( #fields, )* .. } = event;
Self { #( #fields, )* }
}
}
@ -402,9 +389,8 @@ fn expand_from_into(
) -> #full_struct #ty_gen {
let Self { #( #fields, )* } = self;
#full_struct {
#( #fields_without_unsigned, )*
room_id: room_id.clone(),
#into_full_event
#( #fields, )*
room_id,
}
}
}

View File

@ -450,11 +450,10 @@ fn expand_redact(
if let EventKindVariation::Full | EventKindVariation::Sync | EventKindVariation::Stripped = var
{
let (redaction_type, redacted_type, redacted_enum) = match var {
let (redacted_type, redacted_enum) = match var {
EventKindVariation::Full => {
let struct_id = kind.to_event_ident(&EventKindVariation::Redacted)?;
(
quote! { #ruma_events::room::redaction::RedactionEvent },
quote! { #ruma_events::#struct_id },
kind.to_event_enum_ident(&EventKindVariation::Redacted)?,
)
@ -462,7 +461,6 @@ fn expand_redact(
EventKindVariation::Sync => {
let struct_id = kind.to_event_ident(&EventKindVariation::RedactedSync)?;
(
quote! { #ruma_events::room::redaction::SyncRedactionEvent },
quote! { #ruma_events::#struct_id },
kind.to_event_enum_ident(&EventKindVariation::RedactedSync)?,
)
@ -470,7 +468,6 @@ fn expand_redact(
EventKindVariation::Stripped => {
let struct_id = kind.to_event_ident(&EventKindVariation::RedactedStripped)?;
(
quote! { #ruma_events::room::redaction::SyncRedactionEvent },
quote! { #ruma_events::#struct_id },
kind.to_event_enum_ident(&EventKindVariation::RedactedStripped)?,
)
@ -493,7 +490,7 @@ fn expand_redact(
/// Redacts `Self` given a valid `Redaction[Sync]Event`.
pub fn redact(
self,
redaction: #redaction_type,
redaction: #ruma_events::room::redaction::SyncRedactionEvent,
version: &#ruma_identifiers::RoomVersionId,
) -> #redacted_enum {
match self {
@ -591,14 +588,8 @@ fn generate_redacted_fields(
let name = Ident::new(name, Span::call_site());
if name == "unsigned" {
let redacted_type = if let EventKindVariation::Sync = var {
quote! { RedactedSyncUnsigned }
} else {
quote! { RedactedUnsigned }
};
quote! {
unsigned: #ruma_events::#redacted_type {
unsigned: #ruma_events::RedactedUnsigned {
redacted_because: Some(::std::boxed::Box::new(redaction)),
},
}
@ -958,13 +949,13 @@ fn field_return_type(
"event_id" => quote! { #ruma_identifiers::EventId },
"sender" => quote! { #ruma_identifiers::UserId },
"state_key" => quote! { ::std::primitive::str },
"unsigned" if &EventKindVariation::RedactedSync == var => {
quote! { #ruma_events::RedactedSyncUnsigned }
"unsigned" => {
if var.is_redacted() {
quote! { #ruma_events::RedactedUnsigned }
} else {
quote! { #ruma_events::Unsigned }
}
}
"unsigned" if &EventKindVariation::Redacted == var => {
quote! { #ruma_events::RedactedUnsigned }
}
"unsigned" => quote! { #ruma_events::Unsigned },
_ => panic!("the `ruma_events_macros::event_enum::EVENT_FIELD` const was changed"),
}
}

View File

@ -4,11 +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::{RedactionEvent, SyncRedactionEvent},
EventDeHelper,
};
use crate::{from_raw_json_value, room::redaction::SyncRedactionEvent, EventDeHelper};
event_enum! {
/// Any global account data event.
@ -163,7 +159,7 @@ impl AnyRoomEvent {
/// Redacts `self`, referencing the given event in `unsigned.redacted_because`.
///
/// Does nothing for events that are already redacted.
pub fn redact(self, redaction: RedactionEvent, version: &RoomVersionId) -> Self {
pub fn redact(self, redaction: SyncRedactionEvent, version: &RoomVersionId) -> Self {
match self {
Self::Message(ev) => Self::RedactedMessage(ev.redact(redaction, version)),
Self::State(ev) => Self::RedactedState(ev.redact(redaction, version)),

View File

@ -4,7 +4,7 @@ use ruma_identifiers::{EventId, RoomId, UserId};
use crate::{
EphemeralRoomEventContent, GlobalAccountDataEventContent, MessageEventContent,
RedactedMessageEventContent, RedactedStateEventContent, RedactedSyncUnsigned, RedactedUnsigned,
RedactedMessageEventContent, RedactedStateEventContent, RedactedUnsigned,
RoomAccountDataEventContent, StateEventContent, ToDeviceEventContent, Unsigned,
};
@ -135,7 +135,7 @@ pub struct RedactedSyncMessageEvent<C: RedactedMessageEventContent> {
pub origin_server_ts: MilliSecondsSinceUnixEpoch,
/// Additional key-value pairs not signed by the homeserver.
pub unsigned: RedactedSyncUnsigned,
pub unsigned: RedactedUnsigned,
}
/// A state event.
@ -297,7 +297,7 @@ pub struct RedactedSyncStateEvent<C: RedactedStateEventContent> {
pub state_key: String,
/// Additional key-value pairs not signed by the homeserver.
pub unsigned: RedactedSyncUnsigned,
pub unsigned: RedactedUnsigned,
}
/// A stripped-down redacted state event.

View File

@ -118,7 +118,7 @@
use std::fmt::Debug;
use js_int::Int;
use ruma_identifiers::{EventEncryptionAlgorithm, RoomId};
use ruma_identifiers::EventEncryptionAlgorithm;
use ruma_serde::Raw;
use serde::{
de::{self, IgnoredAny},
@ -126,7 +126,7 @@ use serde::{
};
use serde_json::value::RawValue as RawJsonValue;
use self::room::redaction::{RedactionEvent, SyncRedactionEvent};
use self::room::redaction::SyncRedactionEvent;
mod enums;
mod error;
@ -249,7 +249,7 @@ impl Unsigned {
pub struct RedactedUnsigned {
/// The event that redacted this event, if any.
#[serde(skip_serializing_if = "Option::is_none")]
pub redacted_because: Option<Box<RedactionEvent>>,
pub redacted_because: Option<Box<SyncRedactionEvent>>,
}
impl RedactedUnsigned {
@ -264,79 +264,6 @@ impl RedactedUnsigned {
}
}
/// Extra information about a redacted sync event that is not incorporated into the sync event's
/// hash.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct RedactedSyncUnsigned {
/// The event that redacted this event, if any.
#[serde(skip_serializing_if = "Option::is_none")]
pub redacted_because: Option<Box<SyncRedactionEvent>>,
}
impl From<RedactedUnsigned> for RedactedSyncUnsigned {
fn from(redacted: RedactedUnsigned) -> Self {
match redacted.redacted_because.map(|b| *b) {
Some(RedactionEvent {
content,
redacts,
event_id,
sender,
origin_server_ts,
unsigned,
..
}) => Self {
redacted_because: Some(Box::new(SyncRedactionEvent {
content,
redacts,
event_id,
sender,
origin_server_ts,
unsigned,
})),
},
_ => Self { redacted_because: None },
}
}
}
impl RedactedSyncUnsigned {
/// Whether this unsigned data is empty (`redacted_because` is `None`).
///
/// This method is used to determine whether to skip serializing the
/// `unsignedSync` field in redacted room events. Do not use it to determine whether
/// an incoming `unsignedSync` field was present - it could still have been
/// present but contained none of the known fields.
pub fn is_empty(&self) -> bool {
self.redacted_because.is_none()
}
/// Convert a `RedactedSyncUnsigned` into `RedactedUnsigned`, converting the
/// underlying sync redaction event to a full redaction event (with room_id).
pub fn into_full(self, room_id: RoomId) -> RedactedUnsigned {
match self.redacted_because.map(|b| *b) {
Some(SyncRedactionEvent {
content,
redacts,
event_id,
sender,
origin_server_ts,
unsigned,
}) => RedactedUnsigned {
redacted_because: Some(Box::new(RedactionEvent {
content,
redacts,
event_id,
sender,
origin_server_ts,
room_id,
unsigned,
})),
},
_ => RedactedUnsigned { redacted_because: None },
}
}
}
/// The base trait that all event content types implement.
///
/// Implementing this trait allows content types to be serialized as well as deserialized.

View File

@ -7,22 +7,18 @@ use ruma_events::{
aliases::RedactedAliasesEventContent,
create::RedactedCreateEventContent,
message::RedactedMessageEventContent,
redaction::{RedactionEvent, RedactionEventContent, SyncRedactionEvent},
redaction::{RedactionEventContent, SyncRedactionEvent},
},
AnyMessageEvent, AnyRedactedMessageEvent, AnyRedactedSyncMessageEvent,
AnyRedactedSyncStateEvent, AnyRoomEvent, AnySyncRoomEvent, RedactedMessageEvent,
RedactedSyncMessageEvent, RedactedSyncStateEvent, RedactedSyncUnsigned, RedactedUnsigned,
Unsigned,
RedactedSyncMessageEvent, RedactedSyncStateEvent, RedactedUnsigned, Unsigned,
};
use ruma_identifiers::{event_id, room_id, user_id, RoomVersionId};
use ruma_serde::Raw;
use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
fn sync_unsigned() -> RedactedSyncUnsigned {
let mut unsigned = RedactedSyncUnsigned::default();
// The presence of `redacted_because` triggers the event enum to return early
// with `RedactedContent` instead of failing to deserialize according
// to the event type string.
fn unsigned() -> RedactedUnsigned {
let mut unsigned = RedactedUnsigned::default();
unsigned.redacted_because = Some(Box::new(SyncRedactionEvent {
content: RedactionEventContent { reason: Some("redacted because".into()) },
redacts: event_id!("$h29iv0s8:example.com"),
@ -35,21 +31,6 @@ fn sync_unsigned() -> RedactedSyncUnsigned {
unsigned
}
fn full_unsigned() -> RedactedUnsigned {
let mut unsigned = RedactedUnsigned::default();
unsigned.redacted_because = Some(Box::new(RedactionEvent {
content: RedactionEventContent { reason: Some("redacted because".into()) },
room_id: room_id!("!roomid:room.com"),
redacts: event_id!("$h29iv0s8:example.com"),
event_id: event_id!("$h29iv0s8:example.com"),
origin_server_ts: MilliSecondsSinceUnixEpoch(uint!(1)),
sender: user_id!("@carl:example.com"),
unsigned: Unsigned::default(),
}));
unsigned
}
#[test]
fn redacted_message_event_serialize() {
let redacted = RedactedSyncMessageEvent {
@ -57,7 +38,7 @@ fn redacted_message_event_serialize() {
event_id: event_id!("$h29iv0s8:example.com"),
origin_server_ts: MilliSecondsSinceUnixEpoch(uint!(1)),
sender: user_id!("@carl:example.com"),
unsigned: RedactedSyncUnsigned::default(),
unsigned: RedactedUnsigned::default(),
};
let expected = json!({
@ -79,7 +60,7 @@ fn redacted_aliases_event_serialize_no_content() {
state_key: "".into(),
origin_server_ts: MilliSecondsSinceUnixEpoch(uint!(1)),
sender: user_id!("@carl:example.com"),
unsigned: RedactedSyncUnsigned::default(),
unsigned: RedactedUnsigned::default(),
};
let expected = json!({
@ -102,7 +83,7 @@ fn redacted_aliases_event_serialize_with_content() {
state_key: "".to_owned(),
origin_server_ts: MilliSecondsSinceUnixEpoch(uint!(1)),
sender: user_id!("@carl:example.com"),
unsigned: RedactedSyncUnsigned::default(),
unsigned: RedactedUnsigned::default(),
};
let expected = json!({
@ -122,14 +103,12 @@ fn redacted_aliases_event_serialize_with_content() {
#[test]
fn redacted_aliases_deserialize() {
let unsigned = sync_unsigned();
let redacted = json!({
"event_id": "$h29iv0s8:example.com",
"origin_server_ts": 1,
"sender": "@carl:example.com",
"state_key": "hello",
"unsigned": unsigned,
"unsigned": unsigned(),
"type": "m.room.aliases"
});
@ -153,14 +132,12 @@ fn redacted_aliases_deserialize() {
#[test]
fn redacted_deserialize_any_room() {
let unsigned = full_unsigned();
let redacted = json!({
"event_id": "$h29iv0s8:example.com",
"room_id": "!roomid:room.com",
"origin_server_ts": 1,
"sender": "@carl:example.com",
"unsigned": unsigned,
"unsigned": unsigned(),
"type": "m.room.message"
});
@ -185,12 +162,11 @@ fn redacted_deserialize_any_room_sync() {
// The presence of `redacted_because` triggers the event enum (AnySyncRoomEvent in this case)
// to return early with `RedactedContent` instead of failing to deserialize according
// to the event type string.
unsigned.redacted_because = Some(Box::new(RedactionEvent {
unsigned.redacted_because = Some(Box::new(SyncRedactionEvent {
content: RedactionEventContent { reason: Some("redacted because".into()) },
redacts: event_id!("$h29iv0s8:example.com"),
event_id: event_id!("$h29iv0s8:example.com"),
origin_server_ts: MilliSecondsSinceUnixEpoch(uint!(1)),
room_id: room_id!("!roomid:room.com"),
sender: user_id!("@carl:example.com"),
unsigned: Unsigned::default(),
}));
@ -222,8 +198,6 @@ fn redacted_deserialize_any_room_sync() {
#[test]
fn redacted_state_event_deserialize() {
let unsigned = sync_unsigned();
let redacted = json!({
"content": {
"creator": "@carl:example.com",
@ -232,7 +206,7 @@ fn redacted_state_event_deserialize() {
"origin_server_ts": 1,
"sender": "@carl:example.com",
"state_key": "hello there",
"unsigned": unsigned,
"unsigned": unsigned(),
"type": "m.room.create"
});
@ -260,14 +234,12 @@ fn redacted_state_event_deserialize() {
#[test]
fn redacted_custom_event_serialize() {
let unsigned = sync_unsigned();
let redacted = json!({
"event_id": "$h29iv0s8:example.com",
"origin_server_ts": 1,
"sender": "@carl:example.com",
"state_key": "hello there",
"unsigned": unsigned,
"unsigned": unsigned(),
"type": "m.made.up"
});
@ -294,7 +266,7 @@ fn redacted_custom_event_serialize() {
#[test]
fn redacted_custom_event_deserialize() {
let unsigned = sync_unsigned();
let unsigned = unsigned();
let redacted = RedactedSyncStateEvent {
content: RedactedCustomEventContent { event_type: "m.made.up".into() },
@ -333,12 +305,11 @@ fn redact_method_properly_redacts() {
}
});
let redaction = RedactionEvent {
let redaction = SyncRedactionEvent {
content: RedactionEventContent { reason: Some("redacted because".into()) },
redacts: event_id!("$143273582443PhrSn:example.com"),
event_id: event_id!("$h29iv0s8:example.com"),
origin_server_ts: MilliSecondsSinceUnixEpoch(uint!(1)),
room_id: room_id!("!roomid:room.com"),
sender: user_id!("@carl:example.com"),
unsigned: Unsigned::default(),
};