Skip to content

Commit

Permalink
refactor!(media): inline MediaThumbnailSize into `MediaThumbnailSet…
Browse files Browse the repository at this point in the history
…tings`

Changelog: all the fields of `MediaThumbnailSize` have been inlined into
 `MediaThumbnailSettings`, and the former type has been removed.
  • Loading branch information
bnjbvr committed Nov 6, 2024
1 parent 4bbe620 commit 566a13b
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 57 deletions.
21 changes: 4 additions & 17 deletions crates/matrix-sdk-base/src/media.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ impl UniqueKey for MediaFormat {
}
}

/// The requested size of a media thumbnail.
/// The desired settings of a media thumbnail.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct MediaThumbnailSize {
pub struct MediaThumbnailSettings {
/// The desired resizing method.
pub method: Method,

Expand All @@ -57,19 +57,6 @@ pub struct MediaThumbnailSize {
/// The desired height of the thumbnail. The actual thumbnail may not match
/// the size specified.
pub height: UInt,
}

impl UniqueKey for MediaThumbnailSize {
fn unique_key(&self) -> String {
format!("{}{UNIQUE_SEPARATOR}{}x{}", self.method, self.width, self.height)
}
}

/// The desired settings of a media thumbnail.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct MediaThumbnailSettings {
/// The desired size of the thumbnail.
pub size: MediaThumbnailSize,

/// If we want to request an animated thumbnail from the homeserver.
///
Expand All @@ -84,13 +71,13 @@ impl MediaThumbnailSettings {
/// Constructs a new `MediaThumbnailSettings` with the given method, width
/// and height.
pub fn new(method: Method, width: UInt, height: UInt) -> Self {
Self { size: MediaThumbnailSize { method, width, height }, animated: false }
Self { method, width, height, animated: false }
}
}

impl UniqueKey for MediaThumbnailSettings {
fn unique_key(&self) -> String {
let mut key = self.size.unique_key();
let mut key = format!("{}{UNIQUE_SEPARATOR}{}x{}", self.method, self.width, self.height);

if self.animated {
key.push_str(UNIQUE_SEPARATOR);
Expand Down
13 changes: 7 additions & 6 deletions crates/matrix-sdk/src/media.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,16 +437,17 @@ impl Media {

content
}

MediaSource::Plain(uri) => {
if let MediaFormat::Thumbnail(settings) = &request.format {
if use_auth {
let mut request =
authenticated_media::get_content_thumbnail::v1::Request::from_uri(
uri,
settings.size.width,
settings.size.height,
settings.width,
settings.height,
)?;
request.method = Some(settings.size.method.clone());
request.method = Some(settings.method.clone());
request.animated = Some(settings.animated);

self.client.send(request, request_config).await?.file
Expand All @@ -455,10 +456,10 @@ impl Media {
let request = {
let mut request = media::get_content_thumbnail::v3::Request::from_url(
uri,
settings.size.width,
settings.size.height,
settings.width,
settings.height,
)?;
request.method = Some(settings.size.method.clone());
request.method = Some(settings.method.clone());
request.animated = Some(settings.animated);
request
};
Expand Down
10 changes: 4 additions & 6 deletions crates/matrix-sdk/src/room/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ use matrix_sdk_base::{
deserialized_responses::{
RawAnySyncOrStrippedState, RawSyncOrStrippedState, SyncOrStrippedState, TimelineEvent,
},
media::{MediaThumbnailSettings, MediaThumbnailSize},
media::MediaThumbnailSettings,
store::StateStoreExt,
ComposerDraft, RoomInfoNotableUpdateReasons, RoomMemberships, StateChanges, StateStoreDataKey,
StateStoreDataValue,
Expand Down Expand Up @@ -2010,11 +2010,9 @@ impl Room {
let request = MediaRequest {
source: source.clone(),
format: MediaFormat::Thumbnail(MediaThumbnailSettings {
size: MediaThumbnailSize {
method: ruma::media::Method::Scale,
width,
height,
},
method: ruma::media::Method::Scale,
width,
height,
animated: false,
}),
};
Expand Down
6 changes: 4 additions & 2 deletions crates/matrix-sdk/src/send_queue/upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
//! Private implementations of the media upload mechanism.
use matrix_sdk_base::{
media::{MediaFormat, MediaRequest, MediaThumbnailSettings, MediaThumbnailSize},
media::{MediaFormat, MediaRequest, MediaThumbnailSettings},
store::{
ChildTransactionId, FinishUploadThumbnailInfo, QueuedRequestKind, SentMediaInfo,
SentRequestKey, SerializableEventContent,
Expand Down Expand Up @@ -76,7 +76,9 @@ fn make_local_thumbnail_media_request(
let source =
MediaSource::Plain(OwnedMxcUri::from(format!("mxc://send-queue.localhost/{}", txn_id)));
let format = MediaFormat::Thumbnail(MediaThumbnailSettings {
size: MediaThumbnailSize { method: Method::Scale, width, height },
method: Method::Scale,
width,
height,
animated: false,
});
MediaRequest { source, format }
Expand Down
14 changes: 10 additions & 4 deletions crates/matrix-sdk/tests/integration/media.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use matrix_sdk::{
config::RequestConfig,
matrix_auth::{MatrixSession, MatrixSessionTokens},
media::{MediaFormat, MediaRequest, MediaThumbnailSettings, MediaThumbnailSize},
media::{MediaFormat, MediaRequest, MediaThumbnailSettings},
test_utils::logged_in_client_with_server,
Client, SessionMeta,
};
Expand Down Expand Up @@ -183,7 +183,9 @@ async fn test_get_media_file_no_auth() {
.await;

let settings = MediaThumbnailSettings {
size: MediaThumbnailSize { method: Method::Crop, width: uint!(100), height: uint!(100) },
method: Method::Crop,
width: uint!(100),
height: uint!(100),
animated: true,
};
client.media().get_thumbnail(&event_content, settings, true).await.unwrap();
Expand Down Expand Up @@ -293,7 +295,9 @@ async fn test_get_media_file_with_auth_matrix_1_11() {
.await;

let settings = MediaThumbnailSettings {
size: MediaThumbnailSize { method: Method::Crop, width: uint!(100), height: uint!(100) },
method: Method::Crop,
width: uint!(100),
height: uint!(100),
animated: true,
};
client.media().get_thumbnail(&event_content, settings, true).await.unwrap();
Expand Down Expand Up @@ -406,7 +410,9 @@ async fn test_get_media_file_with_auth_matrix_stable_feature() {
.await;

let settings = MediaThumbnailSettings {
size: MediaThumbnailSize { method: Method::Crop, width: uint!(100), height: uint!(100) },
method: Method::Crop,
width: uint!(100),
height: uint!(100),
animated: true,
};
client.media().get_thumbnail(&event_content, settings, true).await.unwrap();
Expand Down
18 changes: 7 additions & 11 deletions crates/matrix-sdk/tests/integration/room/attachment/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use matrix_sdk::{
Thumbnail,
},
config::SyncSettings,
media::{MediaFormat, MediaRequest, MediaThumbnailSettings, MediaThumbnailSize},
media::{MediaFormat, MediaRequest, MediaThumbnailSettings},
test_utils::logged_in_client_with_server,
};
use matrix_sdk_test::{async_test, mocks::mock_encryption_state, test_json, DEFAULT_TEST_ROOM_ID};
Expand Down Expand Up @@ -249,11 +249,9 @@ async fn test_room_attachment_send_info_thumbnail() {
let thumbnail_request = MediaRequest {
source: MediaSource::Plain(thumbnail_mxc.clone()),
format: MediaFormat::Thumbnail(MediaThumbnailSettings {
size: MediaThumbnailSize {
method: ruma::media::Method::Scale,
width: uint!(480),
height: uint!(360),
},
method: ruma::media::Method::Scale,
width: uint!(480),
height: uint!(360),
animated: false,
}),
};
Expand Down Expand Up @@ -312,11 +310,9 @@ async fn test_room_attachment_send_info_thumbnail() {
let thumbnail_request = MediaRequest {
source: MediaSource::Plain(thumbnail_mxc),
format: MediaFormat::Thumbnail(MediaThumbnailSettings {
size: MediaThumbnailSize {
method: ruma::media::Method::Scale,
width: uint!(42),
height: uint!(1337),
},
method: ruma::media::Method::Scale,
width: uint!(42),
height: uint!(1337),
animated: false,
}),
};
Expand Down
18 changes: 7 additions & 11 deletions crates/matrix-sdk/tests/integration/send_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, MediaThumbnailSize},
media::{MediaFormat, MediaRequest, MediaThumbnailSettings},
send_queue::{
LocalEcho, LocalEchoContent, RoomSendQueueError, RoomSendQueueStorageError,
RoomSendQueueUpdate,
Expand Down Expand Up @@ -2150,11 +2150,9 @@ async fn test_media_uploads() {
// TODO: extract this reasonable query into a helper function shared across the
// codebase
format: MediaFormat::Thumbnail(MediaThumbnailSettings {
size: MediaThumbnailSize {
height: tinfo.height.unwrap(),
width: tinfo.width.unwrap(),
method: Method::Scale,
},
height: tinfo.height.unwrap(),
width: tinfo.width.unwrap(),
method: Method::Scale,
animated: false,
}),
},
Expand Down Expand Up @@ -2224,11 +2222,9 @@ async fn test_media_uploads() {
// TODO: extract this reasonable query into a helper function shared across the
// codebase
format: MediaFormat::Thumbnail(MediaThumbnailSettings {
size: MediaThumbnailSize {
height: tinfo.height.unwrap(),
width: tinfo.width.unwrap(),
method: Method::Scale,
},
height: tinfo.height.unwrap(),
width: tinfo.width.unwrap(),
method: Method::Scale,
animated: false,
}),
},
Expand Down

0 comments on commit 566a13b

Please sign in to comment.