Skip to content

Commit d278f20

Browse files
pixlwavestefanceriu
authored andcommitted
feat: Add a method to reset a SpaceRoomList.
1 parent a3111e7 commit d278f20

2 files changed

Lines changed: 69 additions & 1 deletion

File tree

bindings/matrix-sdk-ffi/src/spaces.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ impl SpaceService {
156156
}
157157
}
158158

159-
/// The `SpaceRoomList`represents a paginated list of direct rooms
159+
/// The `SpaceRoomList` represents a paginated list of direct rooms
160160
/// that belong to a particular space.
161161
///
162162
/// It can be used to paginate through the list (and have live updates on the
@@ -249,6 +249,18 @@ impl SpaceRoomList {
249249
pub async fn paginate(&self) -> Result<(), ClientError> {
250250
self.inner.paginate().await.map_err(ClientError::from)
251251
}
252+
253+
/// Clears the room list back to its initial state so that any new changes
254+
/// to the hierarchy will be included the next time [`Self::paginate`] is
255+
/// called.
256+
///
257+
/// This is useful when you've added or removed children from the space as
258+
/// the list is based on a cached state that lives server-side, meaning
259+
/// the /hierarchy request needs to be restarted from scratch to pick up
260+
/// the changes.
261+
pub async fn reset(&self) {
262+
self.inner.reset().await;
263+
}
252264
}
253265

254266
#[matrix_sdk_ffi_macros::export(callback_interface)]

crates/matrix-sdk-ui/src/spaces/room_list.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,24 @@ impl SpaceRoomList {
354354
}
355355
}
356356

357+
/// Clears the room list back to its initial state so that any new changes
358+
/// to the hierarchy will be included the next time [`Self::paginate`] is
359+
/// called.
360+
///
361+
/// This is useful when you've added or removed children from the space as
362+
/// the list is based on a cached state that lives server-side, meaning
363+
/// the /hierarchy request needs to be restarted from scratch to pick up
364+
/// the changes.
365+
pub async fn reset(&self) {
366+
let mut pagination_token = self.token.lock().await;
367+
*pagination_token = None.into();
368+
369+
self.rooms.lock().clear();
370+
self.children_state.lock().take();
371+
372+
self.pagination_state.set(SpaceRoomListPaginationState::Idle { end_reached: false });
373+
}
374+
357375
/// Sorts spare rooms by various criteria as defined in
358376
/// https://spec.matrix.org/latest/client-server-api/#ordering-of-children-within-a-space
359377
fn compare_rooms(
@@ -888,6 +906,44 @@ mod tests {
888906
);
889907
}
890908

909+
#[async_test]
910+
async fn test_reset() {
911+
let server = MatrixMockServer::new().await;
912+
let client = server.client_builder().build().await;
913+
let space_service = SpaceService::new(client.clone()).await;
914+
915+
let parent_space_id = room_id!("!parent_space:example.org");
916+
let child_space_id_1 = room_id!("!1:example.org");
917+
918+
server
919+
.mock_get_hierarchy()
920+
.ok_with_room_ids(vec![child_space_id_1])
921+
.expect(2)
922+
.mount()
923+
.await;
924+
925+
let room_list = space_service.space_room_list(parent_space_id.to_owned()).await;
926+
927+
room_list.paginate().await.unwrap();
928+
929+
// This space contains 1 room
930+
assert_eq!(room_list.rooms().len(), 1);
931+
932+
// Resetting the room list
933+
room_list.reset().await;
934+
935+
// Clears the rooms and pagination token
936+
assert_eq!(room_list.rooms().len(), 0);
937+
assert_matches!(
938+
room_list.pagination_state(),
939+
SpaceRoomListPaginationState::Idle { end_reached: false }
940+
);
941+
942+
// Allows paginating again
943+
room_list.paginate().await.unwrap();
944+
assert_eq!(room_list.rooms().len(), 1);
945+
}
946+
891947
fn make_space_room(
892948
room_id: OwnedRoomId,
893949
order: Option<&str>,

0 commit comments

Comments
 (0)