Skip to content

Commit 659ae57

Browse files
committed
fix(ui): room_list::sorters::recency is no longer based on 2 data.
This patch fixes an issue where the `recency` sorter is based on either the latest event's timestamp, or the room recency stamp. This cannot work with a sort algorithm as the position of a particular room can be different based on what it is compared to (i.e. if the rooms have a latest event value or not). This patch updates the `recency` sorter to only use the recency stamp for now, as the latest event is not yet computed for all rooms.
1 parent dff6cb4 commit 659ae57

File tree

1 file changed

+20
-39
lines changed
  • crates/matrix-sdk-ui/src/room_list_service/sorters

1 file changed

+20
-39
lines changed

crates/matrix-sdk-ui/src/room_list_service/sorters/recency.rs

Lines changed: 20 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ use std::cmp::Ordering;
1616

1717
use super::{RoomListItem, Sorter};
1818

19-
fn cmp<F>(ranks: F, left: &RoomListItem, right: &RoomListItem) -> Ordering
19+
fn cmp<F>(timestamps: F, left: &RoomListItem, right: &RoomListItem) -> Ordering
2020
where
2121
F: Fn(&RoomListItem, &RoomListItem) -> (Option<Rank>, Option<Rank>),
2222
{
23-
match ranks(left, right) {
24-
(Some(left_rank), Some(right_rank)) => left_rank.cmp(&right_rank).reverse(),
23+
match timestamps(left, right) {
24+
(Some(left), Some(right)) => left.cmp(&right).reverse(),
2525

2626
(Some(_), None) => Ordering::Less,
2727

@@ -51,48 +51,23 @@ pub fn new_sorter() -> impl Sorter {
5151
/// stamp of the room). This type hides `u64` for the sake of semantics.
5252
type Rank = u64;
5353

54-
/// Extract the recency _rank_ from either the [`RoomInfo::new_latest_event`] or
55-
/// from [`RoomInfo::recency_stamp`].
56-
///
57-
/// We must be very careful to return data of the same nature: either a
58-
/// _rank_ from the [`LatestEventValue`]'s timestamp, or from the
59-
/// [`RoomInfo::recency_stamp`], but we **must never** mix both. The
60-
/// `RoomInfo::recency_stamp` is not a timestamp, while `LatestEventValue` uses
61-
/// a timestamp.
54+
/// Extract the recency _rank_ from the [`RoomInfo::recency_stamp`].
55+
// TODO @hywan: We must update this method to handle the latest event's
56+
// timestamp instead of the recency stamp.
6257
fn extract_rank(left: &RoomListItem, right: &RoomListItem) -> (Option<Rank>, Option<Rank>) {
63-
// Be careful. This method is called **a lot** in the context of a sorter. Using
64-
// `Room::new_latest_event` would be dramatic as it returns a clone of the
65-
// `LatestEventValue`. It's better to use the more specific method
66-
// `Room::new_latest_event_timestamp`, where the value is cached in this
67-
// module's `Room` type.
68-
match (left.cached_latest_event_timestamp, right.cached_latest_event_timestamp) {
69-
// One of the two rooms has no latest event timestamp. Let's fallback to
70-
// the recency stamp from the `RoomInfo` for both room.
71-
(None, _) | (_, None) => {
72-
(left.cached_recency_stamp.map(Into::into), right.cached_recency_stamp.map(Into::into))
73-
}
74-
75-
// Both rooms have a timestamp. We can use them as a rank.
76-
(Some(left), Some(right)) => (Some(left.get().into()), Some(right.get().into())),
77-
}
58+
(left.cached_recency_stamp.map(Into::into), right.cached_recency_stamp.map(Into::into))
7859
}
7960

8061
#[cfg(test)]
8162
mod tests {
8263
use matrix_sdk::{
8364
RoomRecencyStamp,
84-
latest_events::{LatestEventValue, LocalLatestEventValue, RemoteLatestEventValue},
85-
store::SerializableEventContent,
65+
latest_events::{LatestEventValue, RemoteLatestEventValue},
8666
test_utils::logged_in_client_with_server,
8767
};
8868
use matrix_sdk_base::RoomInfoNotableUpdateReasons;
8969
use matrix_sdk_test::async_test;
90-
use ruma::{
91-
MilliSecondsSinceUnixEpoch,
92-
events::{AnyMessageLikeEventContent, room::message::RoomMessageEventContent},
93-
room_id,
94-
serde::Raw,
95-
};
70+
use ruma::{events::room::message::RoomMessageEventContent, room_id, serde::Raw};
9671
use serde_json::json;
9772

9873
use super::{super::super::filters::new_rooms, *};
@@ -118,6 +93,8 @@ mod tests {
11893
))
11994
}
12095

96+
// TODO @hywan: restore this once `extract_rank` works on latest event's value
97+
/*
12198
fn local_is_sending(origin_server_ts: u32) -> LatestEventValue {
12299
LatestEventValue::LocalIsSending(LocalLatestEventValue {
123100
timestamp: MilliSecondsSinceUnixEpoch(origin_server_ts.into()),
@@ -143,6 +120,7 @@ mod tests {
143120
),
144121
})
145122
}
123+
*/
146124

147125
fn set_latest_event_value(room: &mut RoomListItem, latest_event_value: LatestEventValue) {
148126
let mut room_info = room.clone_info();
@@ -159,7 +137,7 @@ mod tests {
159137
}
160138

161139
#[async_test]
162-
async fn test_extract_recency_stamp_with_none() {
140+
async fn test_extract_rank_with_none() {
163141
let (client, server) = logged_in_client_with_server().await;
164142
let [mut room_a, mut room_b] =
165143
new_rooms([room_id!("!a:b.c"), room_id!("!d:e.f")], &client, &server).await;
@@ -192,8 +170,10 @@ mod tests {
192170
}
193171
}
194172

173+
// TODO @hywan: restore this once `extract_rank` works on latest event's value
174+
/*
195175
#[async_test]
196-
async fn test_extract_recency_stamp_with_remote_or_local() {
176+
async fn test_extract_rank_with_remote_or_local() {
197177
let (client, server) = logged_in_client_with_server().await;
198178
let [mut room_a, mut room_b] =
199179
new_rooms([room_id!("!a:b.c"), room_id!("!d:e.f")], &client, &server).await;
@@ -215,9 +195,10 @@ mod tests {
215195
}
216196
}
217197
}
198+
*/
218199

219200
#[async_test]
220-
async fn test_with_two_recency_stamps() {
201+
async fn test_with_two_ranks() {
221202
let (client, server) = logged_in_client_with_server().await;
222203
let [room_a, room_b] =
223204
new_rooms([room_id!("!a:b.c"), room_id!("!d:e.f")], &client, &server).await;
@@ -244,7 +225,7 @@ mod tests {
244225
}
245226

246227
#[async_test]
247-
async fn test_with_one_recency_stamp() {
228+
async fn test_with_one_rank() {
248229
let (client, server) = logged_in_client_with_server().await;
249230
let [room_a, room_b] =
250231
new_rooms([room_id!("!a:b.c"), room_id!("!d:e.f")], &client, &server).await;
@@ -261,7 +242,7 @@ mod tests {
261242
}
262243

263244
#[async_test]
264-
async fn test_with_zero_recency_stamp() {
245+
async fn test_with_zero_rank() {
265246
let (client, server) = logged_in_client_with_server().await;
266247
let [room_a, room_b] =
267248
new_rooms([room_id!("!a:b.c"), room_id!("!d:e.f")], &client, &server).await;

0 commit comments

Comments
 (0)