Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
0c3161b
feat(send_queue): Implement sending of MSC4274 galleries
Johennes Apr 25, 2025
d749ebe
Merge branch 'main' into johannes/msc4274-step2
Johennes May 23, 2025
9a62767
Use an OwnedTransactionId to avoid cloning
Johennes May 23, 2025
58626d2
Add missing trailing periods in comments
Johennes May 23, 2025
932dbf9
Move client and cache store outside of loop
Johennes May 23, 2025
c6731ca
Prevent sending empty galleries
Johennes May 23, 2025
47fe934
Remove superfluous type annotations
Johennes May 23, 2025
49c9576
Pre-allocate capacity for item vectors
Johennes May 23, 2025
421f5bd
Extract macro for MessageType / GalleryItemType
Johennes May 23, 2025
b5f4828
Remove superfluous self
Johennes May 23, 2025
7149b73
Rename make_message_event to make_media_event
Johennes May 23, 2025
0e58e50
Bundle items with GalleryConfig
Johennes May 23, 2025
fc4aa7e
Avoid type annotation and pre-allocate vector
Johennes May 23, 2025
e47706e
Avoid unnecessary type annotation
Johennes May 23, 2025
4028bb4
Use let / else to reduce indentation
Johennes May 23, 2025
3acc0e8
Relocate and rephrase comment for FinishGalleryItemInfo
Johennes May 23, 2025
7e7a38c
Extract shared function to update cache keys
Johennes May 23, 2025
aaa392c
Extract method to push thumbnail & media requests
Johennes May 26, 2025
26601a4
Move pushing the dependent media request out of the if / else
Johennes May 26, 2025
b5b6723
Avoid unwrapping last_upload_file_txn
Johennes May 26, 2025
9a491b6
Extract method for caching
Johennes May 26, 2025
6b2b3f0
Merge branch 'main' into johannes/msc4274-step2
Johennes May 26, 2025
946e05a
Rename config parameter to gallery
Johennes May 27, 2025
662d6c7
Remove superfluous blank line
Johennes May 27, 2025
f235e56
Reduce indentation by returning early
Johennes May 27, 2025
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
192 changes: 84 additions & 108 deletions crates/matrix-sdk/src/send_queue/upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ use mime::Mime;
use ruma::events::room::message::{GalleryItemType, GalleryMessageEventContent};
use ruma::{
events::{
room::message::{FormattedBody, MessageType, RoomMessageEventContent},
room::{
message::{FormattedBody, MessageType, RoomMessageEventContent},
MediaSource, ThumbnailInfo,
},
AnyMessageLikeEventContent, Mentions,
},
MilliSecondsSinceUnixEpoch, OwnedTransactionId, TransactionId,
Expand All @@ -45,7 +48,7 @@ use tracing::{debug, error, instrument, trace, warn, Span};

use super::{QueueStorage, RoomSendQueue, RoomSendQueueError};
use crate::{
attachment::AttachmentConfig,
attachment::{AttachmentConfig, Thumbnail},
room::edit::update_media_caption,
send_queue::{
LocalEcho, LocalEchoContent, MediaHandles, RoomSendQueueStorageError, RoomSendQueueUpdate,
Expand Down Expand Up @@ -163,6 +166,14 @@ fn update_gallery_event_after_upload(
}
}

#[derive(Default)]

struct MediaCacheResult {
Comment thread
Johennes marked this conversation as resolved.
upload_thumbnail_txn: Option<OwnedTransactionId>,
event_thumbnail_info: Option<(MediaSource, Box<ThumbnailInfo>)>,
queue_thumbnail_info: Option<(FinishUploadThumbnailInfo, MediaRequestParameters, Mime)>,
}

impl RoomSendQueue {
/// Queues an attachment to be sent to the room, using the send queue.
///
Expand Down Expand Up @@ -208,59 +219,9 @@ impl RoomSendQueue {

let file_media_request = Media::make_local_file_media_request(&upload_file_txn);

let (upload_thumbnail_txn, event_thumbnail_info, queue_thumbnail_info) = {
let client = room.client();
let cache_store = client
.event_cache_store()
.lock()
.await
.map_err(RoomSendQueueStorageError::LockError)?;

// Cache the file itself in the cache store.
cache_store
.add_media_content(
&file_media_request,
data.clone(),
// Make sure that the file is stored until it has been uploaded.
IgnoreMediaRetentionPolicy::Yes,
)
.await
.map_err(RoomSendQueueStorageError::EventCacheStoreError)?;

// Process the thumbnail, if it's been provided.
if let Some(thumbnail) = config.thumbnail.take() {
let txn = TransactionId::new();
trace!(upload_thumbnail_txn = %txn, "attachment has a thumbnail");

// Create the information required for filling the thumbnail section of the
// media event.
let (data, content_type, thumbnail_info) = thumbnail.into_parts();

// Cache thumbnail in the cache store.
let thumbnail_media_request = Media::make_local_file_media_request(&txn);
cache_store
.add_media_content(
&thumbnail_media_request,
data,
// Make sure that the thumbnail is stored until it has been uploaded.
IgnoreMediaRetentionPolicy::Yes,
)
.await
.map_err(RoomSendQueueStorageError::EventCacheStoreError)?;

(
Some(txn.clone()),
Some((thumbnail_media_request.source.clone(), thumbnail_info)),
Some((
FinishUploadThumbnailInfo { txn, width: None, height: None },
thumbnail_media_request,
content_type,
)),
)
} else {
Default::default()
}
};
let MediaCacheResult { upload_thumbnail_txn, event_thumbnail_info, queue_thumbnail_info } =
RoomSendQueue::cache_media(&room, data, config.thumbnail.take(), &file_media_request)
.await?;

// Create the content for the media event.
let event_content = room
Expand Down Expand Up @@ -365,13 +326,6 @@ impl RoomSendQueue {
let mut item_queue_infos = Vec::with_capacity(config.len());
let mut media_handles = Vec::with_capacity(config.len());

let client = room.client();
let cache_store = client
.event_cache_store()
.lock()
.await
.map_err(RoomSendQueueStorageError::LockError)?;

for item_info in config.items {
let GalleryItemInfo { filename, content_type, data, .. } = item_info;

Expand All @@ -381,52 +335,12 @@ impl RoomSendQueue {

let file_media_request = Media::make_local_file_media_request(&upload_file_txn);

let (upload_thumbnail_txn, event_thumbnail_info, queue_thumbnail_info) = {
// Cache the file itself in the cache store.
cache_store
.add_media_content(
&file_media_request,
data.clone(),
// Make sure that the file is stored until it has been uploaded.
IgnoreMediaRetentionPolicy::Yes,
)
.await
.map_err(RoomSendQueueStorageError::EventCacheStoreError)?;

// Process the thumbnail, if it's been provided.
if let Some(thumbnail) = item_info.thumbnail {
let txn = TransactionId::new();
trace!(upload_thumbnail_txn = %txn, "gallery item has a thumbnail");

// Create the information required for filling the thumbnail section of the
// gallery event.
let (data, content_type, thumbnail_info) = thumbnail.into_parts();

// Cache thumbnail in the cache store.
let thumbnail_media_request = Media::make_local_file_media_request(&txn);
cache_store
.add_media_content(
&thumbnail_media_request,
data,
// Make sure that the thumbnail is stored until it has been uploaded.
IgnoreMediaRetentionPolicy::Yes,
)
.await
.map_err(RoomSendQueueStorageError::EventCacheStoreError)?;

(
Some(txn.clone()),
Some((thumbnail_media_request.source.clone(), thumbnail_info)),
Some((
FinishUploadThumbnailInfo { txn, width: None, height: None },
thumbnail_media_request,
content_type,
)),
)
} else {
Default::default()
}
};
let MediaCacheResult {
upload_thumbnail_txn,
event_thumbnail_info,
queue_thumbnail_info,
} = RoomSendQueue::cache_media(&room, data, item_info.thumbnail, &file_media_request)
.await?;

item_types.push(Room::make_gallery_item_type(
&content_type,
Expand Down Expand Up @@ -498,6 +412,68 @@ impl RoomSendQueue {

Ok(send_handle)
}

async fn cache_media(
room: &Room,
data: Vec<u8>,
thumbnail: Option<Thumbnail>,
file_media_request: &MediaRequestParameters,
) -> Result<MediaCacheResult, RoomSendQueueError> {
let client = room.client();
let cache_store = client
.event_cache_store()
.lock()
.await
.map_err(RoomSendQueueStorageError::LockError)?;

// Cache the file itself in the cache store.
cache_store
.add_media_content(
file_media_request,
data,
// Make sure that the file is stored until it has been uploaded.
IgnoreMediaRetentionPolicy::Yes,
)
.await
.map_err(RoomSendQueueStorageError::EventCacheStoreError)?;

// Process the thumbnail, if it's been provided.
if let Some(thumbnail) = thumbnail {
let txn = TransactionId::new();
trace!(upload_thumbnail_txn = %txn, "media has a thumbnail");

// Create the information required for filling the thumbnail section of the
// event.
let (data, content_type, thumbnail_info) = thumbnail.into_parts();

// Cache thumbnail in the cache store.
let thumbnail_media_request = Media::make_local_file_media_request(&txn);
cache_store
.add_media_content(
&thumbnail_media_request,
data,
// Make sure that the thumbnail is stored until it has been uploaded.
IgnoreMediaRetentionPolicy::Yes,
)
.await
.map_err(RoomSendQueueStorageError::EventCacheStoreError)?;

Ok(MediaCacheResult {
upload_thumbnail_txn: Some(txn.clone()),
event_thumbnail_info: Some((
thumbnail_media_request.source.clone(),
thumbnail_info,
)),
queue_thumbnail_info: Some((
FinishUploadThumbnailInfo { txn, width: None, height: None },
thumbnail_media_request,
content_type,
)),
})
} else {
Ok(Default::default())
}
}
}

impl QueueStorage {
Expand Down