Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 6 additions & 0 deletions crates/matrix-sdk-base/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ All notable changes to this project will be documented in this file.
- [**breaking**] `BaseClient::set_session_metadata` is renamed
`activate`, and `BaseClient::logged_in` is renamed `is_activated`
([#4850](https://github.com/matrix-org/matrix-rust-sdk/pull/4850))
- [**breaking] `DependentQueuedRequestKind::UploadFileWithThumbnail`
was renamed to `DependentQueuedRequestKind::UploadFileOrThumbnail`.
Under the `unstable-msc4274` feature, `DependentQueuedRequestKind::UploadFileOrThumbnail`
and `SentMediaInfo` were generalized to allow chaining multiple dependent
file / thumbnail uploads.
([#4897](https://github.com/matrix-org/matrix-rust-sdk/pull/4897))

## [0.10.0] - 2025-02-04

Expand Down
3 changes: 3 additions & 0 deletions crates/matrix-sdk-base/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ testing = [
"matrix-sdk-crypto?/testing",
]

# Add support for inline media galleries via msgtypes
unstable-msc4274 = []
Comment thread
Johennes marked this conversation as resolved.

[dependencies]
as_variant = { workspace = true }
assert_matches = { workspace = true, optional = true }
Expand Down
2 changes: 2 additions & 0 deletions crates/matrix-sdk-base/src/store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ mod send_queue;

#[cfg(any(test, feature = "testing"))]
pub use self::integration_tests::StateStoreIntegrationTests;
#[cfg(feature = "unstable-msc4274")]
pub use self::send_queue::AccumulatedSentMediaInfo;
pub use self::{
memory_store::MemoryStore,
send_queue::{
Expand Down
48 changes: 41 additions & 7 deletions crates/matrix-sdk-base/src/store/send_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ pub enum QueuedRequestKind {

/// To which media event transaction does this upload relate?
related_to: OwnedTransactionId,

/// Accumulated list of infos for previously uploaded files and
/// thumbnails if used during a gallery transaction. Otherwise empty.
#[cfg(feature = "unstable-msc4274")]
#[serde(default)]
accumulated: Vec<AccumulatedSentMediaInfo>,
},
}

Expand Down Expand Up @@ -219,17 +225,22 @@ pub enum DependentQueuedRequestKind {
key: String,
},

/// Upload a file that had a thumbnail.
UploadFileWithThumbnail {
/// Content type for the file itself (not the thumbnail).
/// Upload a file or thumbnail depending on another file or thumbnail
/// upload.
UploadFileOrThumbnail {
Comment thread
Johennes marked this conversation as resolved.
/// Content type for the file or thumbnail.
content_type: String,

/// Media request necessary to retrieve the file itself (not the
/// thumbnail).
/// Media request necessary to retrieve the file or thumbnail itself.
cache_key: MediaRequestParameters,

/// To which media transaction id does this upload relate to?
related_to: OwnedTransactionId,

/// Whether the depended upon request was a thumbnail or a file upload.
#[cfg(feature = "unstable-msc4274")]
#[serde(default)]
depends_on_thumbnail: bool,
},

/// Finish an upload by updating references to the media cache and sending
Expand Down Expand Up @@ -310,7 +321,7 @@ impl From<OwnedTransactionId> for ChildTransactionId {
}
}

/// Information about a media (and its thumbnail) that have been sent to an
/// Information about a media (and its thumbnail) that have been sent to a
/// homeserver.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct SentMediaInfo {
Expand All @@ -324,6 +335,29 @@ pub struct SentMediaInfo {
///
/// When uploading a thumbnail, this is set to `None`.
pub thumbnail: Option<MediaSource>,

/// Accumulated list of infos for previously uploaded files and thumbnails
/// if used during a gallery transaction. Otherwise empty.
#[cfg(feature = "unstable-msc4274")]
#[serde(default)]
pub accumulated: Vec<AccumulatedSentMediaInfo>,
Comment on lines +348 to +352
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There might be a different possible data scheme here, if I understand correctly:

  • SentMediaInfo contains only the vec of AccumulatedSentMediaInfo (since a vec can hold the pair for the media, when there's only a single one and no gallery)
  • AccumulatedSentMediaInfo (maybe rename SingleSentMediaInfo?) can keep on holding its current fields.

Again, I see this struct SentMediaInfo is also marked as Serialized/Deserialize, so maybe what I'm suggesting implies having to a data migration 🥴.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think technically this would work, yes. I didn't go for it originally because I wanted to minimize breaking changes and because it felt slightly cleaner to have accumulated only contain the requests that are actually finished. This way, we only push to accumulated. If we do it the other way around, we'll need to push or modify the last item depending on what the request is for. Did you have specific advantages of using this variant in mind?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only advantage is technical, in that it avoids containing what's effectively another flattened AccumulatedSentMediaInfo next to the accumulated vector, and also reduces the number of concepts.

I'm a bit torn, because this is likely a non-trivial change, but on the other hand that might be a nice simplification. Maybe that could be attempted as a follow-up, and we could open an issue to not forget about it?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I see the oddness. Sounds good on the issue. I have opened: #4969

}

/// Accumulated information about a media (and its thumbnail) that have been
/// sent to a homeserver.
#[cfg(feature = "unstable-msc4274")]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct AccumulatedSentMediaInfo {
/// File that was uploaded by this request.
///
/// If the request related to a thumbnail upload, this contains the
/// thumbnail media source.
pub file: MediaSource,

/// Optional thumbnail previously uploaded, when uploading a file.
///
/// When uploading a thumbnail, this is set to `None`.
pub thumbnail: Option<MediaSource>,
Comment thread
bnjbvr marked this conversation as resolved.
}

/// A unique key (identifier) indicating that a transaction has been
Expand Down Expand Up @@ -390,7 +424,7 @@ impl DependentQueuedRequest {
DependentQueuedRequestKind::EditEvent { .. }
| DependentQueuedRequestKind::RedactEvent
| DependentQueuedRequestKind::ReactEvent { .. }
| DependentQueuedRequestKind::UploadFileWithThumbnail { .. } => {
| DependentQueuedRequestKind::UploadFileOrThumbnail { .. } => {
// These are all aggregated events, or non-visible items (file upload producing
// a new MXC ID).
false
Expand Down
5 changes: 5 additions & 0 deletions crates/matrix-sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,11 @@ simpler methods:
([#4879](https://github.com/matrix-org/matrix-rust-sdk/pull/4879))
- `Oidc::issuer()` was removed.
- The `issuer` field of `UserSession` was removed.
- [**breaking] `QueueStorage::handle_dependent_file_upload_with_thumbnail`
was renamed to `handle_dependent_file_or_thumbnail_upload`. Under the
`unstable-msc4274` feature, it was generalized to allow chaining multiple
dependent file / thumbnail uploads.
([#4897](https://github.com/matrix-org/matrix-rust-sdk/pull/4897))
Comment thread
Johennes marked this conversation as resolved.
Outdated

## [0.10.0] - 2025-02-04

Expand Down
3 changes: 3 additions & 0 deletions crates/matrix-sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ experimental-widgets = ["dep:uuid"]

docsrs = ["e2e-encryption", "sqlite", "indexeddb", "sso-login", "qrcode"]

# Add support for inline media galleries via msgtypes
unstable-msc4274 = ["matrix-sdk-base/unstable-msc4274"]

[dependencies]
anyhow = { workspace = true, optional = true }
anymap2 = "0.13.0"
Expand Down
30 changes: 23 additions & 7 deletions crates/matrix-sdk/src/send_queue/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@
//! - the thumbnail is sent first as an [`QueuedRequestKind::MediaUpload`]
//! request,
//! - the file upload is pushed as a dependent request of kind
//! [`DependentQueuedRequestKind::UploadFileWithThumbnail`] (this variant
//! keeps the file's key used to look it up in the cache store).
//! [`DependentQueuedRequestKind::UploadFileOrThumbnail`] (this variant keeps
//! the file's key used to look it up in the cache store).
//! - the media event is then sent as a dependent request as described in the
//! previous section.
//!
Expand Down Expand Up @@ -699,6 +699,8 @@ impl RoomSendQueue {
cache_key,
thumbnail_source,
related_to: relates_to,
#[cfg(feature = "unstable-msc4274")]
accumulated,
} => {
trace!(%relates_to, "uploading media related to event");

Expand Down Expand Up @@ -757,6 +759,8 @@ impl RoomSendQueue {
Ok(SentRequestKey::Media(SentMediaInfo {
file: media_source,
thumbnail: thumbnail_source,
#[cfg(feature = "unstable-msc4274")]
accumulated,
}))
};

Expand Down Expand Up @@ -1215,6 +1219,8 @@ impl QueueStorage {
cache_key: thumbnail_media_request,
thumbnail_source: None, // the thumbnail has no thumbnails :)
related_to: send_event_txn.clone(),
#[cfg(feature = "unstable-msc4274")]
accumulated: vec![],
},
Self::LOW_PRIORITY,
)
Expand All @@ -1227,10 +1233,12 @@ impl QueueStorage {
&upload_thumbnail_txn,
upload_file_txn.clone().into(),
created_at,
DependentQueuedRequestKind::UploadFileWithThumbnail {
DependentQueuedRequestKind::UploadFileOrThumbnail {
content_type: content_type.to_string(),
cache_key: file_media_request,
related_to: send_event_txn.clone(),
#[cfg(feature = "unstable-msc4274")]
depends_on_thumbnail: true,
},
)
.await?;
Expand All @@ -1248,6 +1256,8 @@ impl QueueStorage {
cache_key: file_media_request,
thumbnail_source: None,
related_to: send_event_txn.clone(),
#[cfg(feature = "unstable-msc4274")]
accumulated: vec![],
},
Self::LOW_PRIORITY,
)
Expand Down Expand Up @@ -1376,7 +1386,7 @@ impl QueueStorage {
},
}),

DependentQueuedRequestKind::UploadFileWithThumbnail { .. } => {
DependentQueuedRequestKind::UploadFileOrThumbnail { .. } => {
// Don't reflect these: only the associated event is interesting to observers.
None
}
Expand Down Expand Up @@ -1589,22 +1599,28 @@ impl QueueStorage {
}
}

DependentQueuedRequestKind::UploadFileWithThumbnail {
DependentQueuedRequestKind::UploadFileOrThumbnail {
content_type,
cache_key,
related_to,
#[cfg(feature = "unstable-msc4274")]
depends_on_thumbnail,
} => {
let Some(parent_key) = parent_key else {
// Not finished yet, we should retry later => false.
return Ok(false);
};
self.handle_dependent_file_upload_with_thumbnail(
self.handle_dependent_file_or_thumbnail_upload(
client,
dependent_request.own_transaction_id.into(),
parent_key,
content_type,
cache_key,
related_to,
#[cfg(feature = "unstable-msc4274")]
depends_on_thumbnail,
#[cfg(not(feature = "unstable-msc4274"))]
true,
Comment thread
Johennes marked this conversation as resolved.
Outdated
)
.await?;
}
Expand Down Expand Up @@ -2209,7 +2225,7 @@ fn canonicalize_dependent_requests(
}
}

DependentQueuedRequestKind::UploadFileWithThumbnail { .. }
DependentQueuedRequestKind::UploadFileOrThumbnail { .. }
| DependentQueuedRequestKind::FinishUpload { .. }
| DependentQueuedRequestKind::ReactEvent { .. } => {
// These requests can't be canonicalized, push them as is.
Expand Down
44 changes: 34 additions & 10 deletions crates/matrix-sdk/src/send_queue/upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

//! Private implementations of the media upload mechanism.

#[cfg(feature = "unstable-msc4274")]
use matrix_sdk_base::store::AccumulatedSentMediaInfo;
use matrix_sdk_base::{
event_cache::store::media::IgnoreMediaRetentionPolicy,
media::{MediaFormat, MediaRequestParameters},
Expand Down Expand Up @@ -348,36 +350,58 @@ impl QueueStorage {
}

/// Consumes a finished upload of a thumbnail and queues the file upload.
pub(super) async fn handle_dependent_file_upload_with_thumbnail(
#[allow(clippy::too_many_arguments)]
pub(super) async fn handle_dependent_file_or_thumbnail_upload(
Comment thread
Johennes marked this conversation as resolved.
&self,
client: &Client,
next_upload_txn: OwnedTransactionId,
parent_key: SentRequestKey,
content_type: String,
cache_key: MediaRequestParameters,
event_txn: OwnedTransactionId,
depends_on_thumbnail: bool,
) -> Result<(), RoomSendQueueError> {
// The thumbnail has been sent, now transform the dependent file upload request
// into a ready one.
// The previous file or thumbnail has been sent, now transform the dependent
// file or thumbnail upload request into a ready one.
let sent_media = parent_key
.into_media()
.ok_or(RoomSendQueueError::StorageError(RoomSendQueueStorageError::InvalidParentKey))?;

// The media we just uploaded was a thumbnail, so the thumbnail shouldn't have
// If the previous upload was a thumbnail, it shouldn't have
// a thumbnail itself.
debug_assert!(sent_media.thumbnail.is_none());
if sent_media.thumbnail.is_some() {
warn!("unexpected thumbnail for a thumbnail!");
if depends_on_thumbnail {
debug_assert!(sent_media.thumbnail.is_none());
if sent_media.thumbnail.is_some() {
warn!("unexpected thumbnail for a thumbnail!");
Comment thread
bnjbvr marked this conversation as resolved.
}
}

trace!(related_to = %event_txn, "done uploading thumbnail, now queuing a request to send the media file itself");
trace!(related_to = %event_txn, "done uploading file or thumbnail, now queuing the dependent file or thumbnail upload request");

// If the previous upload was a thumbnail, forward the accumulated sent media
// unchangend. Otherwise, append the sent media to the accumulated
Comment thread
Johennes marked this conversation as resolved.
Outdated
// vector.
#[cfg(feature = "unstable-msc4274")]
let accumulated = if depends_on_thumbnail {
sent_media.accumulated
} else {
let mut accumulated = sent_media.accumulated.clone();
Comment thread
Johennes marked this conversation as resolved.
Outdated
accumulated.push(AccumulatedSentMediaInfo {
file: sent_media.file.clone(),
thumbnail: sent_media.thumbnail,
});
accumulated
};

let request = QueuedRequestKind::MediaUpload {
content_type,
cache_key,
// The thumbnail for the next upload is the file we just uploaded here.
thumbnail_source: Some(sent_media.file),
// If the previous upload was a thumbnail, it becomse the thumbnail source for the next
Comment thread
Johennes marked this conversation as resolved.
Outdated
// upload.
thumbnail_source: if depends_on_thumbnail { Some(sent_media.file) } else { None },
related_to: event_txn,
#[cfg(feature = "unstable-msc4274")]
accumulated,
};

client
Expand Down
Loading