matrix-sdk 0.16.0

A high level Matrix client-server library.
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
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
use std::time::Duration;

use assert_matches2::assert_let;
use eyeball_im::VectorDiff;
use imbl::Vector;
use matrix_sdk::{
    Client, ThreadingSupport, assert_let_timeout,
    deserialized_responses::{ThreadSummaryStatus, TimelineEvent},
    event_cache::{RoomEventCacheSubscriber, RoomEventCacheUpdate, ThreadEventCacheUpdate},
    sleep::sleep,
    test_utils::{
        assert_event_matches_msg,
        mocks::{MatrixMockServer, RoomRelationsResponseTemplate},
    },
};
use matrix_sdk_test::{ALICE, JoinedRoomBuilder, async_test, event_factory::EventFactory};
use ruma::{
    OwnedEventId, OwnedRoomId, event_id,
    events::{AnySyncTimelineEvent, Mentions},
    push::{ConditionalPushRule, Ruleset},
    room_id,
    serde::Raw,
    user_id,
};
use tokio::sync::broadcast;

/// Small helper for backpagination tests, to wait for initial events to
/// stabilize.
async fn wait_for_initial_events(
    mut events: Vec<TimelineEvent>,
    stream: &mut broadcast::Receiver<ThreadEventCacheUpdate>,
) -> Vec<TimelineEvent> {
    if events.is_empty() {
        // Wait for a first update.
        let mut vector = Vector::new();

        assert_let_timeout!(Ok(ThreadEventCacheUpdate { diffs, .. }) = stream.recv());

        for diff in diffs {
            diff.apply(&mut vector);
        }

        events.extend(vector);
        events
    } else {
        events
    }
}

async fn client_with_threading_support(server: &MatrixMockServer) -> Client {
    server
        .client_builder()
        .on_builder(|builder| {
            builder.with_threading_support(ThreadingSupport::Enabled { with_subscriptions: false })
        })
        .build()
        .await
}

#[async_test]
async fn test_thread_can_paginate_even_if_seen_sync_event() {
    let server = MatrixMockServer::new().await;
    let client = client_with_threading_support(&server).await;

    let room_id = room_id!("!galette:saucisse.bzh");

    let event_cache = client.event_cache();
    event_cache.subscribe().unwrap();

    let thread_root_id = event_id!("$thread_root");
    let thread_resp_id = event_id!("$thread_resp");

    // Receive an in-thread event.
    let f = EventFactory::new().room(room_id).sender(*ALICE);
    let room = server
        .sync_room(
            &client,
            JoinedRoomBuilder::new(room_id).add_timeline_event(
                f.text_msg("that's a good point")
                    .in_thread(thread_root_id, thread_root_id)
                    .event_id(thread_resp_id),
            ),
        )
        .await;

    let (room_event_cache, _drop_handles) = room.event_cache().await.unwrap();

    let (thread_events, mut thread_stream) =
        room_event_cache.subscribe_to_thread(thread_root_id.to_owned()).await.unwrap();

    // Sanity check: the sync event is added to the thread.
    let mut thread_events = wait_for_initial_events(thread_events, &mut thread_stream).await;
    assert_eq!(thread_events.len(), 1);
    assert_eq!(thread_events.remove(0).event_id().as_deref(), Some(thread_resp_id));

    // It's possible to paginate the thread, and this will push the thread root
    // because there's no prev-batch token.
    server
        .mock_room_relations()
        .match_target_event(thread_root_id.to_owned())
        .ok(RoomRelationsResponseTemplate::default())
        .mock_once()
        .mount()
        .await;

    server
        .mock_room_event()
        .match_event_id()
        .ok(f.text_msg("Thread root").event_id(thread_root_id).into())
        .mock_once()
        .mount()
        .await;

    let hit_start =
        room_event_cache.paginate_thread_backwards(thread_root_id.to_owned(), 42).await.unwrap();
    assert!(hit_start);

    assert_let_timeout!(Ok(ThreadEventCacheUpdate { diffs, .. }) = thread_stream.recv());
    assert_eq!(diffs.len(), 1);
    assert_let!(VectorDiff::Insert { index: 0, value } = &diffs[0]);
    assert_eq!(value.event_id().as_deref(), Some(thread_root_id));
}

