Skip to content

Commit 52dc64e

Browse files
committed
timeline: add doc comments here and there
1 parent 7b7ee98 commit 52dc64e

File tree

7 files changed

+91
-25
lines changed

7 files changed

+91
-25
lines changed

crates/matrix-sdk-ui/src/timeline/event_handler.rs

Lines changed: 59 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,28 @@ use super::{
6161
};
6262
use crate::{events::SyncTimelineEventWithoutContent, DEFAULT_SANITIZER_MODE};
6363

64+
/// When adding an event, useful information related to the source of the event.
6465
#[derive(Clone)]
6566
pub(super) enum Flow {
67+
/// The event was locally created.
6668
Local {
69+
/// The transaction id we've used in requests associated to this event.
6770
txn_id: OwnedTransactionId,
6871
},
72+
73+
/// The event has been received from a remote source (sync, pagination,
74+
/// etc.). This can be a "remote echo".
6975
Remote {
76+
/// The event identifier as returned by the server.
7077
event_id: OwnedEventId,
78+
/// The transaction id we might have used, if we're the sender of the
79+
/// event.
7180
txn_id: Option<OwnedTransactionId>,
81+
/// The raw serialized JSON event.
7282
raw_event: Raw<AnySyncTimelineEvent>,
83+
/// Where should this be added in the timeline.
7384
position: TimelineItemPosition,
85+
/// Should this event actually be added, based on the event filters.
7486
should_add: bool,
7587
},
7688
}
@@ -194,29 +206,53 @@ impl TimelineEventKind {
194206
}
195207
}
196208

209+
/// The position at which to perform an update of the timeline with events.
197210
#[derive(Clone, Copy, Debug)]
198211
pub(super) enum TimelineItemPosition {
212+
/// One or more items are prepended to the timeline (i.e. they're the
213+
/// oldest).
214+
///
215+
/// Usually this means items coming from back-pagination.
199216
Start,
217+
218+
/// One or more items are appended to the timeline (i.e. they're the most
219+
/// recent).
200220
End {
201221
/// Whether this event is coming from a local cache.
202222
from_cache: bool,
203223
},
224+
225+
/// A single item is updated.
226+
///
227+
/// This only happens when a UTD must be replaced with the decrypted event.
204228
#[cfg(feature = "e2e-encryption")]
205229
Update(usize),
206230
}
207231

232+
/// The outcome of handling a single event with
233+
/// [`TimelineEventHandler::handle_event`].
208234
#[derive(Default)]
209235
pub(super) struct HandleEventResult {
236+
/// Was some timeline item added?
210237
pub(super) item_added: bool,
238+
239+
/// Was some timeline item removed?
240+
///
241+
/// This can happen only if there was a UTD item that has been decrypted
242+
/// into an item that was filtered out with the event filter.
211243
#[cfg(feature = "e2e-encryption")]
212244
pub(super) item_removed: bool,
245+
246+
/// How many items were updated?
213247
pub(super) items_updated: u16,
214248
}
215249

