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

feat(send queue): send attachments with the send queue #4195

Merged
merged 12 commits into from
Nov 6, 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
11 changes: 7 additions & 4 deletions bindings/matrix-sdk-ffi/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ use std::{
use anyhow::{anyhow, Context as _};
use matrix_sdk::{
media::{
MediaFileHandle as SdkMediaFileHandle, MediaFormat, MediaRequest, MediaThumbnailSettings,
MediaFileHandle as SdkMediaFileHandle, MediaFormat, MediaRequestParameters,
MediaThumbnailSettings,
},
oidc::{
registrations::{ClientId, OidcRegistrations},
Expand Down Expand Up @@ -442,7 +443,7 @@ impl Client {
.inner
.media()
.get_media_file(
&MediaRequest { source, format: MediaFormat::File },
&MediaRequestParameters { source, format: MediaFormat::File },
filename,
&mime_type,
use_cache,
Expand Down Expand Up @@ -717,10 +718,11 @@ impl Client {
) -> Result<Vec<u8>, ClientError> {
let source = (*media_source).clone();

debug!(?source, "requesting media file");
Ok(self
.inner
.media()
.get_media_content(&MediaRequest { source, format: MediaFormat::File }, true)
.get_media_content(&MediaRequestParameters { source, format: MediaFormat::File }, true)
.await?)
}

Expand All @@ -732,11 +734,12 @@ impl Client {
) -> Result<Vec<u8>, ClientError> {
let source = (*media_source).clone();

debug!(source = ?media_source, width, height, "requesting media thumbnail");
Ok(self
.inner
.media()
.get_media_content(
&MediaRequest {
&MediaRequestParameters {
source,
format: MediaFormat::Thumbnail(MediaThumbnailSettings::new(
Method::Scale,
Expand Down
17 changes: 17 additions & 0 deletions bindings/matrix-sdk-ffi/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,12 @@ pub enum QueueWedgeError {
/// session before sending.
CrossVerificationRequired,

/// Some media content to be sent has disappeared from the cache.
MissingMediaContent,

/// Some mime type couldn't be parsed.
InvalidMimeType { mime_type: String },

/// Other errors.
GenericApiError { msg: String },
}
Expand All @@ -201,10 +207,17 @@ impl Display for QueueWedgeError {
QueueWedgeError::CrossVerificationRequired => {
f.write_str("Own verification is required")
}
QueueWedgeError::MissingMediaContent => {
f.write_str("Media to be sent disappeared from local storage")
}
QueueWedgeError::InvalidMimeType { mime_type } => {
write!(f, "Invalid mime type '{mime_type}' for media upload")
}
QueueWedgeError::GenericApiError { msg } => f.write_str(msg),
}
}
}

impl From<SdkQueueWedgeError> for QueueWedgeError {
fn from(value: SdkQueueWedgeError) -> Self {
match value {
Expand All @@ -223,6 +236,10 @@ impl From<SdkQueueWedgeError> for QueueWedgeError {
users: users.iter().map(ruma::OwnedUserId::to_string).collect(),
},
SdkQueueWedgeError::CrossVerificationRequired => Self::CrossVerificationRequired,
SdkQueueWedgeError::MissingMediaContent => Self::MissingMediaContent,
SdkQueueWedgeError::InvalidMimeType { mime_type } => {
Self::InvalidMimeType { mime_type }
}
SdkQueueWedgeError::GenericApiError { msg } => Self::GenericApiError { msg },
}
}
Expand Down
26 changes: 13 additions & 13 deletions bindings/matrix-sdk-ffi/src/timeline/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,17 @@ impl Timeline {
filename: String,
mime_type: Option<String>,
attachment_config: AttachmentConfig,
store_in_cache: bool,
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 mut request = self.inner.send_attachment(filename, mime_type, attachment_config);

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

if let Some(progress_watcher) = progress_watcher {
Expand Down Expand Up @@ -286,8 +286,8 @@ impl Timeline {
image_info: ImageInfo,
caption: Option<String>,
formatted_caption: Option<FormattedBody>,
store_in_cache: bool,
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 @@ -303,8 +303,8 @@ impl Timeline {
url,
image_info.mimetype,
attachment_config,
store_in_cache,
progress_watcher,
use_send_queue,
)
.await
}))
Expand All @@ -318,8 +318,8 @@ impl Timeline {
video_info: VideoInfo,
caption: Option<String>,
formatted_caption: Option<FormattedBody>,
store_in_cache: bool,
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 @@ -335,8 +335,8 @@ impl Timeline {
url,
video_info.mimetype,
attachment_config,
store_in_cache,
progress_watcher,
use_send_queue,
)
.await
}))
Expand All @@ -348,8 +348,8 @@ impl Timeline {
audio_info: AudioInfo,
caption: Option<String>,
formatted_caption: Option<FormattedBody>,
store_in_cache: bool,
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 +365,8 @@ impl Timeline {
url,
audio_info.mimetype,
attachment_config,
store_in_cache,
progress_watcher,
use_send_queue,
)
.await
}))
Expand All @@ -380,8 +380,8 @@ impl Timeline {
waveform: Vec<u16>,
caption: Option<String>,
formatted_caption: Option<FormattedBody>,
store_in_cache: bool,
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 @@ -398,8 +398,8 @@ impl Timeline {
url,
audio_info.mimetype,
attachment_config,
store_in_cache,
progress_watcher,
use_send_queue,
)
.await
}))
Expand All @@ -409,8 +409,8 @@ impl Timeline {
self: Arc<Self>,
url: String,
file_info: FileInfo,
store_in_cache: bool,
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 @@ -423,8 +423,8 @@ impl Timeline {
url,
file_info.mimetype,
attachment_config,
store_in_cache,
progress_watcher,
use_send_queue,
)
.await
}))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use ruma::{
};

use super::DynEventCacheStore;
use crate::media::{MediaFormat, MediaRequest, MediaThumbnailSettings};
use crate::media::{MediaFormat, MediaRequestParameters, MediaThumbnailSettings};

/// `EventCacheStore` integration tests.
///
Expand All @@ -41,9 +41,11 @@ pub trait EventCacheStoreIntegrationTests {
impl EventCacheStoreIntegrationTests for DynEventCacheStore {
async fn test_media_content(&self) {
let uri = mxc_uri!("mxc://localhost/media");
let request_file =
MediaRequest { source: MediaSource::Plain(uri.to_owned()), format: MediaFormat::File };
let request_thumbnail = MediaRequest {
let request_file = MediaRequestParameters {
source: MediaSource::Plain(uri.to_owned()),
format: MediaFormat::File,
};
let request_thumbnail = MediaRequestParameters {
source: MediaSource::Plain(uri.to_owned()),
format: MediaFormat::Thumbnail(MediaThumbnailSettings::new(
Method::Crop,
Expand All @@ -53,7 +55,7 @@ impl EventCacheStoreIntegrationTests for DynEventCacheStore {
};

let other_uri = mxc_uri!("mxc://localhost/media-other");
let request_other_file = MediaRequest {
let request_other_file = MediaRequestParameters {
source: MediaSource::Plain(other_uri.to_owned()),
format: MediaFormat::File,
};
Expand Down Expand Up @@ -145,8 +147,10 @@ impl EventCacheStoreIntegrationTests for DynEventCacheStore {

async fn test_replace_media_key(&self) {
let uri = mxc_uri!("mxc://sendqueue.local/tr4n-s4ct-10n1-d");
let req =
MediaRequest { source: MediaSource::Plain(uri.to_owned()), format: MediaFormat::File };
let req = MediaRequestParameters {
source: MediaSource::Plain(uri.to_owned()),
format: MediaFormat::File,
};

let content = "hello".as_bytes().to_owned();

Expand All @@ -161,7 +165,7 @@ impl EventCacheStoreIntegrationTests for DynEventCacheStore {

// Replacing a media request works.
let new_uri = mxc_uri!("mxc://matrix.org/tr4n-s4ct-10n1-d");
let new_req = MediaRequest {
let new_req = MediaRequestParameters {
source: MediaSource::Plain(new_uri.to_owned()),
format: MediaFormat::File,
};
Expand Down
16 changes: 10 additions & 6 deletions crates/matrix-sdk-base/src/event_cache_store/memory_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use matrix_sdk_common::{
use ruma::{MxcUri, OwnedMxcUri};

use super::{EventCacheStore, EventCacheStoreError, Result};
use crate::media::{MediaRequest, UniqueKey as _};
use crate::media::{MediaRequestParameters, UniqueKey as _};

/// In-memory, non-persistent implementation of the `EventCacheStore`.
///
Expand Down Expand Up @@ -66,7 +66,11 @@ impl EventCacheStore for MemoryStore {
Ok(try_take_leased_lock(&self.leases, lease_duration_ms, key, holder))
}

async fn add_media_content(&self, request: &MediaRequest, data: Vec<u8>) -> Result<()> {
async fn add_media_content(
&self,
request: &MediaRequestParameters,
data: Vec<u8>,
) -> Result<()> {
// Avoid duplication. Let's try to remove it first.
self.remove_media_content(request).await?;
// Now, let's add it.
Expand All @@ -77,8 +81,8 @@ impl EventCacheStore for MemoryStore {

async fn replace_media_key(
&self,
from: &MediaRequest,
to: &MediaRequest,
from: &MediaRequestParameters,
to: &MediaRequestParameters,
) -> Result<(), Self::Error> {
let expected_key = from.unique_key();

Expand All @@ -91,7 +95,7 @@ impl EventCacheStore for MemoryStore {
Ok(())
}

async fn get_media_content(&self, request: &MediaRequest) -> Result<Option<Vec<u8>>> {
async fn get_media_content(&self, request: &MediaRequestParameters) -> Result<Option<Vec<u8>>> {
let expected_key = request.unique_key();

let media = self.media.read().unwrap();
Expand All @@ -100,7 +104,7 @@ impl EventCacheStore for MemoryStore {
}))
}

async fn remove_media_content(&self, request: &MediaRequest) -> Result<()> {
async fn remove_media_content(&self, request: &MediaRequestParameters) -> Result<()> {
let expected_key = request.unique_key();

let mut media = self.media.write().unwrap();
Expand Down
Loading
Loading