Skip to content

feat(timeline): report progress for media uploads #5454

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
62 changes: 59 additions & 3 deletions bindings/matrix-sdk-ffi/src/timeline/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ use matrix_sdk_common::{
stream::StreamExt,
};
use matrix_sdk_ui::timeline::{
self, AttachmentConfig, AttachmentSource, EventItemOrigin, Profile, TimelineDetails,
self, AttachmentConfig, AttachmentSource, EventItemOrigin,
EventSendProgress as SdkEventSendProgress, Profile, TimelineDetails,
TimelineUniqueId as SdkTimelineUniqueId,
};
use mime::Mime;
Expand Down Expand Up @@ -241,6 +242,56 @@ impl From<UploadSource> for AttachmentSource {
}
}

/// This type represents the "send progress" of a local event timeline item.
#[derive(Clone, Copy, uniffi::Enum)]
pub enum EventSendProgress {
/// A media is being uploaded.
MediaUpload {
/// The index of the media within the transaction. A file and its
/// thumbnail share the same index. Will always be 0 for non-gallery
/// media uploads.
index: u64,

/// The current combined upload progress for both the file and,
/// if it exists, its thumbnail.
progress: AbstractProgress,
},
}

impl From<SdkEventSendProgress> for EventSendProgress {
fn from(value: SdkEventSendProgress) -> Self {
match value {
SdkEventSendProgress::MediaUpload { index, progress } => {
Self::MediaUpload { index, progress: progress.into() }
}
}
}
}

/// Progress of an operation in abstract units.
///
/// Contrary to [`TransmissionProgress`], this allows tracking the progress
/// of sending or receiving a payload in estimated pseudo units representing a
/// percentage. This is helpful in cases where the exact progress in bytes isn't
/// known, for instance, because encryption (which changes the size) happens on
/// the fly.
#[derive(Clone, Copy, uniffi::Record)]
pub struct AbstractProgress {
/// How many units were already transferred.
pub current: u64,
/// How many units there are in total.
pub total: u64,
}

impl From<matrix_sdk::send_queue::AbstractProgress> for AbstractProgress {
fn from(value: matrix_sdk::send_queue::AbstractProgress) -> Self {
Self {
current: value.current.try_into().unwrap_or(u64::MAX),
total: value.total.try_into().unwrap_or(u64::MAX),
}
}
}

