Thumbnail

rani/matterbridge.git

Clone URL: https://git.buni.party/rani/matterbridge.git

commit 5bae8f760ba3e9598e10724aba6d099e6ad2e174 Author: vpzomtrrfrt <colin@vpzom.click> Date: Sun Jan 09 14:46:59 2022 +0000 Reply support for Matrix (#1664) * Post replies to matrix * Handle replies from matrix * Include protocol in canonical ID return * fmt diff --git a/bridge/matrix/matrix.go b/bridge/matrix/matrix.go index e89002b..739a792 100644 --- a/bridge/matrix/matrix.go +++ b/bridge/matrix/matrix.go @@ -656 +6519 @@ type EditedMessage struct {   matrix.TextMessage  }   +type InReplyToRelationContent struct { + EventID string `json:"event_id"` +} + +type InReplyToRelation struct { + InReplyTo InReplyToRelationContent `json:"m.in_reply_to"` +} + +type ReplyMessage struct { + RelatedTo InReplyToRelation `json:"m.relates_to"` + matrix.TextMessage +} +  func New(cfg *bridge.Config) bridge.Bridger {   b := &Bmatrix{Config: cfg}   b.RoomMap = make(map[string]string) @@ -2756 +28838 @@ func (b *Bmatrix) Send(msg config.Message) (string, error) {   return resp.EventID, err   }   + if msg.ParentValid() { + m := ReplyMessage{ + TextMessage: matrix.TextMessage{ + MsgType: "m.text", + Body: username.plain + msg.Text, + FormattedBody: username.formatted + helper.ParseMarkdown(msg.Text), + }, + } + + m.RelatedTo = InReplyToRelation{ + InReplyTo: InReplyToRelationContent{ + EventID: msg.ParentID, + }, + } + + var ( + resp *matrix.RespSendEvent + err error + ) + + err = b.retry(func() error { + resp, err = b.mc.SendMessageEvent(channel, "m.room.message", m) + + return err + }) + if err != nil { + return "", err + } + + return resp.EventID, err + } +   // Post normal message with HTML support (eg riot.im)   var (   resp *matrix.RespSendEvent @@ -3416 +38635 @@ 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" { @@ -4036 +47711 @@ 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 1c6c21c..85f5a18 100644 --- a/gateway/gateway.go +++ b/gateway/gateway.go @@ -667 +667 @@ func New(rootLogger *logrus.Logger, cfg *config.Gateway, r *Router) *Gateway {  func (gw *Gateway) FindCanonicalMsgID(protocol string, mID string) string {   ID := protocol + " " + mID   if gw.Messages.Contains(ID) { - return mID + return ID   }     // If not keyed, iterate through cache for downstream, and infer upstream. @@ -757 +757 @@ func (gw *Gateway) FindCanonicalMsgID(protocol string, mID string) string {   ids := v.([]*BrMsgID)   for _, downstreamMsgObj := range ids {   if ID == downstreamMsgObj.ID { - return strings.Replace(mid.(string), protocol+" ", "", 1) + return mid.(string)   }   }   } @@ -4549 +4549 @@ 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