From 77ee02f5291d1751289b8e9d06832bb71e6cfeaa Mon Sep 17 00:00:00 2001 From: Benjamin Bouvier Date: Wed, 6 Nov 2024 14:48:53 +0100 Subject: [PATCH] refactor!(media): rename `MediaRequest` to `MediaRequestParameters` Because it's not a request we send to the server; it's some of the request parameters. --- bindings/matrix-sdk-ffi/src/client.rs | 9 +++--- .../event_cache_store/integration_tests.rs | 20 +++++++------ .../src/event_cache_store/memory_store.rs | 16 +++++++---- .../src/event_cache_store/traits.rs | 28 +++++++++++-------- crates/matrix-sdk-base/src/media.rs | 14 ++++++---- .../matrix-sdk-base/src/store/send_queue.rs | 6 ++-- .../src/event_cache_store.rs | 26 ++++++++++------- crates/matrix-sdk/src/account.rs | 4 +-- crates/matrix-sdk/src/media.rs | 21 +++++++++----- crates/matrix-sdk/src/room/member.rs | 4 +-- crates/matrix-sdk/src/room/mod.rs | 9 +++--- crates/matrix-sdk/src/send_queue.rs | 6 ++-- crates/matrix-sdk/src/send_queue/upload.rs | 19 +++++++------ crates/matrix-sdk/tests/integration/media.rs | 4 +-- .../tests/integration/room/attachment/mod.rs | 10 +++---- .../tests/integration/send_queue.rs | 13 +++++---- 16 files changed, 123 insertions(+), 86 deletions(-) diff --git a/bindings/matrix-sdk-ffi/src/client.rs b/bindings/matrix-sdk-ffi/src/client.rs index dd9f6219d82..0b5208c510f 100644 --- a/bindings/matrix-sdk-ffi/src/client.rs +++ b/bindings/matrix-sdk-ffi/src/client.rs @@ -8,7 +8,8 @@ use std::{ use anyhow::{anyhow, Context as _}; use matrix_sdk::{ media::{ - MediaFileHandle as SdkMediaFileHandle, MediaFormat, MediaRequest, MediaThumbnailSettings, + MediaFileHandle as SdkMediaFileHandle, MediaFormat, MediaRequestParameters, + MediaThumbnailSettings, }, oidc::{ registrations::{ClientId, OidcRegistrations}, @@ -442,7 +443,7 @@ impl Client { .inner .media() .get_media_file( - &MediaRequest { source, format: MediaFormat::File }, + &MediaRequestParameters { source, format: MediaFormat::File }, filename, &mime_type, use_cache, @@ -721,7 +722,7 @@ impl Client { Ok(self .inner .media() - .get_media_content(&MediaRequest { source, format: MediaFormat::File }, true) + .get_media_content(&MediaRequestParameters { source, format: MediaFormat::File }, true) .await?) } @@ -738,7 +739,7 @@ impl Client { .inner .media() .get_media_content( - &MediaRequest { + &MediaRequestParameters { source, format: MediaFormat::Thumbnail(MediaThumbnailSettings::new( Method::Scale, diff --git a/crates/matrix-sdk-base/src/event_cache_store/integration_tests.rs b/crates/matrix-sdk-base/src/event_cache_store/integration_tests.rs index b9960489980..788aa4a0cae 100644 --- a/crates/matrix-sdk-base/src/event_cache_store/integration_tests.rs +++ b/crates/matrix-sdk-base/src/event_cache_store/integration_tests.rs @@ -20,7 +20,7 @@ use ruma::{ }; use super::DynEventCacheStore; -use crate::media::{MediaFormat, MediaRequest, MediaThumbnailSettings}; +use crate::media::{MediaFormat, MediaRequestParameters, MediaThumbnailSettings}; /// `EventCacheStore` integration tests. /// @@ -41,9 +41,11 @@ pub trait EventCacheStoreIntegrationTests { impl EventCacheStoreIntegrationTests for DynEventCacheStore { async fn test_media_content(&self) { let uri = mxc_uri!("mxc://localhost/media"); - let request_file = - MediaRequest { source: MediaSource::Plain(uri.to_owned()), format: MediaFormat::File }; - let request_thumbnail = MediaRequest { + 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::new( Method::Crop, @@ -53,7 +55,7 @@ impl EventCacheStoreIntegrationTests for DynEventCacheStore { }; let other_uri = mxc_uri!("mxc://localhost/media-other"); - let request_other_file = MediaRequest { + let request_other_file = MediaRequestParameters { source: MediaSource::Plain(other_uri.to_owned()), format: MediaFormat::File, }; @@ -145,8 +147,10 @@ impl EventCacheStoreIntegrationTests for DynEventCacheStore { async fn test_replace_media_key(&self) { let uri = mxc_uri!("mxc://sendqueue.local/tr4n-s4ct-10n1-d"); - let req = - MediaRequest { source: MediaSource::Plain(uri.to_owned()), format: MediaFormat::File }; + let req = MediaRequestParameters { + source: MediaSource::Plain(uri.to_owned()), + format: MediaFormat::File, + }; let content = "hello".as_bytes().to_owned(); @@ -161,7 +165,7 @@ impl EventCacheStoreIntegrationTests for DynEventCacheStore { // Replacing a media request works. let new_uri = mxc_uri!("mxc://matrix.org/tr4n-s4ct-10n1-d"); - let new_req = MediaRequest { + let new_req = MediaRequestParameters { source: MediaSource::Plain(new_uri.to_owned()), format: MediaFormat::File, }; diff --git a/crates/matrix-sdk-base/src/event_cache_store/memory_store.rs b/crates/matrix-sdk-base/src/event_cache_store/memory_store.rs index d75c9f09776..1b5debbccee 100644 --- a/crates/matrix-sdk-base/src/event_cache_store/memory_store.rs +++ b/crates/matrix-sdk-base/src/event_cache_store/memory_store.rs @@ -21,7 +21,7 @@ use matrix_sdk_common::{ use ruma::{MxcUri, OwnedMxcUri}; use super::{EventCacheStore, EventCacheStoreError, Result}; -use crate::media::{MediaRequest, UniqueKey as _}; +use crate::media::{MediaRequestParameters, UniqueKey as _}; /// In-memory, non-persistent implementation of the `EventCacheStore`. /// @@ -66,7 +66,11 @@ impl EventCacheStore for MemoryStore { Ok(try_take_leased_lock(&self.leases, lease_duration_ms, key, holder)) } - async fn add_media_content(&self, request: &MediaRequest, data: Vec) -> Result<()> { + async fn add_media_content( + &self, + request: &MediaRequestParameters, + data: Vec, + ) -> Result<()> { // Avoid duplication. Let's try to remove it first. self.remove_media_content(request).await?; // Now, let's add it. @@ -77,8 +81,8 @@ impl EventCacheStore for MemoryStore { async fn replace_media_key( &self, - from: &MediaRequest, - to: &MediaRequest, + from: &MediaRequestParameters, + to: &MediaRequestParameters, ) -> Result<(), Self::Error> { let expected_key = from.unique_key(); @@ -91,7 +95,7 @@ impl EventCacheStore for MemoryStore { Ok(()) } - async fn get_media_content(&self, request: &MediaRequest) -> Result>> { + async fn get_media_content(&self, request: &MediaRequestParameters) -> Result>> { let expected_key = request.unique_key(); let media = self.media.read().unwrap(); @@ -100,7 +104,7 @@ impl EventCacheStore for MemoryStore { })) } - async fn remove_media_content(&self, request: &MediaRequest) -> Result<()> { + async fn remove_media_content(&self, request: &MediaRequestParameters) -> Result<()> { let expected_key = request.unique_key(); let mut media = self.media.write().unwrap(); diff --git a/crates/matrix-sdk-base/src/event_cache_store/traits.rs b/crates/matrix-sdk-base/src/event_cache_store/traits.rs index eaa1ada194c..6742851870d 100644 --- a/crates/matrix-sdk-base/src/event_cache_store/traits.rs +++ b/crates/matrix-sdk-base/src/event_cache_store/traits.rs @@ -19,7 +19,7 @@ use matrix_sdk_common::AsyncTraitDeps; use ruma::MxcUri; use super::EventCacheStoreError; -use crate::media::MediaRequest; +use crate::media::MediaRequestParameters; /// An abstract trait that can be used to implement different store backends /// for the event cache of the SDK. @@ -46,7 +46,7 @@ pub trait EventCacheStore: AsyncTraitDeps { /// * `content` - The content of the file. async fn add_media_content( &self, - request: &MediaRequest, + request: &MediaRequestParameters, content: Vec, ) -> Result<(), Self::Error>; @@ -71,8 +71,8 @@ pub trait EventCacheStore: AsyncTraitDeps { /// * `to` - The new `MediaRequest` of the file. async fn replace_media_key( &self, - from: &MediaRequest, - to: &MediaRequest, + from: &MediaRequestParameters, + to: &MediaRequestParameters, ) -> Result<(), Self::Error>; /// Get a media file's content out of the media store. @@ -82,7 +82,7 @@ pub trait EventCacheStore: AsyncTraitDeps { /// * `request` - The `MediaRequest` of the file. async fn get_media_content( &self, - request: &MediaRequest, + request: &MediaRequestParameters, ) -> Result>, Self::Error>; /// Remove a media file's content from the media store. @@ -90,7 +90,10 @@ pub trait EventCacheStore: AsyncTraitDeps { /// # Arguments /// /// * `request` - The `MediaRequest` of the file. - async fn remove_media_content(&self, request: &MediaRequest) -> Result<(), Self::Error>; + async fn remove_media_content( + &self, + request: &MediaRequestParameters, + ) -> Result<(), Self::Error>; /// Remove all the media files' content associated to an `MxcUri` from the /// media store. @@ -127,7 +130,7 @@ impl EventCacheStore for EraseEventCacheStoreError { async fn add_media_content( &self, - request: &MediaRequest, + request: &MediaRequestParameters, content: Vec, ) -> Result<(), Self::Error> { self.0.add_media_content(request, content).await.map_err(Into::into) @@ -135,20 +138,23 @@ impl EventCacheStore for EraseEventCacheStoreError { async fn replace_media_key( &self, - from: &MediaRequest, - to: &MediaRequest, + from: &MediaRequestParameters, + to: &MediaRequestParameters, ) -> Result<(), Self::Error> { self.0.replace_media_key(from, to).await.map_err(Into::into) } async fn get_media_content( &self, - request: &MediaRequest, + request: &MediaRequestParameters, ) -> Result>, Self::Error> { self.0.get_media_content(request).await.map_err(Into::into) } - async fn remove_media_content(&self, request: &MediaRequest) -> Result<(), Self::Error> { + async fn remove_media_content( + &self, + request: &MediaRequestParameters, + ) -> Result<(), Self::Error> { self.0.remove_media_content(request).await.map_err(Into::into) } diff --git a/crates/matrix-sdk-base/src/media.rs b/crates/matrix-sdk-base/src/media.rs index ad4852751eb..b9ea760f644 100644 --- a/crates/matrix-sdk-base/src/media.rs +++ b/crates/matrix-sdk-base/src/media.rs @@ -97,9 +97,11 @@ impl UniqueKey for MediaSource { } } -/// A request for media data. +/// Parameters for a request for retrieve media data. +/// +/// This is used as a key in the media cache too. #[derive(Clone, Debug, Serialize, Deserialize)] -pub struct MediaRequest { +pub struct MediaRequestParameters { /// The source of the media file. pub source: MediaSource, @@ -107,7 +109,7 @@ pub struct MediaRequest { pub format: MediaFormat, } -impl MediaRequest { +impl MediaRequestParameters { /// Get the [`MxcUri`] from `Self`. pub fn uri(&self) -> &MxcUri { match &self.source { @@ -117,7 +119,7 @@ impl MediaRequest { } } -impl UniqueKey for MediaRequest { +impl UniqueKey for MediaRequestParameters { fn unique_key(&self) -> String { format!("{}{UNIQUE_SEPARATOR}{}", self.source.unique_key(), self.format.unique_key()) } @@ -213,14 +215,14 @@ mod tests { fn test_media_request_url() { let mxc_uri = mxc_uri!("mxc://homeserver/media"); - let plain = MediaRequest { + let plain = MediaRequestParameters { source: MediaSource::Plain(mxc_uri.to_owned()), format: MediaFormat::File, }; assert_eq!(plain.uri(), mxc_uri); - let file = MediaRequest { + let file = MediaRequestParameters { source: MediaSource::Encrypted(Box::new( serde_json::from_value(json!({ "url": mxc_uri, diff --git a/crates/matrix-sdk-base/src/store/send_queue.rs b/crates/matrix-sdk-base/src/store/send_queue.rs index 51bc1c7f1c6..f5f0ccaa7e0 100644 --- a/crates/matrix-sdk-base/src/store/send_queue.rs +++ b/crates/matrix-sdk-base/src/store/send_queue.rs @@ -27,7 +27,7 @@ use ruma::{ }; use serde::{Deserialize, Serialize}; -use crate::media::MediaRequest; +use crate::media::MediaRequestParameters; /// A thin wrapper to serialize a `AnyMessageLikeEventContent`. #[derive(Clone, Serialize, Deserialize)] @@ -95,7 +95,7 @@ pub enum QueuedRequestKind { /// The cache key used to retrieve the media's bytes in the event cache /// store. - cache_key: MediaRequest, + cache_key: MediaRequestParameters, /// An optional media source for a thumbnail already uploaded. thumbnail_source: Option, @@ -216,7 +216,7 @@ pub enum DependentQueuedRequestKind { /// Media request necessary to retrieve the file itself (not the /// thumbnail). - cache_key: MediaRequest, + cache_key: MediaRequestParameters, /// To which media transaction id does this upload relate to? related_to: OwnedTransactionId, diff --git a/crates/matrix-sdk-sqlite/src/event_cache_store.rs b/crates/matrix-sdk-sqlite/src/event_cache_store.rs index 0caeda04046..77387de9b6d 100644 --- a/crates/matrix-sdk-sqlite/src/event_cache_store.rs +++ b/crates/matrix-sdk-sqlite/src/event_cache_store.rs @@ -4,7 +4,7 @@ use async_trait::async_trait; use deadpool_sqlite::{Object as SqliteAsyncConn, Pool as SqlitePool, Runtime}; use matrix_sdk_base::{ event_cache_store::EventCacheStore, - media::{MediaRequest, UniqueKey}, + media::{MediaRequestParameters, UniqueKey}, }; use matrix_sdk_store_encryption::StoreCipher; use ruma::MilliSecondsSinceUnixEpoch; @@ -182,7 +182,11 @@ impl EventCacheStore for SqliteEventCacheStore { Ok(num_touched == 1) } - async fn add_media_content(&self, request: &MediaRequest, content: Vec) -> Result<()> { + async fn add_media_content( + &self, + request: &MediaRequestParameters, + content: Vec, + ) -> Result<()> { let uri = self.encode_key(keys::MEDIA, request.source.unique_key()); let format = self.encode_key(keys::MEDIA, request.format.unique_key()); let data = self.encode_value(content)?; @@ -199,8 +203,8 @@ impl EventCacheStore for SqliteEventCacheStore { async fn replace_media_key( &self, - from: &MediaRequest, - to: &MediaRequest, + from: &MediaRequestParameters, + to: &MediaRequestParameters, ) -> Result<(), Self::Error> { let prev_uri = self.encode_key(keys::MEDIA, from.source.unique_key()); let prev_format = self.encode_key(keys::MEDIA, from.format.unique_key()); @@ -219,7 +223,7 @@ impl EventCacheStore for SqliteEventCacheStore { Ok(()) } - async fn get_media_content(&self, request: &MediaRequest) -> Result>> { + async fn get_media_content(&self, request: &MediaRequestParameters) -> Result>> { let uri = self.encode_key(keys::MEDIA, request.source.unique_key()); let format = self.encode_key(keys::MEDIA, request.format.unique_key()); @@ -247,7 +251,7 @@ impl EventCacheStore for SqliteEventCacheStore { data.map(|v| self.decode_value(&v).map(Into::into)).transpose() } - async fn remove_media_content(&self, request: &MediaRequest) -> Result<()> { + async fn remove_media_content(&self, request: &MediaRequestParameters) -> Result<()> { let uri = self.encode_key(keys::MEDIA, request.source.unique_key()); let format = self.encode_key(keys::MEDIA, request.format.unique_key()); @@ -277,7 +281,7 @@ mod tests { use matrix_sdk_base::{ event_cache_store::{EventCacheStore, EventCacheStoreError}, event_cache_store_integration_tests, event_cache_store_integration_tests_time, - media::{MediaFormat, MediaRequest, MediaThumbnailSettings}, + media::{MediaFormat, MediaRequestParameters, MediaThumbnailSettings}, }; use matrix_sdk_test::async_test; use once_cell::sync::Lazy; @@ -318,9 +322,11 @@ mod tests { async fn test_last_access() { let event_cache_store = get_event_cache_store().await.expect("creating media cache failed"); let uri = mxc_uri!("mxc://localhost/media"); - let file_request = - MediaRequest { source: MediaSource::Plain(uri.to_owned()), format: MediaFormat::File }; - let thumbnail_request = MediaRequest { + let file_request = MediaRequestParameters { + source: MediaSource::Plain(uri.to_owned()), + format: MediaFormat::File, + }; + let thumbnail_request = MediaRequestParameters { source: MediaSource::Plain(uri.to_owned()), format: MediaFormat::Thumbnail(MediaThumbnailSettings::new( Method::Crop, diff --git a/crates/matrix-sdk/src/account.rs b/crates/matrix-sdk/src/account.rs index d46ed6c8058..249bfa3129e 100644 --- a/crates/matrix-sdk/src/account.rs +++ b/crates/matrix-sdk/src/account.rs @@ -15,7 +15,7 @@ // limitations under the License. use matrix_sdk_base::{ - media::{MediaFormat, MediaRequest}, + media::{MediaFormat, MediaRequestParameters}, store::StateStoreExt, StateStoreDataKey, StateStoreDataValue, }; @@ -217,7 +217,7 @@ impl Account { /// ``` pub async fn get_avatar(&self, format: MediaFormat) -> Result>> { if let Some(url) = self.get_avatar_url().await? { - let request = MediaRequest { source: MediaSource::Plain(url), format }; + let request = MediaRequestParameters { source: MediaSource::Plain(url), format }; Ok(Some(self.client.media().get_media_content(&request, true).await?)) } else { Ok(None) diff --git a/crates/matrix-sdk/src/media.rs b/crates/matrix-sdk/src/media.rs index ffb3f92da6f..5111812ebbf 100644 --- a/crates/matrix-sdk/src/media.rs +++ b/crates/matrix-sdk/src/media.rs @@ -296,7 +296,7 @@ impl Media { #[cfg(not(target_arch = "wasm32"))] pub async fn get_media_file( &self, - request: &MediaRequest, + request: &MediaRequestParameters, filename: Option, content_type: &Mime, use_cache: bool, @@ -371,7 +371,7 @@ impl Media { /// * `use_cache` - If we should use the media cache for this request. pub async fn get_media_content( &self, - request: &MediaRequest, + request: &MediaRequestParameters, use_cache: bool, ) -> Result> { // Read from the cache. @@ -494,7 +494,7 @@ impl Media { /// # Arguments /// /// * `request` - The `MediaRequest` of the content. - pub async fn remove_media_content(&self, request: &MediaRequest) -> Result<()> { + pub async fn remove_media_content(&self, request: &MediaRequestParameters) -> Result<()> { Ok(self.client.event_cache_store().lock().await?.remove_media_content(request).await?) } @@ -530,7 +530,10 @@ impl Media { ) -> Result>> { let Some(source) = event_content.source() else { return Ok(None) }; let file = self - .get_media_content(&MediaRequest { source, format: MediaFormat::File }, use_cache) + .get_media_content( + &MediaRequestParameters { source, format: MediaFormat::File }, + use_cache, + ) .await?; Ok(Some(file)) } @@ -545,7 +548,11 @@ impl Media { /// * `event_content` - The media event content. pub async fn remove_file(&self, event_content: &impl MediaEventContent) -> Result<()> { if let Some(source) = event_content.source() { - self.remove_media_content(&MediaRequest { source, format: MediaFormat::File }).await?; + self.remove_media_content(&MediaRequestParameters { + source, + format: MediaFormat::File, + }) + .await?; } Ok(()) @@ -578,7 +585,7 @@ impl Media { let Some(source) = event_content.thumbnail_source() else { return Ok(None) }; let thumbnail = self .get_media_content( - &MediaRequest { source, format: MediaFormat::Thumbnail(settings) }, + &MediaRequestParameters { source, format: MediaFormat::Thumbnail(settings) }, use_cache, ) .await?; @@ -602,7 +609,7 @@ impl Media { settings: MediaThumbnailSettings, ) -> Result<()> { if let Some(source) = event_content.source() { - self.remove_media_content(&MediaRequest { + self.remove_media_content(&MediaRequestParameters { source, format: MediaFormat::Thumbnail(settings), }) diff --git a/crates/matrix-sdk/src/room/member.rs b/crates/matrix-sdk/src/room/member.rs index 59c774af616..6b5ae84dad3 100644 --- a/crates/matrix-sdk/src/room/member.rs +++ b/crates/matrix-sdk/src/room/member.rs @@ -3,7 +3,7 @@ use std::ops::Deref; use ruma::events::room::MediaSource; use crate::{ - media::{MediaFormat, MediaRequest}, + media::{MediaFormat, MediaRequestParameters}, BaseRoomMember, Client, Result, }; @@ -61,7 +61,7 @@ impl RoomMember { /// ``` pub async fn avatar(&self, format: MediaFormat) -> Result>> { let Some(url) = self.avatar_url() else { return Ok(None) }; - let request = MediaRequest { source: MediaSource::Plain(url.to_owned()), format }; + let request = MediaRequestParameters { source: MediaSource::Plain(url.to_owned()), format }; Ok(Some(self.client.media().get_media_content(&request, true).await?)) } diff --git a/crates/matrix-sdk/src/room/mod.rs b/crates/matrix-sdk/src/room/mod.rs index 7ec2f77e431..2aba1c9b852 100644 --- a/crates/matrix-sdk/src/room/mod.rs +++ b/crates/matrix-sdk/src/room/mod.rs @@ -132,7 +132,7 @@ use crate::{ error::{BeaconError, WrongRoomState}, event_cache::{self, EventCacheDropHandles, RoomEventCache}, event_handler::{EventHandler, EventHandlerDropGuard, EventHandlerHandle, SyncEvent}, - media::{MediaFormat, MediaRequest}, + media::{MediaFormat, MediaRequestParameters}, notification_settings::{IsEncrypted, IsOneToOne, RoomNotificationMode}, room::power_levels::{RoomPowerLevelChanges, RoomPowerLevelsExt}, sync::RoomUpdate, @@ -264,7 +264,7 @@ impl Room { /// ``` pub async fn avatar(&self, format: MediaFormat) -> Result>> { let Some(url) = self.avatar_url() else { return Ok(None) }; - let request = MediaRequest { source: MediaSource::Plain(url.to_owned()), format }; + let request = MediaRequestParameters { source: MediaSource::Plain(url.to_owned()), format }; Ok(Some(self.client.media().get_media_content(&request, true).await?)) } @@ -1994,7 +1994,8 @@ impl Room { // properly, so only log errors during caching. debug!("caching the media"); - let request = MediaRequest { source: media_source.clone(), format: MediaFormat::File }; + let request = + MediaRequestParameters { source: media_source.clone(), format: MediaFormat::File }; if let Err(err) = cache_store_lock_guard.add_media_content(&request, data).await { warn!("unable to cache the media after uploading it: {err}"); @@ -2007,7 +2008,7 @@ impl Room { // Do a best guess at figuring the media request: not animated, cropped // thumbnail of the original size. - let request = MediaRequest { + let request = MediaRequestParameters { source: source.clone(), format: MediaFormat::Thumbnail(MediaThumbnailSettings { method: ruma::media::Method::Scale, diff --git a/crates/matrix-sdk/src/send_queue.rs b/crates/matrix-sdk/src/send_queue.rs index 1ec961f1eaf..0b96416d049 100644 --- a/crates/matrix-sdk/src/send_queue.rs +++ b/crates/matrix-sdk/src/send_queue.rs @@ -140,7 +140,7 @@ use std::{ use as_variant::as_variant; use matrix_sdk_base::{ event_cache_store::EventCacheStoreError, - media::MediaRequest, + media::MediaRequestParameters, store::{ ChildTransactionId, DependentQueuedRequest, DependentQueuedRequestKind, FinishUploadThumbnailInfo, QueueWedgeError, QueuedRequest, QueuedRequestKind, @@ -1034,8 +1034,8 @@ impl QueueStorage { content_type: Mime, send_event_txn: OwnedTransactionId, upload_file_txn: OwnedTransactionId, - file_media_request: MediaRequest, - thumbnail: Option<(FinishUploadThumbnailInfo, MediaRequest, Mime)>, + file_media_request: MediaRequestParameters, + thumbnail: Option<(FinishUploadThumbnailInfo, MediaRequestParameters, Mime)>, ) -> Result<(), RoomSendQueueStorageError> { // Keep the lock until we're done touching the storage. // TODO refactor to make the relationship between being_sent and the store more diff --git a/crates/matrix-sdk/src/send_queue/upload.rs b/crates/matrix-sdk/src/send_queue/upload.rs index 192c5dcfa1d..e3142dda028 100644 --- a/crates/matrix-sdk/src/send_queue/upload.rs +++ b/crates/matrix-sdk/src/send_queue/upload.rs @@ -15,7 +15,7 @@ //! Private implementations of the media upload mechanism. use matrix_sdk_base::{ - media::{MediaFormat, MediaRequest, MediaThumbnailSettings}, + media::{MediaFormat, MediaRequestParameters, MediaThumbnailSettings}, store::{ ChildTransactionId, FinishUploadThumbnailInfo, QueuedRequestKind, SentMediaInfo, SentRequestKey, SerializableEventContent, @@ -47,7 +47,7 @@ use crate::{ /// sending it. /// /// This uses a MXC ID that is only locally valid. -fn make_local_file_media_request(txn_id: &TransactionId) -> MediaRequest { +fn make_local_file_media_request(txn_id: &TransactionId) -> MediaRequestParameters { // This mustn't represent a potentially valid media server, otherwise it'd be // possible for an attacker to return malicious content under some // preconditions (e.g. the cache store has been cleared before the upload @@ -55,7 +55,7 @@ fn make_local_file_media_request(txn_id: &TransactionId) -> MediaRequest { // which is guaranteed to be on the local machine. As a result, the only attack // possible would be coming from the user themselves, which we consider a // non-threat. - MediaRequest { + MediaRequestParameters { source: MediaSource::Plain(OwnedMxcUri::from(format!( "mxc://send-queue.localhost/{txn_id}" ))), @@ -71,7 +71,7 @@ fn make_local_thumbnail_media_request( txn_id: &TransactionId, height: UInt, width: UInt, -) -> MediaRequest { +) -> MediaRequestParameters { // See comment in [`make_local_file_media_request`]. let source = MediaSource::Plain(OwnedMxcUri::from(format!("mxc://send-queue.localhost/{}", txn_id))); @@ -81,7 +81,7 @@ fn make_local_thumbnail_media_request( height, animated: false, }); - MediaRequest { source, format } + MediaRequestParameters { source, format } } /// Replace the source by the final ones in all the media types handled by @@ -300,7 +300,10 @@ impl QueueStorage { .event_cache_store() .replace_media_key( &from_req, - &MediaRequest { source: sent_media.file.clone(), format: MediaFormat::File }, + &MediaRequestParameters { + source: sent_media.file.clone(), + format: MediaFormat::File, + }, ) .await .map_err(RoomSendQueueStorageError::EventCacheStoreError)?; @@ -321,7 +324,7 @@ impl QueueStorage { .event_cache_store() .replace_media_key( &from_req, - &MediaRequest { source: new_source, format: new_format }, + &MediaRequestParameters { source: new_source, format: new_format }, ) .await .map_err(RoomSendQueueStorageError::EventCacheStoreError)?; @@ -358,7 +361,7 @@ impl QueueStorage { next_upload_txn: OwnedTransactionId, parent_key: SentRequestKey, content_type: String, - cache_key: MediaRequest, + cache_key: MediaRequestParameters, event_txn: OwnedTransactionId, ) -> Result<(), RoomSendQueueError> { // The thumbnail has been sent, now transform the dependent file upload request diff --git a/crates/matrix-sdk/tests/integration/media.rs b/crates/matrix-sdk/tests/integration/media.rs index a44d65627b3..dc108b56a39 100644 --- a/crates/matrix-sdk/tests/integration/media.rs +++ b/crates/matrix-sdk/tests/integration/media.rs @@ -1,7 +1,7 @@ use matrix_sdk::{ config::RequestConfig, matrix_auth::{MatrixSession, MatrixSessionTokens}, - media::{MediaFormat, MediaRequest, MediaThumbnailSettings}, + media::{MediaFormat, MediaRequestParameters, MediaThumbnailSettings}, test_utils::logged_in_client_with_server, Client, SessionMeta, }; @@ -35,7 +35,7 @@ async fn test_get_media_content_no_auth() { let media = client.media(); - let request = MediaRequest { + let request = MediaRequestParameters { source: MediaSource::Plain(mxc_uri!("mxc://localhost/textfile").to_owned()), format: MediaFormat::File, }; diff --git a/crates/matrix-sdk/tests/integration/room/attachment/mod.rs b/crates/matrix-sdk/tests/integration/room/attachment/mod.rs index 4e0a7fb6e5f..012a0832168 100644 --- a/crates/matrix-sdk/tests/integration/room/attachment/mod.rs +++ b/crates/matrix-sdk/tests/integration/room/attachment/mod.rs @@ -6,7 +6,7 @@ use matrix_sdk::{ Thumbnail, }, config::SyncSettings, - media::{MediaFormat, MediaRequest, MediaThumbnailSettings}, + media::{MediaFormat, MediaRequestParameters, MediaThumbnailSettings}, test_utils::logged_in_client_with_server, }; use matrix_sdk_test::{async_test, mocks::mock_encryption_state, test_json, DEFAULT_TEST_ROOM_ID}; @@ -245,8 +245,8 @@ async fn test_room_attachment_send_info_thumbnail() { // Preconditions: nothing is found in the cache. let media_request = - MediaRequest { source: MediaSource::Plain(media_mxc), format: MediaFormat::File }; - let thumbnail_request = MediaRequest { + MediaRequestParameters { source: MediaSource::Plain(media_mxc), format: MediaFormat::File }; + let thumbnail_request = MediaRequestParameters { source: MediaSource::Plain(thumbnail_mxc.clone()), format: MediaFormat::Thumbnail(MediaThumbnailSettings { method: ruma::media::Method::Scale, @@ -297,7 +297,7 @@ async fn test_room_attachment_send_info_thumbnail() { let _ = client .media() .get_media_content( - &MediaRequest { + &MediaRequestParameters { source: MediaSource::Plain(thumbnail_mxc.clone()), format: MediaFormat::File, }, @@ -307,7 +307,7 @@ async fn test_room_attachment_send_info_thumbnail() { .unwrap_err(); // But it is not found when requesting it as a thumbnail with a different size. - let thumbnail_request = MediaRequest { + let thumbnail_request = MediaRequestParameters { source: MediaSource::Plain(thumbnail_mxc), format: MediaFormat::Thumbnail(MediaThumbnailSettings { method: ruma::media::Method::Scale, diff --git a/crates/matrix-sdk/tests/integration/send_queue.rs b/crates/matrix-sdk/tests/integration/send_queue.rs index 6573e0d0243..7ec87d6023e 100644 --- a/crates/matrix-sdk/tests/integration/send_queue.rs +++ b/crates/matrix-sdk/tests/integration/send_queue.rs @@ -11,7 +11,7 @@ use assert_matches2::{assert_let, assert_matches}; use matrix_sdk::{ attachment::{AttachmentConfig, AttachmentInfo, BaseImageInfo, BaseThumbnailInfo, Thumbnail}, config::{RequestConfig, StoreConfig}, - media::{MediaFormat, MediaRequest, MediaThumbnailSettings}, + media::{MediaFormat, MediaRequestParameters, MediaThumbnailSettings}, send_queue::{ LocalEcho, LocalEchoContent, RoomSendQueueError, RoomSendQueueStorageError, RoomSendQueueUpdate, @@ -2122,7 +2122,10 @@ async fn test_media_uploads() { // The media is immediately available from the cache. let file_media = client .media() - .get_media_content(&MediaRequest { source: local_source, format: MediaFormat::File }, true) + .get_media_content( + &MediaRequestParameters { source: local_source, format: MediaFormat::File }, + true, + ) .await .expect("media should be found"); assert_eq!(file_media, b"hello world"); @@ -2145,7 +2148,7 @@ async fn test_media_uploads() { let thumbnail_media = client .media() .get_media_content( - &MediaRequest { + &MediaRequestParameters { source: local_thumbnail_source, // TODO: extract this reasonable query into a helper function shared across the // codebase @@ -2203,7 +2206,7 @@ async fn test_media_uploads() { let file_media = client .media() .get_media_content( - &MediaRequest { source: new_content.source, format: MediaFormat::File }, + &MediaRequestParameters { source: new_content.source, format: MediaFormat::File }, true, ) .await @@ -2217,7 +2220,7 @@ async fn test_media_uploads() { let thumbnail_media = client .media() .get_media_content( - &MediaRequest { + &MediaRequestParameters { source: new_thumbnail_source, // TODO: extract this reasonable query into a helper function shared across the // codebase