Skip to content

Commit c23d92d

Browse files
committed
fixup! feat(timeline): communicate media upload progress through EventSendState::NotSentYet
Retain progress when applying remote echo Signed-off-by: Johannes Marbach <n0-0ne+github@mailbox.org>
1 parent 5ec82b1 commit c23d92d

File tree

2 files changed

+53
-35
lines changed
  • crates/matrix-sdk-ui

2 files changed

+53
-35
lines changed

crates/matrix-sdk-ui/src/timeline/controller/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1072,7 +1072,11 @@ impl<P: RoomDataProvider, D: Decryptor> TimelineController<P, D> {
10721072
warn!("We looked for a local item, but it transitioned as remote??");
10731073
return false;
10741074
};
1075-
prev_local_item.with_send_state(EventSendState::NotSentYet { progress: None })
1075+
// If the local echo had an upload progress, retain it.
1076+
let progress = as_variant!(&prev_local_item.send_state,
1077+
EventSendState::NotSentYet { progress } => progress.clone())
1078+
.flatten();
1079+
prev_local_item.with_send_state(EventSendState::NotSentYet { progress })
10761080
};
10771081

10781082
// Replace the local-related state (kind) and the content state.

crates/matrix-sdk-ui/tests/integration/timeline/media.rs

Lines changed: 48 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ async fn test_send_attachment_from_file() {
145145
}
146146

147147
// The media upload finishes.
148-
{
148+
let (final_index, final_progress) = {
149149
assert_let_timeout!(
150150
Duration::from_secs(3),
151151
Some(VectorDiff::Set { index: 1, value: item }) = timeline_stream.next()
@@ -164,7 +164,9 @@ async fn test_send_attachment_from_file() {
164164
assert_let!(MessageType::File(file) = msg.msgtype());
165165
assert_let!(MediaSource::Plain(uri) = &file.source);
166166
assert!(uri.to_string().contains("localhost"));
167-
}
167+
168+
(*index, *progress)
169+
};
168170

169171
// Eventually, the media is updated with the final MXC IDs…
170172
{
@@ -173,7 +175,14 @@ async fn test_send_attachment_from_file() {
173175
Some(VectorDiff::Set { index: 1, value: item }) = timeline_stream.next()
174176
);
175177
assert_let!(Some(msg) = item.content().as_message());
176-
assert_matches!(item.send_state(), Some(EventSendState::NotSentYet { progress: None }));
178+
assert_let!(
179+
Some(EventSendState::NotSentYet {
180+
progress: Some(EventSendProgress::MediaUpload { index, progress })
181+
}) = item.send_state()
182+
);
183+
assert_eq!(*index, final_index);
184+
assert_eq!(progress.current, final_progress.current);
185+
assert_eq!(progress.total, final_progress.total);
177186
assert_eq!(get_filename_and_caption(msg.msgtype()), ("test.bin", Some("caption")));
178187

179188
// The URI now refers to the final MXC URI.
@@ -284,35 +293,31 @@ async fn test_send_attachment_from_bytes() {
284293

285294
assert_let!(Some(EventSendState::NotSentYet { progress }) = item.send_state());
286295

287-
match progress {
288-
Some(EventSendProgress::MediaUpload { index, progress }) => {
289-
// We're only uploading a single file.
290-
assert_eq!(*index, 0);
291-
292-
// The progress is reported in units of the unencrypted file size.
293-
assert!(progress.current <= progress.total);
294-
assert_eq!(progress.total, size);
295-
296-
// The progress only increases.
297-
if let Some(prev_progress) = prev_progress {
298-
assert!(progress.current >= prev_progress.current);
299-
}
300-
prev_progress = Some(*progress);
301-
302-
// The URI still refers to the local cache.
303-
assert_let!(MessageType::File(file) = msg.msgtype());
304-
assert_let!(MediaSource::Plain(uri) = &file.source);
305-
assert!(uri.to_string().contains("localhost"));
306-
}
307-
None => {
308-
// The upload finished and the URI now refers to the final MXC URI.
309-
assert_let!(MessageType::File(file) = msg.msgtype());
310-
assert_let!(MediaSource::Plain(uri) = &file.source);
311-
assert_eq!(uri.to_string(), "mxc://sdk.rs/media");
312-
313-
break;
314-
}
296+
assert_let!(Some(EventSendProgress::MediaUpload { index, progress }) = progress);
297+
298+
// We're only uploading a single file.
299+
assert_eq!(*index, 0);
300+
301+
// The progress is reported in units of the unencrypted file size.
302+
assert!(progress.current <= progress.total);
303+
assert_eq!(progress.total, size);
304+
305+
// The progress only increases.
306+
if let Some(prev_progress) = prev_progress {
307+
assert!(progress.current >= prev_progress.current);
315308
}
309+
prev_progress = Some(*progress);
310+
311+
assert_let!(MessageType::File(file) = msg.msgtype());
312+
assert_let!(MediaSource::Plain(uri) = &file.source);
313+
314+
// Check if the upload finished and the URI now refers to the final MXC URI.
315+
if progress.current == progress.total && *uri == "mxc://sdk.rs/media" {
316+
break;
317+
}
318+
319+
// Otherwise, the URI still refers to the local cache.
320+
assert!(uri.to_string().contains("localhost"));
316321
}
317322
}
318323

@@ -422,7 +427,7 @@ async fn test_send_gallery_from_bytes() {
422427
}
423428

424429
// The media upload finishes.
425-
{
430+
let (final_index, final_progress) = {
426431
assert_let_timeout!(
427432
Duration::from_secs(3),
428433
Some(VectorDiff::Set { index: 1, value: item }) = timeline_stream.next()
@@ -453,7 +458,9 @@ async fn test_send_gallery_from_bytes() {
453458
// The URI still refers to the local cache.
454459
assert_let!(MediaSource::Plain(uri) = &file.source);
455460
assert!(uri.to_string().contains("localhost"));
456-
}
461+
462+
(*index, progress.clone())
463+
};
457464

458465
// Eventually, the media is updated with the final MXC IDs…
459466
{
@@ -462,7 +469,14 @@ async fn test_send_gallery_from_bytes() {
462469
Some(VectorDiff::Set { index: 1, value: item }) = timeline_stream.next()
463470
);
464471
assert_let!(Some(msg) = item.content().as_message());
465-
assert_matches!(item.send_state(), Some(EventSendState::NotSentYet { progress: None }));
472+
assert_let!(
473+
Some(EventSendState::NotSentYet {
474+
progress: Some(EventSendProgress::MediaUpload { index, progress })
475+
}) = item.send_state()
476+
);
477+
assert_eq!(*index, final_index);
478+
assert_eq!(progress.current, final_progress.current);
479+
assert_eq!(progress.total, final_progress.total);
466480

467481
// Message is gallery of expected length
468482
assert_let!(MessageType::Gallery(content) = msg.msgtype());

0 commit comments

Comments
 (0)