Commit a7999247c5

a7999247c5548663e2d3261199b6578029a10d45

parent: 52e26bb9e8

Unsigned

Darafei Praliaskouski <me@komzpa.net> · 2026-07-30 17:00 UTC
committer: <strk@kbt.io>

fix: keep replies inside Matrix threads

Layout: unified · split

src/commands/reply.rs +38 −5
@@ -1,7 +1,7 @@
11use clap::{App as Argparse, AppSettings as ArgParseSettings, Arg};
22use matrix_sdk::ruma::{
33 events::{
4 relation::InReplyTo,
4 relation::{InReplyTo, Thread},
55 room::message::{Relation, RoomMessageEventContent},
66 },
77 EventId, OwnedEventId,
@@ -138,8 +138,12 @@ impl ReplyCommand {
138138
139139 fn reply(&self, buffer: &Buffer, event_id: OwnedEventId, message: String) {
140140 if let Some(room) = self.servers.find_room(buffer) {
141 let thread_root = buffer
142 .get_localvar("thread_root")
143 .and_then(|root| EventId::parse(root.as_ref()).ok());
141144 Weechat::spawn(async move {
142 room.send_message(reply_content(event_id, message)).await
145 room.send_message(reply_content(event_id, message, thread_root))
146 .await
143147 })
144148 .detach();
145149 } else {
@@ -173,10 +177,16 @@ impl CommandCallback for ReplyCommand {
173177fn reply_content(
174178 event_id: OwnedEventId,
175179 message: String,
180 thread_root: Option<OwnedEventId>,
176181) -> RoomMessageEventContent {
177182 let mut content = RoomMessageEventContent::text_plain(message);
178 content.relates_to = Some(Relation::Reply {
179 in_reply_to: InReplyTo::new(event_id),
183 content.relates_to = Some(match thread_root {
184 Some(thread_root) => {
185 Relation::Thread(Thread::plain(thread_root, event_id))
186 }
187 None => Relation::Reply {
188 in_reply_to: InReplyTo::new(event_id),
189 },
180190 });
181191 content
182192}
@@ -200,7 +210,8 @@ mod tests {
200210 #[test]
201211 fn reply_content_sets_reply_relation() {
202212 let event_id = owned_event_id!("$replyevent:example.org");
203 let content = reply_content(event_id.clone(), "Thanks".to_owned());
213 let content =
214 reply_content(event_id.clone(), "Thanks".to_owned(), None);
204215
205216 let Some(Relation::Reply { in_reply_to }) = content.relates_to else {
206217 panic!("reply command must create a Matrix reply relation");
@@ -234,4 +245,26 @@ mod tests {
234245
235246 assert_eq!(vec!["-2", "message"], arguments);
236247 }
248
249 #[test]
250 fn reply_content_keeps_thread_relation_in_thread_buffer() {
251 let event_id = owned_event_id!("$replyevent:example.org");
252 let thread_root = owned_event_id!("$threadroot:example.org");
253 let content = reply_content(
254 event_id.clone(),
255 "Thanks".to_owned(),
256 Some(thread_root.clone()),
257 );
258
259 let Some(Relation::Thread(thread)) = content.relates_to else {
260 panic!("reply from a thread buffer must stay in that thread");
261 };
262
263 assert_eq!(thread_root, thread.event_id);
264 assert_eq!(
265 event_id,
266 thread.in_reply_to.expect("fallback reply target").event_id
267 );
268 assert!(thread.is_falling_back);
269 }
237270}