#[async_test]
async fn test_ignored_user_empties_threads() {
    let server = MatrixMockServer::new().await;
    let client = client_with_threading_support(&server).await;

    // Immediately subscribe the event cache to sync updates.
    client.event_cache().subscribe().unwrap();

    let room_id = room_id!("!omelette:fromage.fr");

    let dexter = user_id!("@dexter:lab.org");
    let ivan = user_id!("@ivan:lab.ch");

    let f = EventFactory::new();

    let thread_root = event_id!("$thread_root");
    let first_reply_event_id = event_id!("$first_reply");
    let second_reply_event_id = event_id!("$second_reply");

    // Given a room with a thread, that has two replies.
    let room = server
        .sync_room(
            &client,
            JoinedRoomBuilder::new(room_id).add_timeline_bulk(vec![
                f.text_msg("hey there")
                    .sender(dexter)
                    .in_thread(thread_root, thread_root)
                    .event_id(first_reply_event_id)
                    .into_raw_sync(),
                f.text_msg("hoy!")
                    .sender(ivan)
                    .in_thread(thread_root, first_reply_event_id)
                    .event_id(second_reply_event_id)
                    .into_raw_sync(),
            ]),
        )
        .await;

    // And we subscribe to the thread,
    let (room_event_cache, _drop_handles) = room.event_cache().await.unwrap();
    let (events, mut thread_stream) =
        room_event_cache.subscribe_to_thread(thread_root.to_owned()).await.unwrap();

    // Then, at first, the thread contains the two initial events.
    let events = wait_for_initial_events(events, &mut thread_stream).await;
    assert_eq!(events.len(), 2);
    assert_event_matches_msg(&events[0], "hey there");
    assert_event_matches_msg(&events[1], "hoy!");

    // `dexter` is ignored.
    server
        .mock_sync()
        .ok_and_run(&client, |sync_builder| {
            sync_builder.add_global_account_data(f.ignored_user_list([dexter.to_owned()]));
        })
        .await;

    // We do receive a clear.
    {
        assert_let_timeout!(Ok(ThreadEventCacheUpdate { diffs, .. }) = thread_stream.recv());
        assert_eq!(diffs.len(), 1);
        assert_let!(VectorDiff::Clear = &diffs[0]);
    }

    // Receiving new events still works.
    server
        .mock_sync()
        .ok_and_run(&client, |sync_builder| {
            sync_builder.add_joined_room(
                JoinedRoomBuilder::new(room_id).add_timeline_event(
                    f.text_msg("i don't like this dexter")
                        .in_thread(thread_root, second_reply_event_id)
                        .sender(ivan),
                ),
            );
        })
        .await;

    // We do receive the new event.
    {
        assert_let_timeout!(Ok(ThreadEventCacheUpdate { diffs, .. }) = thread_stream.recv());
        assert_eq!(diffs.len(), 1);

        assert_let!(VectorDiff::Append { values: events } = &diffs[0]);
        assert_eq!(events.len(), 1);
        assert_event_matches_msg(&events[0], "i don't like this dexter");
    }

    // That's all, folks!
    assert!(thread_stream.is_empty());
}

