matrix-sdk-ui 0.17.0

GUI-centric utilities on top of matrix-rust-sdk (experimental).
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
// Copyright 2023 The Matrix.org Foundation C.I.C.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use assert_matches::assert_matches;
use assert_matches2::assert_let;
use eyeball_im::VectorDiff;
use futures_util::StreamExt;
use imbl::vector;
use matrix_sdk::{assert_next_with_timeout, test_utils::mocks::MatrixMockServer};
use matrix_sdk_base::ThreadingSupport;
use matrix_sdk_test::{
    ALICE, BOB, CAROL, JoinedRoomBuilder, async_test,
    event_factory::{EventFactory, PreviousMembership},
};
use ruma::{
    MilliSecondsSinceUnixEpoch, event_id,
    events::{
        StateEventContentChange,
        receipt::{Receipt, ReceiptThread, ReceiptType},
        room::{
            ImageInfo,
            member::{MembershipState, RedactedRoomMemberEventContent},
            message::MessageType,
            topic::RedactedRoomTopicEventContent,
        },
    },
    mxc_uri, owned_event_id, owned_mxc_uri, room_id, user_id,
};
use stream_assert::{assert_next_matches, assert_pending};

use super::TestTimeline;
use crate::timeline::{
    MembershipChange, MsgLikeContent, MsgLikeKind, RoomExt, TimelineDetails, TimelineFocus,
    TimelineItemContent, TimelineItemKind, TimelineReadReceiptTracking, VirtualTimelineItem,
    controller::TimelineSettings,
    event_item::{AnyOtherStateEventContentChange, RemoteEventOrigin},
    tests::{ReadReceiptMap, TestRoomDataProvider, TestTimelineBuilder},
};

#[async_test]
async fn test_initial_events() {
    let timeline = TestTimeline::new();
    let mut stream = timeline.subscribe().await;

    let f = &timeline.factory;
    timeline
        .controller
        .handle_remote_events_with_diffs(
            vec![VectorDiff::Append {
                values: vector![
                    f.text_msg("A").sender(*ALICE).into_event(),
                    f.text_msg("B").sender(*BOB).into_event()
                ],
            }],
            RemoteEventOrigin::Sync,
        )
        .await;

    let item = assert_next_matches!(stream, VectorDiff::PushBack { value } => value);
    assert_eq!(item.as_event().unwrap().sender(), *ALICE);

    let item = assert_next_matches!(stream, VectorDiff::PushBack { value } => value);
    assert_eq!(item.as_event().unwrap().sender(), *BOB);

    let item = assert_next_matches!(stream, VectorDiff::PushFront { value } => value);
    assert_matches!(&item.kind, TimelineItemKind::Virtual(VirtualTimelineItem::DateDivider(_)));
}

#[async_test]
async fn test_replace_with_initial_events_and_read_marker() {
    let event_id = owned_event_id!("$1");

    // Add a read receipt for the logged in user (Alice).
    let mut receipts = ReadReceiptMap::default();
    receipts
        .entry(ReceiptType::Read)
        .or_default()
        .entry(ReceiptThread::Unthreaded)
        .or_default()
        .entry(ALICE.to_owned())
        .or_insert_with(|| (event_id.to_owned(), Receipt::new(MilliSecondsSinceUnixEpoch::now())));

    let timeline = TestTimelineBuilder::new()
        .provider(
            TestRoomDataProvider::default()
                // Also add a fully read marker.
                .with_fully_read_marker(event_id)
                .with_initial_user_receipts(receipts),
        )
        .settings(TimelineSettings {
            track_read_receipts: TimelineReadReceiptTracking::AllEvents,
            ..Default::default()
        })
        .build();

    let f = &timeline.factory;
    let ev = f.text_msg("hey").sender(*ALICE).into_event();

    timeline
        .controller
        .handle_remote_events_with_diffs(
            vec![VectorDiff::Append { values: vector![ev] }],
            RemoteEventOrigin::Sync,
        )
        .await;

    let items = timeline.controller.items().await;
    assert_eq!(items.len(), 2);
    assert!(items[0].is_date_divider());
    assert_eq!(items[1].as_event().unwrap().content().as_message().unwrap().body(), "hey");

    let ev = f.text_msg("yo").sender(*BOB).into_event();
    timeline.controller.replace_with_initial_remote_events([ev], RemoteEventOrigin::Sync).await;

    let items = timeline.controller.items().await;
    assert_eq!(items.len(), 2);
    assert!(items[0].is_date_divider());
    assert_eq!(items[1].as_event().unwrap().content().as_message().unwrap().body(), "yo");
}

