@@ -61,16 +61,28 @@ use super::{
6161} ;
6262use crate :: { events:: SyncTimelineEventWithoutContent , DEFAULT_SANITIZER_MODE } ;
6363
64+ /// When adding an event, useful information related to the source of the event.
6465#[ derive( Clone ) ]
6566pub ( 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 ) ]
198211pub ( 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 ) ]
209235pub ( 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).
220256pub ( 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 {
0 commit comments