#[async_test]
async fn test_gappy_sync_empties_all_threads() {
    let server = MatrixMockServer::new().await;
    let client = client_with_threading_support(&server).await;

    // Immediately subscribe the event cache to sync updates.
    client.event_cache().subscribe().unwrap();

    let room_id = room_id!("!omelette:fromage.fr");

    let f = EventFactory::new().sender(*ALICE);

    let thread_root1 = event_id!("$t1root");
    let thread1_reply1 = event_id!("$t1ev1");
    let thread1_reply2 = event_id!("$t1ev2");

    let thread_root2 = event_id!("$t2root");
    let thread2_reply1 = event_id!("$t2ev1");

    // We subscribe to each thread (before the initial sync, so the state is stable
    // before running checks).
    let room = server.sync_joined_room(&client, room_id).await;

    let (room_event_cache, _drop_handles) = room.event_cache().await.unwrap();
    let (thread1_events, mut thread1_stream) =
        room_event_cache.subscribe_to_thread(thread_root1.to_owned()).await.unwrap();

    assert!(thread1_events.is_empty());
    assert!(thread1_stream.is_empty());

    let (thread2_events, mut thread2_stream) =
        room_event_cache.subscribe_to_thread(thread_root2.to_owned()).await.unwrap();

    assert!(thread2_events.is_empty());
    assert!(thread2_stream.is_empty());

    // Also subscribe to the room.
    let (room_events, mut room_stream) = room_event_cache.subscribe().await.unwrap();

    assert!(room_events.is_empty());
    assert!(room_stream.is_empty());

    // Given a room with two threads, each having some replies,
    server
        .sync_room(
            &client,
            JoinedRoomBuilder::new(room_id).add_timeline_bulk(vec![
                // First thread root.
                f.text_msg("say hi in thread").event_id(thread_root1).into_raw_sync(),
                // Second thread root.
                f.text_msg("say bye in thread").event_id(thread_root2).into_raw_sync(),
                // Reply to the first thread.
                f.text_msg("hey there")
                    .in_thread(thread_root1, thread_root1)
                    .event_id(thread1_reply1)
                    .into_raw_sync(),
                // Reply to the second thread.
                f.text_msg("bye there")
                    .in_thread(thread_root2, thread_root2)
                    .event_id(thread2_reply1)
                    .into_raw_sync(),
                // Another reply to the first thread.
                f.text_msg("hoy!")
                    .in_thread(thread_root1, thread1_reply1)
                    .event_id(thread1_reply2)
                    .into_raw_sync(),
            ]),
        )
        .await;

    // The first thread contains all thread events, including the root.
    assert_let_timeout!(Ok(ThreadEventCacheUpdate { diffs, .. }) = thread1_stream.recv());
    assert_eq!(diffs.len(), 1);
    assert_let!(VectorDiff::Append { values: thread1_events } = &diffs[0]);
    assert_eq!(thread1_events.len(), 3);
    assert_event_matches_msg(&thread1_events[0], "say hi in thread");
    assert_event_matches_msg(&thread1_events[1], "hey there");
    assert_event_matches_msg(&thread1_events[2], "hoy!");

    // The second thread contains the in-thread event (but not the root, for the
    // same reason as above).
    assert_let_timeout!(Ok(ThreadEventCacheUpdate { diffs, .. }) = thread2_stream.recv());
    assert_eq!(diffs.len(), 1);
    assert_let!(VectorDiff::Append { values: thread2_events } = &diffs[0]);
    assert_eq!(thread2_events.len(), 2);
    assert_event_matches_msg(&thread2_events[0], "say bye in thread");
    assert_event_matches_msg(&thread2_events[1], "bye there");

    // The room contains all five events.
    assert_let_timeout!(
        Ok(RoomEventCacheUpdate::UpdateTimelineEvents { diffs, .. }) = room_stream.recv()
    );
    assert_eq!(diffs.len(), 3);
    assert_let!(VectorDiff::Append { values: room_events } = &diffs[0]);
    assert_eq!(room_events.len(), 5);
    // Two thread summary updates, for the thread roots.
    assert_let!(VectorDiff::Set { index: 0, .. } = &diffs[1]);
    assert_let!(VectorDiff::Set { index: 1, .. } = &diffs[2]);

    // Receive a gappy sync for the room, with no events (so the event cache doesn't
    // filter the gap out).
    server
        .sync_room(
            &client,
            JoinedRoomBuilder::new(room_id)
                .set_timeline_limited()
                .set_timeline_prev_batch("prev_batch"),
        )
        .await;

    // Both threads are cleared.
    {
        assert_let_timeout!(Ok(ThreadEventCacheUpdate { diffs, .. }) = thread1_stream.recv());
        assert_eq!(diffs.len(), 1);
        assert_let!(VectorDiff::Clear = &diffs[0]);
    }
    {
        assert_let_timeout!(Ok(ThreadEventCacheUpdate { diffs, .. }) = thread2_stream.recv());
        assert_eq!(diffs.len(), 1);
        assert_let!(VectorDiff::Clear = &diffs[0]);
    }

    // The room is shrunk to the gap.
    {
        assert_let_timeout!(
            Ok(RoomEventCacheUpdate::UpdateTimelineEvents { diffs, .. }) = room_stream.recv()
        );
        assert_eq!(diffs.len(), 1);
        assert_let!(VectorDiff::Clear = &diffs[0]);
    }

    // If we manually load the root events from the db, the summaries have been
    // updated too:
    // - the count is still the same, as it's based on the number of replies known
    //   in the room linked chunk (that's persisted on disk). It might be outdated,
    //   but that's a lower bound.
    // - but the latest reply is now `None`, as we're not sure that the latest reply
    //   is still the latest one.

    let reloaded_thread1 = room_event_cache.find_event(thread_root1).await.unwrap().unwrap();
    assert_let!(ThreadSummaryStatus::Some(summary) = reloaded_thread1.thread_summary);
    assert_eq!(summary.num_replies, 2);
    assert!(summary.latest_reply.is_none());

    // That's all, folks!
    assert!(thread1_stream.is_empty());
}

