Skip to content

Commit ed36540

Browse files
committed
feat(send_queue): cache thumbnail sizes to use them in progress reporting later
Signed-off-by: Johannes Marbach <n0-0ne+github@mailbox.org>
1 parent 98ded6d commit ed36540

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

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

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,9 @@ struct QueueThumbnailInfo {
303303

304304
/// The thumbnail's mime type.
305305
content_type: Mime,
306+
307+
/// The thumbnail's file size in bytes.
308+
file_size: usize,
306309
}
307310

308311
/// A specific room's send queue ran into an error, and it has disabled itself.
@@ -964,6 +967,10 @@ struct QueueStorage {
964967

965968
/// To which room is this storage related.
966969
room_id: OwnedRoomId,
970+
971+
/// In-memory mapping of media transaction IDs to thumbnail sizes for the
972+
/// purpose of progress reporting.
973+
thumbnail_size_cache: Arc<Mutex<HashMap<OwnedTransactionId, Vec<Option<usize>>>>>,
967974
}
968975

969976
impl QueueStorage {
@@ -975,7 +982,11 @@ impl QueueStorage {
975982

976983
/// Create a new queue for queuing requests to be sent later.
977984
fn new(client: WeakClient, room: OwnedRoomId) -> Self {
978-
Self { room_id: room, store: StoreLock { client, being_sent: Default::default() } }
985+
Self {
986+
room_id: room,
987+
store: StoreLock { client, being_sent: Default::default() },
988+
thumbnail_size_cache: Arc::new(Mutex::new(HashMap::new())),
989+
}
979990
}
980991

981992
/// Push a new event to be sent in the queue, with a default priority of 0.
@@ -1132,6 +1143,8 @@ impl QueueStorage {
11321143
warn!(txn_id = %transaction_id, "request marked as sent was missing from storage");
11331144
}
11341145

1146+
self.thumbnail_size_cache.lock().await.remove(transaction_id);
1147+
11351148
Ok(())
11361149
}
11371150

@@ -1172,6 +1185,8 @@ impl QueueStorage {
11721185
.remove_send_queue_request(&self.room_id, transaction_id)
11731186
.await?;
11741187

1188+
self.thumbnail_size_cache.lock().await.remove(transaction_id);
1189+
11751190
Ok(removed)
11761191
}
11771192

@@ -1234,6 +1249,8 @@ impl QueueStorage {
12341249
let client = guard.client()?;
12351250
let store = client.state_store();
12361251

1252+
let media_sizes = vec![thumbnail.as_ref().map(|t| t.file_size)];
1253+
12371254
let thumbnail_info = self
12381255
.push_thumbnail_and_media_uploads(
12391256
store,
@@ -1251,7 +1268,7 @@ impl QueueStorage {
12511268
.save_dependent_queued_request(
12521269
&self.room_id,
12531270
&upload_file_txn,
1254-
send_event_txn.into(),
1271+
send_event_txn.clone().into(),
12551272
created_at,
12561273
DependentQueuedRequestKind::FinishUpload {
12571274
local_echo: Box::new(event),
@@ -1261,6 +1278,8 @@ impl QueueStorage {
12611278
)
12621279
.await?;
12631280

1281+
self.thumbnail_size_cache.lock().await.insert(send_event_txn, media_sizes);
1282+
12641283
Ok(())
12651284
}
12661285

@@ -1281,6 +1300,7 @@ impl QueueStorage {
12811300
let store = client.state_store();
12821301

12831302
let mut finish_item_infos = Vec::with_capacity(item_queue_infos.len());
1303+
let mut media_sizes = Vec::with_capacity(item_queue_infos.len());
12841304

12851305
let Some((first, rest)) = item_queue_infos.split_first() else {
12861306
return Ok(());
@@ -1303,6 +1323,7 @@ impl QueueStorage {
13031323

13041324
finish_item_infos
13051325
.push(FinishGalleryItemInfo { file_upload: upload_file_txn.clone(), thumbnail_info });
1326+
media_sizes.push(thumbnail.as_ref().map(|t| t.file_size));
13061327

13071328
let mut last_upload_file_txn = upload_file_txn.clone();
13081329

@@ -1367,6 +1388,7 @@ impl QueueStorage {
13671388
file_upload: upload_file_txn.clone(),
13681389
thumbnail_info: thumbnail_info.cloned(),
13691390
});
1391+
media_sizes.push(thumbnail.as_ref().map(|t| t.file_size));
13701392

13711393
last_upload_file_txn = upload_file_txn.clone();
13721394
}
@@ -1377,7 +1399,7 @@ impl QueueStorage {
13771399
.save_dependent_queued_request(
13781400
&self.room_id,
13791401
&last_upload_file_txn,
1380-
send_event_txn.into(),
1402+
send_event_txn.clone().into(),
13811403
created_at,
13821404
DependentQueuedRequestKind::FinishGallery {
13831405
local_echo: Box::new(event),
@@ -1386,6 +1408,8 @@ impl QueueStorage {
13861408
)
13871409
.await?;
13881410

1411+
self.thumbnail_size_cache.lock().await.insert(send_event_txn, media_sizes);
1412+
13891413
Ok(())
13901414
}
13911415

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,7 @@ impl RoomSendQueue {
445445
// Create the information required for filling the thumbnail section of the
446446
// event.
447447
let (data, content_type, thumbnail_info) = thumbnail.into_parts();
448+
let file_size = data.len();
448449

449450
// Cache thumbnail in the cache store.
450451
let thumbnail_media_request = Media::make_local_file_media_request(&txn);
@@ -472,6 +473,7 @@ impl RoomSendQueue {
472473
},
473474
media_request_parameters: thumbnail_media_request,
474475
content_type,
476+
file_size,
475477
}),
476478
})
477479
} else {

0 commit comments

Comments
 (0)