From ce94fcc2e25e8a72bd2b7dc6785cf5a3769927c8 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Thu, 30 Mar 2023 10:00:21 +0200 Subject: [PATCH] feat(sdk): Update the `SyncOp::Insert` operation. It seems that the `INSERT` operation actually inserts a new entry. So let's update the code to reflect that :-). --- .../matrix-sdk/src/sliding_sync/list/mod.rs | 47 +++++++++++++++---- 1 file changed, 38 insertions(+), 9 deletions(-) diff --git a/crates/matrix-sdk/src/sliding_sync/list/mod.rs b/crates/matrix-sdk/src/sliding_sync/list/mod.rs index 7aa9af709..6a4861760 100644 --- a/crates/matrix-sdk/src/sliding_sync/list/mod.rs +++ b/crates/matrix-sdk/src/sliding_sync/list/mod.rs @@ -384,9 +384,7 @@ impl SlidingSyncListInner { ) -> Result { let mut new_changes = false; - // Create missing room list entries. - // This should be called only once, when the `rooms_list` is empty. Otherwise, - // the `maximum_number_of_rooms` should not change, so there is no update to do. + // Adjust room list entries. { let number_of_missing_rooms = (maximum_number_of_rooms as usize) .saturating_sub(self.rooms_list.read().unwrap().len()); @@ -631,7 +629,7 @@ fn apply_sync_operations( })?; // Index is out of bounds. - if index >= rooms_list.len() { + if index > rooms_list.len() { return Err(Error::BadResponse(format!( "`index` is out of the `rooms_list`' bounds ({} > {})", index, @@ -639,8 +637,8 @@ fn apply_sync_operations( ))); } - // Insert = updating the room list to `Filled`. - rooms_list.set(index, RoomListEntry::Filled(room_id)); + // Insert = inserting a `Filled` entry in the room list . + rooms_list.insert(index, RoomListEntry::Filled(room_id)); } v4::SlidingOp::Invalidate => { @@ -1806,7 +1804,22 @@ mod tests { #[test] fn test_sync_operations_insert() { - // Insert a room entry. + // Insert a room entry in the middle. + assert_sync_operations! { + rooms_list = [E, E, E], + operations = [ + { + "op": SlidingOp::Insert, + "index": 1, + "room_id": "!r1:x.y", + } + ] + => + result = is_ok, + rooms_list = [E, F("!r1:x.y"), E, E], + }; + + // Insert a room entry at the beginning. assert_sync_operations! { rooms_list = [E, E, E], operations = [ @@ -1818,7 +1831,22 @@ mod tests { ] => result = is_ok, - rooms_list = [F("!r0:x.y"), E, E], + rooms_list = [F("!r0:x.y"), E, E, E], + }; + + // Insert a room entry at the end + assert_sync_operations! { + rooms_list = [E, E, E], + operations = [ + { + "op": SlidingOp::Insert, + "index": 3, + "room_id": "!r3:x.y", + } + ] + => + result = is_ok, + rooms_list = [E, E, E, F("!r3:x.y")], }; // Insert an out of bounds room entry. @@ -1827,7 +1855,8 @@ mod tests { operations = [ { "op": SlidingOp::Insert, - "index": 3, + "index": 4, + "room_id": "!r4:x.y", } ] =>