#[async_test]
async fn test_sticker() {
    let timeline = TestTimeline::new();
    let mut stream = timeline.subscribe_events().await;

    timeline
        .handle_live_event(
            EventFactory::new()
                .sticker(
                    "Happy sticker",
                    ImageInfo::new(),
                    owned_mxc_uri!("mxc://server.name/JWEIFJgwEIhweiWJE"),
                )
                .reply_thread(event_id!("$thread_root"), event_id!("$in_reply_to"))
                .event_id(event_id!("$143273582443PhrSn"))
                .sender(user_id!("@alice:server.name"))
                .into_event(),
        )
        .await;

    let item = assert_next_matches!(stream, VectorDiff::PushBack { value } => value);
    assert!(item.content().is_sticker());

    assert_eq!(item.content().thread_root(), Some(owned_event_id!("$thread_root")));

    assert_let!(Some(details) = item.content().in_reply_to());
    assert_eq!(details.event_id, owned_event_id!("$in_reply_to"))
}

#[async_test]
async fn test_room_member() {
    let timeline = TestTimeline::new();
    let mut stream = timeline.subscribe_events().await;

    // Bob invites Alice.
    let f = &timeline.factory;
    timeline.handle_live_event(f.member(&BOB).invited(&ALICE).display_name("Alice")).await;

    let item = assert_next_matches!(stream, VectorDiff::PushBack { value } => value);
    assert!(item.can_be_replied_to());
    assert_let!(TimelineItemContent::MembershipChange(membership) = item.content());
    assert_matches!(membership.content(), StateEventContentChange::Original { .. });
    assert_matches!(membership.change(), Some(MembershipChange::Invited));

    timeline
        .handle_live_event(
            f.member(&ALICE)
                .membership(MembershipState::Join)
                .display_name("Alice")
                .previous(PreviousMembership::new(MembershipState::Invite).display_name("Alice")),
        )
        .await;

    let item = assert_next_matches!(stream, VectorDiff::PushBack { value } => value);
    assert_let!(TimelineItemContent::MembershipChange(membership) = item.content());
    assert_matches!(membership.content(), StateEventContentChange::Original { .. });
    assert_matches!(membership.change(), Some(MembershipChange::InvitationAccepted));

    timeline
        .handle_live_event(
            f.member(&ALICE)
                .membership(MembershipState::Join)
                .display_name("Alice In Wonderland")
                .previous(PreviousMembership::new(MembershipState::Join).display_name("Alice")),
        )
        .await;

    let item = assert_next_matches!(stream, VectorDiff::PushBack { value } => value);
    assert_let!(TimelineItemContent::ProfileChange(profile) = item.content());
    assert_matches!(profile.displayname_change(), Some(_));
    assert_matches!(profile.avatar_url_change(), None);

    timeline
        .handle_live_event(
            f.member(&ALICE)
                .membership(MembershipState::Join)
                .display_name("Alice In Wonderland")
                .avatar_url(mxc_uri!("mxc://lolcathost.io/abc"))
                .previous(
                    PreviousMembership::new(MembershipState::Join)
                        .display_name("Alice In Wonderland"),
                ),
        )
        .await;

    let item = assert_next_matches!(stream, VectorDiff::PushBack { value } => value);
    assert_let!(TimelineItemContent::ProfileChange(profile) = item.content());
    assert_matches!(profile.displayname_change(), None);
    assert_matches!(profile.avatar_url_change(), Some(_));

    {
        // No avatar or display name in the new room member event content, but it's
        // possible to get the previous one using the getters.
        timeline
            .handle_live_event(
                f.member(&ALICE).membership(MembershipState::Leave).previous(
                    PreviousMembership::new(MembershipState::Join)
                        .display_name("Alice In Wonderland")
                        .avatar_url(mxc_uri!("mxc://lolcathost.io/abc")),
                ),
            )
            .await;

        let item = assert_next_matches!(stream, VectorDiff::PushBack { value } => value);
        assert_let!(TimelineItemContent::MembershipChange(membership) = item.content());
        assert_matches!(membership.display_name().as_deref(), Some("Alice In Wonderland"));
        assert_matches!(
            membership.avatar_url().map(|url| url.to_string()).as_deref(),
            Some("mxc://lolcathost.io/abc")
        );
        assert_matches!(membership.content(), StateEventContentChange::Original { .. });
        assert_matches!(membership.change(), Some(MembershipChange::Left));
    }

    timeline
        .handle_live_event(f.redacted_state(
            &ALICE,
            ALICE.as_str(),
            RedactedRoomMemberEventContent::new(MembershipState::Join),
        ))
        .await;

    let item = assert_next_matches!(stream, VectorDiff::PushBack { value } => value);
    assert_let!(TimelineItemContent::MembershipChange(membership) = item.content());
    assert_matches!(membership.content(), StateEventContentChange::Redacted(_));
    assert_matches!(membership.change(), None);
}

