Fix double posting of edited messages by using lru cache (mattermost)

This commit is contained in:
Wim 2017-07-25 23:57:27 +02:00
parent a97804548e
commit 41195c8e48

View File

@ -1,6 +1,7 @@
package matterclient package matterclient
import ( import (
"crypto/md5"
"crypto/tls" "crypto/tls"
"encoding/json" "encoding/json"
"errors" "errors"
@ -16,6 +17,7 @@ import (
log "github.com/Sirupsen/logrus" log "github.com/Sirupsen/logrus"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
"github.com/hashicorp/golang-lru"
"github.com/jpillora/backoff" "github.com/jpillora/backoff"
"github.com/mattermost/platform/model" "github.com/mattermost/platform/model"
) )
@ -66,6 +68,7 @@ type MMClient struct {
WsPingChan chan *model.WebSocketResponse WsPingChan chan *model.WebSocketResponse
ServerVersion string ServerVersion string
OnWsConnect func() OnWsConnect func()
lruCache *lru.Cache
} }
func New(login, pass, team, server string) *MMClient { 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 := &MMClient{Credentials: cred, MessageChan: make(chan *Message, 100), Users: make(map[string]*model.User)}
mmclient.log = log.WithFields(log.Fields{"module": "matterclient"}) mmclient.log = log.WithFields(log.Fields{"module": "matterclient"})
log.SetFormatter(&log.TextFormatter{FullTimestamp: true}) log.SetFormatter(&log.TextFormatter{FullTimestamp: true})
mmclient.lruCache, _ = lru.New(500)
return mmclient return mmclient
} }
@ -270,7 +274,10 @@ func (m *MMClient) WsReceiver() {
m.log.Debugf("WsReceiver event: %#v", event) m.log.Debugf("WsReceiver event: %#v", event)
msg := &Message{Raw: &event, Team: m.Credentials.Team} msg := &Message{Raw: &event, Team: m.Credentials.Team}
m.parseMessage(msg) m.parseMessage(msg)
// check if we didn't empty the message
if msg.Text != "" {
m.MessageChan <- msg m.MessageChan <- msg
}
continue continue
} }
@ -306,6 +313,13 @@ func (m *MMClient) parseResponse(rmsg model.WebSocketResponse) {
} }
func (m *MMClient) parseActionPost(rmsg *Message) { 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))) data := model.PostFromJson(strings.NewReader(rmsg.Raw.Data["post"].(string)))
// we don't have the user, refresh the userlist // we don't have the user, refresh the userlist
if m.GetUser(data.UserId) == nil { if m.GetUser(data.UserId) == nil {
@ -860,3 +874,7 @@ func supportedVersion(version string) bool {
} }
return false return false
} }
func digestString(s string) string {
return fmt.Sprintf("%x", md5.Sum([]byte(s)))
}