Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(media): some preparatory refactorings #4140

Merged
merged 11 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 60 additions & 79 deletions bindings/matrix-sdk-ffi/src/timeline/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,36 +97,17 @@ impl Timeline {
unsafe { Arc::from_raw(Arc::into_raw(inner) as _) }
}

fn build_thumbnail_info(
&self,
thumbnail_url: String,
thumbnail_info: ThumbnailInfo,
) -> Result<Thumbnail, RoomError> {
let thumbnail_data =
fs::read(thumbnail_url).map_err(|_| RoomError::InvalidThumbnailData)?;

let base_thumbnail_info = BaseThumbnailInfo::try_from(&thumbnail_info)
.map_err(|_| RoomError::InvalidAttachmentData)?;

let mime_str =
thumbnail_info.mimetype.as_ref().ok_or(RoomError::InvalidAttachmentMimeType)?;
let mime_type =
mime_str.parse::<Mime>().map_err(|_| RoomError::InvalidAttachmentMimeType)?;

Ok(Thumbnail {
data: thumbnail_data,
content_type: mime_type,
info: Some(base_thumbnail_info),
})
}

async fn send_attachment(
&self,
filename: String,
mime_type: Mime,
mime_type: Option<String>,
attachment_config: AttachmentConfig,
progress_watcher: Option<Box<dyn ProgressWatcher>>,
) -> 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);
if let Some(progress_watcher) = progress_watcher {
let mut subscriber = request.subscribe_to_send_progress();
Expand All @@ -142,6 +123,41 @@ impl Timeline {
}
}

fn build_thumbnail_info(
thumbnail_url: Option<String>,
thumbnail_info: Option<ThumbnailInfo>,
) -> Result<AttachmentConfig, RoomError> {
match (thumbnail_url, thumbnail_info) {
(None, None) => Ok(AttachmentConfig::new()),

(Some(thumbnail_url), Some(thumbnail_info)) => {
let thumbnail_data =
fs::read(thumbnail_url).map_err(|_| RoomError::InvalidThumbnailData)?;

let base_thumbnail_info = BaseThumbnailInfo::try_from(&thumbnail_info)
.map_err(|_| RoomError::InvalidAttachmentData)?;

let mime_str =
thumbnail_info.mimetype.as_ref().ok_or(RoomError::InvalidAttachmentMimeType)?;
let mime_type =
mime_str.parse::<Mime>().map_err(|_| RoomError::InvalidAttachmentMimeType)?;

let thumbnail = Thumbnail {
data: thumbnail_data,
content_type: mime_type,
info: Some(base_thumbnail_info),
};

Ok(AttachmentConfig::with_thumbnail(thumbnail))
}

_ => {
warn!("Ignoring thumbnail because either the thumbnail URL or info isn't defined");
Ok(AttachmentConfig::new())
}
}
}