#[async_test]
async fn test_other_state() {
    let timeline = TestTimeline::new();
    let mut stream = timeline.subscribe().await;

    let f = &timeline.factory;
    timeline.handle_live_event(f.room_name("Alice's room").sender(&ALICE)).await;

    let item = assert_next_matches!(stream, VectorDiff::PushBack { value } => value);
    assert_let!(TimelineItemContent::OtherState(ev) = item.as_event().unwrap().content());
    assert_let!(AnyOtherStateEventContentChange::RoomName(full_content) = ev.content());
    assert_let!(StateEventContentChange::Original { content, prev_content } = full_content);
    assert_eq!(content.name, "Alice's room");
    assert_matches!(prev_content, None);

    let date_divider = assert_next_matches!(stream, VectorDiff::PushFront { value } => value);
    assert!(date_divider.is_date_divider());

    timeline
        .handle_live_event(f.redacted_state(&ALICE, "", RedactedRoomTopicEventContent::new()))
        .await;

    let item = assert_next_matches!(stream, VectorDiff::PushBack { value } => value);
    assert_let!(TimelineItemContent::OtherState(ev) = item.as_event().unwrap().content());
    assert_let!(AnyOtherStateEventContentChange::RoomTopic(full_content) = ev.content());
    assert_matches!(full_content, StateEventContentChange::Redacted(_));
}

#[async_test]
async fn test_internal_id_prefix() {
    let timeline = TestTimelineBuilder::new().internal_id_prefix("le_prefix_".to_owned()).build();

    let f = &timeline.factory;
    let ev_a = f.text_msg("A").sender(*ALICE).into_event();
    let ev_b = f.text_msg("B").sender(*BOB).into_event();
    let ev_c = f.text_msg("C").sender(*CAROL).into_event();

    timeline
        .controller
        .handle_remote_events_with_diffs(
            vec![VectorDiff::Append { values: vector![ev_a, ev_b, ev_c] }],
            RemoteEventOrigin::Sync,
        )
        .await;

    let timeline_items = timeline.controller.items().await;
    assert_eq!(timeline_items.len(), 4);

    assert!(timeline_items[0].is_date_divider());
    assert_eq!(timeline_items[0].unique_id().0, "le_prefix_3");

    let event1 = &timeline_items[1];
    assert_eq!(event1.as_event().unwrap().sender(), *ALICE);
    assert_eq!(event1.unique_id().0, "le_prefix_0");

    let event2 = &timeline_items[2];
    assert_eq!(event2.as_event().unwrap().sender(), *BOB);
    assert_eq!(event2.unique_id().0, "le_prefix_1");

    let event3 = &timeline_items[3];
    assert_eq!(event3.as_event().unwrap().sender(), *CAROL);
    assert_eq!(event3.unique_id().0, "le_prefix_2");
}

