Skip to content

Commit 16d0840

Browse files
committed
refactor(timeline): move code around for loading initial main|unthreaded receipts
1 parent d90576b commit 16d0840

File tree

2 files changed

+35
-48
lines changed

2 files changed

+35
-48
lines changed

crates/matrix-sdk-ui/src/timeline/controller/read_receipts.rs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,41 @@ impl<P: RoomDataProvider> TimelineStateTransaction<'_, P> {
560560
trace!(%event_id, "loading initial receipts for an event");
561561

562562
let receipt_thread = self.focus.receipt_thread();
563-
let read_receipts = room_data_provider.load_event_receipts(event_id, receipt_thread).await;
563+
564+
let mut read_receipts;
565+
566+
if matches!(receipt_thread, ReceiptThread::Unthreaded | ReceiptThread::Main) {
567+
// If the requested receipt thread is unthreaded or main, we maintain maximal
568+
// compatibility with clients using either unthreaded or main-thread read
569+
// receipts by allowing both here.
570+
571+
// First, load the main receipts.
572+
read_receipts =
573+
room_data_provider.load_event_receipts(event_id, ReceiptThread::Main).await;
574+
575+
// Then, load the unthreaded receipts.
576+
let unthreaded_receipts =
577+
room_data_provider.load_event_receipts(event_id, ReceiptThread::Unthreaded).await;
578+
579+
// Only insert unthreaded receipts that are more recent. Ideally we'd compare
580+
// the receipted event position, but we don't have access to that
581+
// here.
582+
for (user_id, unthreaded_receipt) in unthreaded_receipts {
583+
if let Some(main_receipt) = read_receipts.get(&user_id) {
584+
if unthreaded_receipt.ts > main_receipt.ts {
585+
read_receipts.insert(user_id, unthreaded_receipt);
586+
}
587+
} else {
588+
read_receipts.insert(user_id, unthreaded_receipt);
589+
}
590+
}
591+
} else {
592+
// In all other cases, return what's requested, and only that (threaded
593+
// receipts).
594+
read_receipts =
595+
room_data_provider.load_event_receipts(event_id, receipt_thread.clone()).await
596+
}
597+
564598
let own_user_id = room_data_provider.own_user_id();
565599

566600
// Since they are explicit read receipts, we need to check if they are

crates/matrix-sdk-ui/src/timeline/traits.rs

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -232,53 +232,6 @@ impl RoomDataProvider for Room {
232232
event_id: &'a EventId,
233233
receipt_thread: ReceiptThread,
234234
) -> IndexMap<OwnedUserId, Receipt> {
235-
if matches!(receipt_thread, ReceiptThread::Unthreaded | ReceiptThread::Main) {
236-
// If the requested receipt thread is unthreaded or main, we maintain maximal
237-
// compatibility with clients using either unthreaded or main-thread read
238-
// receipts by allowing both here.
239-
240-
// First, load the main receipts.
241-
let mut result = match self
242-
.load_event_receipts(ReceiptType::Read, ReceiptThread::Main, event_id)
243-
.await
244-
{
245-
Ok(receipts) => receipts.into_iter().collect(),
246-
Err(e) => {
247-
error!(?event_id, "Failed to get main thread read receipts for event: {e}");
248-
IndexMap::new()
249-
}
250-
};
251-
252-
// Then, load the unthreaded receipts.
253-
let unthreaded_receipts = match self
254-
.load_event_receipts(ReceiptType::Read, ReceiptThread::Unthreaded, event_id)
255-
.await
256-
{
257-
Ok(receipts) => receipts,
258-
Err(e) => {
259-
error!(?event_id, "Failed to get main thread read receipts for event: {e}");
260-
Vec::new()
261-
}
262-
};
263-
264-
// Only insert unthreaded receipts that are more recent. Ideally we'd compare
265-
// the receipted event position, but we don't have access to that
266-
// here.
267-
for (user_id, unthreaded_receipt) in unthreaded_receipts {
268-
if let Some(main_receipt) = result.get(&user_id) {
269-
if unthreaded_receipt.ts > main_receipt.ts {
270-
result.insert(user_id, unthreaded_receipt);
271-
}
272-
} else {
273-
result.insert(user_id, unthreaded_receipt);
274-
}
275-
}
276-
277-
return result;
278-
}
279-
280-
// In all other cases, return what's requested, and only that (threaded
281-
// receipts).
282235
match self.load_event_receipts(ReceiptType::Read, receipt_thread.clone(), event_id).await {
283236
Ok(receipts) => receipts.into_iter().collect(),
284237
Err(e) => {

0 commit comments

Comments
 (0)