Repository: ruma/ruma
Pull request #152 (2020-07-22 18:16:02)

Use Box instead of Raw for the redacted Unsigned types redacted_because
I had to add the `full_unsigned` helper function because this change caught that we were using sync redaction event in a full event in the `redacted.rs` test.

jimmycuadra: As long as Diesel supports SystemTime (which it looks like it does) this sounds good to me!
mujx: One problem that came up in #137 is how  to go from seconds (`u64` etc) to `Timestamp` so that can be used in diesel queries. Maybe there is a workaround? We used the `time` crate (which is really missing from the std) for the conversions.
farodin91: Could be better to switch `NaiveDateTime` form `chrono`.
mujx: In general we should try to eliminate u64 and i64 where we can when it comes to timestamps.
jimmycuadra: chrono would be fine if the stdlib time APIs don't cut it.

Patch:
diff --git a/ruma-events-macros/src/event_enum.rs b/ruma-events-macros/src/event_enum.rs
index 57fecfec..ed2946d1 100644
--- a/ruma-events-macros/src/event_enum.rs
+++ b/ruma-events-macros/src/event_enum.rs
@@ -379,7 +379,7 @@ fn generate_redacted_fields(
 
             quote! {
                 unsigned: ::ruma_events::#redaction_type {
-                    redacted_because: Some(::ruma_events::EventJson::from(redaction)),
+                    redacted_because: Some(::std::boxed::Box::new(redaction)),
                 },
             }
         } else {
diff --git a/ruma-events/src/lib.rs b/ruma-events/src/lib.rs
index 9c97f447..a65fd4dc 100644
--- a/ruma-events/src/lib.rs
+++ b/ruma-events/src/lib.rs
@@ -222,7 +222,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<Raw<RedactionEvent>>,
+    pub redacted_because: Option<Box<RedactionEvent>>,
 }
 
 impl RedactedUnsigned {
@@ -243,7 +243,7 @@ impl RedactedUnsigned {
 pub struct RedactedSyncUnsigned {
     /// The event that redacted this event, if any.
     #[serde(skip_serializing_if = "Option::is_none")]
-    pub redacted_because: Option<Raw<SyncRedactionEvent>>,
+    pub redacted_because: Option<Box<SyncRedactionEvent>>,
 }
 
 impl RedactedSyncUnsigned {
diff --git a/ruma-identifiers/src/device_id.rs b/ruma-identifiers/src/device_id.rs
index 217c4311..86d3d8bf 100644
--- a/ruma-identifiers/src/device_id.rs
+++ b/ruma-identifiers/src/device_id.rs
@@ -58,6 +58,12 @@ impl ToOwned for DeviceId {
     }
 }
 
+impl From<&DeviceId> for Box<DeviceId> {
+    fn from(id: &DeviceId) -> Self {
+        id.to_owned()
+    }
+}
+
 impl AsRef<str> for DeviceId {
     fn as_ref(&self) -> &str {
         self.as_str()
diff --git a/ruma-identifiers/src/server_name.rs b/ruma-identifiers/src/server_name.rs
index a3783ae5..947bd4fa 100644
--- a/ruma-identifiers/src/server_name.rs
+++ b/ruma-identifiers/src/server_name.rs
@@ -48,6 +48,12 @@ impl ToOwned for ServerName {
     }
 }
 
+impl From<&ServerName> for Box<ServerName> {
+    fn from(s: &ServerName) -> Self {
+        s.to_owned()
+    }
+}
+
 pub(crate) fn validate(server_name: &str) -> Result<(), Error> {
     use std::net::Ipv6Addr;
 
@@ -114,8 +120,8 @@ impl AsRef<str> for Box<ServerName> {
 }
 
 impl From<Box<ServerName>> for String {
-    fn from(id: Box<ServerName>) -> Self {
-        id.into_owned().into()
+    fn from(s: Box<ServerName>) -> Self {
+        s.into_owned().into()
     }
 }
 


Test patch:
diff --git a/ruma-events/tests/redacted.rs b/ruma-events/tests/redacted.rs
index 3e047e51..34c02ad6 100644
--- a/ruma-events/tests/redacted.rs
+++ b/ruma-events/tests/redacted.rs
@@ -21,12 +21,12 @@ use ruma_events::{
 use ruma_identifiers::{EventId, RoomId, RoomVersionId, UserId};
 use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
 
-fn full_unsigned() -> RedactedSyncUnsigned {
+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.
-    unsigned.redacted_because = Some(Raw::from(SyncRedactionEvent {
+    unsigned.redacted_because = Some(Box::new(SyncRedactionEvent {
         content: RedactionEventContent { reason: Some("redacted because".into()) },
         redacts: EventId::try_from("$h29iv0s8:example.com").unwrap(),
         event_id: EventId::try_from("$h29iv0s8:example.com").unwrap(),
@@ -38,6 +38,21 @@ fn full_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: RoomId::try_from("!roomid:room.com").unwrap(),
+        redacts: EventId::try_from("$h29iv0s8:example.com").unwrap(),
+        event_id: EventId::try_from("$h29iv0s8:example.com").unwrap(),
+        origin_server_ts: UNIX_EPOCH + Duration::from_millis(1),
+        sender: UserId::try_from("@carl:example.com").unwrap(),
+        unsigned: Unsigned::default(),
+    }));
+
+    unsigned
+}
+
 #[test]
 fn redacted_message_event_serialize() {
     let redacted = RedactedSyncMessageEvent {
@@ -110,7 +125,7 @@ fn redacted_aliases_event_serialize_with_content() {
 
 #[test]
 fn redacted_aliases_deserialize() {
-    let unsigned = full_unsigned();
+    let unsigned = sync_unsigned();
 
     let redacted = json!({
       "event_id": "$h29iv0s8:example.com",
@@ -170,7 +185,7 @@ 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(Raw::from(RedactionEvent {
+    unsigned.redacted_because = Some(Box::new(RedactionEvent {
         content: RedactionEventContent { reason: Some("redacted because".into()) },
         redacts: EventId::try_from("$h29iv0s8:example.com").unwrap(),
         event_id: EventId::try_from("$h29iv0s8:example.com").unwrap(),
@@ -204,7 +219,7 @@ fn redacted_deserialize_any_room_sync() {
 
 #[test]
 fn redacted_state_event_deserialize() {
-    let unsigned = full_unsigned();
+    let unsigned = sync_unsigned();
 
     let redacted = json!({
       "content": {
@@ -237,7 +252,7 @@ fn redacted_state_event_deserialize() {
 
 #[test]
 fn redacted_custom_event_serialize() {
-    let unsigned = full_unsigned();
+    let unsigned = sync_unsigned();
 
     let redacted = json!({
         "event_id": "$h29iv0s8:example.com",
@@ -271,7 +286,7 @@ fn redacted_custom_event_serialize() {
 
 #[test]
 fn redacted_custom_event_deserialize() {
-    let unsigned = full_unsigned();
+    let unsigned = sync_unsigned();
 
     let redacted = RedactedSyncStateEvent {
         content: RedactedCustomEventContent { event_type: "m.made.up".into() },