Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
bnjbvr committed Nov 18, 2024
1 parent 01c7d19 commit 017431e
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 6 deletions.
11 changes: 9 additions & 2 deletions crates/matrix-sdk-ui/src/timeline/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,8 +478,15 @@ impl Timeline {
}
}

EditedContent::MediaCaption { caption: _, formatted_caption: _ } => {
todo!("bnjbvr you had one job");
EditedContent::MediaCaption { caption, formatted_caption } => {
if handle
.edit_media_caption(caption, formatted_caption)
.await
.map_err(RoomSendQueueError::StorageError)?
{
return Ok(());
}
return Err(EditError::InvalidLocalEchoState.into());
}
};

Expand Down
49 changes: 48 additions & 1 deletion crates/matrix-sdk/src/send_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,10 @@ use ruma::{
events::{
reaction::ReactionEventContent,
relation::Annotation,
room::{message::RoomMessageEventContent, MediaSource},
room::{
message::{FormattedBody, RoomMessageEventContent},
MediaSource,
},
AnyMessageLikeEventContent, EventContent as _,
},
serde::Raw,
Expand Down Expand Up @@ -1804,6 +1807,10 @@ pub enum RoomSendQueueStorageError {
// TODO: remove this
#[error("This operation is not implemented yet for media uploads")]
OperationNotImplementedYet,

/// Trying to edit a media caption for something that's not a media.
#[error("Can't edit a media caption when the underlying event isn't a media")]
InvalidMediaCaptionEdit,
}

/// Extra transaction IDs useful during an upload.
Expand Down Expand Up @@ -1932,6 +1939,46 @@ impl SendHandle {
.await
}

/// Edits the content of a local echo with a media caption.
///
/// Will fail if the event to be sent wasn't a media.
pub async fn edit_media_caption(
&self,
caption: Option<String>,
formatted_caption: Option<FormattedBody>,
) -> Result<bool, RoomSendQueueStorageError> {
let Some(handles) = &self.media_handles else {
return Err(RoomSendQueueStorageError::InvalidMediaCaptionEdit);
};

if let Some(new_content) = self
.room
.inner
.queue
.edit_media_caption(&self.transaction_id, handles, caption, formatted_caption)
.await?
{
trace!("successful edit of media caption");

// Wake up the queue, in case the room was asleep before the edit.
self.room.inner.notifier.notify_one();

let new_content = SerializableEventContent::new(&new_content)
.map_err(RoomSendQueueStorageError::JsonSerialization)?;

// Propagate a replaced update too.
let _ = self.room.inner.updates.send(RoomSendQueueUpdate::ReplacedLocalEvent {
transaction_id: self.transaction_id.clone(),
new_content,
});

Ok(true)
} else {
debug!("local echo doesn't exist anymore, can't edit media caption");
Ok(false)
}
}

/// Unwedge a local echo identified by its transaction identifier and try to
/// resend it.
pub async fn unwedge(&self) -> Result<(), RoomSendQueueError> {
Expand Down
36 changes: 33 additions & 3 deletions crates/matrix-sdk/src/send_queue/upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@ use matrix_sdk_base::{
use mime::Mime;
use ruma::{
assign,
events::room::{
message::{MessageType, RoomMessageEventContent},
MediaSource, ThumbnailInfo,
events::{
room::{
message::{FormattedBody, MessageType, RoomMessageEventContent},
MediaSource, ThumbnailInfo,
},
AnyMessageLikeEventContent,
},
uint, OwnedMxcUri, OwnedTransactionId, TransactionId, UInt,
};
Expand Down Expand Up @@ -542,4 +545,31 @@ impl QueueStorage {
debug!("successfully aborted!");
Ok(true)
}

#[instrument(skip(self, handles))]
pub(super) async fn edit_media_caption(
&self,
event_txn: &TransactionId,
handles: &MediaHandles,
caption: Option<String>,
formatted_caption: Option<FormattedBody>,
) -> Result<Option<AnyMessageLikeEventContent>, RoomSendQueueStorageError> {
// If event is a dependent event: update it.
// 1. retrieve the dependent request from storage,
// -> if not found: skip next steps
// 2. match against the expected dependent request kind of event
// 3. update event
// 4. update dependent request in storage
// 5. done

// If event is a request:
// 1. retrieve the content from the database,
// -> if not found: return false
// 2. match against the expected request + event kind
// 3. create the edited event
// 4.
// - if it is being sent: add a dependent request
// - if it is not: push a queued request
Ok(None)
}
}

0 comments on commit 017431e

Please sign in to comment.