From b08e8cff9d466f74350c2eb9af07bfa18ed1523f Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Tue, 8 Feb 2022 19:12:14 +0100 Subject: [PATCH] events: Remove custom module MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit … and shuffle things around so there's still a content type that can be used for _Custom event enum variants. --- crates/ruma-events-macros/src/event_enum.rs | 2 +- crates/ruma-events/CHANGELOG.md | 4 +- crates/ruma-events/src/_custom.rs | 60 ++++++++ crates/ruma-events/src/custom.rs | 150 -------------------- crates/ruma-events/src/lib.rs | 4 +- crates/ruma-events/tests/custom.rs | 150 -------------------- crates/ruma-events/tests/redacted.rs | 27 ---- 7 files changed, 67 insertions(+), 330 deletions(-) create mode 100644 crates/ruma-events/src/_custom.rs delete mode 100644 crates/ruma-events/src/custom.rs delete mode 100644 crates/ruma-events/tests/custom.rs diff --git a/crates/ruma-events-macros/src/event_enum.rs b/crates/ruma-events-macros/src/event_enum.rs index 51fe0239..52ff4c9e 100644 --- a/crates/ruma-events-macros/src/event_enum.rs +++ b/crates/ruma-events-macros/src/event_enum.rs @@ -89,7 +89,7 @@ fn expand_event_enum( )* /// An event not defined by the Matrix specification #[doc(hidden)] - _Custom(#ruma_events::#event_struct<#ruma_events::custom::_CustomEventContent>), + _Custom(#ruma_events::#event_struct<#ruma_events::_custom::CustomEventContent>), } #deserialize_impl diff --git a/crates/ruma-events/CHANGELOG.md b/crates/ruma-events/CHANGELOG.md index 4e70e72e..3de608ba 100644 --- a/crates/ruma-events/CHANGELOG.md +++ b/crates/ruma-events/CHANGELOG.md @@ -3,7 +3,7 @@ Breaking changes: * Remove `RedactedStrippedStateEvent` - * It was not used anywhere since stripped state events are never actually redacted. + * It was not used anywhere since stripped state events are never actually redacted * Use `Box` instead of `JsonValue` for PDU `content` field * Require `room::message::MessageType` to always contain a body * The `new` constructor now also has a body parameter @@ -11,6 +11,8 @@ Breaking changes: * Remove unneeded redacted event content enums * Update `reply` and `html_reply` types to `impl Display` on `RoomMessageEventContent`'s reply constructors +* Remove the `custom` module, which has been redundant for a while + * If you are still using it and are unclear on the upgrade path, please get in touch Improvements: diff --git a/crates/ruma-events/src/_custom.rs b/crates/ruma-events/src/_custom.rs new file mode 100644 index 00000000..0a99b65f --- /dev/null +++ b/crates/ruma-events/src/_custom.rs @@ -0,0 +1,60 @@ +use ruma_identifiers::RoomVersionId; +use serde::Serialize; +use serde_json::value::RawValue as RawJsonValue; + +use crate::{ + EphemeralRoomEventContent, EventContent, GlobalAccountDataEventContent, HasDeserializeFields, + MessageEventContent, RedactContent, RedactedEventContent, RedactedMessageEventContent, + RedactedStateEventContent, RoomAccountDataEventContent, StateEventContent, + ToDeviceEventContent, +}; + +/// A custom event's type. Used for event enum `_Custom` variants. +// FIXME: Serialize shouldn't be required here, but it's currently a supertrait of EventContent +#[derive(Clone, Debug, Serialize)] +#[allow(clippy::exhaustive_structs)] +pub struct CustomEventContent { + #[serde(skip)] + event_type: Box, +} + +impl RedactContent for CustomEventContent { + type Redacted = Self; + + fn redact(self, _: &RoomVersionId) -> Self { + self + } +} + +impl EventContent for CustomEventContent { + fn event_type(&self) -> &str { + &self.event_type + } + + fn from_parts(event_type: &str, _content: &RawJsonValue) -> serde_json::Result { + Ok(Self { event_type: event_type.into() }) + } +} + +impl RedactedEventContent for CustomEventContent { + fn empty(event_type: &str) -> serde_json::Result { + Ok(Self { event_type: event_type.into() }) + } + + fn has_serialize_fields(&self) -> bool { + false + } + + fn has_deserialize_fields() -> HasDeserializeFields { + HasDeserializeFields::False + } +} + +impl GlobalAccountDataEventContent for CustomEventContent {} +impl RoomAccountDataEventContent for CustomEventContent {} +impl ToDeviceEventContent for CustomEventContent {} +impl EphemeralRoomEventContent for CustomEventContent {} +impl MessageEventContent for CustomEventContent {} +impl StateEventContent for CustomEventContent {} +impl RedactedMessageEventContent for CustomEventContent {} +impl RedactedStateEventContent for CustomEventContent {} diff --git a/crates/ruma-events/src/custom.rs b/crates/ruma-events/src/custom.rs deleted file mode 100644 index d0c02043..00000000 --- a/crates/ruma-events/src/custom.rs +++ /dev/null @@ -1,150 +0,0 @@ -//! Types for custom events outside of the Matrix specification. - -use std::collections::BTreeMap; - -use ruma_identifiers::RoomVersionId; -use serde::Serialize; -use serde_json::{value::RawValue as RawJsonValue, Value as JsonValue}; - -use crate::{ - EphemeralRoomEventContent, EventContent, GlobalAccountDataEventContent, HasDeserializeFields, - MessageEventContent, RedactContent, RedactedEventContent, RedactedMessageEventContent, - RedactedStateEventContent, RoomAccountDataEventContent, StateEventContent, - ToDeviceEventContent, -}; - -/// A custom event's type and `content` JSON object. -#[derive(Clone, Debug, Serialize)] -#[allow(clippy::exhaustive_structs)] -pub struct CustomEventContent { - /// The event type string. - #[serde(skip)] - pub event_type: String, - - /// The actual `content` JSON object. - #[serde(flatten)] - pub data: BTreeMap, -} - -impl RedactContent for CustomEventContent { - type Redacted = RedactedCustomEventContent; - - fn redact(self, _: &RoomVersionId) -> RedactedCustomEventContent { - RedactedCustomEventContent { event_type: self.event_type } - } -} - -impl EventContent for CustomEventContent { - fn event_type(&self) -> &str { - &self.event_type - } - - fn from_parts(event_type: &str, content: &RawJsonValue) -> serde_json::Result { - let data = serde_json::from_str(content.get())?; - Ok(Self { event_type: event_type.to_owned(), data }) - } -} - -// A custom event must satisfy all of the event content marker traits since -// they can be used for any event kind. -impl GlobalAccountDataEventContent for CustomEventContent {} - -impl RoomAccountDataEventContent for CustomEventContent {} - -impl ToDeviceEventContent for CustomEventContent {} - -impl EphemeralRoomEventContent for CustomEventContent {} - -impl MessageEventContent for CustomEventContent {} - -impl StateEventContent for CustomEventContent {} - -/// A custom event that has been redacted. -#[derive(Clone, Debug, Serialize)] -#[allow(clippy::exhaustive_structs)] -pub struct RedactedCustomEventContent { - // This field is marked skipped but will be present because deserialization - // passes the `type` field of the JSON event to the events `EventContent::from_parts` method. - /// The event type string for this custom event "m.whatever". - #[serde(skip)] - pub event_type: String, -} - -impl EventContent for RedactedCustomEventContent { - fn event_type(&self) -> &str { - &self.event_type - } - - fn from_parts(event_type: &str, _content: &RawJsonValue) -> serde_json::Result { - Ok(Self { event_type: event_type.to_owned() }) - } -} - -impl RedactedEventContent for RedactedCustomEventContent { - fn empty(event_type: &str) -> serde_json::Result { - Ok(Self { event_type: event_type.to_owned() }) - } - - fn has_serialize_fields(&self) -> bool { - false - } - - fn has_deserialize_fields() -> HasDeserializeFields { - HasDeserializeFields::False - } -} - -impl RedactedMessageEventContent for RedactedCustomEventContent {} - -impl RedactedStateEventContent for RedactedCustomEventContent {} - -/// A custom event's type. Used for event enum `_Custom` variants. -#[doc(hidden)] -// FIXME: Serialize shouldn't be required here, but it's currently a supertrait of EventContent -#[derive(Clone, Debug, Serialize)] -#[allow(clippy::exhaustive_structs)] -pub struct _CustomEventContent { - #[serde(skip)] - event_type: Box, -} - -impl RedactContent for _CustomEventContent { - type Redacted = Self; - - fn redact(self, _: &RoomVersionId) -> Self { - self - } -} - -impl EventContent for _CustomEventContent { - fn event_type(&self) -> &str { - &self.event_type - } - - fn from_parts(event_type: &str, _content: &RawJsonValue) -> serde_json::Result { - Ok(Self { event_type: event_type.into() }) - } -} - -impl RedactedEventContent for _CustomEventContent { - fn empty(event_type: &str) -> serde_json::Result { - Ok(Self { event_type: event_type.into() }) - } - - fn has_serialize_fields(&self) -> bool { - false - } - - fn has_deserialize_fields() -> HasDeserializeFields { - HasDeserializeFields::False - } -} - -impl GlobalAccountDataEventContent for _CustomEventContent {} -impl RoomAccountDataEventContent for _CustomEventContent {} -impl ToDeviceEventContent for _CustomEventContent {} -impl EphemeralRoomEventContent for _CustomEventContent {} -impl MessageEventContent for _CustomEventContent {} -impl StateEventContent for _CustomEventContent {} -impl RedactedMessageEventContent for _CustomEventContent {} -impl RedactedStateEventContent for _CustomEventContent {} diff --git a/crates/ruma-events/src/lib.rs b/crates/ruma-events/src/lib.rs index ca7d308e..cbd24f38 100644 --- a/crates/ruma-events/src/lib.rs +++ b/crates/ruma-events/src/lib.rs @@ -124,6 +124,9 @@ use serde_json::value::RawValue as RawJsonValue; use self::room::redaction::SyncRoomRedactionEvent; +// Needs to be public for trybuild tests +#[doc(hidden)] +pub mod _custom; mod enums; mod event_kinds; mod unsigned; @@ -151,7 +154,6 @@ pub mod macros { } pub mod call; -pub mod custom; pub mod direct; pub mod dummy; pub mod forwarded_room_key; diff --git a/crates/ruma-events/tests/custom.rs b/crates/ruma-events/tests/custom.rs deleted file mode 100644 index 43b516dd..00000000 --- a/crates/ruma-events/tests/custom.rs +++ /dev/null @@ -1,150 +0,0 @@ -use js_int::uint; -use maplit::btreemap; -use matches::assert_matches; -use ruma_common::MilliSecondsSinceUnixEpoch; -use ruma_events::{ - custom::CustomEventContent, AnyStateEvent, AnySyncRoomEvent, AnySyncStateEvent, MessageEvent, - StateEvent, Unsigned, -}; -use ruma_identifiers::{event_id, room_id, user_id}; -use serde_json::{ - from_value as from_json_value, json, to_value as to_json_value, Value as JsonValue, -}; - -fn custom_state_event() -> JsonValue { - json!({ - "content": { - "m.relates_to": { - "event_id": "$MDitXXXXXX", - "key": "👍", - "rel_type": "m.annotation" - } - }, - "event_id": "$h29iv0s8:example.com", - "origin_server_ts": 10, - "room_id": "!room:room.com", - "sender": "@carl:example.com", - "state_key": "", - "type": "m.reaction", - "unsigned": { - "age": 85 - } - }) -} - -#[test] -fn serialize_custom_message_event() { - let aliases_event = MessageEvent { - content: CustomEventContent { - data: btreemap! { - "body".into() => " * edited message".into(), - "m.new_content".into() => json!({ - "body": "edited message", - "msgtype": "m.text" - }), - "m.relates_to".into() => json!({ - "event_id": "some event id", - "rel_type": "m.replace" - }), - "msgtype".into() => "m.text".into() - }, - event_type: "m.room.message".into(), - }, - event_id: event_id!("$h29iv0s8:example.com").to_owned(), - origin_server_ts: MilliSecondsSinceUnixEpoch(uint!(10)), - room_id: room_id!("!room:room.com").to_owned(), - sender: user_id!("@carl:example.com").to_owned(), - unsigned: Unsigned::default(), - }; - - let actual = to_json_value(&aliases_event).unwrap(); - let expected = json!({ - "content": { - "body": " * edited message", - "m.new_content": { - "body": "edited message", - "msgtype": "m.text" - }, - "m.relates_to": { - "event_id": "some event id", - "rel_type": "m.replace" - }, - "msgtype": "m.text" - }, - "event_id": "$h29iv0s8:example.com", - "origin_server_ts": 10, - "sender": "@carl:example.com", - "room_id": "!room:room.com", - "type": "m.room.message", - }); - - assert_eq!(actual, expected); -} - -#[test] -fn serialize_custom_state_event() { - let aliases_event = StateEvent { - content: CustomEventContent { - data: btreemap! { - "custom".into() => 10.into() - }, - event_type: "m.made.up".into(), - }, - event_id: event_id!("$h29iv0s8:example.com").to_owned(), - origin_server_ts: MilliSecondsSinceUnixEpoch(uint!(10)), - prev_content: None, - room_id: room_id!("!roomid:room.com").to_owned(), - sender: user_id!("@carl:example.com").to_owned(), - state_key: "".into(), - unsigned: Unsigned::default(), - }; - - let actual = to_json_value(&aliases_event).unwrap(); - let expected = json!({ - "content": { - "custom": 10 - }, - "event_id": "$h29iv0s8:example.com", - "origin_server_ts": 10, - "room_id": "!roomid:room.com", - "sender": "@carl:example.com", - "state_key": "", - "type": "m.made.up", - }); - - assert_eq!(actual, expected); -} - -#[test] -fn deserialize_custom_state_event() { - let json_data = custom_state_event(); - assert_matches!(from_json_value::(json_data), Ok(_)); -} - -#[test] -fn deserialize_custom_state_sync_event() { - let json_data = custom_state_event(); - assert_matches!(from_json_value::(json_data), Ok(_)); -} - -#[test] -fn deserialize_custom_message_sync_event() { - let json_data = json!({ - "content": { - "body": "👍" - }, - "event_id": "$h29iv0s8:example.com", - "origin_server_ts": 10, - "room_id": "!room:room.com", - "sender": "@carl:example.com", - "type": "m.ruma_custom", - "unsigned": { - "age": 85 - } - }); - - assert_matches!( - from_json_value::(json_data), - Ok(AnySyncRoomEvent::Message(_)) - ); -} diff --git a/crates/ruma-events/tests/redacted.rs b/crates/ruma-events/tests/redacted.rs index ce4012f5..51ba45be 100644 --- a/crates/ruma-events/tests/redacted.rs +++ b/crates/ruma-events/tests/redacted.rs @@ -2,7 +2,6 @@ use js_int::uint; use matches::assert_matches; use ruma_common::MilliSecondsSinceUnixEpoch; use ruma_events::{ - custom::RedactedCustomEventContent, room::{ aliases::RedactedRoomAliasesEventContent, create::{RedactedRoomCreateEventContent, RoomCreateEventContent}, @@ -244,32 +243,6 @@ fn redacted_custom_event_serialize() { assert_eq!(x.event_id(), event_id!("$h29iv0s8:example.com")) } -#[test] -fn redacted_custom_event_deserialize() { - let unsigned = unsigned(); - - let redacted = RedactedSyncStateEvent { - content: RedactedCustomEventContent { event_type: "m.made.up".into() }, - event_id: event_id!("$h29iv0s8:example.com").to_owned(), - sender: user_id!("@carl:example.com").to_owned(), - state_key: "hello there".into(), - origin_server_ts: MilliSecondsSinceUnixEpoch(uint!(1)), - unsigned: unsigned.clone(), - }; - - let expected = json!({ - "event_id": "$h29iv0s8:example.com", - "origin_server_ts": 1, - "sender": "@carl:example.com", - "state_key": "hello there", - "unsigned": unsigned, - "type": "m.made.up", - }); - - let actual = to_json_value(&redacted).unwrap(); - assert_eq!(actual, expected); -} - #[test] fn redact_method_properly_redacts() { let ev = json!({