Skip to content

Commit

Permalink
sdk: Remove room from in-memory list when calling Room::forget
Browse files Browse the repository at this point in the history
Signed-off-by: Kévin Commaille <[email protected]>
  • Loading branch information
zecakeh authored and bnjbvr committed Oct 15, 2024
1 parent 0b57ef4 commit ee4ef2e
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 1 deletion.
11 changes: 11 additions & 0 deletions crates/matrix-sdk-base/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1408,6 +1408,17 @@ impl BaseClient {
self.store.room(room_id)
}

/// Forget the room with the given room ID.
///
/// The room will be dropped from the room list and the store.
///
/// # Arguments
///
/// * `room_id` - The id of the room that should be forgotten.
pub async fn forget_room(&self, room_id: &RoomId) -> StoreResult<()> {
self.store.forget_room(room_id).await
}

/// Get the olm machine.
#[cfg(feature = "e2e-encryption")]
pub async fn olm_machine(&self) -> RwLockReadGuard<'_, Option<OlmMachine>> {
Expand Down
11 changes: 11 additions & 0 deletions crates/matrix-sdk-base/src/store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,17 @@ impl Store {
})
.clone()
}

/// Forget the room with the given room ID.
///
/// # Arguments
///
/// * `room_id` - The id of the room that should be forgotten.
pub(crate) async fn forget_room(&self, room_id: &RoomId) -> Result<()> {
self.inner.remove_room(room_id).await?;
self.rooms.write().unwrap().remove(room_id);
Ok(())
}
}

#[cfg(not(tarpaulin_include))]
Expand Down
56 changes: 56 additions & 0 deletions crates/matrix-sdk-base/src/store/observable_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,18 @@ mod impl_non_wasm32 {
pub(crate) fn stream(&self) -> (Vector<V>, impl Stream<Item = Vec<VectorDiff<V>>>) {
self.values.subscribe().into_values_and_batched_stream()
}

/// Remove a `V` value based on their ID, if it exists.
///
/// Returns the removed value.
pub(crate) fn remove<L>(&mut self, key: &L) -> Option<V>
where
K: Borrow<L>,
L: Hash + Eq + ?Sized,
{
let position = self.mapping.remove(key)?;
Some(self.values.remove(position))
}
}
}

Expand Down Expand Up @@ -184,6 +196,17 @@ mod impl_wasm32 {
pub(crate) fn iter(&self) -> impl Iterator<Item = &V> {
self.0.values()
}

/// Remove a `V` value based on their ID, if it exists.
///
/// Returns the removed value.
pub(crate) fn remove<L>(&mut self, key: &L) -> Option<V>
where
K: Borrow<L>,
L: Hash + Eq + Ord + ?Sized,
{
self.0.remove(key)
}
}
}

Expand Down Expand Up @@ -249,6 +272,33 @@ mod tests {
assert_eq!(map.get(&'c'), Some(&'G'));
}

#[test]
fn test_remove() {
let mut map = ObservableMap::<char, char>::new();

assert!(map.get(&'a').is_none());
assert!(map.get(&'b').is_none());
assert!(map.get(&'c').is_none());

// new items
map.insert('a', 'e');
map.insert('b', 'f');

assert_eq!(map.get(&'a'), Some(&'e'));
assert_eq!(map.get(&'b'), Some(&'f'));
assert!(map.get(&'c').is_none());

// remove one item
assert_eq!(map.remove(&'b'), Some('f'));

assert_eq!(map.get(&'a'), Some(&'e'));
assert_eq!(map.get(&'b'), None);
assert_eq!(map.get(&'c'), None);

// remove a non-existent item
assert_eq!(map.remove(&'c'), None);
}

#[test]
fn test_iter() {
let mut map = ObservableMap::<char, char>::new();
Expand Down Expand Up @@ -293,6 +343,12 @@ mod tests {

assert_pending!(stream);

// remove one item
map.remove(&'b');
assert_next_eq!(stream, vec![VectorDiff::Remove { index: 0 }]);

assert_pending!(stream);

drop(map);
assert_closed!(stream);
}
Expand Down
2 changes: 1 addition & 1 deletion crates/matrix-sdk/src/room/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2663,7 +2663,7 @@ impl Room {
}
}

self.client.store().remove_room(self.inner.room_id()).await?;
self.client.base_client().forget_room(self.inner.room_id()).await?;

Ok(())
}
Expand Down
4 changes: 4 additions & 0 deletions crates/matrix-sdk/tests/integration/room/left.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ async fn test_forget_non_direct_room() {
assert_eq!(room.state(), RoomState::Left);

room.forget().await.unwrap();

assert!(client.get_room(&DEFAULT_TEST_ROOM_ID).is_none());
}

#[async_test]
Expand Down Expand Up @@ -100,6 +102,8 @@ async fn test_forget_direct_room() {
.await;

room.forget().await.unwrap();

assert!(client.get_room(&DEFAULT_TEST_ROOM_ID).is_none());
}

#[async_test]
Expand Down

0 comments on commit ee4ef2e

Please sign in to comment.