From 55c85f685177eb22f126fdd7382e99959e32e3d8 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Thu, 3 Oct 2024 09:49:28 +0000 Subject: [PATCH] refactor to pdu.get_content() for serde_json::from_ elim Signed-off-by: Jason Volk --- src/api/client/sync.rs | 46 +++++++------------------ src/service/rooms/event_handler/mod.rs | 10 +++--- src/service/rooms/pdu_metadata/mod.rs | 2 +- src/service/rooms/spaces/mod.rs | 10 +++--- src/service/rooms/state/mod.rs | 17 ++------- src/service/rooms/state_accessor/mod.rs | 4 +-- src/service/rooms/timeline/mod.rs | 36 +++++-------------- 7 files changed, 35 insertions(+), 90 deletions(-) diff --git a/src/api/client/sync.rs b/src/api/client/sync.rs index 65d62a78..8c4c6a44 100644 --- a/src/api/client/sync.rs +++ b/src/api/client/sync.rs @@ -635,17 +635,8 @@ async fn load_joined_room( .await? .ready_filter(|(_, pdu)| pdu.kind == RoomMember) .filter_map(|(_, pdu)| async move { - let Ok(content) = serde_json::from_str::(pdu.content.get()) else { - return None; - }; - - let Some(state_key) = &pdu.state_key else { - return None; - }; - - let Ok(user_id) = UserId::parse(state_key) else { - return None; - }; + let content: RoomMemberEventContent = pdu.get_content().ok()?; + let user_id: &UserId = pdu.state_key.as_deref().map(TryInto::try_into).flat_ok()?; if user_id == sender_user { return None; @@ -656,22 +647,17 @@ async fn load_joined_room( return None; } - if !services - .rooms - .state_cache - .is_joined(&user_id, room_id) - .await && services - .rooms - .state_cache - .is_invited(&user_id, room_id) - .await - { + let is_invited = services.rooms.state_cache.is_invited(user_id, room_id); + + let is_joined = services.rooms.state_cache.is_joined(user_id, room_id); + + if !is_joined.await && is_invited.await { return None; } - Some(user_id) + Some(user_id.to_owned()) }) - .collect::>() + .collect::>() .await; Ok::<_, Error>(( @@ -839,11 +825,9 @@ async fn load_joined_room( continue; } - let new_membership = serde_json::from_str::(state_event.content.get()) - .map_err(|_| Error::bad_database("Invalid PDU in database."))? - .membership; + let content: RoomMemberEventContent = state_event.get_content()?; - match new_membership { + match content.membership { MembershipState::Join => { // A new user joined an encrypted room if !share_encrypted_room(services, sender_user, &user_id, Some(room_id)).await { @@ -1357,12 +1341,8 @@ pub(crate) async fn sync_events_v4_route( continue; } - let new_membership = - serde_json::from_str::(pdu.content.get()) - .map_err(|_| Error::bad_database("Invalid PDU in database."))? - .membership; - - match new_membership { + let content: RoomMemberEventContent = pdu.get_content()?; + match content.membership { MembershipState::Join => { // A new user joined an encrypted room if !share_encrypted_room(&services, sender_user, &user_id, Some(room_id)) diff --git a/src/service/rooms/event_handler/mod.rs b/src/service/rooms/event_handler/mod.rs index 4708a86c..05f9a27a 100644 --- a/src/service/rooms/event_handler/mod.rs +++ b/src/service/rooms/event_handler/mod.rs @@ -614,9 +614,7 @@ impl Service { } }, _ => { - let content = serde_json::from_str::(incoming_pdu.content.get()) - .map_err(|_| Error::bad_database("Invalid content in redaction pdu."))?; - + let content: RoomRedactionEventContent = incoming_pdu.get_content()?; if let Some(redact_id) = &content.redacts { !self .services @@ -1432,10 +1430,10 @@ impl Service { } fn get_room_version_id(create_event: &PduEvent) -> Result { - let create_event_content: RoomCreateEventContent = serde_json::from_str(create_event.content.get()) - .map_err(|e| err!(Database("Invalid create event: {e}")))?; + let content: RoomCreateEventContent = create_event.get_content()?; + let room_version = content.room_version; - Ok(create_event_content.room_version) + Ok(room_version) } #[inline] diff --git a/src/service/rooms/pdu_metadata/mod.rs b/src/service/rooms/pdu_metadata/mod.rs index dbaebfbf..fb85d031 100644 --- a/src/service/rooms/pdu_metadata/mod.rs +++ b/src/service/rooms/pdu_metadata/mod.rs @@ -102,7 +102,7 @@ impl Service { return false; } - let Ok(content) = serde_json::from_str::(pdu.content.get()) else { + let Ok(content) = pdu.get_content::() else { return false; }; diff --git a/src/service/rooms/spaces/mod.rs b/src/service/rooms/spaces/mod.rs index a30c2cfc..5aea5f6a 100644 --- a/src/service/rooms/spaces/mod.rs +++ b/src/service/rooms/spaces/mod.rs @@ -596,12 +596,10 @@ impl Service { .await .map_err(|e| err!(Database("Event {id:?} in space state not found: {e:?}")))?; - if serde_json::from_str::(pdu.content.get()) - .ok() - .map(|c| c.via) - .map_or(true, |v| v.is_empty()) - { - continue; + if let Ok(content) = pdu.get_content::() { + if content.via.is_empty() { + continue; + } } if OwnedRoomId::try_from(state_key).is_ok() { diff --git a/src/service/rooms/state/mod.rs b/src/service/rooms/state/mod.rs index 177b7e9b..81760b36 100644 --- a/src/service/rooms/state/mod.rs +++ b/src/service/rooms/state/mod.rs @@ -93,28 +93,17 @@ impl Service { pin_mut!(event_ids); while let Some(event_id) = event_ids.next().await { - let Ok(pdu) = self.services.timeline.get_pdu_json(&event_id).await else { + let Ok(pdu) = self.services.timeline.get_pdu(&event_id).await else { continue; }; - let pdu: PduEvent = match serde_json::from_str( - &serde_json::to_string(&pdu).expect("CanonicalJsonObj can be serialized to JSON"), - ) { - Ok(pdu) => pdu, - Err(_) => continue, - }; - match pdu.kind { TimelineEventType::RoomMember => { - let Ok(membership_event) = serde_json::from_str::(pdu.content.get()) else { + let Some(user_id) = pdu.state_key.as_ref().map(UserId::parse).flat_ok() else { continue; }; - let Some(state_key) = pdu.state_key else { - continue; - }; - - let Ok(user_id) = UserId::parse(state_key) else { + let Ok(membership_event) = pdu.get_content::() else { continue; }; diff --git a/src/service/rooms/state_accessor/mod.rs b/src/service/rooms/state_accessor/mod.rs index 3b2c2931..3855d92a 100644 --- a/src/service/rooms/state_accessor/mod.rs +++ b/src/service/rooms/state_accessor/mod.rs @@ -325,11 +325,9 @@ impl Service { where T: for<'de> Deserialize<'de> + Send, { - use serde_json::from_str; - self.room_state_get(room_id, event_type, state_key) .await - .and_then(|event| from_str::(event.content.get()).map_err(Into::into)) + .and_then(|event| event.get_content()) } pub async fn get_name(&self, room_id: &RoomId) -> Result { diff --git a/src/service/rooms/timeline/mod.rs b/src/service/rooms/timeline/mod.rs index cc5940e6..487262e6 100644 --- a/src/service/rooms/timeline/mod.rs +++ b/src/service/rooms/timeline/mod.rs @@ -471,12 +471,7 @@ impl Service { } }, _ => { - let content = - serde_json::from_str::(pdu.content.get()).map_err(|e| { - warn!("Invalid content in redaction pdu: {e}"); - Error::bad_database("Invalid content in redaction pdu") - })?; - + let content: RoomRedactionEventContent = pdu.get_content()?; if let Some(redact_id) = &content.redacts { if self .services @@ -506,11 +501,7 @@ impl Service { let target_user_id = UserId::parse(state_key.clone()).expect("This state_key was previously validated"); - let content = serde_json::from_str::(pdu.content.get()).map_err(|e| { - error!("Invalid room member event content in pdu: {e}"); - Error::bad_database("Invalid room member event content in pdu.") - })?; - + let content: RoomMemberEventContent = pdu.get_content()?; let invite_state = match content.membership { MembershipState::Invite => self.services.state.summary_stripped(pdu).await.into(), _ => None, @@ -533,9 +524,7 @@ impl Service { } }, TimelineEventType::RoomMessage => { - let content = serde_json::from_str::(pdu.content.get()) - .map_err(|_| Error::bad_database("Invalid content in pdu."))?; - + let content: ExtractBody = pdu.get_content()?; if let Some(body) = content.body { self.services.search.index_pdu(shortroomid, &pdu_id, &body); @@ -549,7 +538,7 @@ impl Service { _ => {}, } - if let Ok(content) = serde_json::from_str::(pdu.content.get()) { + if let Ok(content) = pdu.get_content::() { if let Ok(related_pducount) = self.get_pdu_count(&content.relates_to.event_id).await { self.services .pdu_metadata @@ -557,7 +546,7 @@ impl Service { } } - if let Ok(content) = serde_json::from_str::(pdu.content.get()) { + if let Ok(content) = pdu.get_content::() { match content.relates_to { Relation::Reply { in_reply_to, @@ -712,10 +701,7 @@ impl Service { .room_state_get(room_id, &event_type.to_string().into(), state_key) .await { - unsigned.insert( - "prev_content".to_owned(), - serde_json::from_str(prev_pdu.content.get()).expect("string is valid json"), - ); + unsigned.insert("prev_content".to_owned(), prev_pdu.get_content_as_value()); unsigned.insert( "prev_sender".to_owned(), serde_json::to_value(&prev_pdu.sender).expect("UserId::to_value always works"), @@ -874,9 +860,7 @@ impl Service { }; }, _ => { - let content = serde_json::from_str::(pdu.content.get()) - .map_err(|e| err!(Database("Invalid content in redaction pdu: {e:?}")))?; - + let content: RoomRedactionEventContent = pdu.get_content()?; if let Some(redact_id) = &content.redacts { if !self .services @@ -1026,7 +1010,7 @@ impl Service { .await .map_err(|e| err!(Database(error!(?pdu_id, ?event_id, ?e, "PDU ID points to invalid PDU."))))?; - if let Ok(content) = serde_json::from_str::(pdu.content.get()) { + if let Ok(content) = pdu.get_content::() { if let Some(body) = content.body { self.services .search @@ -1200,9 +1184,7 @@ impl Service { drop(insert_lock); if pdu.kind == TimelineEventType::RoomMessage { - let content = serde_json::from_str::(pdu.content.get()) - .map_err(|e| err!(Database("Invalid content in pdu: {e:?}")))?; - + let content: ExtractBody = pdu.get_content()?; if let Some(body) = content.body { self.services.search.index_pdu(shortroomid, &pdu_id, &body); }