From b4103e9ba8f42d55a08ff48a18c93c7761c1ef02 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Sun, 9 Jul 2023 23:59:03 +0300 Subject: [PATCH] Add support for sending replies --- ROADMAP.md | 2 +- portal.go | 19 +++++++++++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/ROADMAP.md b/ROADMAP.md index 753c2ec..68f6d86 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -3,7 +3,7 @@ * [ ] Message content * [x] Plain text * [ ] Media/files - * [ ] Replies (RCS) + * [x] Replies (RCS) * [ ] Reactions (RCS) * [ ] Typing notifications (RCS) * [ ] Read receipts (RCS) diff --git a/portal.go b/portal.go index 5005198..84efd0c 100644 --- a/portal.go +++ b/portal.go @@ -29,7 +29,7 @@ import ( "github.com/gabriel-vasile/mimetype" "github.com/rs/zerolog" - log "maunium.net/go/maulogger/v2" + "maunium.net/go/maulogger/v2" "maunium.net/go/mautrix" "maunium.net/go/mautrix/appservice" @@ -220,7 +220,7 @@ type Portal struct { bridge *GMBridge // Deprecated: use zerolog - log log.Logger + log maulogger.Logger zlog zerolog.Logger roomCreateLock sync.Mutex @@ -999,6 +999,7 @@ func (portal *Portal) HandleMatrixMessage(sender *User, evt *event.Event, timing ms := metricSender{portal: portal, timings: &timings} log := portal.zlog.With().Str("event_id", evt.ID.String()).Logger() + ctx := log.WithContext(context.TODO()) log.Debug().Dur("age", timings.totalReceive).Msg("Handling Matrix message") content, ok := evt.Content.Parsed.(*event.MessageEventContent) @@ -1006,6 +1007,19 @@ func (portal *Portal) HandleMatrixMessage(sender *User, evt *event.Event, timing return } + var replyToID string + replyToMXID := content.RelatesTo.GetReplyTo() + if replyToMXID != "" { + replyToMsg, err := portal.bridge.DB.Message.GetByMXID(ctx, replyToMXID) + if err != nil { + log.Err(err).Str("reply_to_mxid", replyToMXID.String()).Msg("Failed to get reply target message") + } else if replyToMsg == nil { + log.Warn().Str("reply_to_mxid", replyToMXID.String()).Msg("Reply target message not found") + } else { + replyToID = replyToMsg.ID + } + } + txnID := util.GenerateTmpId() portal.outgoingMessagesLock.Lock() portal.outgoingMessages[txnID] = evt.ID @@ -1020,6 +1034,7 @@ func (portal *Portal) HandleMatrixMessage(sender *User, evt *event.Event, timing sender.Client.NewMessageBuilder(). SetConversationID(portal.ID). SetSelfParticipantID(portal.SelfUserID). + SetReplyMessage(replyToID). SetContent(text). SetTmpID(txnID), )