From ae32bae791e6276b4404d6cb19993c3acf543566 Mon Sep 17 00:00:00 2001
From: Wim <wim@42.be>
Date: Mon, 19 Nov 2018 21:28:23 +0100
Subject: [PATCH] Add protocol to msg.ID in cache (#596)

---
 bridge/slack/handlers.go |  2 +-
 bridge/slack/helpers.go  |  5 +++--
 bridge/slack/slack.go    | 14 ++++----------
 gateway/gateway.go       | 20 ++++++++++----------
 gateway/router.go        |  4 ++--
 5 files changed, 20 insertions(+), 25 deletions(-)

diff --git a/bridge/slack/handlers.go b/bridge/slack/handlers.go
index d4e5e464..a3291c38 100644
--- a/bridge/slack/handlers.go
+++ b/bridge/slack/handlers.go
@@ -205,7 +205,7 @@ func (b *Bslack) handleStatusEvent(ev *slack.MessageEvent, rmsg *config.Message)
 	case sMessageDeleted:
 		rmsg.Text = config.EventMsgDelete
 		rmsg.Event = config.EventMsgDelete
-		rmsg.ID = "slack " + ev.DeletedTimestamp
+		rmsg.ID = ev.DeletedTimestamp
 		// If a message is being deleted we do not need to process
 		// the event any further so we return 'true'.
 		return true
diff --git a/bridge/slack/helpers.go b/bridge/slack/helpers.go
index 861a8758..b0fdaba1 100644
--- a/bridge/slack/helpers.go
+++ b/bridge/slack/helpers.go
@@ -173,9 +173,10 @@ func (b *Bslack) populateReceivedMessage(ev *slack.MessageEvent) (*config.Messag
 		Text:     ev.Text,
 		Channel:  channel.Name,
 		Account:  b.Account,
-		ID:       "slack " + ev.Timestamp,
+		ID:       ev.Timestamp,
 		Extra:    make(map[string][]interface{}),
 		ParentID: ev.ThreadTimestamp,
+		Protocol: b.Protocol,
 	}
 	if b.useChannelID {
 		rmsg.Channel = "ID:" + channel.ID
@@ -183,7 +184,7 @@ func (b *Bslack) populateReceivedMessage(ev *slack.MessageEvent) (*config.Messag
 
 	// Handle 'edit' messages.
 	if ev.SubMessage != nil && !b.GetBool(editDisableConfig) {
-		rmsg.ID = "slack " + ev.SubMessage.Timestamp
+		rmsg.ID = ev.SubMessage.Timestamp
 		if ev.SubMessage.ThreadTimestamp != ev.SubMessage.Timestamp {
 			b.Log.Debugf("SubMessage %#v", ev.SubMessage)
 			rmsg.Text = ev.SubMessage.Text + b.GetString(editSuffixConfig)
diff --git a/bridge/slack/slack.go b/bridge/slack/slack.go
index c4689cbc..0951fb10 100644
--- a/bridge/slack/slack.go
+++ b/bridge/slack/slack.go
@@ -324,10 +324,8 @@ func (b *Bslack) deleteMessage(msg *config.Message, channelInfo *slack.Channel)
 		return true, nil
 	}
 
-	// If we get a "slack <ID>", split it.
-	ts := strings.Fields(msg.ID)
 	for {
-		_, _, err := b.rtm.DeleteMessage(channelInfo.ID, ts[1])
+		_, _, err := b.rtm.DeleteMessage(channelInfo.ID, msg.ID)
 		if err == nil {
 			return true, nil
 		}
@@ -344,9 +342,8 @@ func (b *Bslack) editMessage(msg *config.Message, channelInfo *slack.Channel) (b
 		return false, nil
 	}
 
-	ts := strings.Fields(msg.ID)
 	for {
-		_, _, _, err := b.rtm.UpdateMessage(channelInfo.ID, ts[1], msg.Text)
+		_, _, _, err := b.rtm.UpdateMessage(channelInfo.ID, msg.ID, msg.Text)
 		if err == nil {
 			return true, nil
 		}
@@ -362,7 +359,7 @@ func (b *Bslack) postMessage(msg *config.Message, messageParameters *slack.PostM
 	for {
 		_, id, err := b.rtm.PostMessage(channelInfo.ID, msg.Text, *messageParameters)
 		if err == nil {
-			return "slack " + id, nil
+			return id, nil
 		}
 
 		if err = b.handleRateLimit(err); err != nil {
@@ -413,10 +410,7 @@ func (b *Bslack) prepareMessageParameters(msg *config.Message) *slack.PostMessag
 	params.Username = msg.Username
 	params.LinkNames = 1 // replace mentions
 	params.IconURL = config.GetIconURL(msg, b.GetString(iconURLConfig))
-	msgFields := strings.Fields(msg.ParentID)
-	if len(msgFields) >= 2 {
-		params.ThreadTimestamp = msgFields[1]
-	}
+	params.ThreadTimestamp = msg.ParentID
 	if msg.Avatar != "" {
 		params.IconURL = msg.Avatar
 	}
diff --git a/gateway/gateway.go b/gateway/gateway.go
index d723d854..72a0f6a6 100644
--- a/gateway/gateway.go
+++ b/gateway/gateway.go
@@ -85,8 +85,9 @@ func New(cfg config.Gateway, r *Router) *Gateway {
 }
 
 // Find the canonical ID that the message is keyed under in cache
-func (gw *Gateway) FindCanonicalMsgID(mID string) string {
-	if gw.Messages.Contains(mID) {
+func (gw *Gateway) FindCanonicalMsgID(protocol string, mID string) string {
+	ID := protocol + " " + mID
+	if gw.Messages.Contains(ID) {
 		return mID
 	}
 
@@ -95,8 +96,8 @@ func (gw *Gateway) FindCanonicalMsgID(mID string) string {
 		v, _ := gw.Messages.Peek(mid)
 		ids := v.([]*BrMsgID)
 		for _, downstreamMsgObj := range ids {
-			if mID == downstreamMsgObj.ID {
-				return mid.(string)
+			if ID == downstreamMsgObj.ID {
+				return strings.Replace(mid.(string), protocol+" ", "", 1)
 			}
 		}
 	}
@@ -234,7 +235,7 @@ func (gw *Gateway) getDestMsgID(msgID string, dest *bridge.Bridge, channel confi
 			// 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 strings.Replace(id.ID, dest.Protocol+" ", "", 1)
 			}
 		}
 	}
@@ -280,8 +281,7 @@ func (gw *Gateway) handleMessage(msg config.Message, dest *bridge.Bridge) []*BrM
 	// Get the ID of the parent message in thread
 	var canonicalParentMsgID string
 	if msg.ParentID != "" && (gw.BridgeValues().General.PreserveThreading || dest.GetBool("PreserveThreading")) {
-		thisParentMsgID := dest.Protocol + " " + msg.ParentID
-		canonicalParentMsgID = gw.FindCanonicalMsgID(thisParentMsgID)
+		canonicalParentMsgID = gw.FindCanonicalMsgID(msg.Protocol, msg.ParentID)
 	}
 
 	originchannel := msg.Channel
@@ -309,14 +309,14 @@ func (gw *Gateway) handleMessage(msg config.Message, dest *bridge.Bridge) []*BrM
 		msg.Avatar = gw.modifyAvatar(origmsg, dest)
 		msg.Username = gw.modifyUsername(origmsg, dest)
 
-		msg.ID = gw.getDestMsgID(origmsg.ID, dest, channel)
+		msg.ID = gw.getDestMsgID(origmsg.Protocol+" "+origmsg.ID, dest, channel)
 
 		// for api we need originchannel as channel
 		if dest.Protocol == apiProtocol {
 			msg.Channel = originchannel
 		}
 
-		msg.ParentID = gw.getDestMsgID(canonicalParentMsgID, dest, channel)
+		msg.ParentID = gw.getDestMsgID(origmsg.Protocol+" "+canonicalParentMsgID, dest, channel)
 		if msg.ParentID == "" {
 			msg.ParentID = canonicalParentMsgID
 		}
@@ -335,7 +335,7 @@ func (gw *Gateway) handleMessage(msg config.Message, dest *bridge.Bridge) []*BrM
 		// append the message ID (mID) from this bridge (dest) to our brMsgIDs slice
 		if mID != "" {
 			flog.Debugf("mID %s: %s", dest.Account, mID)
-			brMsgIDs = append(brMsgIDs, &BrMsgID{dest, mID, channel.ID})
+			brMsgIDs = append(brMsgIDs, &BrMsgID{dest, dest.Protocol + " " + mID, channel.ID})
 		}
 	}
 	return brMsgIDs
diff --git a/gateway/router.go b/gateway/router.go
index e62df205..99b34378 100644
--- a/gateway/router.go
+++ b/gateway/router.go
@@ -109,8 +109,8 @@ func (r *Router) handleReceive() {
 					msgIDs = append(msgIDs, gw.handleMessage(msg, br)...)
 				}
 				// only add the message ID if it doesn't already exists
-				if _, ok := gw.Messages.Get(msg.ID); !ok && msg.ID != "" {
-					gw.Messages.Add(msg.ID, msgIDs)
+				if _, ok := gw.Messages.Get(msg.Protocol + " " + msg.ID); !ok && msg.ID != "" {
+					gw.Messages.Add(msg.Protocol+" "+msg.ID, msgIDs)
 				}
 			}
 		}