Skip to content

Commit aed804d

Browse files
committed
feat!(timeline): infer the reply type automatically when sending a media gallery
1 parent 84ffdf0 commit aed804d

File tree

5 files changed

+54
-72
lines changed

5 files changed

+54
-72
lines changed

bindings/matrix-sdk-ffi/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ All notable changes to this project will be documented in this file.
88

99
### Features:
1010

11+
- [**breaking**] [`GalleryUploadParameters::reply`] and [`UploadParameters::reply`] have been both
12+
replaced with a new optional `in_reply_to` field, that's a string which will be parsed into an
13+
`OwnedEventId` when sending the event. The thread relationship will be automatically filled in,
14+
based on the timeline focus.
15+
([5427](https://github.com/matrix-org/matrix-rust-sdk/pull/5427))
1116
- [**breaking**] [`Timeline::send_reply()`] now automatically fills in the thread relationship,
1217
based on the timeline focus. As a result, it only takes an `OwnedEventId` parameter, instead of
1318
the `Reply` type. The proper way to start a thread is now thus to create a threaded-focused

bindings/matrix-sdk-ffi/src/timeline/mod.rs

Lines changed: 14 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,7 @@ use matrix_sdk::{
2424
},
2525
deserialized_responses::{ShieldState as SdkShieldState, ShieldStateCode},
2626
event_cache::RoomPaginationStatus,
27-
room::{
28-
edit::EditedContent as SdkEditedContent,
29-
reply::{EnforceThread, Reply},
30-
},
27+
room::edit::EditedContent as SdkEditedContent,
3128
};
3229
use matrix_sdk_common::{
3330
executor::{AbortHandle, JoinHandle},
@@ -51,8 +48,7 @@ use ruma::{
5148
},
5249
},
5350
room::message::{
54-
LocationMessageEventContent, MessageType, ReplyWithinThread,
55-
RoomMessageEventContentWithoutRelation,
51+
LocationMessageEventContent, MessageType, RoomMessageEventContentWithoutRelation,
5652
},
5753
AnyMessageLikeEventContent,
5854
},
@@ -245,37 +241,6 @@ impl From<UploadSource> for AttachmentSource {
245241
}
246242
}
247243