#[async_test]
async fn test_deduplication() {
    let server = MatrixMockServer::new().await;
    let client = client_with_threading_support(&server).await;

    // Immediately subscribe the event cache to sync updates.
    client.event_cache().subscribe().unwrap();

    let room_id = room_id!("!omelette:fromage.fr");

    let f = EventFactory::new().room(room_id).sender(*ALICE);

    let thread_root = event_id!("$thread_root");
    let first_reply_event_id = event_id!("$first_reply");
    let first_reply = f
        .text_msg("hey there")
        .in_thread(thread_root, thread_root)
        .event_id(first_reply_event_id)
        .into_raw_timeline();
    let second_reply_event_id = event_id!("$second_reply");
    let second_reply = f
        .text_msg("hoy!")
        .in_thread(thread_root, first_reply_event_id)
        .event_id(second_reply_event_id)
        .into_raw_timeline();

    // Given a room with a thread, that has two replies.
    let room = server
        .sync_room(
            &client,
            JoinedRoomBuilder::new(room_id)
                .add_timeline_bulk(vec![first_reply.clone().cast(), second_reply.clone().cast()]),
        )
        .await;

    // And we subscribe to the thread,
    let (room_event_cache, _drop_handles) = room.event_cache().await.unwrap();
    let (events, mut thread_stream) =
        room_event_cache.subscribe_to_thread(thread_root.to_owned()).await.unwrap();

    // Then, at first, the thread contains the two initial events.
    let events = wait_for_initial_events(events, &mut thread_stream).await;
    assert_eq!(events.len(), 2);
    assert_event_matches_msg(&events[0], "hey there");
    assert_event_matches_msg(&events[1], "hoy!");

    // No updates on the stream.
    assert!(thread_stream.is_empty());

    // We receive a sync with the same tail events.
    server
        .sync_room(
            &client,
            JoinedRoomBuilder::new(room_id).add_timeline_bulk(vec![
                f.text_msg("hoy!")
                    .in_thread(thread_root, first_reply_event_id)
                    .event_id(second_reply_event_id)
                    .into_raw_sync(),
            ]),
        )
        .await;

    // Still no updates on the stream: the event has been deduplicated, and there
    // were no gaps.
    assert!(thread_stream.is_empty());

    // If I backpaginate in that thread, and the pagination only returns events I
    // already knew about, the stream is still empty.
    server
        .mock_room_relations()
        .match_target_event(thread_root.to_owned())
        .ok(RoomRelationsResponseTemplate::default()
            .events(vec![first_reply, second_reply])
            .next_batch("next_batch"))
        .mock_once()
        .mount()
        .await;

    room_event_cache.paginate_thread_backwards(thread_root.to_owned(), 42).await.unwrap();

    // The events were already known, so the stream is still empty.
    assert!(thread_stream.is_empty());
}

struct ThreadSubscriptionTestSetup {
    server: MatrixMockServer,
    client: Client,
    factory: EventFactory,
    room_id: OwnedRoomId,
    subscriber: RoomEventCacheSubscriber,
    /// 3 events: 1 non-mention, 1 mention, and another non-mention.
    events: Vec<Raw<AnySyncTimelineEvent>>,
    mention_event_id: OwnedEventId,
    thread_root: OwnedEventId,
}

