From bb417f19e07684a5c5d6cff834ed8bd9ba8af869 Mon Sep 17 00:00:00 2001 From: Patrick Connolly Date: Thu, 29 Nov 2018 01:59:16 +0800 Subject: [PATCH] Extract thread processing into func. Move message modification to slack bridge. --- bridge/slack/slack.go | 6 ++++++ gateway/gateway.go | 41 +++++++++++++++++++++++------------------ 2 files changed, 29 insertions(+), 18 deletions(-) diff --git a/bridge/slack/slack.go b/bridge/slack/slack.go index a38bbb53..5664522a 100644 --- a/bridge/slack/slack.go +++ b/bridge/slack/slack.go @@ -288,6 +288,12 @@ func (b *Bslack) sendRTM(msg config.Message) (string, error) { return "", err } + // Handle prefix hint for unthreaded messages. + if msg.ParentID == "unthreaded" { + msg.ParentID = "" + msg.Text = fmt.Sprintf("thread reply: %s", msg.Text) + } + // Handle message deletions. if handled, err = b.deleteMessage(&msg, channelInfo); handled { return msg.ID, err diff --git a/gateway/gateway.go b/gateway/gateway.go index 89dd7ae3..b257732a 100644 --- a/gateway/gateway.go +++ b/gateway/gateway.go @@ -41,8 +41,7 @@ type BrMsgID struct { var flog *log.Entry const ( - apiProtocol = "api" - threadReplyPrefix = "thread reply" + apiProtocol = "api" ) func New(cfg config.Gateway, r *Router) *Gateway { @@ -251,16 +250,14 @@ func (gw *Gateway) handleMessage(msg config.Message, dest *bridge.Bridge) []*BrM return brMsgIDs } - // Get the ID of the parent message in thread - var canonicalParentMsgID string - if msg.ParentID != "" && dest.GetBool("PreserveThreading") { - canonicalParentMsgID = gw.FindCanonicalMsgID(msg.Protocol, msg.ParentID) - } - originchannel := msg.Channel origmsg := msg channels := gw.getDestChannel(&msg, *dest) for _, channel := range channels { + if parentID, isThreaded := gw.handleThreading(&msg, dest, channel); isThreaded { + msg.ParentID = parentID + } + // Only send the avatar download event to ourselves. if msg.Event == config.EventAvatarDownload { if channel.ID != getChannelID(origmsg) { @@ -289,16 +286,6 @@ func (gw *Gateway) handleMessage(msg config.Message, dest *bridge.Bridge) []*BrM msg.Channel = originchannel } - // Add prefix if reply message is being unthreaded. - if msg.ParentID != "" && canonicalParentMsgID == "" { - msg.Text = fmt.Sprintf("%s: %s", threadReplyPrefix, msg.Text) - } - - msg.ParentID = gw.getDestMsgID(origmsg.Protocol+" "+canonicalParentMsgID, dest, channel) - if msg.ParentID == "" { - msg.ParentID = canonicalParentMsgID - } - // if we are using mattermost plugin account, send messages to MattermostPlugin channel // that can be picked up by the mattermost matterbridge plugin if dest.Account == "mattermost.plugin" { @@ -444,6 +431,24 @@ func (gw *Gateway) modifyMessage(msg *config.Message) { } } +func (gw *Gateway) handleThreading(msg *config.Message, dest *bridge.Bridge, channel config.ChannelInfo) (string, bool) { + if msg.ParentID == "" { + // Message is not threaded. + return "", false + } + + canonicalParentMsgID := gw.FindCanonicalMsgID(msg.Protocol, msg.ParentID) + if !dest.GetBool("PreserveThreading") || canonicalParentMsgID == "" { + // Mark message as unthreaded, either because disabled or uncached. + return "unthreaded", true + } + + if parentID := gw.getDestMsgID(msg.Protocol+" "+canonicalParentMsgID, dest, channel); parentID != "" { + return parentID, true + } + return canonicalParentMsgID, true +} + // handleFiles uploads or places all files on the given msg to the MediaServer and // adds the new URL of the file on the MediaServer onto the given msg. func (gw *Gateway) handleFiles(msg *config.Message) {