diff options
author | Wim <wim@42.be> | 2017-07-25 23:57:27 +0200 |
---|---|---|
committer | Wim <wim@42.be> | 2017-07-25 23:57:27 +0200 |
commit | 41195c8e4858106ca96a29e66dbaf36e0ac71aee (patch) | |
tree | ac0518846c2ccc8eb73589a2ca3d44f82b84e6bc | |
parent | a97804548e2c87bfb170deb4598f54fd728b0078 (diff) | |
download | matterbridge-msglm-41195c8e4858106ca96a29e66dbaf36e0ac71aee.tar.gz matterbridge-msglm-41195c8e4858106ca96a29e66dbaf36e0ac71aee.tar.bz2 matterbridge-msglm-41195c8e4858106ca96a29e66dbaf36e0ac71aee.zip |
Fix double posting of edited messages by using lru cache (mattermost)
-rw-r--r-- | matterclient/matterclient.go | 20 |
1 files changed, 19 insertions, 1 deletions
diff --git a/matterclient/matterclient.go b/matterclient/matterclient.go index 7ca7a0b1..4f0f7593 100644 --- a/matterclient/matterclient.go +++ b/matterclient/matterclient.go @@ -1,6 +1,7 @@ package matterclient import ( + "crypto/md5" "crypto/tls" "encoding/json" "errors" @@ -16,6 +17,7 @@ import ( log "github.com/Sirupsen/logrus" "github.com/gorilla/websocket" + "github.com/hashicorp/golang-lru" "github.com/jpillora/backoff" "github.com/mattermost/platform/model" ) @@ -66,6 +68,7 @@ type MMClient struct { WsPingChan chan *model.WebSocketResponse ServerVersion string OnWsConnect func() + lruCache *lru.Cache } func New(login, pass, team, server string) *MMClient { @@ -73,6 +76,7 @@ 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 } @@ -270,7 +274,10 @@ 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 } @@ -306,6 +313,13 @@ 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 { @@ -860,3 +874,7 @@ func supportedVersion(version string) bool { } return false } + +func digestString(s string) string { + return fmt.Sprintf("%x", md5.Sum([]byte(s))) +} |