Skip to content

Commit 1c7d1b6

Browse files
committed
feat(ui): latest_event sorter handles LatestEventValue::LocalHasBeenSent.
This patch changes the semantics of the Room List `latest_event` sorter by changing “is local” to “is remote like”, to include the new `LatestEventValue::LocalHasBeenSent` variant.
1 parent 14197e5 commit 1c7d1b6

File tree

4 files changed

+102
-74
lines changed

4 files changed

+102
-74
lines changed

crates/matrix-sdk-base/src/latest_event.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,19 @@ impl LatestEventValue {
6565
}
6666
}
6767

68+
/// Check whether an event is remote-like, i.e. has been sent to a
69+
/// homeserver (by this client, or another), i.e. is **not**
70+
/// [`LocalIsSending`] nor [`LocalCannotBeSent`].
71+
///
72+
/// [`LocalIsSending`]: LatestEventValue::LocalIsSending
73+
/// [`LocalCannotBeSent`]: LatestEventValue::LocalCannotBeSent
74+
pub fn is_remote_like(&self) -> bool {
75+
match self {
76+
Self::LocalIsSending(_) | Self::LocalCannotBeSent(_) => false,
77+
Self::LocalHasBeenSent(_) | Self::Remote(_) | Self::None => true,
78+
}
79+
}
80+
6881
/// Check whether the [`LatestEventValue`] is not set, i.e. [`None`].
6982
///
7083
/// [`None`]: LatestEventValue::None

crates/matrix-sdk-base/src/room/latest_event.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ impl Room {
3131
self.info.read().latest_event_value.timestamp()
3232
}
3333

34-
/// Return the value of [`LatestEventValue::is_local`].
35-
pub fn latest_event_is_local(&self) -> bool {
36-
self.info.read().latest_event_value.is_local()
34+
/// Return the value of [`LatestEventValue::is_remote_like`].
35+
pub fn latest_event_is_remote_like(&self) -> bool {
36+
self.info.read().latest_event_value.is_remote_like()
3737
}
3838
}

