Skip to content

Commit a8ca77f

Browse files
committed
feat(base): remove cached events when forgetting about a room
1 parent e647ff9 commit a8ca77f

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

crates/matrix-sdk-base/src/client.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1516,8 +1516,14 @@ impl BaseClient {
15161516
/// # Arguments
15171517
///
15181518
/// * `room_id` - The id of the room that should be forgotten.
1519-
pub async fn forget_room(&self, room_id: &RoomId) -> StoreResult<()> {
1520-
self.store.forget_room(room_id).await
1519+
pub async fn forget_room(&self, room_id: &RoomId) -> Result<()> {
1520+
// Forget the room in the state store.
1521+
self.store.forget_room(room_id).await?;
1522+
1523+
// Remove the room in the event cache store too.
1524+
self.event_cache_store().lock().await?.remove_room(room_id).await?;
1525+
1526+
Ok(())
15211527
}
15221528

15231529
/// Get the olm machine.

crates/matrix-sdk-base/src/error.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@
1515

1616
//! Error conditions.
1717
18+
use matrix_sdk_common::store_locks::LockStoreError;
1819
#[cfg(feature = "e2e-encryption")]
1920
use matrix_sdk_crypto::{CryptoStoreError, MegolmError, OlmError};
2021
use thiserror::Error;
2122

23+
use crate::event_cache::store::EventCacheStoreError;
24+
2225
/// Result type of the rust-sdk.
2326
pub type Result<T, E = Error> = std::result::Result<T, E>;
2427

@@ -42,6 +45,14 @@ pub enum Error {
4245
#[error(transparent)]
4346
StateStore(#[from] crate::store::StoreError),
4447

48+
/// An error happened while manipulating the event cache store.
49+
#[error(transparent)]
50+
EventCacheStore(#[from] EventCacheStoreError),
51+
52+
/// An error happened while attempting to lock the event cache store.
53+
#[error(transparent)]
54+
EventCacheLock(#[from] LockStoreError),
55+
4556
/// An error occurred in the crypto store.
4657
#[cfg(feature = "e2e-encryption")]
4758
#[error(transparent)]

crates/matrix-sdk/tests/integration/room/left.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use ruma::{
1212
user_id, OwnedRoomOrAliasId,
1313
};
1414
use serde_json::json;
15+
use tokio::task::yield_now;
1516
use wiremock::{
1617
matchers::{header, method, path, path_regex},
1718
Mock, ResponseTemplate,
@@ -24,6 +25,10 @@ async fn test_forget_non_direct_room() {
2425
let (client, server) = logged_in_client_with_server().await;
2526
let user_id = client.user_id().unwrap();
2627

28+
let event_cache = client.event_cache();
29+
event_cache.subscribe().unwrap();
30+
event_cache.enable_storage().unwrap();
31+
2732
Mock::given(method("POST"))
2833
.and(path_regex(r"^/_matrix/client/r0/rooms/.*/forget$"))
2934
.and(header("authorization", "Bearer 1234"))
@@ -47,12 +52,29 @@ async fn test_forget_non_direct_room() {
4752
let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000));
4853
let _response = client.sync_once(sync_settings).await.unwrap();
4954

55+
// Let the event cache process updates.
56+
yield_now().await;
57+
58+
{
59+
// There is some data in the cache store.
60+
let event_cache_store = client.event_cache_store().lock().await.unwrap();
61+
let room_data = event_cache_store.reload_linked_chunk(&DEFAULT_TEST_ROOM_ID).await.unwrap();
62+
assert!(!room_data.is_empty());
63+
}
64+
5065
let room = client.get_room(&DEFAULT_TEST_ROOM_ID).unwrap();
5166
assert_eq!(room.state(), RoomState::Left);
5267

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

5570
assert!(client.get_room(&DEFAULT_TEST_ROOM_ID).is_none());
71+
72+
{
73+
// Data in the event cache store has been removed.
74+
let event_cache_store = client.event_cache_store().lock().await.unwrap();
75+
let room_data = event_cache_store.reload_linked_chunk(&DEFAULT_TEST_ROOM_ID).await.unwrap();
76+
assert!(room_data.is_empty());
77+
}
5678
}
5779

5880
#[async_test]

0 commit comments

Comments
 (0)