mirror of
https://github.com/matrix-org/matrix-rust-sdk.git
synced 2026-05-18 21:52:30 -04:00
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 <zecakeh@tedomum.fr>
This commit is contained in:
@@ -137,9 +137,9 @@ impl Timeline {
|
||||
fn build_thumbnail_info(
|
||||
thumbnail_url: Option<String>,
|
||||
thumbnail_info: Option<ThumbnailInfo>,
|
||||
) -> Result<AttachmentConfig, RoomError> {
|
||||
) -> Result<Option<Thumbnail>, RoomError> {
|
||||
match (thumbnail_url, thumbnail_info) {
|
||||
(None, None) => Ok(AttachmentConfig::new()),
|
||||
(None, None) => Ok(None),
|
||||
|
||||
(Some(thumbnail_url), Some(thumbnail_info)) => {
|
||||
let thumbnail_data =
|
||||
@@ -163,15 +163,18 @@ fn build_thumbnail_info(
|
||||
let mime_type =
|
||||
mime_str.parse::<Mime>().map_err(|_| RoomError::InvalidAttachmentMimeType)?;
|
||||
|
||||
let thumbnail =
|
||||
Thumbnail { data: thumbnail_data, content_type: mime_type, height, width, size };
|
||||
|
||||
Ok(AttachmentConfig::with_thumbnail(thumbnail))
|
||||
Ok(Some(Thumbnail {
|
||||
data: thumbnail_data,
|
||||
content_type: mime_type,
|
||||
height,
|
||||
width,
|
||||
size,
|
||||
}))
|
||||
}
|
||||
|
||||
_ => {
|
||||
warn!("Ignoring thumbnail because either the thumbnail URL or info isn't defined");
|
||||
Ok(AttachmentConfig::new())
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -304,8 +307,10 @@ impl Timeline {
|
||||
let base_image_info = BaseImageInfo::try_from(&image_info)
|
||||
.map_err(|_| RoomError::InvalidAttachmentData)?;
|
||||
let attachment_info = AttachmentInfo::Image(base_image_info);
|
||||
let thumbnail = build_thumbnail_info(thumbnail_url, image_info.thumbnail_info)?;
|
||||
|
||||
let attachment_config = build_thumbnail_info(thumbnail_url, image_info.thumbnail_info)?
|
||||
let attachment_config = AttachmentConfig::new()
|
||||
.thumbnail(thumbnail)
|
||||
.info(attachment_info)
|
||||
.caption(caption)
|
||||
.formatted_caption(formatted_caption);
|
||||
@@ -338,8 +343,10 @@ impl Timeline {
|
||||
let base_video_info: BaseVideoInfo = BaseVideoInfo::try_from(&video_info)
|
||||
.map_err(|_| RoomError::InvalidAttachmentData)?;
|
||||
let attachment_info = AttachmentInfo::Video(base_video_info);
|
||||
let thumbnail = build_thumbnail_info(thumbnail_url, video_info.thumbnail_info)?;
|
||||
|
||||
let attachment_config = build_thumbnail_info(thumbnail_url, video_info.thumbnail_info)?
|
||||
let attachment_config = AttachmentConfig::new()
|
||||
.thumbnail(thumbnail)
|
||||
.info(attachment_info)
|
||||
.caption(caption)
|
||||
.formatted_caption(formatted_caption.map(Into::into));
|
||||
|
||||
@@ -12,6 +12,9 @@ All notable changes to this project will be documented in this file.
|
||||
`Client::send()` method to the `with_request_config()` builder method. You
|
||||
should call `Client::send(request).with_request_config(request_config).await`
|
||||
now instead.
|
||||
- [**breaking**] Remove the `AttachmentConfig::with_thumbnail()` constructor and
|
||||
replace it with the `AttachmentConfig::thumbnail()` builder method. You should
|
||||
call `AttachmentConfig::new().thumbnail(thumbnail)` now instead.
|
||||
|
||||
## [0.9.0] - 2024-12-18
|
||||
|
||||
|
||||
@@ -188,21 +188,21 @@ pub struct AttachmentConfig {
|
||||
}
|
||||
|
||||
impl AttachmentConfig {
|
||||
/// Create a new default `AttachmentConfig` without providing a thumbnail.
|
||||
///
|
||||
/// To provide a thumbnail use [`AttachmentConfig::with_thumbnail()`].
|
||||
/// Create a new empty `AttachmentConfig`.
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
/// Create a new default `AttachmentConfig` with a `thumbnail`.
|
||||
/// Set the thumbnail to send.
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `thumbnail` - The thumbnail of the media. If the `content_type` does
|
||||
/// not support it (eg audio clips), it is ignored.
|
||||
pub fn with_thumbnail(thumbnail: Thumbnail) -> Self {
|
||||
Self { thumbnail: Some(thumbnail), ..Default::default() }
|
||||
/// not support it (e.g. audio clips), it is ignored.
|
||||
#[must_use]
|
||||
pub fn thumbnail(mut self, thumbnail: Option<Thumbnail>) -> Self {
|
||||
self.thumbnail = thumbnail;
|
||||
self
|
||||
}
|
||||
|
||||
/// Set the transaction ID to send.
|
||||
|
||||
@@ -204,19 +204,20 @@ async fn test_room_attachment_send_info_thumbnail() {
|
||||
let _ = client.media().get_media_content(&thumbnail_request, true).await.unwrap_err();
|
||||
|
||||
// Send the attachment with a thumbnail.
|
||||
let config = AttachmentConfig::with_thumbnail(Thumbnail {
|
||||
data: b"Thumbnail".to_vec(),
|
||||
content_type: mime::IMAGE_JPEG,
|
||||
height: uint!(360),
|
||||
width: uint!(480),
|
||||
size: uint!(3600),
|
||||
})
|
||||
.info(AttachmentInfo::Image(BaseImageInfo {
|
||||
height: Some(uint!(600)),
|
||||
width: Some(uint!(800)),
|
||||
size: None,
|
||||
blurhash: None,
|
||||
}));
|
||||
let config = AttachmentConfig::new()
|
||||
.thumbnail(Some(Thumbnail {
|
||||
data: b"Thumbnail".to_vec(),
|
||||
content_type: mime::IMAGE_JPEG,
|
||||
height: uint!(360),
|
||||
width: uint!(480),
|
||||
size: uint!(3600),
|
||||
}))
|
||||
.info(AttachmentInfo::Image(BaseImageInfo {
|
||||
height: Some(uint!(600)),
|
||||
width: Some(uint!(800)),
|
||||
size: None,
|
||||
blurhash: None,
|
||||
}));
|
||||
|
||||
let response = room
|
||||
.send_attachment("image", &mime::IMAGE_JPEG, b"Hello world".to_vec(), config)
|
||||
|
||||
@@ -79,13 +79,14 @@ async fn queue_attachment_with_thumbnail(q: &RoomSendQueue) -> (SendHandle, &'st
|
||||
size: uint!(42),
|
||||
};
|
||||
|
||||
let config =
|
||||
AttachmentConfig::with_thumbnail(thumbnail).info(AttachmentInfo::Image(BaseImageInfo {
|
||||
let config = AttachmentConfig::new().thumbnail(Some(thumbnail)).info(AttachmentInfo::Image(
|
||||
BaseImageInfo {
|
||||
height: Some(uint!(13)),
|
||||
width: Some(uint!(37)),
|
||||
size: Some(uint!(42)),
|
||||
blurhash: None,
|
||||
}));
|
||||
},
|
||||
));
|
||||
|
||||
let handle = q
|
||||
.send_attachment(filename, content_type, data, config)
|
||||
@@ -1801,7 +1802,8 @@ async fn test_media_uploads() {
|
||||
|
||||
let transaction_id = TransactionId::new();
|
||||
let mentions = Mentions::with_user_ids([owned_user_id!("@ivan:sdk.rs")]);
|
||||
let config = AttachmentConfig::with_thumbnail(thumbnail)
|
||||
let config = AttachmentConfig::new()
|
||||
.thumbnail(Some(thumbnail))
|
||||
.txn_id(&transaction_id)
|
||||
.caption(Some("caption".to_owned()))
|
||||
.mentions(Some(mentions.clone()))
|
||||
|
||||
Reference in New Issue
Block a user