Skip to content

refactor(send_queue): generalize SentRequestKey::Media and DependentQueuedRequestKind::UploadFileWithThumbnail to prepare for MSC4274 gallery uploads #4897

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

Merged
merged 19 commits into from
Apr 24, 2025
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
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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 = []

[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
57 changes: 50 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,23 @@ 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.
#[serde(alias = "UploadFileWithThumbnail")]
UploadFileOrThumbnail {
/// 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 = "default_parent_is_thumbnail_upload")]
parent_is_thumbnail_upload: bool,
},

/// Finish an upload by updating references to the media cache and sending
Expand All @@ -248,6 +260,14 @@ pub enum DependentQueuedRequestKind {
},
}

/// If parent_is_thumbnail_upload is missing, we assume the request is for a
/// file upload following a thumbnail upload. This was the only possible case
/// before parent_is_thumbnail_upload was introduced.
#[cfg(feature = "unstable-msc4274")]
fn default_parent_is_thumbnail_upload() -> bool {
true
}

/// Detailed record about a thumbnail used when finishing a media upload.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct FinishUploadThumbnailInfo {
Expand Down Expand Up @@ -310,7 +330,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 +344,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
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
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
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
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>,
}

/// A unique key (identifier) indicating that a transaction has been
Expand Down Expand Up @@ -390,7 +433,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
1 change: 1 addition & 0 deletions crates/matrix-sdk-sqlite/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ event-cache = ["dep:matrix-sdk-base"]
state-store = ["dep:matrix-sdk-base"]

[dependencies]
as_variant = { workspace = true }
async-trait = { workspace = true }
deadpool-sqlite = "0.9.0"
itertools = { workspace = true }
Expand Down
46 changes: 43 additions & 3 deletions crates/matrix-sdk-sqlite/src/state_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2237,8 +2237,10 @@ mod migration_tests {
},
};

use as_variant::as_variant;
use deadpool_sqlite::Runtime;
use matrix_sdk_base::{
media::{MediaFormat, MediaRequestParameters},
store::{
ChildTransactionId, DependentQueuedRequestKind, RoomLoadSettings,
SerializableEventContent,
Expand All @@ -2250,13 +2252,14 @@ mod migration_tests {
use once_cell::sync::Lazy;
use ruma::{
events::{
room::{create::RoomCreateEventContent, message::RoomMessageEventContent},
room::{create::RoomCreateEventContent, message::RoomMessageEventContent, MediaSource},
StateEventType,
},
room_id, server_name, user_id, EventId, MilliSecondsSinceUnixEpoch, RoomId, TransactionId,
UserId,
room_id, server_name, user_id, EventId, MilliSecondsSinceUnixEpoch, OwnedTransactionId,
RoomId, TransactionId, UserId,
};
use rusqlite::Transaction;
use serde::{Deserialize, Serialize};
use serde_json::json;
use tempfile::{tempdir, TempDir};
use tokio::fs;
Expand Down Expand Up @@ -2597,4 +2600,41 @@ mod migration_tests {

Ok(())
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum LegacyDependentQueuedRequestKind {
UploadFileWithThumbnail {
content_type: String,
cache_key: MediaRequestParameters,
related_to: OwnedTransactionId,
},
}

#[async_test]
pub async fn test_dependent_queued_request_variant_renaming() {
let path = new_path();
let db = create_fake_db(&path, 7).await.unwrap();

let cache_key = MediaRequestParameters {
format: MediaFormat::File,
source: MediaSource::Plain("https://server.local/foobar".into()),
};
let related_to = TransactionId::new();
let request = LegacyDependentQueuedRequestKind::UploadFileWithThumbnail {
content_type: "image/png".to_owned(),
cache_key,
related_to: related_to.clone(),
};

let data = db
.serialize_json(&request)
.expect("should be able to serialize legacy dependent request");
let deserialized: DependentQueuedRequestKind = db.deserialize_json(&data).expect(
"should be able to deserialize dependent request from legacy dependent request",
);

as_variant!(deserialized, DependentQueuedRequestKind::UploadFileOrThumbnail { related_to: de_related_to, .. } => {
assert_eq!(de_related_to, related_to);
});
}
}
4 changes: 4 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 Expand Up @@ -112,6 +115,7 @@ urlencoding = "2.1.3"
uuid = { workspace = true, features = ["serde", "v4"], optional = true }
vodozemac = { workspace = true }
zeroize = { workspace = true }
cfg-if = "1.0.0"

[target.'cfg(target_arch = "wasm32")'.dependencies]
gloo-timers = { workspace = true, features = ["futures"] }
Expand Down
39 changes: 32 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")]
parent_is_thumbnail_upload: 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,37 @@ impl QueueStorage {
}
}

DependentQueuedRequestKind::UploadFileWithThumbnail {
DependentQueuedRequestKind::UploadFileOrThumbnail {
content_type,
cache_key,
related_to,
#[cfg(feature = "unstable-msc4274")]
parent_is_thumbnail_upload,
} => {
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(
let parent_is_thumbnail_upload = {
cfg_if::cfg_if! {
if #[cfg(feature = "unstable-msc4274")] {
parent_is_thumbnail_upload
} else {
// Before parent_is_thumbnail_upload was introduced, the only
// possible usage for this request was a file upload following
// a thumbnail upload.
true
}
}
};
self.handle_dependent_file_or_thumbnail_upload(
client,
dependent_request.own_transaction_id.into(),
parent_key,
content_type,
cache_key,
related_to,
parent_is_thumbnail_upload,
)
.await?;
}
Expand Down Expand Up @@ -2209,7 +2234,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
Loading
Loading