sdk: Remove room from in-memory list when calling Room::forget

Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
This commit is contained in:
Kévin Commaille
2024-10-09 13:16:50 +02:00
committed by Benjamin Bouvier
parent 0b57ef4bf6
commit ee4ef2eb53
5 changed files with 83 additions and 1 deletions

View File

@@ -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>> {

View File

@@ -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))]

View File

@@ -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))
}
}
}
@@ -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)
}
}
}
@@ -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();
@@ -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);
}

View File

@@ -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(())
}

View File

@@ -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]
@@ -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]