216-
// Bundles together a few things that are needed throughout the different stages
217-
// of handling an event (figuring out whether it should update an existing
218-
// timeline item, transforming that item or creating a new one, updating the
219-
// reactive Vec).
250+
/// Data necessary to update the timeline, given a single event to handle.
251+
///
252+
/// Bundles together a few things that are needed throughout the different
253+
/// stages of handling an event (figuring out whether it should update an
254+
/// existing timeline item, transforming that item or creating a new one,
255+
/// updating the reactive Vec).
220256
pub(super) struct TimelineEventHandler<'a, 'o> {
221257
items: &'a mut ObservableVectorTransaction<'o, Arc<TimelineItem>>,
222258
meta: &'a mut TimelineInnerMetadata,
@@ -769,11 +805,13 @@ impl<'a, 'o> TimelineEventHandler<'a, 'o> {
769805

770806
let kind: EventTimelineItemKind = match &self.ctx.flow {
771807
Flow::Local { txn_id } => {
772-
let send_state = EventSendState::NotSentYet;
773-
let transaction_id = txn_id.to_owned();
774-
LocalEventTimelineItem { send_state, transaction_id }
808+
LocalEventTimelineItem {
809+
send_state: EventSendState::NotSentYet,
810+
transaction_id: txn_id.to_owned(),
811+
}
775812
}
776813
.into(),
814+
777815
Flow::Remote { event_id, raw_event, position, .. } => {
778816
// Drop pending reactions if the message is redacted.
779817
if let TimelineItemContent::RedactedMessage = content {
@@ -784,9 +822,12 @@ impl<'a, 'o> TimelineEventHandler<'a, 'o> {
784822

785823
let origin = match position {
786824
TimelineItemPosition::Start => RemoteEventOrigin::Pagination,
825+
787826
// We only paginate backwards for now, so End only happens for syncs
788827
TimelineItemPosition::End { from_cache: true } => RemoteEventOrigin::Cache,
828+
789829
TimelineItemPosition::End { from_cache: false } => RemoteEventOrigin::Sync,
830+
790831
#[cfg(feature = "e2e-encryption")]
791832
TimelineItemPosition::Update(idx) => self.items[*idx]
792833
.as_event()
@@ -862,10 +903,12 @@ impl<'a, 'o> TimelineEventHandler<'a, 'o> {
862903
if let Some(day_divider_item) =
863904
self.meta.maybe_create_day_divider_from_timestamps(divider_ts, timestamp)
864905
{
906+
trace!("Adding day divider (remote - start)");
865907
self.items.push_front(day_divider_item);
866908
}
867909
} else {
868910
// The list must always start with a day divider.
911+
trace!("Adding first day divider (remote - start)");
869912
let day_divider =
870913
self.meta.new_timeline_item(VirtualTimelineItem::DayDivider(timestamp));
871914
self.items.push_front(day_divider);
@@ -878,18 +921,22 @@ impl<'a, 'o> TimelineEventHandler<'a, 'o> {
878921
Flow::Remote {
879922
position: TimelineItemPosition::End { .. }, txn_id, event_id, ..
880923
} => {
924+
// Look if we already have a corresponding item somewhere, based on the
925+
// transaction id (if a local echo) or the event id (if a
926+
// duplicate remote event).
881927
let result = rfind_event_item(self.items, |it| {
882928
txn_id.is_some() && it.transaction_id() == txn_id.as_deref()
883929
|| it.event_id() == Some(event_id)
884930
});
885931

886932
let mut removed_event_item_id = None;
887933
let mut removed_day_divider_id = None;
934+
888935
if let Some((idx, old_item)) = result {
889936
if old_item.as_remote().is_some() {
890-
// Item was previously received from the server. This
891-
// should be very rare normally, but with the sliding-
892-
// sync proxy, it is actually very common.
937+
// Item was previously received from the server. This should be very rare
938+
// normally, but with the sliding- sync proxy, it is actually very
939+
// common.
893940
// NOTE: SS proxy workaround.
894941
trace!(?item, old_item = ?*old_item, "Received duplicate event");
895942

@@ -983,7 +1030,7 @@ impl<'a, 'o> TimelineEventHandler<'a, 'o> {
9831030
let old_ts = latest_event.timestamp();
9841031

9851032
if timestamp_to_date(old_ts) != timestamp_to_date(timestamp) {
986-
trace!("Adding day divider (remote)");
1033+
trace!("Adding day divider (remote - end)");
9871034

9881035
let id = match removed_day_divider_id {
9891036
// If a day divider was removed for an item about to be moved and we
@@ -1004,7 +1051,7 @@ impl<'a, 'o> TimelineEventHandler<'a, 'o> {
10041051
}
10051052
} else {
10061053
// If there is no event item, there is no day divider yet.
1007-
trace!("Adding first day divider (remote)");
1054+
trace!("Adding first day divider (remote - end)");
10081055
let new_day_divider =
10091056
self.meta.new_timeline_item(VirtualTimelineItem::DayDivider(timestamp));
10101057
if should_push {

crates/matrix-sdk-ui/src/timeline/event_item/mod.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,10 @@ pub(super) enum EventTimelineItemKind {
7575
/// A wrapper that can contain either a transaction id, or an event id.
7676
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
7777
pub enum EventItemIdentifier {
78+
/// The item is local, identified by its transaction id (to be used in
79+
/// subsequent requests).
7880
TransactionId(OwnedTransactionId),
81+
/// The item is remote, identified by its event id.
7982
EventId(OwnedEventId),
8083
}
8184

@@ -90,9 +93,8 @@ impl EventTimelineItem {
9093
Self { sender, sender_profile, timestamp, content, kind }
9194
}
9295

93-
/// If the supplied low-level `SyncTimelineEventy` is suitable for use as
94-
/// the `latest_event` in a message preview, wrap it as an
95-
/// `EventTimelineItem`.
96+
/// If the supplied low-level `SyncTimelineEvent` is suitable for use as the
97+
/// `latest_event` in a message preview, wrap it as an `EventTimelineItem`.
9698
pub async fn from_latest_event(
9799
client: Client,
98100
room_id: &RoomId,
@@ -347,15 +349,15 @@ impl EventTimelineItem {
347349
/// yet.
348350
pub fn original_json(&self) -> Option<&Raw<AnySyncTimelineEvent>> {
349351
match &self.kind {
350-
EventTimelineItemKind::Local(_local_event) => None,
352+
EventTimelineItemKind::Local(_) => None,
351353
EventTimelineItemKind::Remote(remote_event) => remote_event.original_json.as_ref(),
352354
}
353355
}
354356

355357
/// Get the raw JSON representation of the latest edit, if any.
356358
pub fn latest_edit_json(&self) -> Option<&Raw<AnySyncTimelineEvent>> {
357359
match &self.kind {
358-
EventTimelineItemKind::Local(_local_event) => None,
360+
EventTimelineItemKind::Local(_) => None,
359361
EventTimelineItemKind::Remote(remote_event) => remote_event.latest_edit_json.as_ref(),
360362
}
361363
}
@@ -445,12 +447,14 @@ impl From<RemoteEventTimelineItem> for EventTimelineItemKind {
445447
pub struct Profile {
446448
/// The display name, if set.
447449
pub display_name: Option<String>,
450+
448451
/// Whether the display name is ambiguous.
449452
///
450453
/// Note that in rooms with lazy-loading enabled, this could be `false` even
451454
/// though the display name is actually ambiguous if not all member events
452455
/// have been seen yet.
453456
pub display_name_ambiguous: bool,
457+
454458
/// The avatar URL, if set.
455459
pub avatar_url: Option<OwnedMxcUri>,
456460
}
@@ -531,7 +535,7 @@ mod tests {
531535
use crate::timeline::TimelineDetails;
532536

533537
#[async_test]
534-
async fn latest_message_event_can_be_wrapped_as_a_timeline_item() {
538+
async fn test_latest_message_event_can_be_wrapped_as_a_timeline_item() {
535539
// Given a sync event that is suitable to be used as a latest_event
536540

537541
let room_id = room_id!("!q:x.uk");

crates/matrix-sdk-ui/src/timeline/event_item/reactions.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@
1515
use std::ops::Deref;
1616

1717
use indexmap::IndexMap;
18-
use itertools::Itertools;
18+
use itertools::Itertools as _;
1919
use ruma::{OwnedEventId, OwnedTransactionId, UserId};
2020

2121
use super::EventItemIdentifier;
2222
use crate::timeline::ReactionSenderData;
2323

2424
/// The reactions grouped by key.
2525
///
26-
/// Key: The reaction, usually an emoji.\
26+
/// Key: The reaction, usually an emoji.
2727
/// Value: The group of reactions.
2828
pub type BundledReactions = IndexMap<String, ReactionGroup>;
2929

crates/matrix-sdk-ui/src/timeline/event_item/remote.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,27 @@ use super::BundledReactions;
2929
pub(in crate::timeline) struct RemoteEventTimelineItem {
3030
/// The event ID.
3131
pub event_id: OwnedEventId,
32+
3233
/// All bundled reactions about the event.
3334
pub reactions: BundledReactions,
35+
3436
/// All read receipts for the event.
3537
///
3638
/// The key is the ID of a room member and the value are details about the
3739
/// read receipt.
3840
///
3941
/// Note that currently this ignores threads.
4042
pub read_receipts: IndexMap<OwnedUserId, Receipt>,
43+
4144
/// Whether the event has been sent by the the logged-in user themselves.
4245
pub is_own: bool,
46+
4347
/// Whether the item should be highlighted in the timeline.
4448
pub is_highlighted: bool,
49+
4550
/// Encryption information.
4651
pub encryption_info: Option<EncryptionInfo>,
52+
4753
/// JSON of the original event.
4854
///
4955
/// If the event is edited, this *won't* change, instead `latest_edit_json`
@@ -55,8 +61,10 @@ pub(in crate::timeline) struct RemoteEventTimelineItem {
5561
/// thus the whole event is available), but it's not clear whether there is
5662
/// a clear need for that.
5763
pub original_json: Option<Raw<AnySyncTimelineEvent>>,
64+
5865
/// JSON of the latest edit to this item.
5966
pub latest_edit_json: Option<Raw<AnySyncTimelineEvent>>,
67+
6068
/// Where we got this event from: A sync response or pagination.
6169
pub origin: RemoteEventOrigin,
6270
}

crates/matrix-sdk-ui/src/timeline/inner/state.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,10 @@ pub(in crate::timeline) struct TimelineInnerMetadata {
683683
/// are discarded in the timeline items.
684684
pub all_events: VecDeque<EventMeta>,
685685

686+
/// The next internal identifier for timeline items, used for both local and
687+
/// remote echoes.
686688
next_internal_id: u64,
689+
687690
pub reactions: Reactions,
688691
pub poll_pending_events: PollPendingEvents,
689692
pub fully_read_event: Option<OwnedEventId>,
@@ -706,6 +709,7 @@ pub(in crate::timeline) struct TimelineInnerMetadata {
706709
/// The hook to call whenever we run into a unable-to-decrypt event.
707710
pub(crate) unable_to_decrypt_hook: Option<Arc<UtdHookManager>>,
708711

712+
/// Matrix room version of the timeline's room, or a sensible default.
709713
pub room_version: RoomVersionId,
710714
}
711715

crates/matrix-sdk-ui/src/timeline/tests/reaction_group.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use ruma::{server_name, uint, user_id, EventId, MilliSecondsSinceUnixEpoch, Owne
2020
use crate::timeline::{event_item::EventItemIdentifier, ReactionGroup, ReactionSenderData};
2121

2222
#[test]
23-
fn by_sender() {
23+
fn test_by_sender() {
2424
let alice = ALICE.to_owned();
2525
let bob = BOB.to_owned();
2626

@@ -40,7 +40,7 @@ fn by_sender() {
4040
}
4141

4242
#[test]
43-
fn by_sender_with_empty_group() {
43+
fn test_by_sender_with_empty_group() {
4444
let reaction_group = ReactionGroup::default();
4545

4646
let reactions = reaction_group.by_sender(&ALICE).collect::<Vec<_>>();
@@ -49,7 +49,7 @@ fn by_sender_with_empty_group() {
4949
}
5050

5151
#[test]
52-
fn by_sender_with_multiple_users() {
52+
fn test_by_sender_with_multiple_users() {
5353
let alice = ALICE.to_owned();
5454
let bob = BOB.to_owned();
5555
let carol = user_id!("@carol:other.server");
@@ -76,7 +76,7 @@ fn by_sender_with_multiple_users() {
7676
/// is still possible for duplicates to be received over federation. And in
7777
/// that case, clients are expected to treat duplicates as a single annotation.
7878
#[test]
79-
fn senders_are_deduplicated() {
79+
fn test_senders_are_deduplicated() {
8080
let group = {
8181
let mut group = ReactionGroup::default();
8282
insert(&mut group, &ALICE, 3);
@@ -89,7 +89,7 @@ fn senders_are_deduplicated() {
8989
}
9090

9191
#[test]
92-
fn timestamps_are_stored() {
92+
fn test_timestamps_are_stored() {
9393
let reaction = new_reaction();
9494
let reaction_2 = new_reaction();
9595
let timestamp = MilliSecondsSinceUnixEpoch(uint!(0));

crates/matrix-sdk-ui/src/timeline/util.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@ use chrono::{Datelike, Local, TimeZone};
1818
use imbl::Vector;
1919
use ruma::{EventId, MilliSecondsSinceUnixEpoch};
2020

21+
#[cfg(doc)]
22+
use super::inner::TimelineInnerMetadata;
2123
use super::{event_item::EventTimelineItemKind, EventTimelineItem, TimelineItem};
2224

2325
pub(super) struct EventTimelineItemWithId<'a> {
2426
pub inner: &'a EventTimelineItem,
27+
/// Internal identifier generated by [`TimelineInnerMetadata`].
2528
pub internal_id: u64,
2629
}
2730

0 commit comments

Comments
 (0)