From caa07a80076b7aa5065e809527f78e8cd47a1d9e Mon Sep 17 00:00:00 2001 From: Benjamin Bouvier Date: Wed, 4 Jun 2025 13:44:34 +0200 Subject: [PATCH] refactor(sdk): regroup bundled thread extraction in `TimelineEvent` ctors --- .../src/deserialized_responses.rs | 24 ++++++++----------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/crates/matrix-sdk-common/src/deserialized_responses.rs b/crates/matrix-sdk-common/src/deserialized_responses.rs index f1ae7085d..f03180aa6 100644 --- a/crates/matrix-sdk-common/src/deserialized_responses.rs +++ b/crates/matrix-sdk-common/src/deserialized_responses.rs @@ -492,11 +492,7 @@ impl TimelineEvent { /// This is a convenience constructor for a plaintext event when you don't /// need to set `push_action`, for example inside a test. pub fn from_plaintext(event: Raw) -> Self { - let (thread_summary, latest_thread_event) = extract_bundled_thread_summary(&event); - let kind = TimelineEventKind::PlainText { event }; - let bundled_latest_thread_event = - Self::from_bundled_latest_event(&kind, latest_thread_event); - Self { kind, push_actions: None, thread_summary, bundled_latest_thread_event } + Self::new(TimelineEventKind::PlainText { event }, None) } /// Create a new [`TimelineEvent`] from a decrypted event. @@ -504,22 +500,22 @@ impl TimelineEvent { decrypted: DecryptedRoomEvent, push_actions: Option>, ) -> Self { - let (thread_summary, latest_thread_event) = - extract_bundled_thread_summary(decrypted.event.cast_ref()); - let kind = TimelineEventKind::Decrypted(decrypted); - let bundled_latest_thread_event = - Self::from_bundled_latest_event(&kind, latest_thread_event); - Self { kind, push_actions, thread_summary, bundled_latest_thread_event } + Self::new(TimelineEventKind::Decrypted(decrypted), push_actions) } /// Create a new [`TimelineEvent`] to represent the given decryption /// failure. pub fn from_utd(event: Raw, utd_info: UnableToDecryptInfo) -> Self { - let (thread_summary, latest_thread_event) = extract_bundled_thread_summary(&event); - let kind = TimelineEventKind::UnableToDecrypt { event, utd_info }; + Self::new(TimelineEventKind::UnableToDecrypt { event, utd_info }, None) + } + + /// Internal only: helps extracting a thread summary and latest thread event + /// when creating a new [`TimelineEvent`]. + fn new(kind: TimelineEventKind, push_actions: Option>) -> Self { + let (thread_summary, latest_thread_event) = extract_bundled_thread_summary(kind.raw()); let bundled_latest_thread_event = Self::from_bundled_latest_event(&kind, latest_thread_event); - Self { kind, push_actions: None, thread_summary, bundled_latest_thread_event } + Self { kind, push_actions, thread_summary, bundled_latest_thread_event } } /// Try to create a new [`TimelineEvent`] for the bundled latest thread