/// Create a new setup for a thread subscription test, with enough data so that
/// a push context can be created.
///
/// The setup uses custom push rules, to trigger notifications only on mentions.
///
/// The setup includes 3 events (1 non-mention, 1 mention, and another
/// non-mention) in the same thread, for easy testing of automated
/// subscriptions.
async fn thread_subscription_test_setup() -> ThreadSubscriptionTestSetup {
    let server = MatrixMockServer::new().await;

    let thread_root = event_id!("$thread_root");

    // Assuming a client that's interested in thread subscriptions,
    let client = server
        .client_builder()
        .on_builder(|builder| {
            builder.with_threading_support(ThreadingSupport::Enabled { with_subscriptions: true })
        })
        .build()
        .await;

    // Immediately subscribe the event cache to sync updates.
    client.event_cache().subscribe().unwrap();

    let room_id = room_id!("!omelette:fromage.fr");
    let room = server.sync_joined_room(&client, room_id).await;

    let (room_event_cache, _drop_handles) = room.event_cache().await.unwrap();

    let (initial_events, mut subscriber) = room_event_cache.subscribe().await.unwrap();
    assert!(initial_events.is_empty());
    assert!(subscriber.is_empty());

    // Provide a dummy sync with the room's member profile of the current user, so
    // the push context can be created.
    let own_user_id = client.user_id().unwrap();
    let f = EventFactory::new().room(room_id).sender(*ALICE);
    let member = f.member(own_user_id).sender(own_user_id);

    // Override push rules so that only an intentional mention causes a
    // notification.
    let mut push_rules = Ruleset::default();
    push_rules.override_.insert(ConditionalPushRule::is_user_mention(own_user_id));

    server
        .mock_sync()
        .ok_and_run(&client, |sync_builder| {
            sync_builder.add_joined_room(JoinedRoomBuilder::new(room_id).add_state_event(member));
            sync_builder.add_global_account_data(f.push_rules(push_rules));
        })
        .await;

    // Wait for the initial sync processing to complete; it will trigger a member
    // update, at the very least.
    assert_let_timeout!(Ok(RoomEventCacheUpdate::UpdateMembers { .. }) = subscriber.recv());

    let first_reply_event_id = event_id!("$first_reply");
    let first_reply = f
        .text_msg("hey there")
        .in_thread(thread_root, thread_root)
        .event_id(first_reply_event_id)
        .into();

    let second_reply_event_id = event_id!("$second_reply");
    let second_reply = f
        .text_msg("hoy test user!")
        .mentions(Mentions::with_user_ids([own_user_id.to_owned()]))
        .in_thread(thread_root, first_reply_event_id)
        .event_id(second_reply_event_id)
        .into();

    let third_reply_event_id = event_id!("$third_reply");
    let third_reply = f
        .text_msg("ciao!")
        .in_thread(thread_root, second_reply_event_id)
        .event_id(third_reply_event_id)
        .into();

    ThreadSubscriptionTestSetup {
        server,
        client,
        factory: f,
        subscriber,
        events: vec![first_reply, second_reply, third_reply],
        mention_event_id: second_reply_event_id.to_owned(),
        thread_root: thread_root.to_owned(),
        room_id: room_id.to_owned(),
    }
}

#[async_test]
async fn test_auto_subscribe_thread_via_sync() {
    let mut s = thread_subscription_test_setup().await;

    // (The endpoint will be called for the current thread, and with an automatic
    // subscription up to the given event ID.)
    s.server
        .mock_room_put_thread_subscription()
        .match_automatic_event_id(&s.mention_event_id)
        .match_thread_id(s.thread_root.to_owned())
        .ok()
        .mock_once()
        .mount()
        .await;

    let mut thread_subscriber_updates =
        s.client.event_cache().subscribe_thread_subscriber_updates();

    // When I receive 3 events (1 non mention, 1 mention, then 1 non mention again),
    // from sync, I'll get subscribed to the thread because of the second event.
    s.server
        .sync_room(&s.client, JoinedRoomBuilder::new(&s.room_id).add_timeline_bulk(s.events))
        .await;

    // Let the event cache process the update.
    assert_let_timeout!(
        Ok(RoomEventCacheUpdate::UpdateTimelineEvents { .. }) = s.subscriber.recv()
    );
    assert_let_timeout!(Ok(()) = thread_subscriber_updates.recv());

    // The actual check is the `mock_once` call above!
}

#[async_test]
async fn test_dont_auto_subscribe_on_already_subscribed_thread() {
    let mut s = thread_subscription_test_setup().await;

    // Given a thread I'm already subscribed to,
    s.server
        .mock_room_get_thread_subscription()
        .match_thread_id(s.thread_root.to_owned())
        .ok(false)
        .mock_once()
        .mount()
        .await;

    // The PUT endpoint (to subscribe to the thread) shouldn't be called…
    s.server.mock_room_put_thread_subscription().ok().expect(0).mount().await;

    // …when I receive a new in-thread mention for this thread.
    s.server
        .sync_room(&s.client, JoinedRoomBuilder::new(&s.room_id).add_timeline_bulk(s.events))
        .await;

    // Let the event cache process the update.
    assert_let_timeout!(
        Ok(RoomEventCacheUpdate::UpdateTimelineEvents { .. }) = s.subscriber.recv()
    );

    // Let a bit of time for the background thread subscriber task to process the
    // update.
    sleep(Duration::from_millis(200)).await;

    // The actual check is the `expect` call above!
}

