Skip to content

Commit c503583

Browse files
authored
refactor!(sdk): Set thumbnail in AttachmentConfig with builder method instead of constructor
`AttachmentConfig::with_thumbnail()` is replaced by `AttachmentConfig::new().thumbnail()`. Simplifies the use of `AttachmentConfig`, by avoiding code like: ```rust let config = if let Some(thumbnail) = thumbnail { AttachmentConfig::with_thumbnail(thumbnail) } else { AttachmentConfig::new() }; ``` --------- Signed-off-by: Kévin Commaille <[email protected]>
1 parent adb4428 commit c503583

File tree

5 files changed

+46
-33
lines changed

5 files changed

+46
-33
lines changed

bindings/matrix-sdk-ffi/src/timeline/mod.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,9 @@ impl Timeline {
137137
fn build_thumbnail_info(
138138
thumbnail_url: Option<String>,
139139
thumbnail_info: Option<ThumbnailInfo>,
140-
) -> Result<AttachmentConfig, RoomError> {
140+
) -> Result<Option<Thumbnail>, RoomError> {
141141
match (thumbnail_url, thumbnail_info) {
142-
(None, None) => Ok(AttachmentConfig::new()),
142+
(None, None) => Ok(None),
143143

144144
(Some(thumbnail_url), Some(thumbnail_info)) => {
145145
let thumbnail_data =
@@ -163,15 +163,18 @@ fn build_thumbnail_info(
163163
let mime_type =
164164
mime_str.parse::<Mime>().map_err(|_| RoomError::InvalidAttachmentMimeType)?;
165165

166-
let thumbnail =
167-
Thumbnail { data: thumbnail_data, content_type: mime_type, height, width, size };
168-
169-
Ok(AttachmentConfig::with_thumbnail(thumbnail))
166+
Ok(Some(Thumbnail {
167+
data: thumbnail_data,
168+
content_type: mime_type,
169+
height,
170+
width,
171+
size,
172+
}))
170173
}
171174

172175
_ => {
173176
warn!("Ignoring thumbnail because either the thumbnail URL or info isn't defined");
174-
Ok(AttachmentConfig::new())
177+
Ok(None)
175178
}
176179
}
177180
}
@@ -304,8 +307,10 @@ impl Timeline {
304307
let base_image_info = BaseImageInfo::try_from(&image_info)
305308
.map_err(|_| RoomError::InvalidAttachmentData)?;
306309
let attachment_info = AttachmentInfo::Image(base_image_info);
310+
let thumbnail = build_thumbnail_info(thumbnail_url, image_info.thumbnail_info)?;
307311

308-
let attachment_config = build_thumbnail_info(thumbnail_url, image_info.thumbnail_info)?
312+
let attachment_config = AttachmentConfig::new()
313+
.thumbnail(thumbnail)
309314
.info(attachment_info)
310315
.caption(caption)
311316
.formatted_caption(formatted_caption);
@@ -338,8 +343,10 @@ impl Timeline {
338343
let base_video_info: BaseVideoInfo = BaseVideoInfo::try_from(&video_info)
339344
.map_err(|_| RoomError::InvalidAttachmentData)?;
340345
let attachment_info = AttachmentInfo::Video(base_video_info);
346+
let thumbnail = build_thumbnail_info(thumbnail_url, video_info.thumbnail_info)?;
341347

342-
let attachment_config = build_thumbnail_info(thumbnail_url, video_info.thumbnail_info)?
348+
let attachment_config = AttachmentConfig::new()
349+
.thumbnail(thumbnail)
343350
.info(attachment_info)
344351
.caption(caption)
345352
.formatted_caption(formatted_caption.map(Into::into));

crates/matrix-sdk/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ All notable changes to this project will be documented in this file.
1212
`Client::send()` method to the `with_request_config()` builder method. You
1313
should call `Client::send(request).with_request_config(request_config).await`
1414
now instead.
15+
- [**breaking**] Remove the `AttachmentConfig::with_thumbnail()` constructor and
16+
replace it with the `AttachmentConfig::thumbnail()` builder method. You should
17+
call `AttachmentConfig::new().thumbnail(thumbnail)` now instead.
1518

1619
## [0.9.0] - 2024-12-18
1720

crates/matrix-sdk/src/attachment.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -188,21 +188,21 @@ pub struct AttachmentConfig {
188188
}
189189

190190
impl AttachmentConfig {
191-
/// Create a new default `AttachmentConfig` without providing a thumbnail.
192-
///
193-
/// To provide a thumbnail use [`AttachmentConfig::with_thumbnail()`].
191+
/// Create a new empty `AttachmentConfig`.
194192
pub fn new() -> Self {
195193
Self::default()
196194
}
197195

198-
/// Create a new default `AttachmentConfig` with a `thumbnail`.
196+
/// Set the thumbnail to send.
199197
///
200198
/// # Arguments
201199
///
202200
/// * `thumbnail` - The thumbnail of the media. If the `content_type` does
203-
/// not support it (eg audio clips), it is ignored.
204-
pub fn with_thumbnail(thumbnail: Thumbnail) -> Self {
205-
Self { thumbnail: Some(thumbnail), ..Default::default() }
201+
/// not support it (e.g. audio clips), it is ignored.
202+
#[must_use]
203+
pub fn thumbnail(mut self, thumbnail: Option<Thumbnail>) -> Self {
204+
self.thumbnail = thumbnail;
205+
self
206206
}
207207

208208
/// Set the transaction ID to send.

crates/matrix-sdk/tests/integration/room/attachment/mod.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -204,19 +204,20 @@ async fn test_room_attachment_send_info_thumbnail() {
204204
let _ = client.media().get_media_content(&thumbnail_request, true).await.unwrap_err();
205205

206206
// Send the attachment with a thumbnail.
207-
let config = AttachmentConfig::with_thumbnail(Thumbnail {
208-
data: b"Thumbnail".to_vec(),
209-
content_type: mime::IMAGE_JPEG,
210-
height: uint!(360),
211-
width: uint!(480),
212-
size: uint!(3600),
213-
})
214-
.info(AttachmentInfo::Image(BaseImageInfo {
215-
height: Some(uint!(600)),
216-
width: Some(uint!(800)),
217-
size: None,
218-
blurhash: None,
219-
}));
207+
let config = AttachmentConfig::new()
208+
.thumbnail(Some(Thumbnail {
209+
data: b"Thumbnail".to_vec(),
210+
content_type: mime::IMAGE_JPEG,
211+
height: uint!(360),
212+
width: uint!(480),
213+
size: uint!(3600),
214+
}))
215+
.info(AttachmentInfo::Image(BaseImageInfo {
216+
height: Some(uint!(600)),
217+
width: Some(uint!(800)),
218+
size: None,
219+
blurhash: None,
220+
}));
220221

221222
let response = room
222223
.send_attachment("image", &mime::IMAGE_JPEG, b"Hello world".to_vec(), config)

crates/matrix-sdk/tests/integration/send_queue.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,14 @@ async fn queue_attachment_with_thumbnail(q: &RoomSendQueue) -> (SendHandle, &'st
7979
size: uint!(42),
8080
};
8181

82-
let config =
83-
AttachmentConfig::with_thumbnail(thumbnail).info(AttachmentInfo::Image(BaseImageInfo {
82+
let config = AttachmentConfig::new().thumbnail(Some(thumbnail)).info(AttachmentInfo::Image(
83+
BaseImageInfo {
8484
height: Some(uint!(13)),
8585
width: Some(uint!(37)),
8686
size: Some(uint!(42)),
8787
blurhash: None,
88-
}));
88+
},
89+
));
8990

9091
let handle = q
9192
.send_attachment(filename, content_type, data, config)
@@ -1801,7 +1802,8 @@ async fn test_media_uploads() {
18011802

18021803
let transaction_id = TransactionId::new();
18031804
let mentions = Mentions::with_user_ids([owned_user_id!("@ivan:sdk.rs")]);
1804-
let config = AttachmentConfig::with_thumbnail(thumbnail)
1805+
let config = AttachmentConfig::new()
1806+
.thumbnail(Some(thumbnail))
18051807
.txn_id(&transaction_id)
18061808
.caption(Some("caption".to_owned()))
18071809
.mentions(Some(mentions.clone()))

0 commit comments

Comments
 (0)