crates/matrix-sdk-ui/src/room_list_service/room_list.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -423,8 +423,8 @@ pub struct RoomListItem {
423423
/// Cache of `Room::latest_event_timestamp`.
424424
pub(super) cached_latest_event_timestamp: Option<MilliSecondsSinceUnixEpoch>,
425425

426-
/// Cache of `Room::latest_event_is_local`.
427-
pub(super) cached_latest_event_is_local: bool,
426+
/// Cache of `Room::latest_event_is_remote_like`.
427+
pub(super) cached_latest_event_is_remote_like: bool,
428428

429429
/// Cache of `Room::recency_stamp`.
430430
pub(super) cached_recency_stamp: Option<RoomRecencyStamp>,
@@ -448,7 +448,7 @@ impl RoomListItem {
448448
/// Refresh the cached data.
449449
pub(super) fn refresh_cached_data(&mut self) {
450450
self.cached_latest_event_timestamp = self.inner.latest_event_timestamp();
451-
self.cached_latest_event_is_local = self.inner.latest_event_is_local();
451+
self.cached_latest_event_is_remote_like = self.inner.latest_event_is_remote_like();
452452
self.cached_recency_stamp = self.inner.recency_stamp();
453453
self.cached_display_name = self.inner.cached_display_name().map(|name| name.to_string());
454454
self.cached_is_space = self.inner.is_space();
@@ -459,7 +459,7 @@ impl RoomListItem {
459459
impl From<Room> for RoomListItem {
460460
fn from(inner: Room) -> Self {
461461
let cached_latest_event_timestamp = inner.latest_event_timestamp();
462-
let cached_latest_event_is_local = inner.latest_event_is_local();
462+
let cached_latest_event_is_remote_like = inner.latest_event_is_remote_like();
463463
let cached_recency_stamp = inner.recency_stamp();
464464
let cached_display_name = inner.cached_display_name().map(|name| name.to_string());
465465
let cached_is_space = inner.is_space();
@@ -468,7 +468,7 @@ impl From<Room> for RoomListItem {
468468
Self {
469469
inner,
470470
cached_latest_event_timestamp,
471-
cached_latest_event_is_local,
471+
cached_latest_event_is_remote_like,
472472
cached_recency_stamp,
473473
cached_display_name,
474474
cached_is_space,

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

Lines changed: 81 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,22 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
use std::cmp::Ordering;
15+
use std::{cmp::Ordering, ops::Not};
1616

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

19-
fn cmp<F>(are_latest_events_locals: F, left: &RoomListItem, right: &RoomListItem) -> Ordering
19+
fn cmp<F>(
20+
are_latest_events_not_remote_like: F,
21+
left: &RoomListItem,
22+
right: &RoomListItem,
23+
) -> Ordering
2024
where
2125
F: Fn(&RoomListItem, &RoomListItem) -> (bool, bool),
2226
{
2327
// We want local latest event to come first. When there is a remote latest event
2428
// or no latest event, we don't want to sort them.
2529
// NOTE: This is the same as a.cmp(b).reverse() for booleans.
26-
match are_latest_events_locals(left, right) {
30+
match are_latest_events_not_remote_like(left, right) {
2731
// `false` and `false`, i.e.:
2832
// - `None` == `None`.
2933
// - `None` == `Remote`.
@@ -61,9 +65,12 @@ pub fn new_sorter() -> impl Sorter {
6165
// Be careful. This method is called **a lot** in the context of a sorter. Using
6266
// `Room::latest_event` would be dramatic as it returns a clone of the
6367
// `LatestEventValue`. It's better to use the more specific method
64-
// `Room::latest_event_is_local`, where the value is cached in this module's
65-
// `Room` type.
66-
(left.cached_latest_event_is_local, right.cached_latest_event_is_local)
68+
// `Room::latest_event_is_remote_like`, where the value is cached in
69+
// `RoomListItem`.
70+
(
71+
left.cached_latest_event_is_remote_like.not(),
72+
right.cached_latest_event_is_remote_like.not(),
73+
)
6774
};
6875

6976
move |left, right| -> Ordering { cmp(latest_events, left, right) }
@@ -119,6 +126,16 @@ mod tests {
119126
})
120127
}
121128

129+
fn local_has_been_sent() -> LatestEventValue {
130+
LatestEventValue::LocalHasBeenSent(LocalLatestEventValue {
131+
timestamp: MilliSecondsSinceUnixEpoch(uint!(42)),
132+
content: SerializableEventContent::new(&AnyMessageLikeEventContent::RoomMessage(
133+
RoomMessageEventContent::text_plain("raclette"),
134+
))
135+
.unwrap(),
136+
})
137+
}
138+
122139
fn local_cannot_be_sent() -> LatestEventValue {
123140
LatestEventValue::LocalCannotBeSent(LocalLatestEventValue {
124141
timestamp: MilliSecondsSinceUnixEpoch(uint!(42)),
@@ -129,6 +146,10 @@ mod tests {
129146
})
130147
}
131148

149+
fn pair(left: LatestEventValue, right: LatestEventValue) -> (bool, bool) {
150+
(left.is_remote_like().not(), right.is_remote_like().not())
151+
}
152+
132153
#[async_test]
133154
async fn test_none_or_remote_and_none_or_remote() {
134155
let (client, server) = logged_in_client_with_server().await;
@@ -138,34 +159,22 @@ mod tests {
138159

139160
// `None` and `None`.
140161
{
141-
assert_eq!(
142-
cmp(|_, _| (none().is_local(), none().is_local()), &room_a, &room_b),
143-
Ordering::Equal
144-
);
162+
assert_eq!(cmp(|_, _| pair(none(), none()), &room_a, &room_b), Ordering::Equal);
145163
}
146164

147165
// `None` and `Remote`.
148166
{
149-
assert_eq!(
150-
cmp(|_, _| (none().is_local(), remote().is_local()), &room_a, &room_b),
151-
Ordering::Equal
152-
);
167+
assert_eq!(cmp(|_, _| pair(none(), remote()), &room_a, &room_b), Ordering::Equal);
153168
}
154169

155170
// `Remote` and `None`.
156171
{
157-
assert_eq!(
158-
cmp(|_, _| (remote().is_local(), none().is_local()), &room_a, &room_b),
159-
Ordering::Equal
160-
);
172+
assert_eq!(cmp(|_, _| pair(remote(), none()), &room_a, &room_b), Ordering::Equal);
161173
}
162174

163175
// `Remote` and `None`.
164176
{
165-
assert_eq!(
166-
cmp(|_, _| (remote().is_local(), remote().is_local()), &room_a, &room_b),
167-
Ordering::Equal
168-
);
177+
assert_eq!(cmp(|_, _| pair(remote(), remote()), &room_a, &room_b), Ordering::Equal);
169178
}
170179
}
171180

@@ -179,31 +188,31 @@ mod tests {
179188
// `None` and `Local*`.
180189
{
181190
assert_eq!(
182-
cmp(|_, _| (none().is_local(), local_is_sending().is_local()), &room_a, &room_b),
191+
cmp(|_, _| pair(none(), local_is_sending()), &room_a, &room_b),
183192
Ordering::Greater
184193
);
185194
assert_eq!(
186-
cmp(
187-
|_, _| (none().is_local(), local_cannot_be_sent().is_local()),
188-
&room_a,
189-
&room_b
190-
),
195+
cmp(|_, _| pair(none(), local_has_been_sent()), &room_a, &room_b),
196+
Ordering::Equal
197+
);
198+
assert_eq!(
199+
cmp(|_, _| pair(none(), local_cannot_be_sent()), &room_a, &room_b),
191200
Ordering::Greater
192201
);
193202
}
194203

195204
// `Remote` and `Local*`.
196205
{
197206
assert_eq!(
198-
cmp(|_, _| (remote().is_local(), local_is_sending().is_local()), &room_a, &room_b),
207+
cmp(|_, _| pair(remote(), local_is_sending()), &room_a, &room_b),
199208
Ordering::Greater
200209
);
201210
assert_eq!(
202-
cmp(
203-
|_, _| (remote().is_local(), local_cannot_be_sent().is_local()),
204-
&room_a,
205-
&room_b
206-
),
211+
cmp(|_, _| pair(remote(), local_has_been_sent()), &room_a, &room_b),
212+
Ordering::Equal
213+
);
214+
assert_eq!(
215+
cmp(|_, _| pair(remote(), local_cannot_be_sent()), &room_a, &room_b),
207216
Ordering::Greater
208217
);
209218
}
@@ -219,31 +228,31 @@ mod tests {
219228
// `Local*` and `None`.
220229
{
221230
assert_eq!(
222-
cmp(|_, _| (local_is_sending().is_local(), none().is_local()), &room_a, &room_b),
231+
cmp(|_, _| pair(local_is_sending(), none()), &room_a, &room_b),
223232
Ordering::Less
224233
);
225234
assert_eq!(
226-
cmp(
227-
|_, _| (local_cannot_be_sent().is_local(), none().is_local()),
228-
&room_a,
229-
&room_b
230-
),
235+
cmp(|_, _| pair(local_has_been_sent(), none()), &room_a, &room_b),
236+
Ordering::Equal
237+
);
238+
assert_eq!(
239+
cmp(|_, _| pair(local_cannot_be_sent(), none()), &room_a, &room_b),
231240
Ordering::Less
232241
);
233242
}
234243

235244
// `Local*` and `Remote`.
236245
{
237246
assert_eq!(
238-
cmp(|_, _| (local_is_sending().is_local(), remote().is_local()), &room_a, &room_b),
247+
cmp(|_, _| pair(local_is_sending(), remote()), &room_a, &room_b),
239248
Ordering::Less
240249
);
241250
assert_eq!(
242-
cmp(
243-
|_, _| (local_cannot_be_sent().is_local(), remote().is_local()),
244-
&room_a,
245-
&room_b
246-
),
251+
cmp(|_, _| pair(local_has_been_sent(), remote()), &room_a, &room_b),
252+
Ordering::Equal
253+
);
254+
assert_eq!(
255+
cmp(|_, _| pair(local_cannot_be_sent(), remote()), &room_a, &room_b),
247256
Ordering::Less
248257
);
249258
}
@@ -259,35 +268,41 @@ mod tests {
259268
// `Local*` and `Local*`.
260269
{
261270
assert_eq!(
262-
cmp(
263-
|_, _| (local_is_sending().is_local(), local_is_sending().is_local()),
264-
&room_a,
265-
&room_b
266-
),
271+
cmp(|_, _| pair(local_is_sending(), local_is_sending()), &room_a, &room_b),
267272
Ordering::Equal
268273
);
269274
assert_eq!(
270-
cmp(
271-
|_, _| (local_is_sending().is_local(), local_cannot_be_sent().is_local()),
272-
&room_a,
273-
&room_b
274-
),
275+
cmp(|_, _| pair(local_is_sending(), local_has_been_sent()), &room_a, &room_b),
276+
Ordering::Less
277+
);
278+
assert_eq!(
279+
cmp(|_, _| pair(local_is_sending(), local_cannot_be_sent()), &room_a, &room_b),
275280
Ordering::Equal
276281
);
282+
277283
assert_eq!(
278-
cmp(
279-
|_, _| (local_cannot_be_sent().is_local(), local_is_sending().is_local()),
280-
&room_a,
281-
&room_b
282-
),
284+
cmp(|_, _| pair(local_has_been_sent(), local_is_sending()), &room_a, &room_b),
285+
Ordering::Greater
286+
);
287+
assert_eq!(
288+
cmp(|_, _| pair(local_has_been_sent(), local_has_been_sent()), &room_a, &room_b),
289+
Ordering::Equal
290+
);
291+
assert_eq!(
292+
cmp(|_, _| pair(local_has_been_sent(), local_cannot_be_sent()), &room_a, &room_b),
293+
Ordering::Greater
294+
);
295+
296+
assert_eq!(
297+
cmp(|_, _| pair(local_cannot_be_sent(), local_is_sending()), &room_a, &room_b),
283298
Ordering::Equal
284299
);
285300
assert_eq!(
286-
cmp(
287-
|_, _| (local_cannot_be_sent().is_local(), local_cannot_be_sent().is_local()),
288-
&room_a,
289-
&room_b
290-
),
301+
cmp(|_, _| pair(local_cannot_be_sent(), local_has_been_sent()), &room_a, &room_b),
302+
Ordering::Less
303+
);
304+
assert_eq!(
305+
cmp(|_, _| pair(local_cannot_be_sent(), local_cannot_be_sent()), &room_a, &room_b),
291306
Ordering::Equal
292307
);
293308
}

0 commit comments

Comments
 (0)