matrix_sdk_ui/timeline/
date_dividers.rs

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
// Copyright 2024 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.

//! Algorithm to adjust (insert/replace/remove) date dividers after new events
//! have been received from any source.

use std::{fmt::Display, sync::Arc};

use ruma::MilliSecondsSinceUnixEpoch;
use tracing::{error, event_enabled, instrument, trace, warn, Level};

use super::{
    controller::{ObservableItemsTransaction, TimelineMetadata},
    util::timestamp_to_date,
    DateDividerMode, TimelineItem, TimelineItemKind, VirtualTimelineItem,
};

/// Algorithm ensuring that date dividers are adjusted correctly, according to
/// new items that have been inserted.
pub(super) struct DateDividerAdjuster {
    /// The list of recorded operations to apply, after analyzing the latest
    /// items.
    ops: Vec<DateDividerOperation>,

    /// A boolean indicating whether the struct has been used and thus must be
    /// mark unused manually by calling [`Self::run`].
    consumed: bool,

    mode: DateDividerMode,
}

impl Drop for DateDividerAdjuster {
    fn drop(&mut self) {
        // Only run the assert if we're not currently panicking.
        if !std::thread::panicking() && !self.consumed {
            error!("a DateDividerAdjuster has not been consumed with run()");
        }
    }
}

/// A descriptor for a previous item.
struct PrevItemDesc<'a> {
    /// The index of the item in the `self.items` array.
    item_index: usize,

    /// The previous timeline item.
    item: &'a Arc<TimelineItem>,

    // The insert position of the operation in the `ops` array.
    insert_op_at: usize,
}

impl DateDividerAdjuster {
    pub fn new(mode: DateDividerMode) -> Self {
        Self {
            ops: Default::default(),
            // The adjuster starts as consumed, and it will be marked no consumed iff it's used
            // with `mark_used`.
            consumed: true,
            mode,
        }
    }

    /// Marks this [`DateDividerAdjuster`] as used, which means it'll require a
    /// call to [`DateDividerAdjuster::run`] before getting dropped.
    pub fn mark_used(&mut self) {
        // Mark the adjuster as needing to be consumed.
        self.consumed = false;
    }

    /// Ensures that date separators are properly inserted/removed when needs
    /// be.
    #[instrument(skip_all)]
    pub fn run(&mut self, items: &mut ObservableItemsTransaction<'_>, meta: &mut TimelineMetadata) {
        // We're going to record vector operations like inserting, replacing and
        // removing date dividers. Since we may remove or insert new items,
        // recorded offsets will change as we're iterating over the array. The
        // only way this is possible is because we're recording operations
        // happening in non-decreasing order of the indices, i.e. we can't do an
        // operation on index I and then on any index J<I later.
        //
        // Note we can't "just" iterate in reverse order, because we may have a
        // `Remove(i)` followed by a `Replace((i+1) -1)`, which wouldn't do what
        // we want, if running in reverse order.
        //
        // Also note that we can remove a few items at position J, then later decide to
        // replace/remove an item (in `handle_event`) at position I, with I<J. That
        // would break the above invariant (that operations happen in
        // non-decreasing order of the indices), so we must record the insert
        // position for an operation related to the previous item.

        let mut prev_item: Option<PrevItemDesc<'_>> = None;
        let mut latest_event_ts = None;

        for (i, item) in items.iter().enumerate() {
            match item.kind() {
                TimelineItemKind::Virtual(VirtualTimelineItem::DateDivider(ts)) => {
                    // Record what the last alive item pair is only if we haven't removed the date
                    // divider.
                    if !self.handle_date_divider(i, *ts, prev_item.as_ref().map(|desc| desc.item)) {
                        prev_item = Some(PrevItemDesc {
                            item_index: i,
                            item,
                            insert_op_at: self.ops.len(),
                        });
                    }
                }

                TimelineItemKind::Event(event) => {
                    let ts = event.timestamp();

                    self.handle_event(i, ts, prev_item, latest_event_ts);

                    prev_item =
                        Some(PrevItemDesc { item_index: i, item, insert_op_at: self.ops.len() });
                    latest_event_ts = Some(ts);
                }

                TimelineItemKind::Virtual(VirtualTimelineItem::ReadMarker) => {
                    // Nothing to do.
                }
            }
        }

        // Also chase trailing date dividers explicitly, by iterating from the end to
        // the start. Since they wouldn't be the prev_item of anything, we
        // wouldn't analyze them in the previous loop.
        for (i, item) in items.iter().enumerate().rev() {
            if item.is_date_divider() {
                // The item is a trailing date divider: remove it, if it wasn't already
                // scheduled for deletion.
                if !self
                    .ops
                    .iter()
                    .any(|op| matches!(op, DateDividerOperation::Remove(j) if i == *j))
                {
                    trace!("removing trailing date divider @ {i}");

                    // Find the index at which to insert the removal operation. It must be before
                    // any other operation on a bigger index, to maintain the
                    // non-decreasing invariant.
                    let index =
                        self.ops.iter().position(|op| op.index() > i).unwrap_or(self.ops.len());

                    self.ops.insert(index, DateDividerOperation::Remove(i));
                }
            }

            if item.is_event() {
                // Stop as soon as we run into the first (trailing) event.
                break;
            }
        }

        // Only record the initial state if we've enabled the trace log level, and not
        // otherwise.
        let initial_state =
            if event_enabled!(Level::TRACE) { Some(items.iter().cloned().collect()) } else { None };

        self.process_ops(items, meta);

        // Then check invariants.
        if let Some(report) = self.check_invariants(items, initial_state) {
            warn!("Errors encountered when checking invariants.");
            warn!("{report}");
            #[cfg(any(debug_assertions, test))]
            panic!("There was an error checking date separator invariants");
        }

        self.consumed = true;
    }