#[async_test]
async fn test_internal_id_reuse() {
    let timeline = TestTimelineBuilder::new().build();

    let f = &timeline.factory;
    let ev_a = f.text_msg("A").sender(*ALICE).into_event();
    let ev_b = f.text_msg("B").sender(*BOB).into_event();
    let ev_c = f.text_msg("C").sender(*CAROL).into_event();

    // First, push three events.
    timeline
        .controller
        .handle_remote_events_with_diffs(
            vec![VectorDiff::Append { values: vector![ev_a, ev_b, ev_c.clone()] }],
            RemoteEventOrigin::Sync,
        )
        .await;

    let timeline_items = timeline.controller.items().await;
    assert_eq!(timeline_items.len(), 4);

    assert!(timeline_items[0].is_date_divider());
    assert_eq!(timeline_items[0].unique_id().0, "3");

    let event1 = &timeline_items[1];
    assert_eq!(event1.as_event().unwrap().sender(), *ALICE);
    assert_eq!(event1.unique_id().0, "0");

    let event2 = &timeline_items[2];
    assert_eq!(event2.as_event().unwrap().sender(), *BOB);
    assert_eq!(event2.unique_id().0, "1");

    let event3 = &timeline_items[3];
    assert_eq!(event3.as_event().unwrap().sender(), *CAROL);
    assert_eq!(event3.unique_id().0, "2");

    // Then, handle a deduplication (removal then reinsertion of the same event).
    timeline
        .controller
        .handle_remote_events_with_diffs(
            vec![
                // Note: indices are in the *event* array index space, not in the *timeline item*
                // array index space.
                VectorDiff::Remove { index: 2 },
                VectorDiff::Insert { index: 2, value: ev_c },
            ],
            RemoteEventOrigin::Sync,
        )
        .await;

    let timeline_items = timeline.controller.items().await;
    assert_eq!(timeline_items.len(), 4);

    // The IDs haven't changed.
    assert!(timeline_items[0].is_date_divider());
    assert_eq!(timeline_items[0].unique_id().0, "3");

    let event1 = &timeline_items[1];
    assert_eq!(event1.as_event().unwrap().sender(), *ALICE);
    assert_eq!(event1.unique_id().0, "0");

    let event2 = &timeline_items[2];
    assert_eq!(event2.as_event().unwrap().sender(), *BOB);
    assert_eq!(event2.unique_id().0, "1");

    let event3 = &timeline_items[3];
    assert_eq!(event3.as_event().unwrap().sender(), *CAROL);
    assert_eq!(event3.unique_id().0, "2");
}

#[async_test]
async fn test_sanitized() {
    let timeline = TestTimeline::new();
    let mut stream = timeline.subscribe().await;

    let f = &timeline.factory;

    timeline
        .handle_live_event(
            f.text_html(
                "\
                    @@Unknown text@@
                    Some text\n\n\
                    !!code```
                        Some code
                    ```
                ",
                "\
                    <unknown>Unknown text</unknown>\
                    <p>Some text</p>\
                    <code unknown=\"code\">Some code</code>\
                ",
            )
            .sender(&ALICE),
        )
        .await;

    let item = assert_next_matches!(stream, VectorDiff::PushBack { value } => value);
    let event = item.as_event().unwrap();
    assert_let!(Some(message) = event.content().as_message());
    assert_let!(MessageType::Text(text) = message.msgtype());
    assert_eq!(
        text.formatted.as_ref().unwrap().body,
        "\
            Unknown text\
            <p>Some text</p>\
            <code>Some code</code>\
        "
    );

    let date_divider = assert_next_matches!(stream, VectorDiff::PushFront { value } => value);
    assert!(date_divider.is_date_divider());
}

