Skip to content

Commit

Permalink
feat(ffi): generate formatted captions for send_* media fns
Browse files Browse the repository at this point in the history
Changelog: For `Timeline::send_*` fns, treat the passed `caption` parameter as markdown and use the HTML generated from it as the `formatted_caption` if there is none.
  • Loading branch information
jmartinesp committed Nov 14, 2024
1 parent aca83fb commit afaecdc
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions bindings/matrix-sdk-ffi/src/timeline/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ use ruma::{
},
receipt::ReceiptThread,
room::message::{
ForwardThread, LocationMessageEventContent, MessageType,
RoomMessageEventContentWithoutRelation,
FormattedBody as RumaFormattedBody, ForwardThread, LocationMessageEventContent,
MessageType, RoomMessageEventContentWithoutRelation,
},
AnyMessageLikeEventContent,
},
Expand Down Expand Up @@ -289,6 +289,7 @@ impl Timeline {
progress_watcher: Option<Box<dyn ProgressWatcher>>,
use_send_queue: bool,
) -> Arc<SendAttachmentJoinHandle> {
let formatted_caption = formatted_caption_from(&caption, &formatted_caption);
SendAttachmentJoinHandle::new(RUNTIME.spawn(async move {
let base_image_info = BaseImageInfo::try_from(&image_info)
.map_err(|_| RoomError::InvalidAttachmentData)?;
Expand All @@ -297,7 +298,7 @@ impl Timeline {
let attachment_config = build_thumbnail_info(thumbnail_url, image_info.thumbnail_info)?
.info(attachment_info)
.caption(caption)
.formatted_caption(formatted_caption.map(Into::into));
.formatted_caption(formatted_caption);

self.send_attachment(
url,
Expand All @@ -321,6 +322,7 @@ impl Timeline {
progress_watcher: Option<Box<dyn ProgressWatcher>>,
use_send_queue: bool,
) -> Arc<SendAttachmentJoinHandle> {
let formatted_caption = formatted_caption_from(&caption, &formatted_caption);
SendAttachmentJoinHandle::new(RUNTIME.spawn(async move {
let base_video_info: BaseVideoInfo = BaseVideoInfo::try_from(&video_info)
.map_err(|_| RoomError::InvalidAttachmentData)?;
Expand Down Expand Up @@ -351,6 +353,7 @@ impl Timeline {
progress_watcher: Option<Box<dyn ProgressWatcher>>,
use_send_queue: bool,
) -> Arc<SendAttachmentJoinHandle> {
let formatted_caption = formatted_caption_from(&caption, &formatted_caption);
SendAttachmentJoinHandle::new(RUNTIME.spawn(async move {
let base_audio_info: BaseAudioInfo = BaseAudioInfo::try_from(&audio_info)
.map_err(|_| RoomError::InvalidAttachmentData)?;
Expand Down Expand Up @@ -383,6 +386,7 @@ impl Timeline {
progress_watcher: Option<Box<dyn ProgressWatcher>>,
use_send_queue: bool,
) -> Arc<SendAttachmentJoinHandle> {
let formatted_caption = formatted_caption_from(&caption, &formatted_caption);
SendAttachmentJoinHandle::new(RUNTIME.spawn(async move {
let base_audio_info: BaseAudioInfo = BaseAudioInfo::try_from(&audio_info)
.map_err(|_| RoomError::InvalidAttachmentData)?;
Expand Down Expand Up @@ -710,6 +714,24 @@ impl Timeline {
}
}

/// Given a pair of optional `caption` and `formatted_caption` parameters,
/// return a formatted caption:
///
/// - If a `formatted_caption` exists, return it.
/// - If it doesn't exist but there is a `caption`, parse it as markdown and
/// return the result.
/// - Return `None` if there are no `caption` or `formatted_caption` parameters.
fn formatted_caption_from(
caption: &Option<String>,
formatted_caption: &Option<FormattedBody>,
) -> Option<RumaFormattedBody> {
match (&caption, formatted_caption) {
(None, None) => None,
(Some(body), None) => RumaFormattedBody::markdown(body),
(_, Some(formatted_body)) => Some(formatted_body.clone().into()),
}
}

/// A handle to perform actions onto a local echo.
#[derive(uniffi::Object)]
pub struct SendHandle {
Expand Down

0 comments on commit afaecdc

Please sign in to comment.