Skip to content

Commit 6bd2ca8

Browse files
committed
base: Remove duplicated events from store instead of ignoring them
1 parent 3a72f4b commit 6bd2ca8

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
@@ -787,30 +787,22 @@ impl SledStore {
787787
}
788788
};
789789

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

812-
for (position, event) in final_events.iter().enumerate() {
813-
let position = position + current_position;
804+
for (position, event) in events.iter().enumerate() {
805+
let position = position + expandable.current_position;
814806

815807
let old_event = timeline_events.insert(
816808
(room.as_str(), expandable.batch_idx, position).encode(),
@@ -875,7 +867,7 @@ impl SledStore {
875867
(room.as_str(), timeline.start.as_str()).encode(),
876868
EventPosition {
877869
batch_idx: expandable.batch_idx,
878-
position: current_position,
870+
position: expandable.current_position,
879871
},
880872
)?;
881873
}
@@ -891,7 +883,7 @@ impl SledStore {
891883
(room.as_str(), end.as_str()).encode(),
892884
EventPosition {
893885
batch_idx: expandable.batch_idx,
894-
position: current_position + final_events.len(),
886+
position: expandable.current_position + events.len(),
895887
},
896888
)?;
897889
}
@@ -1978,6 +1970,7 @@ mod test {
19781970
.await
19791971
.unwrap()
19801972
.unwrap();
1973+
19811974
assert_eq!(backward_events.events.len(), 9);
19821975
assert_eq!(backward_events.token, messages.end.clone());
19831976

0 commit comments

Comments
 (0)