From 6a550baf5fb43d04ee41c07c3238d27fb000c4f1 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Mon, 16 Feb 2026 04:39:14 +0000 Subject: [PATCH] Add generic timeline.get_pdu suite to deserialize into other structs. Signed-off-by: Jason Volk --- src/service/rooms/timeline/mod.rs | 94 ++++++++++++++++++++----------- 1 file changed, 61 insertions(+), 33 deletions(-) diff --git a/src/service/rooms/timeline/mod.rs b/src/service/rooms/timeline/mod.rs index 959f50bf..892bc94f 100644 --- a/src/service/rooms/timeline/mod.rs +++ b/src/service/rooms/timeline/mod.rs @@ -423,7 +423,6 @@ fn pdu_count_to_id(shortroomid: ShortRoomId, count: PduCount, dir: Direction) -> } /// Returns the pdu from shorteventid -/// /// Checks the `eventid_outlierpdu` Tree if not found in the timeline. #[implement(Service)] pub async fn get_pdu_from_shorteventid(&self, shorteventid: ShortEventId) -> Result { @@ -437,54 +436,69 @@ pub async fn get_pdu_from_shorteventid(&self, shorteventid: ShortEventId) -> Res } /// Returns the pdu. -/// /// Checks the `eventid_outlierpdu` Tree if not found in the timeline. #[implement(Service)] -pub async fn get_pdu(&self, event_id: &EventId) -> Result { - let accepted = self.get_non_outlier_pdu(event_id); - let outlier = self.get_outlier_pdu(event_id); - - pin_mut!(accepted, outlier); - select_ok([Left(accepted), Right(outlier)]) - .await - .map(at!(0)) -} +pub async fn get_pdu(&self, event_id: &EventId) -> Result { self.get(event_id).await } /// Returns the pdu. -/// /// Checks the `eventid_outlierpdu` Tree if not found in the timeline. #[implement(Service)] pub async fn get_outlier_pdu(&self, event_id: &EventId) -> Result { - self.db - .eventid_outlierpdu - .get(event_id) - .await - .deserialized() + self.get_outlier(event_id).await } /// Returns the pdu. -/// /// Checks the `eventid_outlierpdu` Tree if not found in the timeline. #[implement(Service)] pub async fn get_non_outlier_pdu(&self, event_id: &EventId) -> Result { - let pdu_id = self.get_pdu_id(event_id).await?; - - self.get_pdu_from_id(&pdu_id).await + self.get_non_outlier(event_id).await } /// Returns the pdu. -/// /// This does __NOT__ check the outliers `Tree`. #[implement(Service)] pub async fn get_pdu_from_id(&self, pdu_id: &RawPduId) -> Result { - self.db.pduid_pdu.get(pdu_id).await.deserialized() + self.get_from_id(pdu_id).await } /// Returns the json of a pdu. +/// Checks the `eventid_outlierpdu` Tree if not found in the timeline. #[implement(Service)] pub async fn get_pdu_json(&self, event_id: &EventId) -> Result { - let accepted = self.get_non_outlier_pdu_json(event_id); - let outlier = self.get_outlier_pdu_json(event_id); + self.get(event_id).await +} + +/// Returns the json of a pdu. +/// Checks the `eventid_outlierpdu` Tree if not found in the timeline. +#[implement(Service)] +pub async fn get_outlier_pdu_json(&self, event_id: &EventId) -> Result { + self.get_outlier(event_id).await +} + +/// Returns the json of a pdu. +/// Checks the `eventid_outlierpdu` Tree if not found in the timeline. +#[implement(Service)] +pub async fn get_non_outlier_pdu_json(&self, event_id: &EventId) -> Result { + self.get_non_outlier(event_id).await +} + +/// Returns the pdu as a `BTreeMap`. +/// This does __NOT__ check the outliers `Tree`. +#[implement(Service)] +pub async fn get_pdu_json_from_id(&self, pdu_id: &RawPduId) -> Result { + self.get_from_id(pdu_id).await +} + +/// Returns the pdu into T. +/// Checks the `eventid_outlierpdu` Tree if not found in the timeline. +#[implement(Service)] +#[inline] +pub async fn get(&self, event_id: &EventId) -> Result +where + T: for<'de> Deserialize<'de>, +{ + let accepted = self.get_non_outlier(event_id); + let outlier = self.get_outlier(event_id); pin_mut!(accepted, outlier); select_ok([Left(accepted), Right(outlier)]) @@ -492,9 +506,14 @@ pub async fn get_pdu_json(&self, event_id: &EventId) -> Result Result { +#[inline] +pub async fn get_outlier(&self, event_id: &EventId) -> Result +where + T: for<'de> Deserialize<'de>, +{ self.db .eventid_outlierpdu .get(event_id) @@ -502,22 +521,31 @@ pub async fn get_outlier_pdu_json(&self, event_id: &EventId) -> Result Result { +#[inline] +pub async fn get_non_outlier(&self, event_id: &EventId) -> Result +where + T: for<'de> Deserialize<'de>, +{ let pdu_id = self.get_pdu_id(event_id).await?; - self.get_pdu_json_from_id(&pdu_id).await + self.get_from_id(&pdu_id).await } -/// Returns the pdu as a `BTreeMap`. +/// Returns the pdu into T. +/// This does __NOT__ check the outliers `Tree`. #[implement(Service)] -pub async fn get_pdu_json_from_id(&self, pdu_id: &RawPduId) -> Result { +#[inline] +pub async fn get_from_id(&self, pdu_id: &RawPduId) -> Result +where + T: for<'de> Deserialize<'de>, +{ self.db.pduid_pdu.get(pdu_id).await.deserialized() } /// Checks if pdu exists -/// /// Checks the `eventid_outlierpdu` Tree if not found in the timeline. #[implement(Service)] pub async fn pdu_exists<'a>(&'a self, event_id: &'a EventId) -> bool {