Skip to content

Commit 935bdf2

Browse files
committed
fixup! refactor(timeline): simplify removal of duplicated local echo item
1 parent ef2fe40 commit 935bdf2

File tree

1 file changed

+29
-45
lines changed

1 file changed

+29
-45
lines changed

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

Lines changed: 29 additions & 45 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,
1076+
self.meta,
1077+
item,
10771078
event_id,
1078-
txn_id.as_ref().map(AsRef::as_ref),
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,
1095+
self.meta,
1096+
item,
10961097
event_id,
1097-
txn_id.as_ref().map(AsRef::as_ref),
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,
1158+
self.meta,
1159+
item,
11591160
event_id,
1160-
txn_id.as_ref().map(AsRef::as_ref),
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,16 +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-
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(
12391240
items: &mut ObservableItemsTransaction<'_>,
1240-
new_event_timeline_item: &mut EventTimelineItem,
1241+
meta: &mut TimelineMetadata,
1242+
mut new_item: EventTimelineItem,
12411243
event_id: &EventId,
12421244
transaction_id: Option<&TransactionId>,
1243-
) -> Option<Arc<TimelineItem>> {
1245+
) -> Arc<TimelineItem> {
12441246
// Detect a local timeline item that matches `event_id` or `transaction_id`.
12451247
if let Some((local_timeline_item_index, local_timeline_item)) = items
12461248
.iter()
@@ -1291,13 +1293,15 @@ impl<'a, 'o> TimelineEventHandler<'a, 'o> {
12911293
"Removing local timeline item"
12921294
);
12931295

1294-
transfer_details(new_event_timeline_item, local_timeline_item);
1296+
transfer_details(&mut new_item, local_timeline_item);
12951297

12961298
// Remove the local timeline item.
1297-
return Some(items.remove(local_timeline_item_index));
1298-
};
1299-
1300-
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+
}
13011305
}
13021306

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

0 commit comments

Comments
 (0)