Skip to content

Commit 35f6a7e

Browse files
committed
refactor(timeline): simplify removal of duplicated local echo item
1 parent 9dc3646 commit 35f6a7e

File tree

1 file changed

+34
-51
lines changed

1 file changed

+34
-51
lines changed

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

Lines changed: 34 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,7 +1050,7 @@ impl<'a, 'o> TimelineEventHandler<'a, 'o> {
10501050

10511051
let is_room_encrypted = self.meta.is_room_encrypted;
10521052

1053-
let mut item = EventTimelineItem::new(
1053+
let item = EventTimelineItem::new(
10541054
sender,
10551055
sender_profile,
10561056
timestamp,
@@ -1071,13 +1071,13 @@ impl<'a, 'o> TimelineEventHandler<'a, 'o> {
10711071
Flow::Remote {
10721072
position: TimelineItemPosition::Start { .. }, event_id, txn_id, ..
10731073
} => {
1074-
let removed_duplicated_timeline_item = Self::deduplicate_local_timeline_item(
1074+
let item = Self::recycle_local_or_create_item(
10751075
self.items,
1076-
&mut item,
1077-
Some(event_id),
1078-
txn_id.as_ref().map(AsRef::as_ref),
1076+
self.meta,
1077+
item,
1078+
event_id,
1079+
txn_id.as_deref(),
10791080
);
1080-
let item = new_timeline_item(self.meta, item, removed_duplicated_timeline_item);
10811081

10821082
trace!("Adding new remote timeline item at the start");
10831083

@@ -1090,13 +1090,13 @@ impl<'a, 'o> TimelineEventHandler<'a, 'o> {
10901090
txn_id,
10911091
..
10921092
} => {
1093-
let removed_duplicated_timeline_item = Self::deduplicate_local_timeline_item(
1093+
let item = Self::recycle_local_or_create_item(
10941094
self.items,
1095-
&mut item,
1096-
Some(event_id),
1097-
txn_id.as_ref().map(AsRef::as_ref),
1095+
self.meta,
1096+
item,
1097+
event_id,
1098+
txn_id.as_deref(),
10981099
);
1099-
let item = new_timeline_item(self.meta, item, removed_duplicated_timeline_item);
11001100

11011101
let all_remote_events = self.items.all_remote_events();
11021102
let event_index = *event_index;
@@ -1153,13 +1153,13 @@ impl<'a, 'o> TimelineEventHandler<'a, 'o> {
11531153
Flow::Remote {
11541154
position: TimelineItemPosition::End { .. }, event_id, txn_id, ..
11551155
} => {
1156-
let removed_duplicated_timeline_item = Self::deduplicate_local_timeline_item(
1156+
let item = Self::recycle_local_or_create_item(
11571157
self.items,
1158-
&mut item,
1159-
Some(event_id),
1160-
txn_id.as_ref().map(AsRef::as_ref),
1158+
self.meta,
1159+
item,
1160+
event_id,
1161+
txn_id.as_deref(),
11611162
);
1162-
let item = new_timeline_item(self.meta, item, removed_duplicated_timeline_item);
11631163

11641164
// Local events are always at the bottom. Let's find the latest remote event
11651165
// and insert after it, otherwise, if there is no remote event, insert at 0.
@@ -1231,17 +1231,18 @@ impl<'a, 'o> TimelineEventHandler<'a, 'o> {
12311231
}
12321232
}
12331233

1234-
/// Remove the local timeline item matching the `event_id` or the
1235-
/// `transaction_id` of `new_event_timeline_item` if it exists.
1236-
// Note: this method doesn't take `&mut self` to avoid a borrow checker
1237-
// conflict with `TimelineEventHandler::add_item`.
1238-
// TODO(bnjbvr): refactor
1239-
fn deduplicate_local_timeline_item(
1234+
/// Try to recycle a local timeline item for the same event, or create a new
1235+
/// timeline item for it.
1236+
///
1237+
/// Note: this method doesn't take `&mut self` to avoid a borrow checker
1238+
/// conflict with `TimelineEventHandler::add_item`.
1239+
fn recycle_local_or_create_item(
12401240
items: &mut ObservableItemsTransaction<'_>,
1241-
new_event_timeline_item: &mut EventTimelineItem,
1242-
event_id: Option<&EventId>,
1241+
meta: &mut TimelineMetadata,
1242+
mut new_item: EventTimelineItem,
1243+
event_id: &EventId,
12431244
transaction_id: Option<&TransactionId>,
1244-
) -> Option<Arc<TimelineItem>> {
1245+
) -> Arc<TimelineItem> {
12451246
// Detect a local timeline item that matches `event_id` or `transaction_id`.
12461247
if let Some((local_timeline_item_index, local_timeline_item)) = items
12471248
.iter()
@@ -1270,7 +1271,7 @@ impl<'a, 'o> TimelineEventHandler<'a, 'o> {
12701271
return ControlFlow::Break(None);
12711272
}
12721273

1273-
if event_id == event_timeline_item.event_id()
1274+
if Some(event_id) == event_timeline_item.event_id()
12741275
|| (transaction_id.is_some()
12751276
&& transaction_id == event_timeline_item.transaction_id())
12761277
{
@@ -1292,13 +1293,15 @@ impl<'a, 'o> TimelineEventHandler<'a, 'o> {
12921293
"Removing local timeline item"
12931294
);
12941295

1295-
transfer_details(new_event_timeline_item, local_timeline_item);
1296+
transfer_details(&mut new_item, local_timeline_item);
12961297

12971298
// Remove the local timeline item.
1298-
return Some(items.remove(local_timeline_item_index));
1299-
};
1300-
1301-
None
1299+
let recycled = items.remove(local_timeline_item_index);
1300+
TimelineItem::new(new_item, recycled.internal_id.clone())
1301+
} else {
1302+
// We haven't found a matching local item to recycle; create a new item.
1303+
meta.new_timeline_item(new_item)
1304+
}
13021305
}
13031306

13041307
/// After updating the timeline item `new_item` which id is
@@ -1366,23 +1369,3 @@ fn transfer_details(new_item: &mut EventTimelineItem, old_item: &EventTimelineIt
13661369
in_reply_to.event = old_in_reply_to.event.clone();
13671370
}
13681371
}
1369-
1370-
/// Create a new timeline item from an [`EventTimelineItem`].
1371-
///
1372-
/// It is possible that the new timeline item replaces a duplicated timeline
1373-
/// event (see [`TimelineEventHandler::deduplicate_local_timeline_item`]) in
1374-
/// case it replaces a local timeline item.
1375-
fn new_timeline_item(
1376-
metadata: &mut TimelineMetadata,
1377-
event_timeline_item: EventTimelineItem,
1378-
replaced_timeline_item: Option<Arc<TimelineItem>>,
1379-
) -> Arc<TimelineItem> {
1380-
match replaced_timeline_item {
1381-
// Reuse the internal ID.
1382-
Some(to_replace_timeline_item) => {
1383-
TimelineItem::new(event_timeline_item, to_replace_timeline_item.internal_id.clone())
1384-
}
1385-
1386-
None => metadata.new_timeline_item(event_timeline_item),
1387-
}
1388-
}

0 commit comments

Comments
 (0)