    /// Decides what to do with a date divider.
    ///
    /// Returns whether it's been removed or not.
    #[inline]
    fn handle_date_divider(
        &mut self,
        i: usize,
        ts: MilliSecondsSinceUnixEpoch,
        prev_item: Option<&Arc<TimelineItem>>,
    ) -> bool {
        let Some(prev_item) = prev_item else {
            // No interesting item prior to the date divider: it must be the first one,
            // nothing to do.
            return false;
        };

        match prev_item.kind() {
            TimelineItemKind::Event(event) => {
                // This date divider is preceded by an event.
                if self.is_same_date_divider_group_as(event.timestamp(), ts) {
                    // The event has the same date as the date divider: remove the current date
                    // divider.
                    trace!("removing date divider following event with same timestamp @ {i}");
                    self.ops.push(DateDividerOperation::Remove(i));
                    return true;
                }
            }

            TimelineItemKind::Virtual(VirtualTimelineItem::DateDivider(_)) => {
                trace!("removing duplicate date divider @ {i}");
                // This date divider is preceded by another one: remove the current one.
                self.ops.push(DateDividerOperation::Remove(i));
                return true;
            }

            TimelineItemKind::Virtual(VirtualTimelineItem::ReadMarker) => {
                // Nothing to do for read markers.
            }
        }

        false
    }