#[async_test]
async fn test_reply() {
    let timeline = TestTimeline::new();
    let mut stream = timeline.subscribe().await;

    let f = &timeline.factory;
    timeline.handle_live_event(f.text_msg("I want you to reply").sender(&ALICE)).await;

    let item = assert_next_matches!(stream, VectorDiff::PushBack { value } => value);
    let first_event = item.as_event().unwrap();
    assert!(first_event.can_be_replied_to());
    let first_event_id = first_event.event_id().unwrap();
    let first_event_sender = *ALICE;

    let date_divider = assert_next_matches!(stream, VectorDiff::PushFront { value } => value);
    assert!(date_divider.is_date_divider());

    let reply_formatted_body = format!(
        "\
            <mx-reply>\
                <blockquote>\
                    <a href=\"https://matrix.to/#/!my_room:server.name/{first_event_id}\">In reply to</a> \
                    <a href=\"https://matrix.to/#/{first_event_sender}\">{first_event_sender}</a>\
                    <br>\
                    I want you to reply\
                </blockquote>\
            </mx-reply>\
            <p>I'm replying!</p>\
        "
    );
    let reply_plain = format!(
        "> <{first_event_sender}> I want you to reply\n\
         I'm replying!"
    );

    timeline
        .handle_live_event(
            f.text_html(reply_plain, reply_formatted_body).reply_to(first_event_id).sender(&BOB),
        )
        .await;

    let item = assert_next_matches!(stream, VectorDiff::PushBack { value } => value);
    assert_let!(
        TimelineItemContent::MsgLike(MsgLikeContent {
            kind: MsgLikeKind::Message(message),
            in_reply_to,
            ..
        }) = item.as_event().unwrap().content()
    );

    assert_let!(MessageType::Text(text) = message.msgtype());
    assert_eq!(text.body, "I'm replying!");
    assert_eq!(text.formatted.as_ref().unwrap().body, "<p>I'm replying!</p>");

    let in_reply_to = in_reply_to.clone().unwrap();
    assert_eq!(in_reply_to.event_id, first_event_id);
    assert_let!(TimelineDetails::Ready(replied_to_event) = &in_reply_to.event);
    assert_eq!(replied_to_event.sender, *ALICE);
}

#[async_test]
async fn test_thread() {
    let timeline = TestTimeline::new();
    let mut stream = timeline.subscribe().await;

    let f = &timeline.factory;
    timeline.handle_live_event(f.text_msg("I want you to reply").sender(&ALICE)).await;

    let item = assert_next_matches!(stream, VectorDiff::PushBack { value } => value);
    let first_event = item.as_event().unwrap();
    let first_event_id = first_event.event_id().unwrap();

    let date_divider = assert_next_matches!(stream, VectorDiff::PushFront { value } => value);
    assert!(date_divider.is_date_divider());

    timeline
        .handle_live_event(
            f.text_msg("I'm replying in a thread")
                .sender(&BOB)
                .in_thread(first_event_id, first_event_id),
        )
        .await;

    let item = assert_next_matches!(stream, VectorDiff::PushBack { value } => value);
    assert_let!(
        TimelineItemContent::MsgLike(MsgLikeContent {
            kind: MsgLikeKind::Message(message),
            in_reply_to,
            ..
        }) = item.as_event().unwrap().content()
    );

    assert_let!(MessageType::Text(text) = message.msgtype());
    assert_eq!(text.body, "I'm replying in a thread");
    assert_matches!(text.formatted, None);

    let in_reply_to = in_reply_to.clone().unwrap();
    assert_eq!(in_reply_to.event_id, first_event_id);
    assert_let!(TimelineDetails::Ready(replied_to_event) = &in_reply_to.event);
    assert_eq!(replied_to_event.sender, *ALICE);
}