#[async_test]
async fn test_auto_subscribe_on_thread_paginate() {
    // In this scenario, we're back-paginating a thread and making sure that the
    // back-paginated events do cause a subscription.

    let s = thread_subscription_test_setup().await;

    let event_cache = s.client.event_cache();
    event_cache.subscribe().unwrap();

    let mut thread_subscriber_updates =
        s.client.event_cache().subscribe_thread_subscriber_updates();

    let thread_root_id = event_id!("$thread_root");
    let thread_resp_id = event_id!("$thread_resp");

    // Receive an in-thread event.
    let room = s
        .server
        .sync_room(
            &s.client,
            JoinedRoomBuilder::new(&s.room_id).add_timeline_event(
                s.factory
                    .text_msg("that's a good point")
                    .in_thread(thread_root_id, thread_root_id)
                    .event_id(thread_resp_id),
            ),
        )
        .await;

    let (room_event_cache, _drop_handles) = room.event_cache().await.unwrap();

    let (thread_events, mut thread_stream) =
        room_event_cache.subscribe_to_thread(thread_root_id.to_owned()).await.unwrap();

    // Sanity check: the sync event is added to the thread.
    let mut thread_events = wait_for_initial_events(thread_events, &mut thread_stream).await;
    assert_eq!(thread_events.len(), 1);
    assert_eq!(thread_events.remove(0).event_id().as_deref(), Some(thread_resp_id));

    assert!(thread_subscriber_updates.is_empty());

    // It's possible to paginate the thread, and this will push the thread root
    // because there's no prev-batch token.
    let reversed_events = s.events.into_iter().rev().map(Raw::cast_unchecked).collect();
    s.server
        .mock_room_relations()
        .match_target_event(thread_root_id.to_owned())
        .ok(RoomRelationsResponseTemplate::default().events(reversed_events))
        .mock_once()
        .mount()
        .await;

    s.server
        .mock_room_event()
        .match_event_id()
        .ok(s.factory.text_msg("Thread root").event_id(thread_root_id).into())
        .mock_once()
        .mount()
        .await;

    // (The endpoint will be called for the current thread, and with an automatic
    // subscription up to the given event ID.)
    s.server
        .mock_room_put_thread_subscription()
        .match_automatic_event_id(&s.mention_event_id)
        .match_thread_id(s.thread_root.to_owned())
        .ok()
        .mock_once()
        .mount()
        .await;

    let hit_start =
        room_event_cache.paginate_thread_backwards(thread_root_id.to_owned(), 42).await.unwrap();
    assert!(hit_start);

    // Let the event cache process the update.
    assert_let_timeout!(Ok(ThreadEventCacheUpdate { .. }) = thread_stream.recv());
    assert_let_timeout!(Ok(()) = thread_subscriber_updates.recv());
    assert!(thread_subscriber_updates.is_empty());
}