    #[inline]
    fn handle_event(
        &mut self,
        i: usize,
        ts: MilliSecondsSinceUnixEpoch,
        prev_item_desc: Option<PrevItemDesc<'_>>,
        latest_event_ts: Option<MilliSecondsSinceUnixEpoch>,
    ) {
        let Some(PrevItemDesc { item_index, insert_op_at, item }) = prev_item_desc else {
            // The event was the first item, so there wasn't any date divider before it:
            // insert one.
            trace!("inserting the first date divider @ {}", i);
            self.ops.push(DateDividerOperation::Insert(i, ts));
            return;
        };

        match item.kind() {
            TimelineItemKind::Event(prev_event) => {
                // The event is preceded by another event. If they're not the same date,
                // insert a date divider.
                let prev_ts = prev_event.timestamp();

                if !self.is_same_date_divider_group_as(prev_ts, ts) {
                    trace!(
                        "inserting date divider @ {} between two events with different dates",
                        i
                    );
                    self.ops.push(DateDividerOperation::Insert(i, ts));
                }
            }

            TimelineItemKind::Virtual(VirtualTimelineItem::DateDivider(prev_ts)) => {
                let event_date = timestamp_to_date(ts);

                // The event is preceded by a date divider.
                if timestamp_to_date(*prev_ts) != event_date {
                    // The date divider is wrong. Should we replace it with the correct value, or
                    // remove it entirely?
                    if let Some(last_event_ts) = latest_event_ts {
                        if timestamp_to_date(last_event_ts) == event_date {
                            // There's a previous event with the same date: remove the divider.
                            trace!("removed date divider @ {item_index} between two events that have the same date");
                            self.ops.insert(insert_op_at, DateDividerOperation::Remove(item_index));
                            return;
                        }
                    }

                    // There's no previous event or there's one with a different date: replace
                    // the current divider.
                    trace!("replacing date divider @ {item_index} with new timestamp from event");
                    self.ops.insert(insert_op_at, DateDividerOperation::Replace(item_index, ts));
                }
            }

            TimelineItemKind::Virtual(VirtualTimelineItem::ReadMarker) => {
                // Nothing to do.
            }
        }
    }

    fn process_ops(&self, items: &mut ObservableItemsTransaction<'_>, meta: &mut TimelineMetadata) {
        // Record the deletion offset.
        let mut offset = 0i64;
        // Remember what the maximum index was, so we can assert that it's
        // non-decreasing.
        let mut max_i = 0;

        for op in &self.ops {
            match *op {
                DateDividerOperation::Insert(i, ts) => {
                    assert!(i >= max_i, "trying to insert at {i} < max_i={max_i}");

                    let at = (i64::try_from(i).unwrap() + offset)
                        .min(i64::try_from(items.len()).unwrap());
                    assert!(at >= 0);
                    let at = at as usize;

                    let item = meta.new_timeline_item(VirtualTimelineItem::DateDivider(ts));

                    // Keep push semantics, if we're inserting at the front or the back.
                    if at == items.len() {
                        items.push_back(item, None);
                    } else if at == 0 {
                        items.push_front(item, None);
                    } else {
                        items.insert(at, item, None);
                    }

                    offset += 1;
                    max_i = i;
                }

                DateDividerOperation::Replace(i, ts) => {
                    assert!(i >= max_i, "trying to replace at {i} < max_i={max_i}");

                    let at = i64::try_from(i).unwrap() + offset;
                    assert!(at >= 0);
                    let at = at as usize;

                    let replaced = &items[at];
                    if !replaced.is_date_divider() {
                        error!("we replaced a non date-divider @ {i}: {:?}", replaced.kind());
                    }

                    let unique_id = replaced.unique_id();
                    let item = TimelineItem::new(
                        VirtualTimelineItem::DateDivider(ts),
                        unique_id.to_owned(),
                    );

                    items.replace(at, item);
                    max_i = i;
                }

                DateDividerOperation::Remove(i) => {
                    assert!(i >= max_i, "trying to replace at {i} < max_i={max_i}");

                    let at = i64::try_from(i).unwrap() + offset;
                    assert!(at >= 0);

                    let removed = items.remove(at as usize);
                    if !removed.is_date_divider() {
                        error!("we removed a non date-divider @ {i}: {:?}", removed.kind());
                    }

                    offset -= 1;
                    max_i = i;
                }
            }
        }
    }

