Skip to content

Commit c4d9ec9

Browse files
authored
feat!(ffi): merge send_reply and send_thread_reply (matrix-org#4880)
This pushes down the `Reply` struct to be provided when sending a reply, and merges the `send_reply` and `send_thread_reply` FFI functions. This is a small follow-up on https://github.com/matrix-org/matrix-rust-sdk/pull/4852/files#r2016594024. Signed-off-by: Johannes Marbach <n0-0ne+github@mailbox.org>
1 parent dccd836 commit c4d9ec9

File tree

12 files changed

+118
-132
lines changed

12 files changed

+118
-132
lines changed

bindings/matrix-sdk-ffi/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ Breaking changes:
6565
- `ClientBuilder::passphrase` is renamed `session_passphrase`
6666
([#4870](https://github.com/matrix-org/matrix-rust-sdk/pull/4870/))
6767

68+
- Merge `Timeline::send_thread_reply` into `Timeline::send_reply`. This
69+
changes the parameters of `send_reply` which now requires passing the
70+
event ID (and thread reply behaviour) inside a `ReplyParameters` struct.
71+
([#4880](https://github.com/matrix-org/matrix-rust-sdk/pull/4880/))
72+
6873
Additions:
6974

7075
- Add `Encryption::get_user_identity` which returns `UserIdentity`

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

Lines changed: 28 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,14 @@ use futures_util::{pin_mut, StreamExt as _};
2323
use matrix_sdk::{
2424
attachment::{
2525
AttachmentConfig, AttachmentInfo, BaseAudioInfo, BaseFileInfo, BaseImageInfo,
26-
BaseVideoInfo, Reply, Thumbnail,
26+
BaseVideoInfo, Thumbnail,
2727
},
2828
deserialized_responses::{ShieldState as SdkShieldState, ShieldStateCode},
2929
event_cache::RoomPaginationStatus,
30-
room::{edit::EditedContent as SdkEditedContent, reply::EnforceThread},
30+
room::{
31+
edit::EditedContent as SdkEditedContent,
32+
reply::{EnforceThread, Reply},
33+
},
3134
};
3235
use matrix_sdk_ui::timeline::{
3336
self, EventItemOrigin, Profile, RepliedToEvent, TimelineDetails,
@@ -116,31 +119,13 @@ impl Timeline {
116119
params.formatted_caption.map(Into::into),
117120
);
118121

119-
let reply = if let Some(reply_params) = params.reply_params {
120-
let event_id = EventId::parse(reply_params.event_id)
121-
.map_err(|_| RoomError::InvalidRepliedToEventId)?;
122-
let enforce_thread = if reply_params.enforce_thread {
123-
EnforceThread::Threaded(if reply_params.reply_within_thread {
124-
ReplyWithinThread::Yes
125-
} else {
126-
ReplyWithinThread::No
127-
})
128-
} else {
129-
EnforceThread::MaybeThreaded
130-
};
131-
132-
Some(Reply { event_id, enforce_thread })
133-
} else {
134-
None
135-
};
136-
137122
let attachment_config = AttachmentConfig::new()
138123
.thumbnail(thumbnail)
139124
.info(attachment_info)
140125
.caption(params.caption)
141126
.formatted_caption(formatted_caption)
142127
.mentions(params.mentions.map(Into::into))
143-
.reply(reply);
128+
.reply(params.reply_params.map(|p| p.try_into()).transpose()?);
144129

145130
let handle = SendAttachmentJoinHandle::new(get_runtime_handle().spawn(async move {
146131
let mut request =
@@ -241,6 +226,26 @@ pub struct ReplyParameters {
241226
reply_within_thread: bool,
242227
}
243228

229+
impl TryInto<Reply> for ReplyParameters {
230+
type Error = RoomError;
231+
232+
fn try_into(self) -> Result<Reply, Self::Error> {
233+
let event_id =
234+
EventId::parse(&self.event_id).map_err(|_| RoomError::InvalidRepliedToEventId)?;
235+
let enforce_thread = if self.enforce_thread {
236+
EnforceThread::Threaded(if self.reply_within_thread {
237+
ReplyWithinThread::Yes
238+
} else {
239+
ReplyWithinThread::No
240+
})
241+
} else {
242+
EnforceThread::MaybeThreaded
243+
};
244+
245+
Ok(Reply { event_id, enforce_thread })
246+
}
247+
}
248+
244249
#[matrix_sdk_ffi_macros::export]
245250
impl Timeline {
246251
pub async fn add_listener(&self, listener: Box<dyn TimelineListener>) -> Arc<TaskHandle> {
@@ -502,45 +507,10 @@ impl Timeline {
502507
pub async fn send_reply(
503508
&self,
504509
msg: Arc<RoomMessageEventContentWithoutRelation>,
505-
event_id: String,
510+
reply_params: ReplyParameters,
506511
) -> Result<(), ClientError> {
507-
let event_id = EventId::parse(event_id)?;
508-
self.inner
509-
.send_reply((*msg).clone(), event_id, EnforceThread::MaybeThreaded)
510-
.await
511-
.map_err(|err| anyhow::anyhow!(err))?;
512-
Ok(())
513-
}
514-
515-
/// Send a message on a thread.
516-
///
517-
/// If the replied to event does not have a thread relation, it becomes the
518-
/// root of a new thread.
519-
///
520-
/// # Arguments
521-
///
522-
/// * `msg` - Message content to send
523-
///
524-
/// * `event_id` - ID of the event to reply to
525-
///
526-
/// * `is_reply` - Whether the message is a reply on a thread
527-
pub async fn send_thread_reply(
528-
&self,
529-
msg: Arc<RoomMessageEventContentWithoutRelation>,
530-
event_id: String,
531-
is_reply: bool,
532-
) -> Result<(), ClientError> {
533-
let event_id = EventId::parse(event_id)?;
534512
self.inner
535-
.send_reply(
536-
(*msg).clone(),
537-
event_id,
538-
EnforceThread::Threaded(if is_reply {
539-
ReplyWithinThread::Yes
540-
} else {
541-
ReplyWithinThread::No
542-
}),
543-
)
513+
.send_reply((*msg).clone(), reply_params.try_into()?)
544514
.await
545515
.map_err(|err| anyhow::anyhow!(err))?;
546516
Ok(())

crates/matrix-sdk-ui/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ All notable changes to this project will be documented in this file.
2828
from an [`EventTimelineItem`] by calling `.content().reactions()`. They're also returned by
2929
ownership (cloned) instead of by reference.
3030
([#4576](https://github.com/matrix-org/matrix-rust-sdk/pull/4576))
31+
- [**breaking**] The parameters `event_id` and `enforce_thread` on [`Timeline::send_reply()`]
32+
have been wrapped in a `reply` struct parameter.
33+
([#4880](https://github.com/matrix-org/matrix-rust-sdk/pull/4880/))
3134

3235
## [0.10.0] - 2025-02-04
3336

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use matrix_sdk::{
2828
event_cache::{EventCacheDropHandles, RoomEventCache},
2929
event_handler::EventHandlerHandle,
3030
executor::JoinHandle,
31-
room::{edit::EditedContent, reply::EnforceThread, Receipts, Room},
31+
room::{edit::EditedContent, reply::Reply, Receipts, Room},
3232
send_queue::{RoomSendQueueError, SendHandle},
3333
Client, Result,
3434
};
@@ -278,10 +278,9 @@ impl Timeline {
278278
pub async fn send_reply(
279279
&self,
280280
content: RoomMessageEventContentWithoutRelation,
281-
event_id: OwnedEventId,
282-
enforce_thread: EnforceThread,
281+
reply: Reply,
283282
) -> Result<(), Error> {
284-
let content = self.room().make_reply_event(content, &event_id, enforce_thread).await?;
283+
let content = self.room().make_reply_event(content, reply).await?;
285284
self.send(content.into()).await?;
286285
Ok(())
287286
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ use eyeball_im::VectorDiff;
2020
use futures_util::{FutureExt, StreamExt};
2121
use matrix_sdk::{
2222
assert_let_timeout,
23-
attachment::{AttachmentConfig, Reply},
24-
room::reply::EnforceThread,
23+
attachment::AttachmentConfig,
24+
room::reply::{EnforceThread, Reply},
2525
test_utils::mocks::MatrixMockServer,
2626
};
2727
use matrix_sdk_test::{async_test, event_factory::EventFactory, JoinedRoomBuilder, ALICE};

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

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ use assert_matches::assert_matches;
44
use assert_matches2::assert_let;
55
use eyeball_im::VectorDiff;
66
use futures_util::StreamExt;
7-
use matrix_sdk::{room::reply::EnforceThread, test_utils::mocks::MatrixMockServer};
7+
use matrix_sdk::{
8+
room::reply::{EnforceThread, Reply},
9+
test_utils::mocks::MatrixMockServer,
10+
};
811
use matrix_sdk_base::timeout::timeout;
912
use matrix_sdk_test::{
1013
async_test, event_factory::EventFactory, JoinedRoomBuilder, ALICE, BOB, CAROL,
@@ -692,8 +695,10 @@ async fn test_send_reply() {
692695
timeline
693696
.send_reply(
694697
RoomMessageEventContentWithoutRelation::text_plain("Replying to Bob"),
695-
event_id_from_bob.to_owned(),
696-
EnforceThread::MaybeThreaded,
698+
Reply {
699+
event_id: event_id_from_bob.into(),
700+
enforce_thread: EnforceThread::MaybeThreaded,
701+
},
697702
)
698703
.await
699704
.unwrap();
@@ -792,8 +797,10 @@ async fn test_send_reply_to_self() {
792797
timeline
793798
.send_reply(
794799
RoomMessageEventContentWithoutRelation::text_plain("Replying to self"),
795-
event_id_from_self.to_owned(),
796-
EnforceThread::MaybeThreaded,
800+
Reply {
801+
event_id: event_id_from_self.into(),
802+
enforce_thread: EnforceThread::MaybeThreaded,
803+
},
797804
)
798805
.await
799806
.unwrap();
@@ -858,8 +865,7 @@ async fn test_send_reply_to_threaded() {
858865
timeline
859866
.send_reply(
860867
RoomMessageEventContentWithoutRelation::text_plain("Hello, Bob!"),
861-
event_id_1.to_owned(),
862-
EnforceThread::MaybeThreaded,
868+
Reply { event_id: event_id_1.into(), enforce_thread: EnforceThread::MaybeThreaded },
863869
)
864870
.await
865871
.unwrap();
@@ -961,8 +967,10 @@ async fn test_send_reply_with_event_id() {
961967
timeline
962968
.send_reply(
963969
RoomMessageEventContentWithoutRelation::text_plain("Replying to Bob"),
964-
event_id_from_bob.to_owned(),
965-
EnforceThread::MaybeThreaded,
970+
Reply {
971+
event_id: event_id_from_bob.into(),
972+
enforce_thread: EnforceThread::MaybeThreaded,
973+
},
966974
)
967975
.await
968976
.unwrap();
@@ -1047,8 +1055,10 @@ async fn test_send_reply_enforce_thread() {
10471055
timeline
10481056
.send_reply(
10491057
RoomMessageEventContentWithoutRelation::text_plain("Replying to Bob"),
1050-
event_id_from_bob.to_owned(),
1051-
EnforceThread::Threaded(ReplyWithinThread::No),
1058+
Reply {
1059+
event_id: event_id_from_bob.into(),
1060+
enforce_thread: EnforceThread::Threaded(ReplyWithinThread::No),
1061+
},
10521062
)
10531063
.await
10541064
.unwrap();
@@ -1144,8 +1154,10 @@ async fn test_send_reply_enforce_thread_is_reply() {
11441154
timeline
11451155
.send_reply(
11461156
RoomMessageEventContentWithoutRelation::text_plain("Replying to Bob"),
1147-
event_id_from_bob.to_owned(),
1148-
EnforceThread::Threaded(ReplyWithinThread::Yes),
1157+
Reply {
1158+
event_id: event_id_from_bob.into(),
1159+
enforce_thread: EnforceThread::Threaded(ReplyWithinThread::Yes),
1160+
},
11491161
)
11501162
.await
11511163
.unwrap();
@@ -1237,8 +1249,10 @@ async fn test_send_reply_with_event_id_that_is_redacted() {
12371249
timeline
12381250
.send_reply(
12391251
RoomMessageEventContentWithoutRelation::text_plain("Replying to Bob"),
1240-
redacted_event_id_from_bob.to_owned(),
1241-
EnforceThread::MaybeThreaded,
1252+
Reply {
1253+
event_id: redacted_event_id_from_bob.into(),
1254+
enforce_thread: EnforceThread::MaybeThreaded,
1255+
},
12421256
)
12431257
.await
12441258
.unwrap();

crates/matrix-sdk/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,9 @@ simpler methods:
235235
([#4831](https://github.com/matrix-org/matrix-rust-sdk/pull/4831))
236236
- [**breaking**] `Client::store` is renamed `state_store`
237237
([#4851](https://github.com/matrix-org/matrix-rust-sdk/pull/4851))
238+
- [**breaking**] The parameters `event_id` and `enforce_thread` on [`Room::make_reply_event()`]
239+
have been wrapped in a `reply` struct parameter.
240+
([#4880](https://github.com/matrix-org/matrix-rust-sdk/pull/4880/))
238241

239242
## [0.10.0] - 2025-02-04
240243

crates/matrix-sdk/src/attachment.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ use ruma::{
2525
},
2626
Mentions,
2727
},
28-
OwnedEventId, OwnedTransactionId, TransactionId, UInt,
28+
OwnedTransactionId, TransactionId, UInt,
2929
};
3030

31-
use crate::room::reply::EnforceThread;
31+
use crate::room::reply::Reply;
3232

3333
/// Base metadata about an image.
3434
#[derive(Debug, Clone, Default)]
@@ -181,15 +181,6 @@ impl Thumbnail {
181181
}
182182
}
183183

184-
/// Information needed to reply to an event.
185-
#[derive(Debug)]
186-
pub struct Reply {
187-
/// The event ID of the event to reply to.
188-
pub event_id: OwnedEventId,
189-
/// Whether to enforce a thread relation.
190-
pub enforce_thread: EnforceThread,
191-
}
192-
193184
/// Configuration for sending an attachment.
194185
#[derive(Debug, Default)]
195186
pub struct AttachmentConfig {

crates/matrix-sdk/src/room/mod.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ use matrix_sdk_common::{
5858
timeout::timeout,
5959
};
6060
use mime::Mime;
61+
use reply::Reply;
6162
#[cfg(feature = "e2e-encryption")]
6263
use ruma::events::{
6364
room::encrypted::OriginalSyncRoomEncryptedEvent, AnySyncMessageLikeEvent, AnySyncTimelineEvent,
@@ -138,7 +139,7 @@ pub use self::{
138139
#[cfg(doc)]
139140
use crate::event_cache::EventCache;
140141
use crate::{
141-
attachment::{AttachmentConfig, AttachmentInfo, Reply},
142+
attachment::{AttachmentConfig, AttachmentInfo},
142143
client::WeakClient,
143144
config::RequestConfig,
144145
error::{BeaconError, WrongRoomState},
@@ -2272,9 +2273,7 @@ impl Room {
22722273
if let Some(reply) = reply {
22732274
// Since we just created the event, there is no relation attached to it. Thus,
22742275
// it is safe to add the reply relation without overriding anything.
2275-
content = self
2276-
.make_reply_event(content.into(), &reply.event_id, reply.enforce_thread)
2277-
.await?;
2276+
content = self.make_reply_event(content.into(), reply).await?;
22782277
}
22792278
Ok(content)
22802279
}

0 commit comments

Comments
 (0)