From c173ce43a5e6ca6abe0b23a590c124e85173593c Mon Sep 17 00:00:00 2001 From: Devin Ragotzy Date: Mon, 30 Nov 2020 14:46:47 -0500 Subject: [PATCH] convert_to_outgoing_federation_event takes CanonicalJsonObj --- src/client_server/membership.rs | 5 +---- src/database/rooms.rs | 4 ++-- src/pdu.rs | 31 ++++++++++++++++--------------- 3 files changed, 19 insertions(+), 21 deletions(-) diff --git a/src/client_server/membership.rs b/src/client_server/membership.rs index d5c50e5d..b5e4042f 100644 --- a/src/client_server/membership.rs +++ b/src/client_server/membership.rs @@ -541,10 +541,7 @@ async fn join_room_by_id_helper( federation::membership::create_join_event::v2::Request { room_id, event_id: &event_id, - pdu_stub: PduEvent::convert_to_outgoing_federation_event( - serde_json::to_value(&join_event) - .expect("we just validated and ser/de this event"), - ), + pdu_stub: PduEvent::convert_to_outgoing_federation_event(join_event.clone()), }, ) .await?; diff --git a/src/database/rooms.rs b/src/database/rooms.rs index 651a596d..e7b6eaac 100644 --- a/src/database/rooms.rs +++ b/src/database/rooms.rs @@ -385,8 +385,8 @@ impl Rooms { }) } - /// Returns the pdu. - pub fn get_pdu_json_from_id(&self, pdu_id: &[u8]) -> Result> { + /// Returns the pdu as a `BTreeMap`. + pub fn get_pdu_json_from_id(&self, pdu_id: &[u8]) -> Result> { self.pduid_pdu.get(pdu_id)?.map_or(Ok(None), |pdu| { Ok(Some( serde_json::from_slice(&pdu) diff --git a/src/pdu.rs b/src/pdu.rs index effbc5d4..cffd4a32 100644 --- a/src/pdu.rs +++ b/src/pdu.rs @@ -5,6 +5,7 @@ use ruma::{ pdu::EventHash, room::member::MemberEventContent, AnyEvent, AnyRoomEvent, AnyStateEvent, AnyStrippedStateEvent, AnySyncRoomEvent, AnySyncStateEvent, EventType, StateEvent, }, + serde::{CanonicalJsonObject, CanonicalJsonValue}, EventId, Raw, RoomId, ServerKeyId, ServerName, UserId, }; use serde::{Deserialize, Serialize}; @@ -200,25 +201,25 @@ impl PduEvent { } pub fn convert_to_outgoing_federation_event( - mut pdu_json: serde_json::Value, + mut pdu_json: CanonicalJsonObject, ) -> Raw { - if let Some(unsigned) = pdu_json - .as_object_mut() - .expect("json is object") - .get_mut("unsigned") - { - unsigned - .as_object_mut() - .expect("unsigned is object") - .remove("transaction_id"); + if let Some(CanonicalJsonValue::Object(unsigned)) = pdu_json.get_mut("unsigned") { + unsigned.remove("transaction_id"); } - pdu_json - .as_object_mut() - .expect("json is object") - .remove("event_id"); + pdu_json.remove("event_id"); - serde_json::from_value::>(pdu_json).expect("Raw::from_value always works") + // TODO: another option would be to convert it to a canonical string to validate size + // and return a Result> + // serde_json::from_str::>( + // ruma::serde::to_canonical_json_string(pdu_json).expect("CanonicalJson is valid serde_json::Value"), + // ) + // .expect("Raw::from_value always works") + + serde_json::from_value::>( + serde_json::to_value(pdu_json).expect("CanonicalJson is valid serde_json::Value"), + ) + .expect("Raw::from_value always works") } }