    /// Checks the invariants that must hold at any time after inserting date
    /// dividers.
    ///
    /// Returns a report if and only if there was at least one error.
    fn check_invariants<'a, 'o>(
        &mut self,
        items: &'a ObservableItemsTransaction<'o>,
        initial_state: Option<Vec<Arc<TimelineItem>>>,
    ) -> Option<DateDividerInvariantsReport<'a, 'o>> {
        let mut report = DateDividerInvariantsReport {
            initial_state,
            errors: Vec::new(),
            operations: std::mem::take(&mut self.ops),
            final_state: items,
        };

        // Assert invariants.
        // 1. The timeline starts with a date divider.
        if let Some(item) = items.get(0) {
            if item.is_read_marker() {
                if let Some(next_item) = items.get(1) {
                    if !next_item.is_date_divider() {
                        report.errors.push(DateDividerInsertError::FirstItemNotDateDivider);
                    }
                }
            } else if !item.is_date_divider() {
                report.errors.push(DateDividerInsertError::FirstItemNotDateDivider);
            }
        }

        // 2. There are no two date dividers following each other.
        {
            let mut prev_was_date_divider = false;
            for (i, item) in items.iter().enumerate() {
                if item.is_date_divider() {
                    if prev_was_date_divider {
                        report.errors.push(DateDividerInsertError::DuplicateDateDivider { at: i });
                    }
                    prev_was_date_divider = true;
                } else {
                    prev_was_date_divider = false;
                }
            }
        };

        // 3. There's no trailing date divider.
        if let Some(last) = items.last() {
            if last.is_date_divider() {
                report.errors.push(DateDividerInsertError::TrailingDateDivider);
            }
        }

        // 4. Items are properly separated with date dividers.
        {
            let mut prev_event_ts = None;
            let mut prev_date_divider_ts = None;

            for (i, item) in items.iter().enumerate() {
                if let Some(ev) = item.as_event() {
                    let ts = ev.timestamp();

                    // We have the same date as the previous event we've seen.
                    if let Some(prev_ts) = prev_event_ts {
                        if !self.is_same_date_divider_group_as(prev_ts, ts) {
                            report.errors.push(
                                DateDividerInsertError::MissingDateDividerBetweenEvents { at: i },
                            );
                        }
                    }

                    // There is a date divider before us, and it's the same date as our timestamp.
                    if let Some(prev_ts) = prev_date_divider_ts {
                        if !self.is_same_date_divider_group_as(prev_ts, ts) {
                            report.errors.push(
                                DateDividerInsertError::InconsistentDateAfterPreviousDateDivider {
                                    at: i,
                                },
                            );
                        }
                    } else {
                        report
                            .errors
                            .push(DateDividerInsertError::MissingDateDividerBeforeEvent { at: i });
                    }

                    prev_event_ts = Some(ts);
                } else if let TimelineItemKind::Virtual(VirtualTimelineItem::DateDivider(ts)) =
                    item.kind()
                {
                    // The previous date divider is for a different date.
                    if let Some(prev_ts) = prev_date_divider_ts {
                        if self.is_same_date_divider_group_as(prev_ts, *ts) {
                            report
                                .errors
                                .push(DateDividerInsertError::DuplicateDateDivider { at: i });
                        }
                    }

                    prev_event_ts = None;
                    prev_date_divider_ts = Some(*ts);
                }
            }
        }

        // 5. If there was a read marker at the beginning, there should be one at the
        //    end.
        if let Some(state) = &report.initial_state {
            if state.iter().any(|item| item.is_read_marker())
                && !report.final_state.iter().any(|item| item.is_read_marker())
            {
                report.errors.push(DateDividerInsertError::ReadMarkerDisappeared);
            }
        }

        if report.errors.is_empty() {
            None
        } else {
            Some(report)
        }
    }

    /// Returns whether the two dates for the given timestamps are the same or
    /// not.
    fn is_same_date_divider_group_as(
        &self,
        lhs: MilliSecondsSinceUnixEpoch,
        rhs: MilliSecondsSinceUnixEpoch,
    ) -> bool {
        match self.mode {
            DateDividerMode::Daily => timestamp_to_date(lhs) == timestamp_to_date(rhs),
            DateDividerMode::Monthly => {
                timestamp_to_date(lhs).is_same_month_as(timestamp_to_date(rhs))
            }
        }
    }
}

#[derive(Debug)]
enum DateDividerOperation {
    Insert(usize, MilliSecondsSinceUnixEpoch),
    Replace(usize, MilliSecondsSinceUnixEpoch),
    Remove(usize),
}

