Skip to content

Commit 6caf20d

Browse files
committed
refactor(sdk): Use TextMessageEventContent to send a caption
It doesn't make sense to send a formatted caption without a plain text caption so using TextMessageEventContent forces the latter to be present. This also allows to use the helpful constructors of TextMessageEventContent. Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
1 parent 681b221 commit 6caf20d

File tree

10 files changed

+86
-140
lines changed

10 files changed

+86
-140
lines changed

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

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ use matrix_sdk_ui::timeline::{
3737
use mime::Mime;
3838
use reply::{EmbeddedEventDetails, InReplyToDetails};
3939
use ruma::{
40+
assign,
4041
events::{
4142
location::{AssetType as RumaAssetType, LocationContent, ZoomLevel},
4243
poll::{
@@ -49,6 +50,7 @@ use ruma::{
4950
},
5051
room::message::{
5152
LocationMessageEventContent, MessageType, RoomMessageEventContentWithoutRelation,
53+
TextMessageEventContent,
5254
},
5355
AnyMessageLikeEventContent,
5456
},
@@ -111,16 +113,16 @@ impl Timeline {
111113
.transpose()
112114
.map_err(|_| RoomError::InvalidRepliedToEventId)?;
113115

114-
let formatted_caption = formatted_body_from(
115-
params.caption.as_deref(),
116-
params.formatted_caption.map(Into::into),
117-
);
116+
let caption = params.caption.map(|caption| {
117+
let formatted =
118+
formatted_body_from(Some(&caption), params.formatted_caption.map(Into::into));
119+
assign!(TextMessageEventContent::plain(caption), { formatted })
120+
});
118121

119122
let attachment_config = AttachmentConfig {
120123
info: Some(attachment_info),
121124
thumbnail,
122-
caption: params.caption,
123-
formatted_caption,
125+
caption,
124126
mentions: params.mentions.map(Into::into),
125127
in_reply_to: in_reply_to_event_id,
126128
..Default::default()
@@ -1337,7 +1339,7 @@ mod galleries {
13371339
use matrix_sdk_common::executor::{AbortHandle, JoinHandle};
13381340
use matrix_sdk_ui::timeline::GalleryConfig;
13391341
use mime::Mime;
1340-
use ruma::EventId;
1342+
use ruma::{assign, events::room::message::TextMessageEventContent, EventId};
13411343
use tokio::sync::Mutex;
13421344
use tracing::error;
13431345

@@ -1476,15 +1478,18 @@ mod galleries {
14761478
let mime_str = self.mimetype().as_ref().ok_or(RoomError::InvalidAttachmentMimeType)?;
14771479
let mime_type =
14781480
mime_str.parse::<Mime>().map_err(|_| RoomError::InvalidAttachmentMimeType)?;
1481+
let caption = self.caption().as_ref().map(|caption| {
1482+
let formatted = formatted_body_from(
1483+
Some(caption),
1484+
self.formatted_caption().clone().map(Into::into),
1485+
);
1486+
assign!(TextMessageEventContent::plain(caption), { formatted })
1487+
});
14791488
Ok(matrix_sdk_ui::timeline::GalleryItemInfo {
14801489
source: self.source().clone().into(),
14811490
content_type: mime_type,
14821491
attachment_info: self.attachment_info()?,
1483-
caption: self.caption().clone(),
1484-
formatted_caption: self
1485-
.formatted_caption()
1486-
.clone()
1487-
.map(ruma::events::room::message::FormattedBody::from),
1492+
caption,
14881493
thumbnail: self.thumbnail()?,
14891494
})
14901495
}
@@ -1543,10 +1548,11 @@ mod galleries {
15431548
params: GalleryUploadParameters,
15441549
item_infos: Vec<GalleryItemInfo>,
15451550
) -> Result<Arc<SendGalleryJoinHandle>, RoomError> {
1546-
let formatted_caption = formatted_body_from(
1547-
params.caption.as_deref(),
1548-
params.formatted_caption.map(Into::into),
1549-
);
1551+
let caption = params.caption.map(|caption| {
1552+
let formatted =
1553+
formatted_body_from(Some(&caption), params.formatted_caption.map(Into::into));
1554+
assign!(TextMessageEventContent::plain(caption), { formatted })
1555+
});
15501556

15511557
let in_reply_to = params
15521558
.in_reply_to
@@ -1556,8 +1562,7 @@ mod galleries {
15561562
.map_err(|_| RoomError::InvalidRepliedToEventId)?;
15571563

15581564
let mut gallery_config = GalleryConfig::new()
1559-
.caption(params.caption)
1560-
.formatted_caption(formatted_caption)
1565+
.caption(caption)
15611566
.mentions(params.mentions.map(Into::into))
15621567
.in_reply_to(in_reply_to);
15631568

crates/matrix-sdk-ui/src/timeline/futures.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ impl<'a> IntoFuture for SendAttachment<'a> {
7979
info: config.info,
8080
thumbnail: config.thumbnail,
8181
caption: config.caption,
82-
formatted_caption: config.formatted_caption,
8382
mentions: config.mentions,
8483
reply,
8584
};
@@ -153,11 +152,7 @@ mod galleries {
153152
config = config.add_item(item.try_into()?);
154153
}
155154

156-
config = config
157-
.caption(gallery.caption)
158-
.formatted_caption(gallery.formatted_caption)
159-
.mentions(gallery.mentions)
160-
.reply(reply);
155+
config = config.caption(gallery.caption).mentions(gallery.mentions).reply(reply);
161156

162157
timeline
163158
.room()

crates/matrix-sdk-ui/src/timeline/mod.rs

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ use ruma::{
5050
relation::Thread,
5151
room::{
5252
message::{
53-
FormattedBody, Relation, RelationWithoutReplacement, ReplyWithinThread,
54-
RoomMessageEventContentWithoutRelation,
53+
Relation, RelationWithoutReplacement, ReplyWithinThread,
54+
RoomMessageEventContentWithoutRelation, TextMessageEventContent,
5555
},
5656
pinned_events::RoomPinnedEventsEventContent,
5757
},
@@ -185,8 +185,7 @@ pub struct AttachmentConfig {
185185
pub txn_id: Option<OwnedTransactionId>,
186186
pub info: Option<AttachmentInfo>,
187187
pub thumbnail: Option<Thumbnail>,
188-
pub caption: Option<String>,
189-
pub formatted_caption: Option<FormattedBody>,
188+
pub caption: Option<TextMessageEventContent>,
190189
pub mentions: Option<Mentions>,
191190
pub in_reply_to: Option<OwnedEventId>,
192191
}
@@ -985,8 +984,7 @@ where
985984
pub struct GalleryConfig {
986985
pub(crate) txn_id: Option<OwnedTransactionId>,
987986
pub(crate) items: Vec<GalleryItemInfo>,
988-
pub(crate) caption: Option<String>,
989-
pub(crate) formatted_caption: Option<FormattedBody>,
987+
pub(crate) caption: Option<TextMessageEventContent>,
990988
pub(crate) mentions: Option<Mentions>,
991989
pub(crate) in_reply_to: Option<OwnedEventId>,
992990
}
@@ -1027,21 +1025,11 @@ impl GalleryConfig {
10271025
/// # Arguments
10281026
///
10291027
/// * `caption` - The optional caption.
1030-
pub fn caption(mut self, caption: Option<String>) -> Self {
1028+
pub fn caption(mut self, caption: Option<TextMessageEventContent>) -> Self {
10311029
self.caption = caption;
10321030
self
10331031
}
10341032

1035-
/// Set the optional formatted caption.
1036-
///
1037-
/// # Arguments
1038-
///
1039-
/// * `formatted_caption` - The optional formatted caption.
1040-
pub fn formatted_caption(mut self, formatted_caption: Option<FormattedBody>) -> Self {
1041-
self.formatted_caption = formatted_caption;
1042-
self
1043-
}
1044-
10451033
/// Set the mentions of the message.
10461034
///
10471035
/// # Arguments
@@ -1084,9 +1072,7 @@ pub struct GalleryItemInfo {
10841072
/// The attachment info.
10851073
pub attachment_info: AttachmentInfo,
10861074
/// The caption.
1087-
pub caption: Option<String>,
1088-
/// The formatted caption.
1089-
pub formatted_caption: Option<FormattedBody>,
1075+
pub caption: Option<TextMessageEventContent>,
10901076
/// The thumbnail.
10911077
pub thumbnail: Option<Thumbnail>,
10921078
}
@@ -1103,7 +1089,6 @@ impl TryFrom<GalleryItemInfo> for matrix_sdk::attachment::GalleryItemInfo {
11031089
data,
11041090
attachment_info: value.attachment_info,
11051091
caption: value.caption,
1106-
formatted_caption: value.formatted_caption,
11071092
thumbnail: value.thumbnail,
11081093
})
11091094
}

crates/matrix-sdk-ui/tests/integration/timeline/media.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ use ruma::events::room::message::GalleryItemType;
3737
use ruma::owned_mxc_uri;
3838
use ruma::{
3939
event_id,
40-
events::room::{MediaSource, message::MessageType},
40+
events::room::{
41+
MediaSource,
42+
message::{MessageType, TextMessageEventContent},
43+
},
4144
room_id,
4245
};
4346
use serde_json::json;
@@ -119,7 +122,10 @@ async fn test_send_attachment_from_file() -> TestResult {
119122
.with_focus(TimelineFocus::Thread { root_event_id: event_id.to_owned() })
120123
.build()
121124
.await?;
122-
let config = AttachmentConfig { caption: Some("caption".to_owned()), ..Default::default() };
125+
let config = AttachmentConfig {
126+
caption: Some(TextMessageEventContent::plain("caption")),
127+
..Default::default()
128+
};
123129
thread_timeline.send_attachment(&file_path, mime::TEXT_PLAIN, config).use_send_queue().await?;
124130

125131
{
@@ -256,7 +262,10 @@ async fn test_send_attachment_from_bytes() -> TestResult {
256262
mock.mock_room_send().ok(event_id!("$media")).mock_once().mount().await;
257263

258264
// Queue sending of an attachment.
259-
let config = AttachmentConfig { caption: Some("caption".to_owned()), ..Default::default() };
265+
let config = AttachmentConfig {
266+
caption: Some(TextMessageEventContent::plain("caption")),
267+
..Default::default()
268+
};
260269
timeline.send_attachment(source, mime::TEXT_PLAIN, config).use_send_queue().await?;
261270

262271
{
@@ -392,13 +401,13 @@ async fn test_send_gallery_from_bytes() -> TestResult {
392401
mock.mock_room_send().ok(event_id!("$media")).mock_once().mount().await;
393402

394403
// Queue sending of a gallery.
395-
let gallery =
396-
GalleryConfig::new().caption(Some("caption".to_owned())).add_item(GalleryItemInfo {
404+
let gallery = GalleryConfig::new()
405+
.caption(Some(TextMessageEventContent::plain("caption")))
406+
.add_item(GalleryItemInfo {
397407
source: AttachmentSource::Data { bytes: data, filename: filename.to_owned() },
398408
content_type: mime::TEXT_PLAIN,
399409
attachment_info: AttachmentInfo::File(BaseFileInfo { size: None }),
400-
caption: Some("item caption".to_owned()),
401-
formatted_caption: None,
410+
caption: Some(TextMessageEventContent::plain("item caption")),
402411
thumbnail: None,
403412
});
404413
timeline.send_gallery(gallery).await?;

crates/matrix-sdk/src/attachment.rs

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use ruma::{
2222
Mentions,
2323
room::{
2424
ImageInfo, ThumbnailInfo,
25-
message::{AudioInfo, FileInfo, FormattedBody, VideoInfo},
25+
message::{AudioInfo, FileInfo, TextMessageEventContent, VideoInfo},
2626
},
2727
},
2828
};
@@ -195,10 +195,7 @@ pub struct AttachmentConfig {
195195
pub thumbnail: Option<Thumbnail>,
196196

197197
/// An optional caption for the attachment.
198-
pub caption: Option<String>,
199-
200-
/// An optional formatted caption for the attachment.
201-
pub formatted_caption: Option<FormattedBody>,
198+
pub caption: Option<TextMessageEventContent>,
202199

203200
/// Intentional mentions to be included in the media event.
204201
pub mentions: Option<Mentions>,
@@ -256,21 +253,11 @@ impl AttachmentConfig {
256253
/// # Arguments
257254
///
258255
/// * `caption` - The optional caption.
259-
pub fn caption(mut self, caption: Option<String>) -> Self {
256+
pub fn caption(mut self, caption: Option<TextMessageEventContent>) -> Self {
260257
self.caption = caption;
261258
self
262259
}
263260

264-
/// Set the optional formatted caption.
265-
///
266-
/// # Arguments
267-
///
268-
/// * `formatted_caption` - The optional formatted caption.
269-
pub fn formatted_caption(mut self, formatted_caption: Option<FormattedBody>) -> Self {
270-
self.formatted_caption = formatted_caption;
271-
self
272-
}
273-
274261
/// Set the mentions of the message.
275262
///
276263
/// # Arguments
@@ -298,8 +285,7 @@ impl AttachmentConfig {
298285
pub struct GalleryConfig {
299286
pub(crate) txn_id: Option<OwnedTransactionId>,
300287
pub(crate) items: Vec<GalleryItemInfo>,
301-
pub(crate) caption: Option<String>,
302-
pub(crate) formatted_caption: Option<FormattedBody>,
288+
pub(crate) caption: Option<TextMessageEventContent>,
303289
pub(crate) mentions: Option<Mentions>,
304290
pub(crate) reply: Option<Reply>,
305291
}
@@ -340,21 +326,11 @@ impl GalleryConfig {
340326
/// # Arguments
341327
///
342328
/// * `caption` - The optional caption.
343-
pub fn caption(mut self, caption: Option<String>) -> Self {
329+
pub fn caption(mut self, caption: Option<TextMessageEventContent>) -> Self {
344330
self.caption = caption;
345331
self
346332
}
347333

348-
/// Set the optional formatted caption.
349-
///
350-
/// # Arguments
351-
///
352-
/// * `formatted_caption` - The optional formatted caption.
353-
pub fn formatted_caption(mut self, formatted_caption: Option<FormattedBody>) -> Self {
354-
self.formatted_caption = formatted_caption;
355-
self
356-
}
357-
358334
/// Set the mentions of the message.
359335
///
360336
/// # Arguments
@@ -399,9 +375,7 @@ pub struct GalleryItemInfo {
399375
/// The attachment info.
400376
pub attachment_info: AttachmentInfo,
401377
/// The caption.
402-
pub caption: Option<String>,
403-
/// The formatted caption.
404-
pub formatted_caption: Option<FormattedBody>,
378+
pub caption: Option<TextMessageEventContent>,
405379
/// The thumbnail.
406380
pub thumbnail: Option<Thumbnail>,
407381
}

0 commit comments

Comments
 (0)