#[async_test]
async fn test_replace_with_initial_events_when_batched() {
    let timeline = TestTimelineBuilder::new()
        .provider(TestRoomDataProvider::default())
        .settings(TimelineSettings::default())
        .build();

    let f = &timeline.factory;
    let ev = f.text_msg("hey").sender(*ALICE).into_event();

    timeline
        .controller
        .handle_remote_events_with_diffs(
            vec![VectorDiff::Append { values: vector![ev] }],
            RemoteEventOrigin::Sync,
        )
        .await;

    let (items, mut stream) = timeline.controller.subscribe().await;
    assert_eq!(items.len(), 2);
    assert!(items[0].is_date_divider());
    assert_eq!(items[1].as_event().unwrap().content().as_message().unwrap().body(), "hey");

    let ev = f.text_msg("yo").sender(*BOB).into_event();
    timeline.controller.replace_with_initial_remote_events([ev], RemoteEventOrigin::Sync).await;

    // Assert there are more than a single Clear diff in the next batch:
    // Clear + PushBack (event) + PushFront (date divider)
    let batched_diffs = stream.next().await.unwrap();
    assert_eq!(batched_diffs.len(), 3);
    assert_matches!(batched_diffs[0], VectorDiff::Clear);
    assert_matches!(&batched_diffs[1], VectorDiff::PushBack { value } => {
        assert!(value.as_event().is_some());
    });
    assert_matches!(&batched_diffs[2], VectorDiff::PushFront { value } => {
        assert_matches!(value.as_virtual(), Some(VirtualTimelineItem::DateDivider(_)));
    });
}

#[async_test]
async fn test_latest_event_id_in_main_timeline() {
    let server = MatrixMockServer::new().await;
    let client = server
        .client_builder()
        .on_builder(|b| {
            b.with_threading_support(ThreadingSupport::Enabled { with_subscriptions: true })
        })
        .build()
        .await;

    let room_id = room_id!("!a98sd12bjh:example.org");
    let event_id = event_id!("$message");
    let reaction_event_id = event_id!("$reaction");
    let thread_event_id = event_id!("$thread");

    let room = server.sync_joined_room(&client, room_id).await;
    let timeline = room
        .timeline_builder()
        .with_focus(TimelineFocus::Live { hide_threaded_events: true })
        .track_read_marker_and_receipts(TimelineReadReceiptTracking::AllEvents)
        .build()
        .await
        .expect("Could not build live timeline");

    let (items, mut stream) = timeline.subscribe().await;
    assert!(items.is_empty());
    assert_pending!(stream);

    let f = EventFactory::new().room(room_id).sender(user_id!("@a:b.c"));

    // If we receive a message in the live timeline
    server
        .sync_room(
            &client,
            JoinedRoomBuilder::new(room_id)
                .add_timeline_event(f.text_msg("A message").event_id(event_id).into_raw_sync()),
        )
        .await;

    let items = assert_next_with_timeout!(stream);
    // We receive the day divider and the items
    assert_eq!(items.len(), 2);
    assert_let!(Some(latest_event_id) = timeline.controller.latest_event_id().await);
    // The latest event id is from the event in the live timeline
    assert_eq!(event_id, latest_event_id);

    // If we then receive a reaction
    server
        .sync_room(
            &client,
            JoinedRoomBuilder::new(room_id).add_timeline_event(
                f.reaction(event_id, ":D").event_id(reaction_event_id).into_raw_sync(),
            ),
        )
        .await;

    let items = assert_next_with_timeout!(stream);
    // The reaction is added
    assert_eq!(items.len(), 1);
    assert_let!(Some(latest_event_id) = timeline.controller.latest_event_id().await);
    // And it's now the latest event id
    assert_eq!(reaction_event_id, latest_event_id);

    // If we now get a message in a thread inside that room
    server
        .sync_room(
            &client,
            JoinedRoomBuilder::new(room_id).add_timeline_event(
                f.text_msg("In thread")
                    .in_thread(event_id, thread_event_id)
                    .event_id(thread_event_id)
                    .into_raw_sync(),
            ),
        )
        .await;
    // The thread root event is updated
    assert_eq!(items.len(), 1);
    assert_let!(Some(latest_event_id) = timeline.controller.latest_event_id().await);

    // But the latest event in the live timeline is still the reaction, since the
    // threaded event is not part of the live timeline
    assert_eq!(reaction_event_id, latest_event_id);
}