diff options
author | Patrick Connolly <patrick.c.connolly@gmail.com> | 2018-11-07 16:14:31 +0800 |
---|---|---|
committer | Wim <wim@42.be> | 2018-11-07 09:14:31 +0100 |
commit | a20b7895a9414882934d0160daefd5e6ef888a24 (patch) | |
tree | 0298ccedefe4a283f376f42153dc95318b91dcdd /gateway/gateway.go | |
parent | 5666821e7bc02d5b3b5afb21355be911ff32bfbc (diff) | |
download | matterbridge-msglm-a20b7895a9414882934d0160daefd5e6ef888a24.tar.gz matterbridge-msglm-a20b7895a9414882934d0160daefd5e6ef888a24.tar.bz2 matterbridge-msglm-a20b7895a9414882934d0160daefd5e6ef888a24.zip |
Preserve threading between Slack instances (#529)
* Opportunistically preserve Slack threading when parent thread in cache. [#529]
* Removed slack-specific processing from gateway.
* Added docs.
* Add option to enable threading, with default to off.
* Did cleanup on @42wim's comments.
* Update gateway/gateway.go
Co-Authored-By: patcon <patrick.c.connolly@gmail.com>
* Suggestion from @42wim :)
* Suggestions from @42wim.
* More suggestions.
Diffstat (limited to 'gateway/gateway.go')
-rw-r--r-- | gateway/gateway.go | 62 |
1 files changed, 51 insertions, 11 deletions
diff --git a/gateway/gateway.go b/gateway/gateway.go index 5bfb00e2..c1905a9b 100644 --- a/gateway/gateway.go +++ b/gateway/gateway.go @@ -83,6 +83,25 @@ func New(cfg config.Gateway, r *Router) *Gateway { return gw } +// Find the canonical ID that the message is keyed under in cache +func (gw *Gateway) FindCanonicalMsgID(mID string) string { + if gw.Messages.Contains(mID) { + return mID + } + + // If not keyed, iterate through cache for downstream, and infer upstream. + for _, mid := range gw.Messages.Keys() { + v, _ := gw.Messages.Peek(mid) + ids := v.([]*BrMsgID) + for _, downstreamMsgObj := range ids { + if mID == downstreamMsgObj.ID { + return mid.(string) + } + } + } + return "" +} + func (gw *Gateway) AddBridge(cfg *config.Bridge) error { br := gw.Router.getBridge(cfg.Account) if br == nil { @@ -206,6 +225,20 @@ func (gw *Gateway) getDestChannel(msg *config.Message, dest bridge.Bridge) []con return channels } +func (gw *Gateway) getDestMsgID(msgID string, dest *bridge.Bridge, channel config.ChannelInfo) string { + if res, ok := gw.Messages.Get(msgID); ok { + IDs := res.([]*BrMsgID) + for _, id := range IDs { + // check protocol, bridge name and channelname + // for people that reuse the same bridge multiple times. see #342 + if dest.Protocol == id.br.Protocol && dest.Name == id.br.Name && channel.ID == id.ChannelID { + return id.ID + } + } + } + return "" +} + func (gw *Gateway) handleMessage(msg config.Message, dest *bridge.Bridge) []*BrMsgID { var brMsgIDs []*BrMsgID @@ -242,6 +275,13 @@ 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 != "" && (gw.Config.General.PreserveThreading || dest.GetBool("PreserveThreading")) { + thisParentMsgID := dest.Protocol + " " + msg.ParentID + canonicalParentMsgID = gw.FindCanonicalMsgID(thisParentMsgID) + } + originchannel := msg.Channel origmsg := msg channels := gw.getDestChannel(&msg, *dest) @@ -258,28 +298,28 @@ func (gw *Gateway) handleMessage(msg config.Message, dest *bridge.Bridge) []*BrM } } flog.Debugf("=> Sending %#v from %s (%s) to %s (%s)", msg, msg.Account, originchannel, dest.Account, channel.Name) + msg.Channel = channel.Name msg.Avatar = gw.modifyAvatar(origmsg, dest) msg.Username = gw.modifyUsername(origmsg, dest) - msg.ID = "" - if res, ok := gw.Messages.Get(origmsg.ID); ok { - IDs := res.([]*BrMsgID) - for _, id := range IDs { - // check protocol, bridge name and channelname - // for people that reuse the same bridge multiple times. see #342 - if dest.Protocol == id.br.Protocol && dest.Name == id.br.Name && channel.ID == id.ChannelID { - msg.ID = id.ID - } - } - } + + msg.ID = gw.getDestMsgID(origmsg.ID, dest, channel) + // for api we need originchannel as channel if dest.Protocol == "api" { msg.Channel = originchannel } + + msg.ParentID = gw.getDestMsgID(canonicalParentMsgID, dest, channel) + if msg.ParentID == "" { + msg.ParentID = canonicalParentMsgID + } + mID, err := dest.Send(msg) if err != nil { flog.Error(err) } + // append the message ID (mID) from this bridge (dest) to our brMsgIDs slice if mID != "" { flog.Debugf("mID %s: %s", dest.Account, mID) |