From 79188818e2339019492a902b700248c9e3900dcf Mon Sep 17 00:00:00 2001 From: Colin Reeder Date: Sat, 18 Dec 2021 12:23:54 -0700 Subject: [PATCH] Handle replies from matrix --- bridge/matrix/matrix.go | 34 ++++++++++++++++++++++++++++++++++ gateway/gateway.go | 4 ++-- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/bridge/matrix/matrix.go b/bridge/matrix/matrix.go index 01a2379c..577c89f8 100644 --- a/bridge/matrix/matrix.go +++ b/bridge/matrix/matrix.go @@ -386,6 +386,35 @@ func (b *Bmatrix) handleEdit(ev *matrix.Event, rmsg config.Message) bool { return true } +func (b *Bmatrix) handleReply(ev *matrix.Event, rmsg config.Message) bool { + relationInterface, present := ev.Content["m.relates_to"] + if !present { + return false + } + + var relation InReplyToRelation + if err := interface2Struct(relationInterface, &relation); err != nil { + // probably fine + return false + } + + body := rmsg.Text + for strings.HasPrefix(body, "> ") { + lineIdx := strings.IndexRune(body, '\n') + if lineIdx == -1 { + body = "" + } else { + body = body[(lineIdx + 1):] + } + } + + rmsg.Text = body + rmsg.ParentID = relation.InReplyTo.EventID + b.Remote <- rmsg + + return true +} + func (b *Bmatrix) handleMemberChange(ev *matrix.Event) { // Update the displayname on join messages, according to https://matrix.org/docs/spec/client_server/r0.6.1#events-on-change-of-profile-information if ev.Content["membership"] == "join" { @@ -448,6 +477,11 @@ func (b *Bmatrix) handleEvent(ev *matrix.Event) { return } + // Is it a reply? + if b.handleReply(ev, rmsg) { + return + } + // Do we have attachments if b.containsAttachment(ev.Content) { err := b.handleDownloadFile(&rmsg, ev.Content) diff --git a/gateway/gateway.go b/gateway/gateway.go index 1c6c21c3..7da4defa 100644 --- a/gateway/gateway.go +++ b/gateway/gateway.go @@ -454,9 +454,9 @@ func (gw *Gateway) SendMessage( msg.Channel = rmsg.Channel } - msg.ParentID = gw.getDestMsgID(rmsg.Protocol+" "+canonicalParentMsgID, dest, channel) + msg.ParentID = gw.getDestMsgID(canonicalParentMsgID, dest, channel) if msg.ParentID == "" { - msg.ParentID = canonicalParentMsgID + msg.ParentID = strings.Replace(canonicalParentMsgID, dest.Protocol+" ", "", 1) } // if the parentID is still empty and we have a parentID set in the original message