#[matrix_sdk_ffi_macros::export]
impl Timeline {
pub async fn add_listener(&self, listener: Box<dyn TimelineListener>) -> Arc<TaskHandle> {
Expand Down Expand Up @@ -263,28 +279,17 @@ impl Timeline {
progress_watcher: Option<Box<dyn ProgressWatcher>>,
) -> Arc<SendAttachmentJoinHandle> {
SendAttachmentJoinHandle::new(RUNTIME.spawn(async move {
let mime_str =
image_info.mimetype.as_ref().ok_or(RoomError::InvalidAttachmentMimeType)?;
let mime_type =
mime_str.parse::<Mime>().map_err(|_| RoomError::InvalidAttachmentMimeType)?;

let base_image_info = BaseImageInfo::try_from(&image_info)
.map_err(|_| RoomError::InvalidAttachmentData)?;

let attachment_info = AttachmentInfo::Image(base_image_info);

let attachment_config = match (thumbnail_url, image_info.thumbnail_info) {
(Some(thumbnail_url), Some(thumbnail_image_info)) => {
let thumbnail =
self.build_thumbnail_info(thumbnail_url, thumbnail_image_info)?;
AttachmentConfig::with_thumbnail(thumbnail).info(attachment_info)
}
_ => AttachmentConfig::new().info(attachment_info),
}
.caption(caption)
.formatted_caption(formatted_caption.map(Into::into));
let attachment_config = build_thumbnail_info(thumbnail_url, image_info.thumbnail_info)?
.info(attachment_info)
.caption(caption)
.formatted_caption(formatted_caption.map(Into::into));

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

Expand All @@ -298,28 +303,17 @@ impl Timeline {
progress_watcher: Option<Box<dyn ProgressWatcher>>,
) -> Arc<SendAttachmentJoinHandle> {
SendAttachmentJoinHandle::new(RUNTIME.spawn(async move {
let mime_str =
video_info.mimetype.as_ref().ok_or(RoomError::InvalidAttachmentMimeType)?;
let mime_type =
mime_str.parse::<Mime>().map_err(|_| RoomError::InvalidAttachmentMimeType)?;

let base_video_info: BaseVideoInfo = BaseVideoInfo::try_from(&video_info)
.map_err(|_| RoomError::InvalidAttachmentData)?;

let attachment_info = AttachmentInfo::Video(base_video_info);

let attachment_config = match (thumbnail_url, video_info.thumbnail_info) {
(Some(thumbnail_url), Some(thumbnail_image_info)) => {
let thumbnail =
self.build_thumbnail_info(thumbnail_url, thumbnail_image_info)?;
AttachmentConfig::with_thumbnail(thumbnail).info(attachment_info)
}
_ => AttachmentConfig::new().info(attachment_info),
}
.caption(caption)
.formatted_caption(formatted_caption.map(Into::into));
let attachment_config = build_thumbnail_info(thumbnail_url, video_info.thumbnail_info)?
.info(attachment_info)
.caption(caption)
.formatted_caption(formatted_caption.map(Into::into));

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

Expand All @@ -332,21 +326,17 @@ impl Timeline {
progress_watcher: Option<Box<dyn ProgressWatcher>>,
) -> Arc<SendAttachmentJoinHandle> {
SendAttachmentJoinHandle::new(RUNTIME.spawn(async move {
let mime_str =
audio_info.mimetype.as_ref().ok_or(RoomError::InvalidAttachmentMimeType)?;
let mime_type =
mime_str.parse::<Mime>().map_err(|_| RoomError::InvalidAttachmentMimeType)?;

let base_audio_info: BaseAudioInfo = BaseAudioInfo::try_from(&audio_info)
.map_err(|_| RoomError::InvalidAttachmentData)?;

let attachment_info = AttachmentInfo::Audio(base_audio_info);

let attachment_config = AttachmentConfig::new()
.info(attachment_info)
.caption(caption)
.formatted_caption(formatted_caption.map(Into::into));

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

Expand All @@ -360,22 +350,18 @@ impl Timeline {
progress_watcher: Option<Box<dyn ProgressWatcher>>,
) -> Arc<SendAttachmentJoinHandle> {
SendAttachmentJoinHandle::new(RUNTIME.spawn(async move {
let mime_str =
audio_info.mimetype.as_ref().ok_or(RoomError::InvalidAttachmentMimeType)?;
let mime_type =
mime_str.parse::<Mime>().map_err(|_| RoomError::InvalidAttachmentMimeType)?;

let base_audio_info: BaseAudioInfo = BaseAudioInfo::try_from(&audio_info)
.map_err(|_| RoomError::InvalidAttachmentData)?;

let attachment_info =
AttachmentInfo::Voice { audio_info: base_audio_info, waveform: Some(waveform) };

let attachment_config = AttachmentConfig::new()
.info(attachment_info)
.caption(caption)
.formatted_caption(formatted_caption.map(Into::into));

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

Expand All @@ -386,18 +372,13 @@ impl Timeline {
progress_watcher: Option<Box<dyn ProgressWatcher>>,
) -> Arc<SendAttachmentJoinHandle> {
SendAttachmentJoinHandle::new(RUNTIME.spawn(async move {
let mime_str =
file_info.mimetype.as_ref().ok_or(RoomError::InvalidAttachmentMimeType)?;
let mime_type =
mime_str.parse::<Mime>().map_err(|_| RoomError::InvalidAttachmentMimeType)?;

let base_file_info: BaseFileInfo =
BaseFileInfo::try_from(&file_info).map_err(|_| RoomError::InvalidAttachmentData)?;

let attachment_info = AttachmentInfo::File(base_file_info);

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

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

Expand Down
13 changes: 6 additions & 7 deletions crates/matrix-sdk-ui/src/timeline/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -573,22 +573,21 @@ impl Timeline {
Ok(())
}

/// Sends an attachment to the room. It does not currently support local
/// echoes
/// Sends an attachment to the room.
///
/// It does not currently support local echoes.
///
/// If the encryption feature is enabled, this method will transparently
/// encrypt the room message if the room is encrypted.
///
/// # Arguments
///
/// * `path` - The path of the file to be sent
/// * `path` - The path of the file to be sent.
///
/// * `mime_type` - The attachment's mime type
/// * `mime_type` - The attachment's mime type.
///
/// * `config` - An attachment configuration object containing details about
/// the attachment
///
/// like a thumbnail, its size, duration etc.
/// the attachment like a thumbnail, its size, duration etc.
#[instrument(skip_all)]
pub fn send_attachment(
&self,
Expand Down
8 changes: 4 additions & 4 deletions crates/matrix-sdk/src/encryption/futures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ use ruma::events::room::{EncryptedFile, EncryptedFileInit};

use crate::{Client, Result, TransmissionProgress};

/// Future returned by [`Client::prepare_encrypted_file`].
/// Future returned by [`Client::upload_encrypted_file`].
#[allow(missing_debug_implementations)]
pub struct PrepareEncryptedFile<'a, R: ?Sized> {
pub struct UploadEncryptedFile<'a, R: ?Sized> {
client: &'a Client,
content_type: &'a mime::Mime,
reader: &'a mut R,
send_progress: SharedObservable<TransmissionProgress>,
}

impl<'a, R: ?Sized> PrepareEncryptedFile<'a, R> {
impl<'a, R: ?Sized> UploadEncryptedFile<'a, R> {
pub(crate) fn new(client: &'a Client, content_type: &'a mime::Mime, reader: &'a mut R) -> Self {
Self { client, content_type, reader, send_progress: Default::default() }
}
Expand All @@ -63,7 +63,7 @@ impl<'a, R: ?Sized> PrepareEncryptedFile<'a, R> {
}
}

impl<'a, R> IntoFuture for PrepareEncryptedFile<'a, R>
impl<'a, R> IntoFuture for UploadEncryptedFile<'a, R>
where
R: Read + Send + ?Sized + 'a,
{
Expand Down
Loading
Loading