#[async_test]
async fn test_auto_subscribe_on_thread_paginate_root_event() {
    // In this scenario, the root of a thread is the event that would cause the
    // subscription.

    let s = thread_subscription_test_setup().await;

    let event_cache = s.client.event_cache();
    event_cache.subscribe().unwrap();

    let mut thread_subscriber_updates =
        s.client.event_cache().subscribe_thread_subscriber_updates();

    let thread_root_id = event_id!("$thread_root");
    let thread_resp_id = event_id!("$thread_resp");

    // Receive an in-thread event.
    let room = s
        .server
        .sync_room(
            &s.client,
            JoinedRoomBuilder::new(&s.room_id).add_timeline_event(
                s.factory
                    .text_msg("that's a good point")
                    .in_thread(thread_root_id, thread_root_id)
                    .event_id(thread_resp_id),
            ),
        )
        .await;

    let (room_event_cache, _drop_handles) = room.event_cache().await.unwrap();

    let (thread_events, mut thread_stream) =
        room_event_cache.subscribe_to_thread(thread_root_id.to_owned()).await.unwrap();

    // Sanity check: the sync event is added to the thread.
    let mut thread_events = wait_for_initial_events(thread_events, &mut thread_stream).await;
    assert_eq!(thread_events.len(), 1);
    assert_eq!(thread_events.remove(0).event_id().as_deref(), Some(thread_resp_id));

    assert!(thread_subscriber_updates.is_empty());

    // It's possible to paginate the thread, and this will push the thread root
    // because there's no prev-batch token.
    s.server
        .mock_room_relations()
        .match_target_event(thread_root_id.to_owned())
        .ok(RoomRelationsResponseTemplate::default())
        .mock_once()
        .mount()
        .await;

    s.server
        .mock_room_event()
        .match_event_id()
        .ok(s
            .factory
            .text_msg("da r00t")
            .event_id(thread_root_id)
            .mentions(Mentions::with_user_ids(s.client.user_id().map(ToOwned::to_owned)))
            .into())
        .mock_once()
        .mount()
        .await;

    // (The endpoint will be called for the current thread, and with an automatic
    // subscription up to the given event ID.)
    s.server
        .mock_room_put_thread_subscription()
        .match_automatic_event_id(thread_root_id)
        .match_thread_id(thread_root_id.to_owned())
        .ok()
        .mock_once()
        .mount()
        .await;

    let hit_start =
        room_event_cache.paginate_thread_backwards(thread_root_id.to_owned(), 42).await.unwrap();
    assert!(hit_start);

    // Let the event cache process the update.
    assert_let_timeout!(Ok(ThreadEventCacheUpdate { .. }) = thread_stream.recv());
    assert_let_timeout!(Ok(()) = thread_subscriber_updates.recv());
}