248-
#[derive(uniffi::Record)]
249-
pub struct ReplyParameters {
250-
/// The ID of the event to reply to.
251-
event_id: String,
252-
/// Whether to enforce a thread relation.
253-
enforce_thread: bool,
254-
/// If enforcing a threaded relation, whether the message is a reply on a
255-
/// thread.
256-
reply_within_thread: bool,
257-
}
258-
259-
impl TryInto<Reply> for ReplyParameters {
260-
type Error = RoomError;
261-
262-
fn try_into(self) -> Result<Reply, Self::Error> {
263-
let event_id =
264-
EventId::parse(&self.event_id).map_err(|_| RoomError::InvalidRepliedToEventId)?;
265-
let enforce_thread = if self.enforce_thread {
266-
EnforceThread::Threaded(if self.reply_within_thread {
267-
ReplyWithinThread::Yes
268-
} else {
269-
ReplyWithinThread::No
270-
})
271-
} else {
272-
EnforceThread::MaybeThreaded
273-
};
274-
275-
Ok(Reply { event_id, enforce_thread })
276-
}
277-
}
278-
279244
#[matrix_sdk_ffi_macros::export]
280245
impl Timeline {
281246
pub async fn add_listener(&self, listener: Box<dyn TimelineListener>) -> Arc<TaskHandle> {
@@ -1401,14 +1366,15 @@ mod galleries {
14011366
use matrix_sdk_common::executor::{AbortHandle, JoinHandle};
14021367
use matrix_sdk_ui::timeline::GalleryConfig;
14031368
use mime::Mime;
1369+
use ruma::EventId;
14041370
use tokio::sync::Mutex;
14051371
use tracing::error;
14061372

14071373
use crate::{
14081374
error::RoomError,
14091375
ruma::{AudioInfo, FileInfo, FormattedBody, ImageInfo, Mentions, VideoInfo},
14101376
runtime::get_runtime_handle,
1411-
timeline::{build_thumbnail_info, ReplyParameters, Timeline},
1377+
timeline::{build_thumbnail_info, Timeline},
14121378
};
14131379

14141380
#[derive(uniffi::Record)]
@@ -1419,8 +1385,8 @@ mod galleries {
14191385
formatted_caption: Option<FormattedBody>,
14201386
/// Optional intentional mentions to be sent with the gallery.
14211387
mentions: Option<Mentions>,
1422-
/// Optional parameters for sending the media as (threaded) reply.
1423-
reply_params: Option<ReplyParameters>,
1388+
/// Optional Event ID to reply to.
1389+
in_reply_to: Option<String>,
14241390
}
14251391

14261392
#[derive(uniffi::Enum)]
@@ -1605,11 +1571,18 @@ mod galleries {
16051571
params.formatted_caption.map(Into::into),
16061572
);
16071573

1574+
let in_reply_to = params
1575+
.in_reply_to
1576+
.as_ref()
1577+
.map(|event_id| EventId::parse(event_id))
1578+
.transpose()
1579+
.map_err(|_| RoomError::InvalidRepliedToEventId)?;
1580+
16081581
let mut gallery_config = GalleryConfig::new()
16091582
.caption(params.caption)
16101583
.formatted_caption(formatted_caption)
16111584
.mentions(params.mentions.map(Into::into))
1612-
.reply(params.reply_params.map(|p| p.try_into()).transpose()?);
1585+
.in_reply_to(in_reply_to);
16131586

16141587
for item_info in item_infos {
16151588
gallery_config = gallery_config.add_item(item_info.try_into()?);

crates/matrix-sdk-ui/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ All notable changes to this project will be documented in this file.
88

99
### Features
1010

11+
- [**breaking**] [`Timeline::send_gallery()`] now automatically fills in the thread relationship,
12+
based on the timeline focus. As a result, the `GalleryConfig::reply()` builder method has been
13+
replaced with `GalleryConfig::in_reply_to`, and only takes an optional event id (the event that is
14+
effectively replied to) instead of the `Reply` type. The proper way to start a thread with a
15+
gallery event is now thus to create a threaded-focused timeline, and then use
16+
`Timeline::send_gallery()`.
17+
([5427](https://github.com/matrix-org/matrix-rust-sdk/pull/5427))
1118
- [**breaking**] [`Timeline::send_attachment()`] now automatically fills in the thread
1219
relationship, based on the timeline focus. As a result, there's a new
1320
`matrix_sdk_ui::timeline::AttachmentConfig` type in town, that has a simplified optional parameter

crates/matrix-sdk-ui/src/timeline/futures.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,30 @@ mod galleries {
141141
let Self { timeline, gallery, tracing_span } = self;
142142

143143
let fut = async move {
144-
let send_queue = timeline.room().send_queue();
145-
let fut = send_queue.send_gallery(gallery.try_into()?);
146-
fut.await.map_err(|_| Error::FailedSendingAttachment)?;
144+
let reply = timeline.infer_reply(gallery.in_reply_to).await;
145+
146+
let mut config = matrix_sdk::attachment::GalleryConfig::new();
147+
148+
if let Some(txn_id) = gallery.txn_id {
149+
config = config.txn_id(txn_id);
150+
}
151+
152+
for item in gallery.items {
153+
config = config.add_item(item.try_into()?);
154+
}
155+
156+
config = config
157+
.caption(gallery.caption)
158+
.formatted_caption(gallery.formatted_caption)
159+
.mentions(gallery.mentions)
160+
.reply(reply);
161+
162+
timeline
163+
.room()
164+
.send_queue()
165+
.send_gallery(config)
166+
.await
167+
.map_err(|_| Error::FailedSendingAttachment)?;
147168

148169
Ok(())
149170
};

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

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -958,7 +958,7 @@ pub struct GalleryConfig {
958958
pub(crate) caption: Option<String>,
959959
pub(crate) formatted_caption: Option<FormattedBody>,
960960
pub(crate) mentions: Option<Mentions>,
961-
pub(crate) reply: Option<Reply>,
961+
pub(crate) in_reply_to: Option<OwnedEventId>,
962962
}
963963

964964
#[cfg(feature = "unstable-msc4274")]
@@ -1026,9 +1026,9 @@ impl GalleryConfig {
10261026
///
10271027
/// # Arguments
10281028
///
1029-
/// * `reply` - The reply information of the message.
1030-
pub fn reply(mut self, reply: Option<Reply>) -> Self {
1031-
self.reply = reply;
1029+
/// * `event_id` - The event ID to reply to.
1030+
pub fn in_reply_to(mut self, event_id: Option<OwnedEventId>) -> Self {
1031+
self.in_reply_to = event_id;
10321032
self
10331033
}
10341034

@@ -1043,30 +1043,6 @@ impl GalleryConfig {
10431043
}
10441044
}
10451045

1046-
#[cfg(feature = "unstable-msc4274")]
1047-
impl TryFrom<GalleryConfig> for matrix_sdk::attachment::GalleryConfig {
1048-
type Error = Error;
1049-
1050-
fn try_from(value: GalleryConfig) -> Result<Self, Self::Error> {
1051-
let mut config = matrix_sdk::attachment::GalleryConfig::new();
1052-
1053-
if let Some(txn_id) = value.txn_id {
1054-
config = config.txn_id(txn_id);
1055-
}
1056-
1057-
for item in value.items {
1058-
config = config.add_item(item.try_into()?);
1059-
}
1060-
1061-
config = config.caption(value.caption);
1062-
config = config.formatted_caption(value.formatted_caption);
1063-
config = config.mentions(value.mentions);
1064-
config = config.reply(value.reply);
1065-
1066-
Ok(config)
1067-
}
1068-
}
1069-
10701046
#[cfg(feature = "unstable-msc4274")]
10711047
#[derive(Debug)]
10721048
/// Metadata for a gallery item

0 commit comments

Comments
 (0)