impl DateDividerOperation {
    fn index(&self) -> usize {
        match self {
            DateDividerOperation::Insert(i, _)
            | DateDividerOperation::Replace(i, _)
            | DateDividerOperation::Remove(i) => *i,
        }
    }
}

/// A report returned by [`DateDividerAdjuster::check_invariants`].
struct DateDividerInvariantsReport<'a, 'o> {
    /// Initial state before inserting the items.
    initial_state: Option<Vec<Arc<TimelineItem>>>,
    /// The operations that have been applied on the list.
    operations: Vec<DateDividerOperation>,
    /// Final state after inserting the date dividers.
    final_state: &'a ObservableItemsTransaction<'o>,
    /// Errors encountered in the algorithm.
    errors: Vec<DateDividerInsertError>,
}

impl Display for DateDividerInvariantsReport<'_, '_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        // Write all the items of a slice of timeline items.
        fn write_items(
            f: &mut std::fmt::Formatter<'_>,
            items: &[Arc<TimelineItem>],
        ) -> std::fmt::Result {
            for (i, item) in items.iter().enumerate() {
                if let TimelineItemKind::Virtual(VirtualTimelineItem::DateDivider(ts)) = item.kind()
                {
                    writeln!(f, "#{i} --- {}", ts.0)?;
                } else if let Some(event) = item.as_event() {
                    // id: timestamp
                    writeln!(
                        f,
                        "#{i} {}: {}",
                        event
                            .event_id()
                            .map_or_else(|| "(no event id)".to_owned(), |id| id.to_string()),
                        event.timestamp().0
                    )?;
                } else {
                    writeln!(f, "#{i} (other virtual item)")?;
                }
            }

            Ok(())
        }

        if let Some(initial_state) = &self.initial_state {
            writeln!(f, "Initial state:")?;
            write_items(f, initial_state)?;

            writeln!(f, "\nOperations to apply:")?;
            for op in &self.operations {
                match *op {
                    DateDividerOperation::Insert(i, ts) => writeln!(f, "insert @ {i}: {}", ts.0)?,
                    DateDividerOperation::Replace(i, ts) => writeln!(f, "replace @ {i}: {}", ts.0)?,
                    DateDividerOperation::Remove(i) => writeln!(f, "remove @ {i}")?,
                }
            }

            writeln!(f, "\nFinal state:")?;
            write_items(f, self.final_state.iter().cloned().collect::<Vec<_>>().as_slice())?;

            writeln!(f)?;
        }

        for err in &self.errors {
            writeln!(f, "{err}")?;
        }

        Ok(())
    }
}

#[derive(Debug, thiserror::Error)]
enum DateDividerInsertError {
    /// The first item isn't a date divider.
    #[error("The first item isn't a date divider")]
    FirstItemNotDateDivider,

    /// There are two date dividers for the same date.
    #[error("Duplicate date divider @ {at}.")]
    DuplicateDateDivider { at: usize },

    /// The last item is a date divider.
    #[error("The last item is a date divider.")]
    TrailingDateDivider,

    /// Two events are following each other but they have different dates
    /// without a date divider between them.
    #[error("Missing date divider between events @ {at}")]
    MissingDateDividerBetweenEvents { at: usize },

    /// Some event is missing a date divider before it.
    #[error("Missing date divider before event @ {at}")]
    MissingDateDividerBeforeEvent { at: usize },

    /// An event and the previous date divider aren't focused on the same date.
    #[error("Event @ {at} and the previous date divider aren't targeting the same date")]
    InconsistentDateAfterPreviousDateDivider { at: usize },

    /// The read marker has been removed.
    #[error("The read marker has been removed")]
    ReadMarkerDisappeared,
}

#[cfg(test)]
mod tests {
    use assert_matches2::assert_let;
    use ruma::{owned_event_id, owned_user_id, uint, MilliSecondsSinceUnixEpoch};

