Thumbnail

rani/matterbridge.git

Clone URL: https://git.buni.party/rani/matterbridge.git

commit 16e2f2dec720bed260914fcaf632142e12ddc61f Author: Wim <wim@42.be> Date: Mon Nov 19 21:28:23 2018 +0000 Add protocol to msg.ID in cache (#596) diff --git a/bridge/slack/handlers.go b/bridge/slack/handlers.go index d4e5e46..a3291c3 100644 --- a/bridge/slack/handlers.go +++ b/bridge/slack/handlers.go @@ -2057 +2057 @@ 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 861a875..b0fdaba 100644 --- a/bridge/slack/helpers.go +++ b/bridge/slack/helpers.go @@ -1739 +17310 @@ 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 @@ -1837 +1847 @@ 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 c4689cb..0951fb1 100644 --- a/bridge/slack/slack.go +++ b/bridge/slack/slack.go @@ -32410 +3248 @@ 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   } @@ -3449 +3428 @@ 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   } @@ -3627 +3597 @@ 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 { @@ -41310 +4107 @@ 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 d723d85..72a0f6a 100644 --- a/gateway/gateway.go +++ b/gateway/gateway.go @@ -858 +859 @@ 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   }   @@ -958 +968 @@ 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)   }   }   } @@ -2347 +2357 @@ 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)   }   }   } @@ -2808 +2817 @@ 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 @@ -30914 +30914 @@ 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   } @@ -3357 +3357 @@ 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 e62df20..99b3437 100644 --- a/gateway/router.go +++ b/gateway/router.go @@ -1098 +1098 @@ 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)   }   }   }