#[async_test]
async fn test_redact_touches_threads() {
    // We start with a thread with some replies, then receive redactions for those
    // replies over sync. We observe that the thread linked chunks are correctly
    // updated, as well as the thread summary on the thread root event.

    let s = thread_subscription_test_setup().await;
    let f = s.factory;

    let event_cache = s.client.event_cache();
    event_cache.subscribe().unwrap();

    let thread_root_id = s.thread_root;
    let thread_resp1 = s.events[0].get_field::<OwnedEventId>("event_id").unwrap().unwrap();
    let thread_resp2 = s.events[1].get_field::<OwnedEventId>("event_id").unwrap().unwrap();

    let room = s.server.sync_joined_room(&s.client, &s.room_id).await;

    let (room_event_cache, _drop_handles) = room.event_cache().await.unwrap();

    let (thread_events, mut thread_stream) =
        room_event_cache.subscribe_to_thread(thread_root_id.to_owned()).await.unwrap();

    // Receive a thread root, and a threaded reply.
    s.server
        .sync_room(
            &s.client,
            JoinedRoomBuilder::new(&s.room_id)
                .add_timeline_event(f.text_msg("da r00t").event_id(&thread_root_id))
                .add_timeline_event(s.events[0].clone())
                .add_timeline_event(s.events[1].clone()),
        )
        .await;

    // Sanity check: both events are present in the thread, and the thread summary
    // is correct.
    let mut thread_events = wait_for_initial_events(thread_events, &mut thread_stream).await;
    assert_eq!(thread_events.len(), 3);
    assert_eq!(thread_events.remove(0).event_id().as_ref(), Some(&thread_root_id));
    assert_eq!(thread_events.remove(0).event_id().as_ref(), Some(&thread_resp1));
    assert_eq!(thread_events.remove(0).event_id().as_ref(), Some(&thread_resp2));

    let (room_events, mut room_stream) = room_event_cache.subscribe().await.unwrap();
    assert_eq!(room_events.len(), 3);

    {
        assert_eq!(room_events[0].event_id().as_ref(), Some(&thread_root_id));
        let summary = room_events[0].thread_summary.summary().unwrap();
        assert_eq!(summary.latest_reply.as_ref(), Some(&thread_resp2));
        assert_eq!(summary.num_replies, 2);
    }

    assert_eq!(room_events[1].event_id().as_ref(), Some(&thread_resp1));
    assert_eq!(room_events[2].event_id().as_ref(), Some(&thread_resp2));

    assert!(thread_stream.is_empty());
    assert!(room_stream.is_empty());

    // A redaction for the first reply comes through sync.
    let thread_resp1_redaction = event_id!("$redact_thread_resp1");
    s.server
        .sync_room(
            &s.client,
            JoinedRoomBuilder::new(&s.room_id)
                .add_timeline_event(f.redaction(&thread_resp1).event_id(thread_resp1_redaction)),
        )
        .await;

    // The redaction affects the thread cache: it *removes* the redacted event.
    {
        assert_let_timeout!(Ok(ThreadEventCacheUpdate { diffs, .. }) = thread_stream.recv());
        assert_eq!(diffs.len(), 1);
        assert_let!(VectorDiff::Remove { index: 1 } = &diffs[0]);

        assert!(thread_stream.is_empty());
    }

    // The redaction affects the room cache too:
    // - the redaction event is pushed to the room history,
    // - the redaction's target is, well, redacted,
    // - the thread summary is updated correctly.
    {
        assert_let_timeout!(
            Ok(RoomEventCacheUpdate::UpdateTimelineEvents { diffs, .. }) = room_stream.recv()
        );
        assert_eq!(diffs.len(), 3);

        // The redaction event is appended to the room cache.
        assert_let!(VectorDiff::Append { values: new_events } = &diffs[0]);
        assert_eq!(new_events.len(), 1);
        assert_eq!(new_events[0].event_id().as_deref(), Some(thread_resp1_redaction));

        // The room event is redacted.
        {
            assert_let!(VectorDiff::Set { index: 1, value: new_event } = &diffs[1]);
            let deserialized = new_event.raw().deserialize().unwrap();

            // TODO: replace with https://github.com/ruma/ruma/pull/2254 when it's been merged in Ruma.
            assert!(match deserialized {
                AnySyncTimelineEvent::MessageLike(ev) => ev.is_redacted(),
                AnySyncTimelineEvent::State(_ev) => unreachable!(),
            });
        }

        // The thread summary is updated.
        {
            assert_let!(VectorDiff::Set { index: 0, value: new_root } = &diffs[2]);
            assert_eq!(new_root.event_id().as_ref(), Some(&thread_root_id));
            let summary = new_root.thread_summary.summary().unwrap();
            assert_eq!(summary.latest_reply.as_ref(), Some(&thread_resp2));
            assert_eq!(summary.num_replies, 1);
        }
    }

    // A redaction for the second (and last) reply comes through sync.
    let thread_resp2_redaction = event_id!("$redact_thread_resp2");
    s.server
        .sync_room(
            &s.client,
            JoinedRoomBuilder::new(&s.room_id)
                .add_timeline_event(f.redaction(&thread_resp2).event_id(thread_resp2_redaction)),
        )
        .await;

    // The redaction affects the thread cache: it *removes* the redacted event.
    {
        assert_let_timeout!(Ok(ThreadEventCacheUpdate { diffs, .. }) = thread_stream.recv());
        assert_eq!(diffs.len(), 1);
        assert_let!(VectorDiff::Remove { index: 1 } = &diffs[0]);

        assert!(thread_stream.is_empty());
    }

    // The redaction affects the room cache too:
    // - the redaction event is pushed to the room history,
    // - the redaction's target is, well, redacted,
    // - the thread summary is removed from the thread root.
    {
        assert_let_timeout!(
            Ok(RoomEventCacheUpdate::UpdateTimelineEvents { diffs, .. }) = room_stream.recv()
        );
        assert_eq!(diffs.len(), 3);

        // The redaction event is appended to the room cache.
        assert_let!(VectorDiff::Append { values: new_events } = &diffs[0]);
        assert_eq!(new_events.len(), 1);
        assert_eq!(new_events[0].event_id().as_deref(), Some(thread_resp2_redaction));

        // The room event is redacted.
        {
            assert_let!(VectorDiff::Set { index: 2, value: new_event } = &diffs[1]);
            let deserialized = new_event.raw().deserialize().unwrap();

            // TODO: replace with https://github.com/ruma/ruma/pull/2254 when it's been merged in Ruma.
            assert!(match deserialized {
                AnySyncTimelineEvent::MessageLike(ev) => ev.is_redacted(),
                AnySyncTimelineEvent::State(_ev) => unreachable!(),
            });
        }

        // The thread summary is removed.
        {
            assert_let!(VectorDiff::Set { index: 0, value: new_root } = &diffs[2]);
            assert_eq!(new_root.event_id().as_ref(), Some(&thread_root_id));
            assert!(new_root.thread_summary.summary().is_none());
        }
    }
}