    use super::{super::controller::ObservableItems, DateDividerAdjuster};
    use crate::timeline::{
        controller::TimelineMetadata,
        event_item::{EventTimelineItemKind, RemoteEventTimelineItem},
        util::timestamp_to_date,
        DateDividerMode, EventTimelineItem, TimelineItemContent, VirtualTimelineItem,
    };

    fn event_with_ts(timestamp: MilliSecondsSinceUnixEpoch) -> EventTimelineItem {
        let event_kind = EventTimelineItemKind::Remote(RemoteEventTimelineItem {
            event_id: owned_event_id!("$1"),
            transaction_id: None,
            read_receipts: Default::default(),
            is_own: false,
            is_highlighted: false,
            encryption_info: None,
            original_json: None,
            latest_edit_json: None,
            origin: crate::timeline::event_item::RemoteEventOrigin::Sync,
        });
        EventTimelineItem::new(
            owned_user_id!("@alice:example.org"),
            crate::timeline::TimelineDetails::Pending,
            timestamp,
            TimelineItemContent::RedactedMessage,
            event_kind,
            Default::default(),
            false,
        )
    }

    fn test_metadata() -> TimelineMetadata {
        TimelineMetadata::new(
            owned_user_id!("@a:b.c"),
            ruma::RoomVersionId::V11,
            None,
            None,
            Some(false),
        )
    }

    #[test]
    fn test_no_trailing_date_divider() {
        let mut items = ObservableItems::new();
        let mut txn = items.transaction();

        let mut meta = test_metadata();

        let timestamp = MilliSecondsSinceUnixEpoch(uint!(42));
        let timestamp_next_day =
            MilliSecondsSinceUnixEpoch((42 + 3600 * 24 * 1000).try_into().unwrap());

        txn.push_back(meta.new_timeline_item(event_with_ts(timestamp)), None);
        txn.push_back(
            meta.new_timeline_item(VirtualTimelineItem::DateDivider(timestamp_next_day)),
            None,
        );
        txn.push_back(meta.new_timeline_item(VirtualTimelineItem::ReadMarker), None);

        let mut adjuster = DateDividerAdjuster::new(DateDividerMode::Daily);
        adjuster.run(&mut txn, &mut meta);

        txn.commit();

        let mut iter = items.iter();

        assert_let!(Some(item) = iter.next());
        assert!(item.is_date_divider());

        assert_let!(Some(item) = iter.next());
        assert!(item.is_remote_event());

        assert_let!(Some(item) = iter.next());
        assert!(item.is_read_marker());

        assert!(iter.next().is_none());
    }

    #[test]
    fn test_read_marker_in_between_event_and_date_divider() {
        let mut items = ObservableItems::new();
        let mut txn = items.transaction();

        let mut meta = test_metadata();

        let timestamp = MilliSecondsSinceUnixEpoch(uint!(42));
        let timestamp_next_day =
            MilliSecondsSinceUnixEpoch((42 + 3600 * 24 * 1000).try_into().unwrap());
        assert_ne!(timestamp_to_date(timestamp), timestamp_to_date(timestamp_next_day));

        let event = event_with_ts(timestamp);
        txn.push_back(meta.new_timeline_item(event.clone()), None);
        txn.push_back(
            meta.new_timeline_item(VirtualTimelineItem::DateDivider(timestamp_next_day)),
            None,
        );
        txn.push_back(meta.new_timeline_item(VirtualTimelineItem::ReadMarker), None);
        txn.push_back(meta.new_timeline_item(event), None);

        let mut adjuster = DateDividerAdjuster::new(DateDividerMode::Daily);
        adjuster.run(&mut txn, &mut meta);

        txn.commit();

        let mut iter = items.iter();

        assert!(iter.next().unwrap().is_date_divider());
        assert!(iter.next().unwrap().is_remote_event());
        assert!(iter.next().unwrap().is_read_marker());
        assert!(iter.next().unwrap().is_remote_event());
        assert!(iter.next().is_none());
    }

