From ad5541acc6e25ff64d2115c52f979199dc22c75a Mon Sep 17 00:00:00 2001 From: Colin Reeder Date: Sat, 18 Dec 2021 11:07:02 -0700 Subject: [PATCH] Post replies to matrix --- bridge/matrix/matrix.go | 45 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/bridge/matrix/matrix.go b/bridge/matrix/matrix.go index e89002b2..01a2379c 100644 --- a/bridge/matrix/matrix.go +++ b/bridge/matrix/matrix.go @@ -65,6 +65,19 @@ 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) @@ -275,6 +288,38 @@ 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