Skip to content

Commit

Permalink
feat(timeline): use the send queue for media uploads behind a feature…
Browse files Browse the repository at this point in the history
… toggle
  • Loading branch information
bnjbvr committed Nov 6, 2024
1 parent d7c8aa4 commit 13d5de4
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 14 deletions.
61 changes: 51 additions & 10 deletions bindings/matrix-sdk-ffi/src/timeline/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,17 @@ impl Timeline {
mime_type: Option<String>,
attachment_config: AttachmentConfig,
progress_watcher: Option<Box<dyn ProgressWatcher>>,
use_send_queue: bool,
) -> Result<(), RoomError> {
let mime_str = mime_type.as_ref().ok_or(RoomError::InvalidAttachmentMimeType)?;
let mime_type =
mime_str.parse::<Mime>().map_err(|_| RoomError::InvalidAttachmentMimeType)?;

let request = self.inner.send_attachment(filename, mime_type, attachment_config);
let mut request = self.inner.send_attachment(filename, mime_type, attachment_config);

if use_send_queue {
request = request.use_send_queue();
}

if let Some(progress_watcher) = progress_watcher {
let mut subscriber = request.subscribe_to_send_progress();
Expand Down Expand Up @@ -281,6 +286,7 @@ impl Timeline {
caption: Option<String>,
formatted_caption: Option<FormattedBody>,
progress_watcher: Option<Box<dyn ProgressWatcher>>,
use_send_queue: bool,
) -> Arc<SendAttachmentJoinHandle> {
SendAttachmentJoinHandle::new(RUNTIME.spawn(async move {
let base_image_info = BaseImageInfo::try_from(&image_info)
Expand All @@ -292,8 +298,14 @@ impl Timeline {
.caption(caption)
.formatted_caption(formatted_caption.map(Into::into));

self.send_attachment(url, image_info.mimetype, attachment_config, progress_watcher)
.await
self.send_attachment(
url,
image_info.mimetype,
attachment_config,
progress_watcher,
use_send_queue,
)
.await
}))
}

Expand All @@ -305,6 +317,7 @@ impl Timeline {
caption: Option<String>,
formatted_caption: Option<FormattedBody>,
progress_watcher: Option<Box<dyn ProgressWatcher>>,
use_send_queue: bool,
) -> Arc<SendAttachmentJoinHandle> {
SendAttachmentJoinHandle::new(RUNTIME.spawn(async move {
let base_video_info: BaseVideoInfo = BaseVideoInfo::try_from(&video_info)
Expand All @@ -316,8 +329,14 @@ impl Timeline {
.caption(caption)
.formatted_caption(formatted_caption.map(Into::into));

self.send_attachment(url, video_info.mimetype, attachment_config, progress_watcher)
.await
self.send_attachment(
url,
video_info.mimetype,
attachment_config,
progress_watcher,
use_send_queue,
)
.await
}))
}

Expand All @@ -328,6 +347,7 @@ impl Timeline {
caption: Option<String>,
formatted_caption: Option<FormattedBody>,
progress_watcher: Option<Box<dyn ProgressWatcher>>,
use_send_queue: bool,
) -> Arc<SendAttachmentJoinHandle> {
SendAttachmentJoinHandle::new(RUNTIME.spawn(async move {
let base_audio_info: BaseAudioInfo = BaseAudioInfo::try_from(&audio_info)
Expand All @@ -339,8 +359,14 @@ impl Timeline {
.caption(caption)
.formatted_caption(formatted_caption.map(Into::into));

self.send_attachment(url, audio_info.mimetype, attachment_config, progress_watcher)
.await
self.send_attachment(
url,
audio_info.mimetype,
attachment_config,
progress_watcher,
use_send_queue,
)
.await
}))
}

Expand All @@ -353,6 +379,7 @@ impl Timeline {
caption: Option<String>,
formatted_caption: Option<FormattedBody>,
progress_watcher: Option<Box<dyn ProgressWatcher>>,
use_send_queue: bool,
) -> Arc<SendAttachmentJoinHandle> {
SendAttachmentJoinHandle::new(RUNTIME.spawn(async move {
let base_audio_info: BaseAudioInfo = BaseAudioInfo::try_from(&audio_info)
Expand All @@ -365,8 +392,14 @@ impl Timeline {
.caption(caption)
.formatted_caption(formatted_caption.map(Into::into));

self.send_attachment(url, audio_info.mimetype, attachment_config, progress_watcher)
.await
self.send_attachment(
url,
audio_info.mimetype,
attachment_config,
progress_watcher,
use_send_queue,
)
.await
}))
}

Expand All @@ -375,6 +408,7 @@ impl Timeline {
url: String,
file_info: FileInfo,
progress_watcher: Option<Box<dyn ProgressWatcher>>,
use_send_queue: bool,
) -> Arc<SendAttachmentJoinHandle> {
SendAttachmentJoinHandle::new(RUNTIME.spawn(async move {
let base_file_info: BaseFileInfo =
Expand All @@ -383,7 +417,14 @@ impl Timeline {

let attachment_config = AttachmentConfig::new().info(attachment_info);

self.send_attachment(url, file_info.mimetype, attachment_config, progress_watcher).await
self.send_attachment(
url,
file_info.mimetype,
attachment_config,
progress_watcher,
use_send_queue,
)
.await
}))
}

Expand Down
32 changes: 28 additions & 4 deletions crates/matrix-sdk-ui/src/timeline/futures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub struct SendAttachment<'a> {
config: AttachmentConfig,
tracing_span: Span,
pub(crate) send_progress: SharedObservable<TransmissionProgress>,
use_send_queue: bool,
}

impl<'a> SendAttachment<'a> {
Expand All @@ -31,9 +32,22 @@ impl<'a> SendAttachment<'a> {
config,
tracing_span: Span::current(),
send_progress: Default::default(),
use_send_queue: false,
}
}

/// (Experimental) Uses the send queue to upload this media.
///
/// This uses the send queue to upload the medias, and as such it provides
/// local echoes for the uploaded media too, not blocking the sending
/// request.
///
/// This will be the default in future versions, when the feature work will
/// be done there.
pub fn use_send_queue(self) -> Self {
Self { use_send_queue: true, ..self }
}

/// Get a subscriber to observe the progress of sending the request
/// body.
#[cfg(not(target_arch = "wasm32"))]
Expand All @@ -47,7 +61,8 @@ impl<'a> IntoFuture for SendAttachment<'a> {
boxed_into_future!(extra_bounds: 'a);

fn into_future(self) -> Self::IntoFuture {
let Self { timeline, path, mime_type, config, tracing_span, send_progress: _ } = self;
let Self { timeline, path, mime_type, config, tracing_span, use_send_queue, send_progress } =
self;

let fut = async move {
let filename = path
Expand All @@ -57,9 +72,18 @@ impl<'a> IntoFuture for SendAttachment<'a> {
.ok_or(Error::InvalidAttachmentFileName)?;
let data = fs::read(&path).map_err(|_| Error::InvalidAttachmentData)?;

let send_queue = timeline.room().send_queue();
let fut = send_queue.send_attachment(filename, mime_type, data, config);
fut.await.map_err(|_| Error::FailedSendingAttachment)?;
if use_send_queue {
let send_queue = timeline.room().send_queue();
let fut = send_queue.send_attachment(filename, mime_type, data, config);
fut.await.map_err(|_| Error::FailedSendingAttachment)?;
} else {
let fut = timeline
.room()
.send_attachment(filename, &mime_type, data, config)
.with_send_progress_observable(send_progress)
.store_in_cache();
fut.await.map_err(|_| Error::FailedSendingAttachment)?;
}

Ok(())
};
Expand Down

0 comments on commit 13d5de4

Please sign in to comment.