    #[test]
    fn test_read_marker_in_between_date_dividers() {
        let mut items = ObservableItems::new();
        let mut txn = items.transaction();

        let mut meta = test_metadata();

        let timestamp = MilliSecondsSinceUnixEpoch(uint!(42));
        let timestamp_next_day =
            MilliSecondsSinceUnixEpoch((42 + 3600 * 24 * 1000).try_into().unwrap());
        assert_ne!(timestamp_to_date(timestamp), timestamp_to_date(timestamp_next_day));

        txn.push_back(meta.new_timeline_item(event_with_ts(timestamp)), None);
        txn.push_back(meta.new_timeline_item(VirtualTimelineItem::DateDivider(timestamp)), None);
        txn.push_back(meta.new_timeline_item(VirtualTimelineItem::DateDivider(timestamp)), None);
        txn.push_back(meta.new_timeline_item(VirtualTimelineItem::ReadMarker), None);
        txn.push_back(meta.new_timeline_item(VirtualTimelineItem::DateDivider(timestamp)), None);
        txn.push_back(meta.new_timeline_item(event_with_ts(timestamp_next_day)), None);

        let mut adjuster = DateDividerAdjuster::new(DateDividerMode::Daily);
        adjuster.run(&mut txn, &mut meta);

        txn.commit();

        let mut iter = items.iter();

        assert!(iter.next().unwrap().is_date_divider());
        assert!(iter.next().unwrap().is_remote_event());
        assert!(iter.next().unwrap().is_read_marker());
        assert!(iter.next().unwrap().is_date_divider());
        assert!(iter.next().unwrap().is_remote_event());
        assert!(iter.next().is_none());
    }

    #[test]
    fn test_remove_all_date_dividers() {
        let mut items = ObservableItems::new();
        let mut txn = items.transaction();

        let mut meta = test_metadata();

        let timestamp = MilliSecondsSinceUnixEpoch(uint!(42));
        let timestamp_next_day =
            MilliSecondsSinceUnixEpoch((42 + 3600 * 24 * 1000).try_into().unwrap());
        assert_ne!(timestamp_to_date(timestamp), timestamp_to_date(timestamp_next_day));

        txn.push_back(meta.new_timeline_item(event_with_ts(timestamp_next_day)), None);
        txn.push_back(meta.new_timeline_item(VirtualTimelineItem::DateDivider(timestamp)), None);
        txn.push_back(meta.new_timeline_item(VirtualTimelineItem::DateDivider(timestamp)), None);
        txn.push_back(meta.new_timeline_item(event_with_ts(timestamp_next_day)), None);

        let mut adjuster = DateDividerAdjuster::new(DateDividerMode::Daily);
        adjuster.run(&mut txn, &mut meta);

        txn.commit();

        let mut iter = items.iter();

        assert!(iter.next().unwrap().is_date_divider());
        assert!(iter.next().unwrap().is_remote_event());
        assert!(iter.next().unwrap().is_remote_event());
        assert!(iter.next().is_none());
    }

    #[test]
    fn test_event_read_marker_spurious_date_divider() {
        let mut items = ObservableItems::new();
        let mut txn = items.transaction();

        let mut meta = test_metadata();

        let timestamp = MilliSecondsSinceUnixEpoch(uint!(42));

        txn.push_back(meta.new_timeline_item(event_with_ts(timestamp)), None);
        txn.push_back(meta.new_timeline_item(VirtualTimelineItem::ReadMarker), None);
        txn.push_back(meta.new_timeline_item(VirtualTimelineItem::DateDivider(timestamp)), None);

        let mut adjuster = DateDividerAdjuster::new(DateDividerMode::Daily);
        adjuster.run(&mut txn, &mut meta);

        txn.commit();

        let mut iter = items.iter();

        assert!(iter.next().unwrap().is_date_divider());
        assert!(iter.next().unwrap().is_remote_event());
        assert!(iter.next().unwrap().is_read_marker());
        assert!(iter.next().is_none());
    }

