Skip to content

Commit

Permalink
timeline: move populate_initial_user_receipt to TimelineInnerState
Browse files Browse the repository at this point in the history
This makes the API less weird, and is more consistent with other read
receipts methods.
  • Loading branch information
bnjbvr committed Apr 23, 2024
1 parent ec45ce3 commit 9547e3c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 30 deletions.
33 changes: 4 additions & 29 deletions crates/matrix-sdk-ui/src/timeline/inner/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,33 +381,6 @@ impl<P: RoomDataProvider> TimelineInner<P> {
Ok(result)
}

/// Populates our own latest read receipt in the in-memory by-user read
/// receipt cache.
pub(super) async fn populate_initial_user_receipt(
&self,
state: &mut TimelineInnerState,
receipt_type: ReceiptType,
) {
let own_user_id = self.room_data_provider.own_user_id().to_owned();

let mut read_receipt = self
.room_data_provider
.load_user_receipt(receipt_type.clone(), ReceiptThread::Unthreaded, &own_user_id)
.await;

// Fallback to the one in the main thread.
if read_receipt.is_none() {
read_receipt = self
.room_data_provider
.load_user_receipt(receipt_type.clone(), ReceiptThread::Main, &own_user_id)
.await;
}

if let Some(read_receipt) = read_receipt {
state.meta.read_receipts.upsert_latest(own_user_id, receipt_type, read_receipt);
}
}

/// Handle a list of events at the given end of the timeline.
///
/// Note: when the `position` is [`TimelineEnd::Front`], prepended events
Expand Down Expand Up @@ -446,8 +419,10 @@ impl<P: RoomDataProvider> TimelineInner<P> {

let track_read_markers = self.settings.track_read_receipts;
if track_read_markers {
self.populate_initial_user_receipt(&mut *state, ReceiptType::Read).await;
self.populate_initial_user_receipt(&mut *state, ReceiptType::ReadPrivate).await;
state.populate_initial_user_receipt(&self.room_data_provider, ReceiptType::Read).await;
state
.populate_initial_user_receipt(&self.room_data_provider, ReceiptType::ReadPrivate)
.await;
}

if !events.is_empty() {
Expand Down
27 changes: 26 additions & 1 deletion crates/matrix-sdk-ui/src/timeline/read_receipts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl ReadReceipts {

/// Insert or update in the local cache the latest read receipt for the
/// given user.
pub fn upsert_latest(
fn upsert_latest(
&mut self,
user_id: OwnedUserId,
receipt_type: ReceiptType,
Expand Down Expand Up @@ -493,6 +493,31 @@ impl TimelineInnerStateTransaction<'_> {
}

impl TimelineInnerState {
/// Populates our own latest read receipt in the in-memory by-user read
/// receipt cache.
pub(super) async fn populate_initial_user_receipt<P: RoomDataProvider>(
&mut self,
room_data_provider: &P,
receipt_type: ReceiptType,
) {
let own_user_id = room_data_provider.own_user_id().to_owned();

let mut read_receipt = room_data_provider
.load_user_receipt(receipt_type.clone(), ReceiptThread::Unthreaded, &own_user_id)
.await;

// Fallback to the one in the main thread.
if read_receipt.is_none() {
read_receipt = room_data_provider
.load_user_receipt(receipt_type.clone(), ReceiptThread::Main, &own_user_id)
.await;
}

if let Some(read_receipt) = read_receipt {
self.meta.read_receipts.upsert_latest(own_user_id, receipt_type, read_receipt);
}
}

/// Get the latest read receipt for the given user.
///
/// Useful to get the latest read receipt, whether it's private or public.
Expand Down

0 comments on commit 9547e3c

Please sign in to comment.