feat(base): remove cached events when forgetting about a room

This commit is contained in:
Benjamin Bouvier
2025-01-13 15:08:04 +01:00
parent e647ff935e
commit a8ca77f4fc
3 changed files with 41 additions and 2 deletions

View File

@@ -1516,8 +1516,14 @@ impl BaseClient {
/// # 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
pub async fn forget_room(&self, room_id: &RoomId) -> Result<()> {
// Forget the room in the state store.
self.store.forget_room(room_id).await?;
// Remove the room in the event cache store too.
self.event_cache_store().lock().await?.remove_room(room_id).await?;
Ok(())
}
/// Get the olm machine.

View File

@@ -15,10 +15,13 @@
//! Error conditions.
use matrix_sdk_common::store_locks::LockStoreError;
#[cfg(feature = "e2e-encryption")]
use matrix_sdk_crypto::{CryptoStoreError, MegolmError, OlmError};
use thiserror::Error;
use crate::event_cache::store::EventCacheStoreError;
/// Result type of the rust-sdk.
pub type Result<T, E = Error> = std::result::Result<T, E>;
@@ -42,6 +45,14 @@ pub enum Error {
#[error(transparent)]
StateStore(#[from] crate::store::StoreError),
/// An error happened while manipulating the event cache store.
#[error(transparent)]
EventCacheStore(#[from] EventCacheStoreError),
/// An error happened while attempting to lock the event cache store.
#[error(transparent)]
EventCacheLock(#[from] LockStoreError),
/// An error occurred in the crypto store.
#[cfg(feature = "e2e-encryption")]
#[error(transparent)]

View File

@@ -12,6 +12,7 @@ use ruma::{
user_id, OwnedRoomOrAliasId,
};
use serde_json::json;
use tokio::task::yield_now;
use wiremock::{
matchers::{header, method, path, path_regex},
Mock, ResponseTemplate,
@@ -24,6 +25,10 @@ async fn test_forget_non_direct_room() {
let (client, server) = logged_in_client_with_server().await;
let user_id = client.user_id().unwrap();
let event_cache = client.event_cache();
event_cache.subscribe().unwrap();
event_cache.enable_storage().unwrap();
Mock::given(method("POST"))
.and(path_regex(r"^/_matrix/client/r0/rooms/.*/forget$"))
.and(header("authorization", "Bearer 1234"))
@@ -47,12 +52,29 @@ async fn test_forget_non_direct_room() {
let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000));
let _response = client.sync_once(sync_settings).await.unwrap();
// Let the event cache process updates.
yield_now().await;
{
// There is some data in the cache store.
let event_cache_store = client.event_cache_store().lock().await.unwrap();
let room_data = event_cache_store.reload_linked_chunk(&DEFAULT_TEST_ROOM_ID).await.unwrap();
assert!(!room_data.is_empty());
}
let room = client.get_room(&DEFAULT_TEST_ROOM_ID).unwrap();
assert_eq!(room.state(), RoomState::Left);
room.forget().await.unwrap();
assert!(client.get_room(&DEFAULT_TEST_ROOM_ID).is_none());
{
// Data in the event cache store has been removed.
let event_cache_store = client.event_cache_store().lock().await.unwrap();
let room_data = event_cache_store.reload_linked_chunk(&DEFAULT_TEST_ROOM_ID).await.unwrap();
assert!(room_data.is_empty());
}
}
#[async_test]