    #[test]
    fn test_multiple_trailing_date_dividers() {
        let mut items = ObservableItems::new();
        let mut txn = items.transaction();

        let mut meta = test_metadata();

        let timestamp = MilliSecondsSinceUnixEpoch(uint!(42));

        txn.push_back(meta.new_timeline_item(VirtualTimelineItem::ReadMarker), None);
        txn.push_back(meta.new_timeline_item(VirtualTimelineItem::DateDivider(timestamp)), None);
        txn.push_back(meta.new_timeline_item(VirtualTimelineItem::DateDivider(timestamp)), None);

        let mut adjuster = DateDividerAdjuster::new(DateDividerMode::Daily);
        adjuster.run(&mut txn, &mut meta);

        txn.commit();

        let mut iter = items.iter();

        assert!(iter.next().unwrap().is_read_marker());
        assert!(iter.next().is_none());
    }

    #[test]
    fn test_start_with_read_marker() {
        let mut items = ObservableItems::new();
        let mut txn = items.transaction();

        let mut meta = test_metadata();
        let timestamp = MilliSecondsSinceUnixEpoch(uint!(42));

        txn.push_back(meta.new_timeline_item(VirtualTimelineItem::ReadMarker), None);
        txn.push_back(meta.new_timeline_item(event_with_ts(timestamp)), None);

        let mut adjuster = DateDividerAdjuster::new(DateDividerMode::Daily);
        adjuster.run(&mut txn, &mut meta);

        txn.commit();

        let mut iter = items.iter();

        assert!(iter.next().unwrap().is_read_marker());
        assert!(iter.next().unwrap().is_date_divider());
        assert!(iter.next().unwrap().is_remote_event());
        assert!(iter.next().is_none());
    }

    #[test]
    fn test_daily_divider_mode() {
        let mut items = ObservableItems::new();
        let mut txn = items.transaction();

        let mut meta = test_metadata();

        txn.push_back(
            meta.new_timeline_item(event_with_ts(MilliSecondsSinceUnixEpoch(uint!(0)))),
            None,
        );
        txn.push_back(
            meta.new_timeline_item(event_with_ts(MilliSecondsSinceUnixEpoch(uint!(86_400_000)))), // One day later
            None,
        );
        txn.push_back(
            meta.new_timeline_item(event_with_ts(MilliSecondsSinceUnixEpoch(uint!(2_678_400_000)))), // One month later
            None,
        );

        let mut adjuster = DateDividerAdjuster::new(DateDividerMode::Daily);
        adjuster.run(&mut txn, &mut meta);

        txn.commit();

        let mut iter = items.iter();

        assert!(iter.next().unwrap().is_date_divider());
        assert!(iter.next().unwrap().is_remote_event());
        assert!(iter.next().unwrap().is_date_divider());
        assert!(iter.next().unwrap().is_remote_event());
        assert!(iter.next().unwrap().is_date_divider());
        assert!(iter.next().unwrap().is_remote_event());
        assert!(iter.next().is_none());
    }

    #[test]
    fn test_monthly_divider_mode() {
        let mut items = ObservableItems::new();
        let mut txn = items.transaction();

        let mut meta = test_metadata();

        txn.push_back(
            meta.new_timeline_item(event_with_ts(MilliSecondsSinceUnixEpoch(uint!(0)))),
            None,
        );
        txn.push_back(
            meta.new_timeline_item(event_with_ts(MilliSecondsSinceUnixEpoch(uint!(86_400_000)))), // One day later
            None,
        );
        txn.push_back(
            meta.new_timeline_item(event_with_ts(MilliSecondsSinceUnixEpoch(uint!(2_678_400_000)))), // One month later
            None,
        );

        let mut adjuster = DateDividerAdjuster::new(DateDividerMode::Monthly);
        adjuster.run(&mut txn, &mut meta);

        txn.commit();

        let mut iter = items.iter();

        assert!(iter.next().unwrap().is_date_divider());
        assert!(iter.next().unwrap().is_remote_event());
        assert!(iter.next().unwrap().is_remote_event());
        assert!(iter.next().unwrap().is_date_divider());
        assert!(iter.next().unwrap().is_remote_event());
        assert!(iter.next().is_none());
    }
}