Skip to content

Commit

Permalink
feat(base): remove cached events when forgetting about a room
Browse files Browse the repository at this point in the history
  • Loading branch information
bnjbvr committed Jan 13, 2025
1 parent 171c500 commit 48df627
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
10 changes: 8 additions & 2 deletions crates/matrix-sdk-base/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
11 changes: 11 additions & 0 deletions crates/matrix-sdk-base/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>;

Expand All @@ -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)]
Expand Down
22 changes: 22 additions & 0 deletions crates/matrix-sdk/tests/integration/room/left.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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"))
Expand All @@ -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]
Expand Down

0 comments on commit 48df627

Please sign in to comment.