Skip to content

Commit a4a50db

Browse files
committed
base: Remove duplicated events from store instead of ignoring them
1 parent 54e2fd0 commit a4a50db

File tree

1 file changed

+18
-25
lines changed
  • crates/matrix-sdk-base/src/store/sled_store

1 file changed

+18
-25
lines changed

crates/matrix-sdk-base/src/store/sled_store/mod.rs

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -790,30 +790,22 @@ impl SledStore {
790790
}
791791
};
792792

793-
let final_events: Vec<_> = events
794-
.iter()
795-
.filter(|e| {
796-
event_id_to_position
797-
.get((room.as_str(), e.event_id().unwrap().as_str()).encode())
798-
.transpose()
799-
.is_none()
800-
})
801-
.collect();
802-
803-
let duplicated = events.len() - final_events.len();
804-
805-
let current_position = if expandable.start_token_changed
806-
&& !expandable.end_token_changed
807-
{
808-
expandable.current_position - duplicated
809-
} else if expandable.end_token_changed && !expandable.start_token_changed {
810-
expandable.current_position + duplicated
811-
} else {
812-
expandable.current_position
813-
};
793+
// Remove events already known from the store
794+
for event in &events {
795+
if let Some(batch) = event_id_to_position
796+
.remove(
797+
(room.as_str(), event.event_id().unwrap().as_str()).encode(),
798+
)?
799+
.map(EventPosition::from)
800+
{
801+
timeline_events.remove(
802+
(room.as_str(), batch.batch_idx, batch.position).encode(),
803+
)?;
804+
}
805+
}
814806

815-
for (position, event) in final_events.iter().enumerate() {
816-
let position = position + current_position;
807+
for (position, event) in events.iter().enumerate() {
808+
let position = position + expandable.current_position;
817809

818810
let old_event = timeline_events.insert(
819811
(room.as_str(), expandable.batch_idx, position).encode(),
@@ -888,7 +880,7 @@ impl SledStore {
888880
(room.as_str(), timeline.start.as_str()).encode(),
889881
EventPosition {
890882
batch_idx: expandable.batch_idx,
891-
position: current_position,
883+
position: expandable.current_position,
892884
},
893885
)?;
894886
}
@@ -904,7 +896,7 @@ impl SledStore {
904896
(room.as_str(), end.as_str()).encode(),
905897
EventPosition {
906898
batch_idx: expandable.batch_idx,
907-
position: current_position + final_events.len(),
899+
position: expandable.current_position + events.len(),
908900
},
909901
)?;
910902
}
@@ -1980,6 +1972,7 @@ mod test {
19801972
.await
19811973
.unwrap()
19821974
.unwrap();
1975+
19831976
assert_eq!(backward_events.events.len(), 9);
19841977
assert_eq!(backward_events.token, messages.end.clone());
19851978

0 commit comments

Comments
 (0)