Thumbnail

rani/matterbridge.git

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

commit 2d5f696165263a9286a4470fd3322eb347077e48 Author: Wim <wim@42.be> Date: Tue Jul 25 23:57:27 2017 +0000 Fix double posting of edited messages by using lru cache (mattermost) diff --git a/matterclient/matterclient.go b/matterclient/matterclient.go index 7ca7a0b..4f0f759 100644 --- a/matterclient/matterclient.go +++ b/matterclient/matterclient.go @@ -16 +17 @@  package matterclient    import ( + "crypto/md5"   "crypto/tls"   "encoding/json"   "errors" @@ -166 +177 @@ import (   log "github.com/Sirupsen/logrus"     "github.com/gorilla/websocket" + "github.com/hashicorp/golang-lru"   "github.com/jpillora/backoff"   "github.com/mattermost/platform/model"  ) @@ -666 +687 @@ type MMClient struct {   WsPingChan chan *model.WebSocketResponse   ServerVersion string   OnWsConnect func() + lruCache *lru.Cache  }    func New(login, pass, team, server string) *MMClient { @@ -736 +767 @@ func New(login, pass, team, server string) *MMClient {   mmclient := &MMClient{Credentials: cred, MessageChan: make(chan *Message, 100), Users: make(map[string]*model.User)}   mmclient.log = log.WithFields(log.Fields{"module": "matterclient"})   log.SetFormatter(&log.TextFormatter{FullTimestamp: true}) + mmclient.lruCache, _ = lru.New(500)   return mmclient  }   @@ -2707 +27410 @@ func (m *MMClient) WsReceiver() {   m.log.Debugf("WsReceiver event: %#v", event)   msg := &Message{Raw: &event, Team: m.Credentials.Team}   m.parseMessage(msg) - m.MessageChan <- msg + // check if we didn't empty the message + if msg.Text != "" { + m.MessageChan <- msg + }   continue   }   @@ -3066 +31313 @@ func (m *MMClient) parseResponse(rmsg model.WebSocketResponse) {  }    func (m *MMClient) parseActionPost(rmsg *Message) { + // add post to cache, if it already exists don't relay this again. + // this should fix reposts + if ok, _ := m.lruCache.ContainsOrAdd(digestString(rmsg.Raw.Data["post"].(string)), true); ok { + m.log.Debugf("message %#v in cache, not processing again", rmsg.Raw.Data["post"].(string)) + rmsg.Text = "" + return + }   data := model.PostFromJson(strings.NewReader(rmsg.Raw.Data["post"].(string)))   // we don't have the user, refresh the userlist   if m.GetUser(data.UserId) == nil { @@ -8603 +8747 @@ func supportedVersion(version string) bool {   }   return false  } + +func digestString(s string) string { + return fmt.Sprintf("%x", md5.Sum([]byte(s))) +}