Skip to content

Commit

Permalink
feat(send queue): allow setting intentional mentions in media caption…
Browse files Browse the repository at this point in the history
…s edits

Fixes #4302.
  • Loading branch information
bnjbvr committed Jan 6, 2025
1 parent 1480fad commit 58e923b
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 21 deletions.
24 changes: 18 additions & 6 deletions bindings/matrix-sdk-ffi/src/timeline/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ use crate::{
event::EventOrTransactionId,
helpers::unwrap_or_clone_arc,
ruma::{
AssetType, AudioInfo, FileInfo, FormattedBody, ImageInfo, PollKind, ThumbnailInfo,
VideoInfo,
AssetType, AudioInfo, FileInfo, FormattedBody, ImageInfo, Mentions, PollKind,
ThumbnailInfo, VideoInfo,
},
task_handle::TaskHandle,
utils::Timestamp,
Expand Down Expand Up @@ -1295,22 +1295,32 @@ impl From<ReceiptType> for ruma::api::client::receipt::create_receipt::v3::Recei

#[derive(Clone, uniffi::Enum)]
pub enum EditedContent {
RoomMessage { content: Arc<RoomMessageEventContentWithoutRelation> },
MediaCaption { caption: Option<String>, formatted_caption: Option<FormattedBody> },
PollStart { poll_data: PollData },
RoomMessage {
content: Arc<RoomMessageEventContentWithoutRelation>,
},
MediaCaption {
caption: Option<String>,
formatted_caption: Option<FormattedBody>,
mentions: Option<Mentions>,
},
PollStart {
poll_data: PollData,
},
}

impl TryFrom<EditedContent> for SdkEditedContent {
type Error = ClientError;

fn try_from(value: EditedContent) -> Result<Self, Self::Error> {
match value {
EditedContent::RoomMessage { content } => {
Ok(SdkEditedContent::RoomMessage((*content).clone()))
}
EditedContent::MediaCaption { caption, formatted_caption } => {
EditedContent::MediaCaption { caption, formatted_caption, mentions } => {
Ok(SdkEditedContent::MediaCaption {
caption,
formatted_caption: formatted_caption.map(Into::into),
mentions: mentions.map(Into::into),
})
}
EditedContent::PollStart { poll_data } => {
Expand All @@ -1332,12 +1342,14 @@ impl TryFrom<EditedContent> for SdkEditedContent {
fn create_caption_edit(
caption: Option<String>,
formatted_caption: Option<FormattedBody>,
mentions: Option<Mentions>,
) -> EditedContent {
let formatted_caption =
formatted_body_from(caption.as_deref(), formatted_caption.map(Into::into));
EditedContent::MediaCaption {
caption,
formatted_caption: formatted_caption.as_ref().map(Into::into),
mentions,
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/matrix-sdk-ui/src/timeline/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,9 +486,9 @@ impl Timeline {
}
}

EditedContent::MediaCaption { caption, formatted_caption } => {
EditedContent::MediaCaption { caption, formatted_caption, mentions } => {
if handle
.edit_media_caption(caption, formatted_caption)
.edit_media_caption(caption, formatted_caption, mentions)
.await
.map_err(RoomSendQueueError::StorageError)?
{
Expand Down
25 changes: 18 additions & 7 deletions crates/matrix-sdk/src/room/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ use ruma::{
RoomMessageEventContentWithoutRelation,
},
AnyMessageLikeEvent, AnyMessageLikeEventContent, AnySyncMessageLikeEvent,
AnySyncTimelineEvent, AnyTimelineEvent, MessageLikeEvent, OriginalMessageLikeEvent,
SyncMessageLikeEvent,
AnySyncTimelineEvent, AnyTimelineEvent, Mentions, MessageLikeEvent,
OriginalMessageLikeEvent, SyncMessageLikeEvent,
},
EventId, RoomId, UserId,
};
Expand All @@ -54,6 +54,10 @@ pub enum EditedContent {
///
/// Set to `None` to remove an existing formatted caption.
formatted_caption: Option<FormattedBody>,

/// New set of intentional mentions to be included in the edited
/// caption.
mentions: Option<Mentions>,
},

/// The content is a new poll start.
Expand Down Expand Up @@ -196,7 +200,7 @@ async fn make_edit_event<S: EventSource>(
Ok(replacement.into())
}

EditedContent::MediaCaption { caption, formatted_caption } => {
EditedContent::MediaCaption { caption, formatted_caption, mentions } => {
// Handle edits of m.room.message.
let AnySyncMessageLikeEvent::RoomMessage(SyncMessageLikeEvent::Original(original)) =
message_like_event
Expand All @@ -207,7 +211,7 @@ async fn make_edit_event<S: EventSource>(
});
};

let mentions = original.content.mentions.clone();
let original_mentions = original.content.mentions.clone();
let replied_to_original_room_msg =
extract_replied_to(source, room_id, original.content.relates_to.clone()).await;

Expand All @@ -220,8 +224,10 @@ async fn make_edit_event<S: EventSource>(
});
}

prev_content.mentions = mentions;

let replacement = prev_content.make_replacement(
ReplacementMetadata::new(event_id.to_owned(), mentions),
ReplacementMetadata::new(event_id.to_owned(), original_mentions),
replied_to_original_room_msg.as_ref(),
);

Expand Down Expand Up @@ -506,7 +512,11 @@ mod tests {
room_id,
own_user_id,
event_id,
EditedContent::MediaCaption { caption: Some("yo".to_owned()), formatted_caption: None },
EditedContent::MediaCaption {
caption: Some("yo".to_owned()),
formatted_caption: None,
mentions: None,
},
)
.await
.unwrap_err();
Expand Down Expand Up @@ -543,6 +553,7 @@ mod tests {
EditedContent::MediaCaption {
caption: Some("Best joke ever".to_owned()),
formatted_caption: None,
mentions: None,
},
)
.await
Expand Down Expand Up @@ -602,7 +613,7 @@ mod tests {
own_user_id,
event_id,
// Remove the caption by setting it to None.
EditedContent::MediaCaption { caption: None, formatted_caption: None },
EditedContent::MediaCaption { caption: None, formatted_caption: None, mentions: None },
)
.await
.unwrap();
Expand Down
5 changes: 3 additions & 2 deletions crates/matrix-sdk/src/send_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ use ruma::{
message::{FormattedBody, RoomMessageEventContent},
MediaSource,
},
AnyMessageLikeEventContent, EventContent as _,
AnyMessageLikeEventContent, EventContent as _, Mentions,
},
serde::Raw,
OwnedEventId, OwnedRoomId, OwnedTransactionId, TransactionId,
Expand Down Expand Up @@ -1996,12 +1996,13 @@ impl SendHandle {
&self,
caption: Option<String>,
formatted_caption: Option<FormattedBody>,
mentions: Option<Mentions>,
) -> Result<bool, RoomSendQueueStorageError> {
if let Some(new_content) = self
.room
.inner
.queue
.edit_media_caption(&self.transaction_id, caption, formatted_caption)
.edit_media_caption(&self.transaction_id, caption, formatted_caption, mentions)
.await?
{
trace!("successful edit of media caption");
Expand Down
7 changes: 6 additions & 1 deletion crates/matrix-sdk/src/send_queue/upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use mime::Mime;
use ruma::{
events::{
room::message::{FormattedBody, MessageType, RoomMessageEventContent},
AnyMessageLikeEventContent,
AnyMessageLikeEventContent, Mentions,
},
OwnedTransactionId, TransactionId,
};
Expand Down Expand Up @@ -488,6 +488,7 @@ impl QueueStorage {
txn: &TransactionId,
caption: Option<String>,
formatted_caption: Option<FormattedBody>,
mentions: Option<Mentions>,
) -> Result<Option<AnyMessageLikeEventContent>, RoomSendQueueStorageError> {
// This error will be popular here.
use RoomSendQueueStorageError::InvalidMediaCaptionEdit;
Expand Down Expand Up @@ -526,6 +527,8 @@ impl QueueStorage {
return Err(InvalidMediaCaptionEdit);
}

local_echo.mentions = mentions;

let new_dependent_request = DependentQueuedRequestKind::FinishUpload {
local_echo: local_echo.clone(),
file_upload,
Expand Down Expand Up @@ -565,6 +568,8 @@ impl QueueStorage {
return Err(InvalidMediaCaptionEdit);
}

content.mentions = mentions;

let any_content: AnyMessageLikeEventContent = content.into();
let new_serialized = SerializableEventContent::new(&any_content.clone())?;

Expand Down
9 changes: 6 additions & 3 deletions crates/matrix-sdk/tests/integration/send_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2631,7 +2631,8 @@ async fn test_update_caption_while_sending_media() {
assert_eq!(local_content.filename(), filename);

// We can edit the caption while the file is being uploaded.
let edited = upload_handle.edit_media_caption(Some("caption".to_owned()), None).await.unwrap();
let edited =
upload_handle.edit_media_caption(Some("caption".to_owned()), None, None).await.unwrap();
assert!(edited);

{
Expand Down Expand Up @@ -2740,7 +2741,8 @@ async fn test_update_caption_before_event_is_sent() {
assert!(watch.is_empty());

// We can edit the caption here.
let edited = upload_handle.edit_media_caption(Some("caption".to_owned()), None).await.unwrap();
let edited =
upload_handle.edit_media_caption(Some("caption".to_owned()), None, None).await.unwrap();
assert!(edited);

// The media event is updated with the captions.
Expand Down Expand Up @@ -2855,7 +2857,8 @@ async fn test_update_caption_while_sending_media_event() {
};

// We can edit the caption while the event is beint sent.
let edited = upload_handle.edit_media_caption(Some("caption".to_owned()), None).await.unwrap();
let edited =
upload_handle.edit_media_caption(Some("caption".to_owned()), None, None).await.unwrap();
assert!(edited);

// The media event is updated with the captions.
Expand Down

0 comments on commit 58e923b

Please sign in to comment.