Skip to content

Commit e33c40e

Browse files
committed
misc: Use filename for the send queue and gallery too
1 parent 80d5bb3 commit e33c40e

File tree

4 files changed

+106
-28
lines changed

4 files changed

+106
-28
lines changed

crates/matrix-sdk-base/src/store/send_queue.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ pub enum QueuedRequestKind {
109109
#[cfg(feature = "unstable-msc4274")]
110110
#[serde(default)]
111111
accumulated: Vec<AccumulatedSentMediaInfo>,
112+
113+
/// The name of the file to upload, if known or public.
114+
filename: Option<String>,
112115
},
113116
}
114117

@@ -241,6 +244,9 @@ pub enum DependentQueuedRequestKind {
241244
#[cfg(feature = "unstable-msc4274")]
242245
#[serde(default = "default_parent_is_thumbnail_upload")]
243246
parent_is_thumbnail_upload: bool,
247+
248+
/// The name of the file to upload, if known.
249+
filename: Option<String>,
244250
},
245251

246252
/// Finish an upload by updating references to the media cache and sending

crates/matrix-sdk/src/send_queue/mod.rs

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ use ruma::{
158158
reaction::ReactionEventContent,
159159
relation::Annotation,
160160
room::{
161-
message::{FormattedBody, RoomMessageEventContent},
161+
message::{FormattedBody, MessageType, RoomMessageEventContent},
162162
MediaSource,
163163
},
164164
AnyMessageLikeEventContent, EventContent as _, Mentions,
@@ -753,6 +753,7 @@ impl RoomSendQueue {
753753
related_to: relates_to,
754754
#[cfg(feature = "unstable-msc4274")]
755755
accumulated,
756+
filename,
756757
} => {
757758
trace!(%relates_to, "uploading media related to event");
758759

@@ -788,8 +789,11 @@ impl RoomSendQueue {
788789
trace!("upload will be in clear text (room without encryption)");
789790
let request_config = RequestConfig::short_retry()
790791
.timeout(Media::reasonable_upload_timeout(&data));
791-
let res =
792-
room.client().media().upload(&mime, data, Some(request_config)).await?;
792+
let res = room
793+
.client()
794+
.media()
795+
.upload(&mime, data, filename, Some(request_config))
796+
.await?;
793797
MediaSource::Plain(res.content_uri)
794798
};
795799

@@ -1279,10 +1283,25 @@ impl QueueStorage {
12791283
let client = guard.client()?;
12801284
let store = client.state_store();
12811285

1286+
let mut filename = match &event.msgtype {
1287+
MessageType::Image(msgtype) => msgtype.filename.clone(),
1288+
MessageType::Audio(msgtype) => msgtype.filename.clone(),
1289+
MessageType::Video(msgtype) => msgtype.filename.clone(),
1290+
MessageType::File(msgtype) => msgtype.filename.clone(),
1291+
_ => None,
1292+
};
1293+
1294+
// If there is no filename, use the `body` if it's not empty, since for media
1295+
// events with no captions it'll contain the filename
1296+
if filename.is_none() && !event.body().is_empty() {
1297+
filename = Some(event.body().to_owned());
1298+
}
1299+
12821300
let thumbnail_info = self
12831301
.push_thumbnail_and_media_uploads(
12841302
store,
12851303
&content_type,
1304+
filename,
12861305
send_event_txn.clone(),
12871306
created_at,
12881307
upload_file_txn.clone(),
@@ -1331,13 +1350,19 @@ impl QueueStorage {
13311350
return Ok(());
13321351
};
13331352

1334-
let GalleryItemQueueInfo { content_type, upload_file_txn, file_media_request, thumbnail } =
1335-
first;
1353+
let GalleryItemQueueInfo {
1354+
content_type,
1355+
upload_file_txn,
1356+
file_media_request,
1357+
filename,
1358+
thumbnail,
1359+
} = first;
13361360

13371361
let thumbnail_info = self
13381362
.push_thumbnail_and_media_uploads(
13391363
store,
13401364
content_type,
1365+
filename.clone(),
13411366
send_event_txn.clone(),
13421367
created_at,
13431368
upload_file_txn.clone(),
@@ -1356,6 +1381,7 @@ impl QueueStorage {
13561381
content_type,
13571382
upload_file_txn,
13581383
file_media_request,
1384+
filename,
13591385
thumbnail,
13601386
} = item_queue_info;
13611387

@@ -1378,6 +1404,7 @@ impl QueueStorage {
13781404
cache_key: thumbnail_media_request.clone(),
13791405
related_to: send_event_txn.clone(),
13801406
parent_is_thumbnail_upload: false,
1407+
filename: filename.clone().map(|name| format!("thumbnail-{name}")),
13811408
},
13821409
)
13831410
.await?;
@@ -1401,6 +1428,7 @@ impl QueueStorage {
14011428
cache_key: file_media_request.clone(),
14021429
related_to: send_event_txn.clone(),
14031430
parent_is_thumbnail_upload: thumbnail.is_some(),
1431+
filename: filename.clone(),
14041432
},
14051433
)
14061434
.await?;
@@ -1441,6 +1469,7 @@ impl QueueStorage {
14411469
&self,
14421470
store: &DynStateStore,
14431471
content_type: &Mime,
1472+
filename: Option<String>,
14441473
send_event_txn: OwnedTransactionId,
14451474
created_at: MilliSecondsSinceUnixEpoch,
14461475
upload_file_txn: OwnedTransactionId,
@@ -1463,6 +1492,7 @@ impl QueueStorage {
14631492
related_to: send_event_txn.clone(),
14641493
#[cfg(feature = "unstable-msc4274")]
14651494
accumulated: vec![],
1495+
filename: filename.clone().map(|name| format!("thumbnail-{name}")),
14661496
},
14671497
Self::LOW_PRIORITY,
14681498
)
@@ -1481,6 +1511,7 @@ impl QueueStorage {
14811511
related_to: send_event_txn,
14821512
#[cfg(feature = "unstable-msc4274")]
14831513
parent_is_thumbnail_upload: true,
1514+
filename,
14841515
},
14851516
)
14861517
.await?;
@@ -1500,6 +1531,7 @@ impl QueueStorage {
15001531
related_to: send_event_txn,
15011532
#[cfg(feature = "unstable-msc4274")]
15021533
accumulated: vec![],
1534+
filename,
15031535
},
15041536
Self::LOW_PRIORITY,
15051537
)
@@ -1873,7 +1905,9 @@ impl QueueStorage {
18731905
related_to,
18741906
#[cfg(feature = "unstable-msc4274")]
18751907
parent_is_thumbnail_upload,
1908+
filename,
18761909
} => {
1910+
warn!("Saving send queue request for filename {filename:?}");
18771911
let Some(parent_key) = parent_key else {
18781912
// Not finished yet, we should retry later => false.
18791913
return Ok(false);
@@ -1898,6 +1932,7 @@ impl QueueStorage {
18981932
cache_key,
18991933
related_to,
19001934
parent_is_thumbnail_upload,
1935+
filename,
19011936
)
19021937
.await?;
19031938
}
@@ -2039,6 +2074,7 @@ struct GalleryItemQueueInfo {
20392074
content_type: Mime,
20402075
upload_file_txn: OwnedTransactionId,
20412076
file_media_request: MediaRequestParameters,
2077+
filename: Option<String>,
20422078
thumbnail: Option<(FinishUploadThumbnailInfo, MediaRequestParameters, Mime)>,
20432079
}
20442080

crates/matrix-sdk/src/send_queue/upload.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ impl RoomSendQueue {
344344

345345
item_types.push(Room::make_gallery_item_type(
346346
&content_type,
347-
filename,
347+
filename.clone(),
348348
file_media_request.source.clone(),
349349
item_info.caption,
350350
item_info.formatted_caption,
@@ -356,6 +356,7 @@ impl RoomSendQueue {
356356
content_type,
357357
upload_file_txn: upload_file_txn.clone(),
358358
file_media_request,
359+
filename: Some(filename),
359360
thumbnail: queue_thumbnail_info,
360361
});
361362

@@ -609,6 +610,7 @@ impl QueueStorage {
609610
cache_key: MediaRequestParameters,
610611
event_txn: OwnedTransactionId,
611612
parent_is_thumbnail_upload: bool,
613+
filename: Option<String>,
612614
) -> Result<(), RoomSendQueueError> {
613615
// The previous file or thumbnail has been sent, now transform the dependent
614616
// file or thumbnail upload request into a ready one.
@@ -657,6 +659,7 @@ impl QueueStorage {
657659
related_to: event_txn,
658660
#[cfg(feature = "unstable-msc4274")]
659661
accumulated,
662+
filename,
660663
};
661664

662665
client

crates/matrix-sdk/tests/integration/send_queue.rs

Lines changed: 55 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,18 @@ async fn queue_attachment_with_thumbnail(q: &RoomSendQueue) -> (SendHandle, &'st
110110
fn mock_jpeg_upload<'a>(
111111
mock: &'a MatrixMockServer,
112112
mxc: &MxcUri,
113+
filename: Option<String>,
113114
lock: Arc<Mutex<()>>,
114115
) -> MatrixMock<'a> {
115116
let mxc = mxc.to_owned();
116-
mock.mock_upload().expect_mime_type("image/jpeg").respond_with(move |_req: &Request| {
117+
let mut mocked_upload = mock.mock_upload().expect_mime_type("image/jpeg");
118+
mocked_upload = if let Some(filename) = filename {
119+
mocked_upload.expect_filename(&filename)
120+
} else {
121+
mocked_upload
122+
};
123+
124+
mocked_upload.respond_with(move |_req: &Request| {
117125
// Wait for the signal from the main task that we can process this query.
118126
let mock_lock = lock.clone();
119127
std::thread::spawn(move || {
@@ -1903,14 +1911,19 @@ async fn test_media_uploads() {
19031911
let allow_upload_lock = Arc::new(Mutex::new(()));
19041912
let block_upload = allow_upload_lock.lock().await;
19051913

1906-
mock_jpeg_upload(&mock, mxc_uri!("mxc://sdk.rs/thumbnail"), allow_upload_lock.clone())
1907-
.mock_once()
1908-
.mount()
1909-
.await;
1910-
mock_jpeg_upload(&mock, mxc_uri!("mxc://sdk.rs/media"), allow_upload_lock.clone())
1914+
mock_jpeg_upload(&mock, mxc_uri!("mxc://sdk.rs/thumbnail"), None, allow_upload_lock.clone())
19111915
.mock_once()
19121916
.mount()
19131917
.await;
1918+
mock_jpeg_upload(
1919+
&mock,
1920+
mxc_uri!("mxc://sdk.rs/media"),
1921+
Some(filename.to_owned()),
1922+
allow_upload_lock.clone(),
1923+
)
1924+
.mock_once()
1925+
.mount()
1926+
.await;
19141927

19151928
// ----------------------
19161929
// Send the media.
@@ -2200,22 +2213,42 @@ async fn test_gallery_uploads() {
22002213
let allow_upload_lock = Arc::new(Mutex::new(()));
22012214
let block_upload = allow_upload_lock.lock().await;
22022215

2203-
mock_jpeg_upload(&mock, mxc_uri!("mxc://sdk.rs/thumbnail1"), allow_upload_lock.clone())
2204-
.mock_once()
2205-
.mount()
2206-
.await;
2207-
mock_jpeg_upload(&mock, mxc_uri!("mxc://sdk.rs/media1"), allow_upload_lock.clone())
2208-
.mock_once()
2209-
.mount()
2210-
.await;
2211-
mock_jpeg_upload(&mock, mxc_uri!("mxc://sdk.rs/thumbnail2"), allow_upload_lock.clone())
2212-
.mock_once()
2213-
.mount()
2214-
.await;
2215-
mock_jpeg_upload(&mock, mxc_uri!("mxc://sdk.rs/media2"), allow_upload_lock.clone())
2216-
.mock_once()
2217-
.mount()
2218-
.await;
2216+
mock_jpeg_upload(
2217+
&mock,
2218+
mxc_uri!("mxc://sdk.rs/thumbnail1"),
2219+
None,
2220+
allow_upload_lock.clone(),
2221+
)
2222+
.mock_once()
2223+
.mount()
2224+
.await;
2225+
mock_jpeg_upload(
2226+
&mock,
2227+
mxc_uri!("mxc://sdk.rs/media1"),
2228+
Some(filename1.to_owned()),
2229+
allow_upload_lock.clone(),
2230+
)
2231+
.mock_once()
2232+
.mount()
2233+
.await;
2234+
mock_jpeg_upload(
2235+
&mock,
2236+
mxc_uri!("mxc://sdk.rs/thumbnail2"),
2237+
None,
2238+
allow_upload_lock.clone(),
2239+
)
2240+
.mock_once()
2241+
.mount()
2242+
.await;
2243+
mock_jpeg_upload(
2244+
&mock,
2245+
mxc_uri!("mxc://sdk.rs/media2"),
2246+
Some(filename2.to_owned()),
2247+
allow_upload_lock.clone(),
2248+
)
2249+
.mock_once()
2250+
.mount()
2251+
.await;
22192252

22202253
// ----------------------
22212254
// Send the media.

0 commit comments

Comments
 (0)