Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions bindings/matrix-sdk-ffi/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use matrix_sdk::{
authentication::oauth::{
AccountManagementActionFull, ClientId, OAuthAuthorizationData, OAuthSession,
},
event_cache::EventCacheError,
media::{MediaFormat, MediaRequestParameters, MediaRetentionPolicy, MediaThumbnailSettings},
ruma::{
api::client::{
Expand All @@ -39,7 +38,7 @@ use matrix_sdk::{
},
sliding_sync::Version as SdkSlidingSyncVersion,
store::RoomLoadSettings as SdkRoomLoadSettings,
Account, AuthApi, AuthSession, Client as MatrixClient, SessionChange, SessionTokens,
Account, AuthApi, AuthSession, Client as MatrixClient, Error, SessionChange, SessionTokens,
STATE_STORE_DATABASE_NAME,
};
use matrix_sdk_common::{stream::StreamExt, SendOutsideWasm, SyncOutsideWasm};
Expand Down Expand Up @@ -1510,8 +1509,8 @@ impl Client {
&self,
policy: MediaRetentionPolicy,
) -> Result<(), ClientError> {
let closure = async || -> Result<_, EventCacheError> {
let store = self.inner.event_cache_store().lock().await?;
let closure = async || -> Result<_, Error> {
let store = self.inner.media_store().lock().await?;
Ok(store.set_media_retention_policy(policy).await?)
};

Expand Down Expand Up @@ -1559,13 +1558,13 @@ impl Client {

// Clean up the media cache according to the current media retention policy.
self.inner
.event_cache_store()
.media_store()
.lock()
.await
.map_err(EventCacheError::from)?
.clean_up_media_cache()
.map_err(Error::from)?
.clean()
.await
.map_err(EventCacheError::from)?;
.map_err(Error::from)?;

// Clear all the room chunks. It's important to *not* call
// `EventCacheStore::clear_all_linked_chunks` here, because there might be live
Expand Down
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 @@ -58,6 +58,7 @@ use crate::{
deserialized_responses::DisplayName,
error::{Error, Result},
event_cache::store::EventCacheStoreLock,
media::store::MediaStoreLock,
response_processors::{self as processors, Context},
room::{
Room, RoomInfoNotableUpdate, RoomInfoNotableUpdateReasons, RoomMembersUpdate, RoomState,
Expand Down Expand Up @@ -92,6 +93,9 @@ pub struct BaseClient {
/// The store used by the event cache.
event_cache_store: EventCacheStoreLock,

/// The store used by the media cache.
media_store: MediaStoreLock,

/// The store used for encryption.
///
/// This field is only meant to be used for `OlmMachine` initialization.
Expand Down Expand Up @@ -190,6 +194,7 @@ impl BaseClient {
BaseClient {
state_store: store,
event_cache_store: config.event_cache_store,
media_store: config.media_store,
#[cfg(feature = "e2e-encryption")]
crypto_store: config.crypto_store,
#[cfg(feature = "e2e-encryption")]
Expand Down Expand Up @@ -223,6 +228,7 @@ impl BaseClient {
let copy = Self {
state_store: BaseStateStore::new(config.state_store),
event_cache_store: config.event_cache_store,
media_store: config.media_store,
// We copy the crypto store as well as the `OlmMachine` for two reasons:
// 1. The `self.crypto_store` is the same as the one used inside the `OlmMachine`.
// 2. We need to ensure that the parent and child use the same data and caches inside
Expand Down Expand Up @@ -307,6 +313,11 @@ impl BaseClient {
&self.event_cache_store
}

/// Get a reference to the media store.
pub fn media_store(&self) -> &MediaStoreLock {
&self.media_store
}

/// Check whether the client has been activated.
///
/// See [`BaseClient::activate`] to know what it means.
Expand Down
223 changes: 6 additions & 217 deletions crates/matrix-sdk-base/src/event_cache/store/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,17 @@ use matrix_sdk_common::{
};
use matrix_sdk_test::{ALICE, DEFAULT_TEST_ROOM_ID, event_factory::EventFactory};
use ruma::{
EventId, RoomId,
api::client::media::get_content_thumbnail::v3::Method,
event_id,
EventId, RoomId, event_id,
events::{
AnyMessageLikeEvent, AnyTimelineEvent,
relation::RelationType,
room::{MediaSource, message::RoomMessageEventContentWithoutRelation},
AnyMessageLikeEvent, AnyTimelineEvent, relation::RelationType,
room::message::RoomMessageEventContentWithoutRelation,
},
mxc_uri,
push::Action,
room_id, uint,
room_id,
};

use super::{DynEventCacheStore, media::IgnoreMediaRetentionPolicy};
use crate::{
event_cache::{Gap, store::DEFAULT_CHUNK_CAPACITY},
media::{MediaFormat, MediaRequestParameters, MediaThumbnailSettings},
};
use super::DynEventCacheStore;
use crate::event_cache::{Gap, store::DEFAULT_CHUNK_CAPACITY};

/// Create a test event with all data filled, for testing that linked chunk
/// correctly stores event data.
Expand Down Expand Up @@ -118,12 +111,6 @@ pub fn check_test_event(event: &TimelineEvent, text: &str) {
/// `event_cache_store_integration_tests!` macro.
#[allow(async_fn_in_trait)]
pub trait EventCacheStoreIntegrationTests {
/// Test media content storage.
async fn test_media_content(&self);

/// Test replacing a MXID.
async fn test_replace_media_key(&self);

/// Test handling updates to a linked chunk and reloading these updates from
/// the store.
async fn test_handle_updates_and_rebuild_linked_chunk(&self);
Expand Down Expand Up @@ -163,190 +150,6 @@ pub trait EventCacheStoreIntegrationTests {
}

impl EventCacheStoreIntegrationTests for DynEventCacheStore {
async fn test_media_content(&self) {
let uri = mxc_uri!("mxc://localhost/media");
let request_file = MediaRequestParameters {
source: MediaSource::Plain(uri.to_owned()),
format: MediaFormat::File,
};
let request_thumbnail = MediaRequestParameters {
source: MediaSource::Plain(uri.to_owned()),
format: MediaFormat::Thumbnail(MediaThumbnailSettings::with_method(
Method::Crop,
uint!(100),
uint!(100),
)),
};

let other_uri = mxc_uri!("mxc://localhost/media-other");
let request_other_file = MediaRequestParameters {
source: MediaSource::Plain(other_uri.to_owned()),
format: MediaFormat::File,
};

let content: Vec<u8> = "hello".into();
let thumbnail_content: Vec<u8> = "world".into();
let other_content: Vec<u8> = "foo".into();

// Media isn't present in the cache.
assert!(
self.get_media_content(&request_file).await.unwrap().is_none(),
"unexpected media found"
);
assert!(
self.get_media_content(&request_thumbnail).await.unwrap().is_none(),
"media not found"
);

// Let's add the media.
self.add_media_content(&request_file, content.clone(), IgnoreMediaRetentionPolicy::No)
.await
.expect("adding media failed");

// Media is present in the cache.
assert_eq!(
self.get_media_content(&request_file).await.unwrap().as_ref(),
Some(&content),
"media not found though added"
);
assert_eq!(
self.get_media_content_for_uri(uri).await.unwrap().as_ref(),
Some(&content),
"media not found by URI though added"
);

// Let's remove the media.
self.remove_media_content(&request_file).await.expect("removing media failed");

// Media isn't present in the cache.
assert!(
self.get_media_content(&request_file).await.unwrap().is_none(),
"media still there after removing"
);
assert!(
self.get_media_content_for_uri(uri).await.unwrap().is_none(),
"media still found by URI after removing"
);

// Let's add the media again.
self.add_media_content(&request_file, content.clone(), IgnoreMediaRetentionPolicy::No)
.await
.expect("adding media again failed");

assert_eq!(
self.get_media_content(&request_file).await.unwrap().as_ref(),
Some(&content),
"media not found after adding again"
);

// Let's add the thumbnail media.
self.add_media_content(
&request_thumbnail,
thumbnail_content.clone(),
IgnoreMediaRetentionPolicy::No,
)
.await
.expect("adding thumbnail failed");

// Media's thumbnail is present.
assert_eq!(
self.get_media_content(&request_thumbnail).await.unwrap().as_ref(),
Some(&thumbnail_content),
"thumbnail not found"
);

// We get a file with the URI, we don't know which one.
assert!(
self.get_media_content_for_uri(uri).await.unwrap().is_some(),
"media not found by URI though two where added"
);

// Let's add another media with a different URI.
self.add_media_content(
&request_other_file,
other_content.clone(),
IgnoreMediaRetentionPolicy::No,
)
.await
.expect("adding other media failed");

// Other file is present.
assert_eq!(
self.get_media_content(&request_other_file).await.unwrap().as_ref(),
Some(&other_content),
"other file not found"
);
assert_eq!(
self.get_media_content_for_uri(other_uri).await.unwrap().as_ref(),
Some(&other_content),
"other file not found by URI"
);

// Let's remove media based on URI.
self.remove_media_content_for_uri(uri).await.expect("removing all media for uri failed");

assert!(
self.get_media_content(&request_file).await.unwrap().is_none(),
"media wasn't removed"
);
assert!(
self.get_media_content(&request_thumbnail).await.unwrap().is_none(),
"thumbnail wasn't removed"
);
assert!(
self.get_media_content(&request_other_file).await.unwrap().is_some(),
"other media was removed"
);
assert!(
self.get_media_content_for_uri(uri).await.unwrap().is_none(),
"media found by URI wasn't removed"
);
assert!(
self.get_media_content_for_uri(other_uri).await.unwrap().is_some(),
"other media found by URI was removed"
);
}

async fn test_replace_media_key(&self) {
let uri = mxc_uri!("mxc://sendqueue.local/tr4n-s4ct-10n1-d");
let req = MediaRequestParameters {
source: MediaSource::Plain(uri.to_owned()),
format: MediaFormat::File,
};

let content = "hello".as_bytes().to_owned();

// Media isn't present in the cache.
assert!(self.get_media_content(&req).await.unwrap().is_none(), "unexpected media found");

// Add the media.
self.add_media_content(&req, content.clone(), IgnoreMediaRetentionPolicy::No)
.await
.expect("adding media failed");

// Sanity-check: media is found after adding it.
assert_eq!(self.get_media_content(&req).await.unwrap().unwrap(), b"hello");

// Replacing a media request works.
let new_uri = mxc_uri!("mxc://matrix.org/tr4n-s4ct-10n1-d");
let new_req = MediaRequestParameters {
source: MediaSource::Plain(new_uri.to_owned()),
format: MediaFormat::File,
};
self.replace_media_key(&req, &new_req)
.await
.expect("replacing the media request key failed");

// Finding with the previous request doesn't work anymore.
assert!(
self.get_media_content(&req).await.unwrap().is_none(),
"unexpected media found with the old key"
);

// Finding with the new request does work.
assert_eq!(self.get_media_content(&new_req).await.unwrap().unwrap(), b"hello");
}

async fn test_handle_updates_and_rebuild_linked_chunk(&self) {
let room_id = room_id!("!r0:matrix.org");
let linked_chunk_id = LinkedChunkId::Room(room_id);
Expand Down Expand Up @@ -1333,20 +1136,6 @@ macro_rules! event_cache_store_integration_tests {

use super::get_event_cache_store;

#[async_test]
async fn test_media_content() {
let event_cache_store =
get_event_cache_store().await.unwrap().into_event_cache_store();
event_cache_store.test_media_content().await;
}

#[async_test]
async fn test_replace_media_key() {
let event_cache_store =
get_event_cache_store().await.unwrap().into_event_cache_store();
event_cache_store.test_replace_media_key().await;
}

#[async_test]
async fn test_handle_updates_and_rebuild_linked_chunk() {
let event_cache_store =
Expand Down
28 changes: 0 additions & 28 deletions crates/matrix-sdk-base/src/event_cache/store/media/mod.rs

This file was deleted.

Loading
Loading