#[matrix_sdk_ffi_macros::export]
impl Timeline {
pub async fn add_listener(&self, listener: Box<dyn TimelineListener>) -> Arc<TaskHandle> {
Expand Down Expand Up @@ -990,7 +1041,10 @@ impl TimelineItem {
#[derive(Clone, uniffi::Enum)]
pub enum EventSendState {
/// The local event has not been sent yet.
NotSentYet,
NotSentYet {
/// The progress of the sending operation, if any is available.
progress: Option<EventSendProgress>,
},

/// The local event has been sent to the server, but unsuccessfully: The
/// sending has failed.
Expand All @@ -1015,7 +1069,9 @@ impl From<&matrix_sdk_ui::timeline::EventSendState> for EventSendState {
use matrix_sdk_ui::timeline::EventSendState::*;

match value {
NotSentYet => Self::NotSentYet,
NotSentYet { progress } => {
Self::NotSentYet { progress: progress.clone().map(|p| p.into()) }
}
SendingFailed { error, is_recoverable } => {
let as_queue_wedge_error: matrix_sdk::QueueWedgeError = (&**error).into();
Self::SendingFailed {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ mod tests {

fn local_event() -> Arc<TimelineItem> {
let event_kind = EventTimelineItemKind::Local(LocalEventTimelineItem {
send_state: EventSendState::NotSentYet,
send_state: EventSendState::NotSentYet { progress: None },
transaction_id: OwnedTransactionId::from("trans"),
send_handle: None,
});
Expand Down
29 changes: 21 additions & 8 deletions crates/matrix-sdk-ui/src/timeline/controller/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ pub(super) use self::{
state_transaction::TimelineStateTransaction,
};
use super::{
DateDividerMode, EmbeddedEvent, Error, EventSendState, EventTimelineItem, InReplyToDetails,
PaginationError, Profile, TimelineDetails, TimelineEventItemId, TimelineFocus, TimelineItem,
TimelineItemContent, TimelineItemKind, VirtualTimelineItem,
DateDividerMode, EmbeddedEvent, Error, EventSendProgress, EventSendState, EventTimelineItem,
InReplyToDetails, PaginationError, Profile, TimelineDetails, TimelineEventItemId,
TimelineFocus, TimelineItem, TimelineItemContent, TimelineItemKind, VirtualTimelineItem,
algorithms::{rfind_event_by_id, rfind_event_item},
event_item::{ReactionStatus, RemoteEventOrigin},
item::TimelineUniqueId,
Expand Down Expand Up @@ -1072,7 +1072,11 @@ impl<P: RoomDataProvider, D: Decryptor> TimelineController<P, D> {
warn!("We looked for a local item, but it transitioned as remote??");
return false;
};
prev_local_item.with_send_state(EventSendState::NotSentYet)
// If the local echo had an upload progress, retain it.
let progress = as_variant!(&prev_local_item.send_state,
EventSendState::NotSentYet { progress } => progress.clone())
.flatten();
prev_local_item.with_send_state(EventSendState::NotSentYet { progress })
};

// Replace the local-related state (kind) and the content state.
Expand Down Expand Up @@ -1358,17 +1362,26 @@ impl<P: RoomDataProvider, D: Decryptor> TimelineController<P, D> {
}

RoomSendQueueUpdate::RetryEvent { transaction_id } => {
self.update_event_send_state(&transaction_id, EventSendState::NotSentYet).await;
self.update_event_send_state(
&transaction_id,
EventSendState::NotSentYet { progress: None },
)
.await;
}

RoomSendQueueUpdate::SentEvent { transaction_id, event_id } => {
self.update_event_send_state(&transaction_id, EventSendState::Sent { event_id })
.await;
}

RoomSendQueueUpdate::MediaUpload { related_to, .. } => {
// TODO(bnjbvr): Do something else?
info!(txn_id = %related_to, "some media for a media event has been uploaded");
RoomSendQueueUpdate::MediaUpload { related_to, index, progress, .. } => {
self.update_event_send_state(
&related_to,
EventSendState::NotSentYet {
progress: Some(EventSendProgress::MediaUpload { index, progress }),
},
)
.await;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -781,7 +781,7 @@ mod observable_items_tests {
thread_summary: None,
}),
EventTimelineItemKind::Local(LocalEventTimelineItem {
send_state: EventSendState::NotSentYet,
send_state: EventSendState::NotSentYet { progress: None },
transaction_id: transaction_id.into(),
send_handle: None,
}),
Expand Down
2 changes: 1 addition & 1 deletion crates/matrix-sdk-ui/src/timeline/event_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,7 @@ impl<'a, 'o> TimelineEventHandler<'a, 'o> {

let kind: EventTimelineItemKind = match &self.ctx.flow {
Flow::Local { txn_id, send_handle } => LocalEventTimelineItem {
send_state: EventSendState::NotSentYet,
send_state: EventSendState::NotSentYet { progress: None },
transaction_id: txn_id.to_owned(),
send_handle: send_handle.clone(),
}
Expand Down
28 changes: 26 additions & 2 deletions crates/matrix-sdk-ui/src/timeline/event_item/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
use std::sync::Arc;

use as_variant::as_variant;
use matrix_sdk::{Error, send_queue::SendHandle};
use matrix_sdk::{
Error,
send_queue::{AbstractProgress, SendHandle},
};
use ruma::{EventId, OwnedEventId, OwnedTransactionId};

use super::TimelineEventItemId;
Expand Down Expand Up @@ -65,7 +68,10 @@ impl LocalEventTimelineItem {
#[derive(Clone, Debug)]
pub enum EventSendState {
/// The local event has not been sent yet.
NotSentYet,
NotSentYet {
/// The progress of the sending operation, if any is available.
progress: Option<EventSendProgress>,
},
/// The local event has been sent to the server, but unsuccessfully: The
/// sending has failed.
SendingFailed {
Expand All @@ -84,3 +90,21 @@ pub enum EventSendState {
event_id: OwnedEventId,
},
}

/// This type represents the "send progress" of a local event timeline item.
#[derive(Clone, Debug)]
pub enum EventSendProgress {
/// A media (consisting of a file and possibly a thumbnail) is being
/// uploaded.
MediaUpload {
/// The index of the media within the transaction. A file and its
/// thumbnail share the same index. Will always be 0 for non-gallery
/// media uploads.
index: u64,

/// The combined upload progress across the file and, if existing, its
/// thumbnail. For gallery uploads, the progress is reported per indexed
/// gallery item.
progress: AbstractProgress,
},
}
2 changes: 1 addition & 1 deletion crates/matrix-sdk-ui/src/timeline/event_item/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub use self::{
PollResult, PollState, RoomMembershipChange, RoomPinnedEventsChange, Sticker,
ThreadSummary, TimelineItemContent,
},
local::EventSendState,
local::{EventSendProgress, EventSendState},
};
pub(super) use self::{
content::{
Expand Down
10 changes: 5 additions & 5 deletions crates/matrix-sdk-ui/src/timeline/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,11 @@ pub use self::{
error::*,
event_item::{
AnyOtherFullStateEventContent, EmbeddedEvent, EncryptedMessage, EventItemOrigin,
EventSendState, EventTimelineItem, InReplyToDetails, MemberProfileChange, MembershipChange,
Message, MsgLikeContent, MsgLikeKind, OtherState, PollResult, PollState, Profile,
ReactionInfo, ReactionStatus, ReactionsByKeyBySender, RoomMembershipChange,
RoomPinnedEventsChange, Sticker, ThreadSummary, TimelineDetails, TimelineEventItemId,
TimelineItemContent,
EventSendProgress, EventSendState, EventTimelineItem, InReplyToDetails,
MemberProfileChange, MembershipChange, Message, MsgLikeContent, MsgLikeKind, OtherState,
PollResult, PollState, Profile, ReactionInfo, ReactionStatus, ReactionsByKeyBySender,
RoomMembershipChange, RoomPinnedEventsChange, Sticker, ThreadSummary, TimelineDetails,
TimelineEventItemId, TimelineItemContent,
},
event_type_filter::TimelineEventTypeFilter,
item::{TimelineItem, TimelineItemKind, TimelineUniqueId},
Expand Down
7 changes: 5 additions & 2 deletions crates/matrix-sdk-ui/src/timeline/tests/echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ async fn test_remote_echo_full_trip() {
let item = assert_next_matches!(stream, VectorDiff::PushBack { value } => value);
let event_item = item.as_event().unwrap();
assert!(event_item.is_local_echo());
assert_matches!(event_item.send_state(), Some(EventSendState::NotSentYet));
assert_matches!(
event_item.send_state(),
Some(EventSendState::NotSentYet { progress: None })
);
assert!(!event_item.can_be_replied_to());
item.unique_id().to_owned()
};
Expand Down Expand Up @@ -308,7 +311,7 @@ async fn test_no_reuse_of_counters() {
let local_id = assert_next_matches_with_timeout!(stream, VectorDiff::PushBack { value: item } => {
let event_item = item.as_event().unwrap();
assert!(event_item.is_local_echo());
assert_matches!(event_item.send_state(), Some(EventSendState::NotSentYet));
assert_matches!(event_item.send_state(), Some(EventSendState::NotSentYet { progress: None }));
assert!(!event_item.can_be_replied_to());
item.unique_id().to_owned()
});
Expand Down
8 changes: 4 additions & 4 deletions crates/matrix-sdk-ui/tests/integration/timeline/echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ async fn test_echo() {

assert_let!(VectorDiff::PushBack { value: local_echo } = &timeline_updates[0]);
let item = local_echo.as_event().unwrap();
assert_matches!(item.send_state(), Some(EventSendState::NotSentYet));
assert_matches!(item.send_state(), Some(EventSendState::NotSentYet { progress: None }));
assert_let!(Some(msg) = item.content().as_message());
assert_let!(MessageType::Text(text) = msg.msgtype());
assert_eq!(text.body, "Hello, World!");
Expand Down Expand Up @@ -150,7 +150,7 @@ async fn test_retry_failed() {

// First, local echo is added.
assert_next_matches!(timeline_stream, VectorDiff::PushBack { value } => {
assert_matches!(value.send_state(), Some(EventSendState::NotSentYet));
assert_matches!(value.send_state(), Some(EventSendState::NotSentYet { progress: None }));
});

// Sending fails, because the error is a transient one that's recoverable,
Expand Down Expand Up @@ -216,7 +216,7 @@ async fn test_dedup_by_event_id_late() {
// Timeline: [local echo]
assert_let!(VectorDiff::PushBack { value: local_echo } = &timeline_updates[0]);
let item = local_echo.as_event().unwrap();
assert_matches!(item.send_state(), Some(EventSendState::NotSentYet));
assert_matches!(item.send_state(), Some(EventSendState::NotSentYet { progress: None }));

// Timeline: [date-divider, local echo]
assert_let!(VectorDiff::PushFront { value: date_divider } = &timeline_updates[1]);
Expand Down Expand Up @@ -283,7 +283,7 @@ async fn test_cancel_failed() {

// Local echo is added (immediately)
assert_next_matches!(timeline_stream, VectorDiff::PushBack { value } => {
assert_matches!(value.send_state(), Some(EventSendState::NotSentYet));
assert_matches!(value.send_state(), Some(EventSendState::NotSentYet { progress: None }));
});

// Sending fails, the mock server has no matching route
Expand Down
8 changes: 4 additions & 4 deletions crates/matrix-sdk-ui/tests/integration/timeline/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ async fn test_edit_local_echo() {
let internal_id = item.unique_id();

let item = item.as_event().unwrap();
assert_matches!(item.send_state(), Some(EventSendState::NotSentYet));
assert_matches!(item.send_state(), Some(EventSendState::NotSentYet { progress: None }));

assert_let!(VectorDiff::PushFront { value: date_divider } = &timeline_updates[1]);
assert!(date_divider.is_date_divider());
Expand Down Expand Up @@ -248,7 +248,7 @@ async fn test_edit_local_echo() {
assert!(item.is_local_echo());

// The send state has been reset.
assert_matches!(item.send_state(), Some(EventSendState::NotSentYet));
assert_matches!(item.send_state(), Some(EventSendState::NotSentYet { progress: None }));

let edit_message = item.content().as_message().unwrap();
assert_eq!(edit_message.body(), "hello, world");
Expand Down Expand Up @@ -634,7 +634,7 @@ async fn test_edit_local_echo_with_unsupported_content() {
assert_let!(VectorDiff::PushBack { value: item } = &timeline_updates[0]);

let item = item.as_event().unwrap();
assert_matches!(item.send_state(), Some(EventSendState::NotSentYet));
assert_matches!(item.send_state(), Some(EventSendState::NotSentYet { progress: None }));

assert_let!(VectorDiff::PushFront { value: date_divider } = &timeline_updates[1]);
assert!(date_divider.is_date_divider());
Expand Down Expand Up @@ -688,7 +688,7 @@ async fn test_edit_local_echo_with_unsupported_content() {
assert_let!(VectorDiff::PushBack { value: item } = &timeline_updates[0]);

let item = item.as_event().unwrap();
assert_matches!(item.send_state(), Some(EventSendState::NotSentYet));
assert_matches!(item.send_state(), Some(EventSendState::NotSentYet { progress: None }));

// Let's edit the local echo (poll start) with an unsupported type (message).
let edit_err = timeline
Expand Down
Loading
Loading