refactor(timeline): extract the method to fetch a latest thread reply as an item

Do the ~~~loco~~ code motion 🎶
This commit is contained in:
Benjamin Bouvier
2025-06-03 15:24:28 +02:00
parent 281faa7a0b
commit fe11fda832

View File

@@ -16,9 +16,11 @@ use std::collections::{HashMap, HashSet};
use eyeball_im::VectorDiff;
use itertools::Itertools as _;
use matrix_sdk::deserialized_responses::{TimelineEvent, TimelineEventKind, UnsignedEventLocation};
use matrix_sdk::deserialized_responses::{
ThreadSummaryStatus, TimelineEvent, TimelineEventKind, UnsignedEventLocation,
};
use ruma::{
events::AnySyncTimelineEvent, push::Action, serde::Raw, MilliSecondsSinceUnixEpoch,
events::AnySyncTimelineEvent, push::Action, serde::Raw, EventId, MilliSecondsSinceUnixEpoch,
OwnedEventId, OwnedTransactionId, OwnedUserId, UserId,
};
use tracing::{debug, instrument, warn};
@@ -544,6 +546,36 @@ impl<'a> TimelineStateTransaction<'a> {
}
}
// Attempt to load a thread's latest reply as an embedded timeline item, either
// using the event cache or the storage.
async fn fetch_latest_thread_reply(
&mut self,
event_id: &EventId,
room_data_provider: &impl RoomDataProvider,
) -> Option<Box<EmbeddedEvent>> {
let event = room_data_provider
.load_event(event_id)
.await
.inspect_err(|err| {
warn!("Failed to load thread latest event: {err}");
})
.ok()?;
EmbeddedEvent::try_from_timeline_event(
event,
room_data_provider,
&self.items,
&mut self.meta,
)
.await
.inspect_err(|err| {
warn!("Failed to extract thread latest event into a timeline item content: {err}");
})
.ok()
.flatten()
.map(Box::new)
}
/// Handle a remote event.
///
/// Returns whether an item has been removed from the timeline.
@@ -557,39 +589,12 @@ impl<'a> TimelineStateTransaction<'a> {
) -> RemovedItem {
let TimelineEvent { push_actions, kind, thread_summary } = event;
let thread_summary = if let Some(summary) = thread_summary.summary() {
let latest_reply_item = if let Some(event_id) = summary.latest_reply.as_ref() {
// Attempt to load the timeline event, either from the event cache or the
// storage.
let event = room_data_provider
.load_event(event_id)
.await
.inspect_err(|err| {
warn!("Failed to load thread latest event: {err}");
})
.ok();
if let Some(event) = event {
EmbeddedEvent::try_from_timeline_event(
event,
room_data_provider,
&self.items,
&mut self.meta,
)
.await
.inspect_err(|err| {
warn!("Failed to extract thread event into a timeline item content: {err}");
})
.ok()
.flatten()
.map(Box::new)
} else {
None
}
let thread_summary = if let ThreadSummaryStatus::Some(summary) = thread_summary {
let latest_reply_item = if let Some(latest_reply) = summary.latest_reply {
self.fetch_latest_thread_reply(&latest_reply, room_data_provider).await
} else {
None
};
Some(ThreadSummary {
latest_event: TimelineDetails::from_initial_value(latest_reply_item),
num_replies: summary.num_replies,