mirror of
https://github.com/cwinfo/matterbridge.git
synced 2024-11-22 20:40:27 +00:00
Add caching to fix issue with slack API changes (slack). #481
This commit is contained in:
parent
351b423e15
commit
b51fdbce9f
@ -14,6 +14,7 @@ import (
|
|||||||
"github.com/42wim/matterbridge/bridge/config"
|
"github.com/42wim/matterbridge/bridge/config"
|
||||||
"github.com/42wim/matterbridge/bridge/helper"
|
"github.com/42wim/matterbridge/bridge/helper"
|
||||||
"github.com/42wim/matterbridge/matterhook"
|
"github.com/42wim/matterbridge/matterhook"
|
||||||
|
"github.com/hashicorp/golang-lru"
|
||||||
"github.com/nlopes/slack"
|
"github.com/nlopes/slack"
|
||||||
"github.com/rs/xid"
|
"github.com/rs/xid"
|
||||||
)
|
)
|
||||||
@ -26,6 +27,7 @@ type Bslack struct {
|
|||||||
Usergroups []slack.UserGroup
|
Usergroups []slack.UserGroup
|
||||||
si *slack.Info
|
si *slack.Info
|
||||||
channels []slack.Channel
|
channels []slack.Channel
|
||||||
|
cache *lru.Cache
|
||||||
UseChannelID bool
|
UseChannelID bool
|
||||||
uuid string
|
uuid string
|
||||||
*bridge.Config
|
*bridge.Config
|
||||||
@ -35,7 +37,9 @@ type Bslack struct {
|
|||||||
const messageDeleted = "message_deleted"
|
const messageDeleted = "message_deleted"
|
||||||
|
|
||||||
func New(cfg *bridge.Config) bridge.Bridger {
|
func New(cfg *bridge.Config) bridge.Bridger {
|
||||||
return &Bslack{Config: cfg, uuid: xid.New().String()}
|
b := &Bslack{Config: cfg, uuid: xid.New().String()}
|
||||||
|
b.cache, _ = lru.New(5000)
|
||||||
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bslack) Command(cmd string) string {
|
func (b *Bslack) Command(cmd string) string {
|
||||||
@ -456,15 +460,26 @@ func (b *Bslack) handleDownloadFile(rmsg *config.Message, file *slack.File) erro
|
|||||||
|
|
||||||
// handleUploadFile handles native upload of files
|
// handleUploadFile handles native upload of files
|
||||||
func (b *Bslack) handleUploadFile(msg *config.Message, channelID string) (string, error) {
|
func (b *Bslack) handleUploadFile(msg *config.Message, channelID string) (string, error) {
|
||||||
var err error
|
|
||||||
for _, f := range msg.Extra["file"] {
|
for _, f := range msg.Extra["file"] {
|
||||||
fi := f.(config.FileInfo)
|
fi := f.(config.FileInfo)
|
||||||
_, err = b.sc.UploadFile(slack.FileUploadParameters{
|
if msg.Text == fi.Comment {
|
||||||
|
msg.Text = ""
|
||||||
|
}
|
||||||
|
/* because the result of the UploadFile is slower than the MessageEvent from slack
|
||||||
|
we can't match on the file ID yet, so we have to match on the filename too
|
||||||
|
*/
|
||||||
|
b.Log.Debugf("Adding file %s to cache %s", fi.Name, time.Now().String())
|
||||||
|
b.cache.Add("filename"+fi.Name, time.Now())
|
||||||
|
res, err := b.sc.UploadFile(slack.FileUploadParameters{
|
||||||
Reader: bytes.NewReader(*fi.Data),
|
Reader: bytes.NewReader(*fi.Data),
|
||||||
Filename: fi.Name,
|
Filename: fi.Name,
|
||||||
Channels: []string{channelID},
|
Channels: []string{channelID},
|
||||||
InitialComment: fi.Comment,
|
InitialComment: fi.Comment,
|
||||||
})
|
})
|
||||||
|
if res.ID != "" {
|
||||||
|
b.Log.Debugf("Adding fileid %s to cache %s", res.ID, time.Now().String())
|
||||||
|
b.cache.Add("file"+res.ID, time.Now())
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
b.Log.Errorf("uploadfile %#v", err)
|
b.Log.Errorf("uploadfile %#v", err)
|
||||||
}
|
}
|
||||||
@ -694,6 +709,24 @@ func (b *Bslack) skipMessageEvent(ev *slack.MessageEvent) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(ev.Files) > 0 {
|
||||||
|
for _, f := range ev.Files {
|
||||||
|
// if the file is in the cache and isn't older then a minute, skip it
|
||||||
|
if ts, ok := b.cache.Get("file" + f.ID); ok && time.Since(ts.(time.Time)) < time.Minute {
|
||||||
|
b.Log.Debugf("Not downloading file id %s which we uploaded", f.ID)
|
||||||
|
return true
|
||||||
|
} else {
|
||||||
|
if ts, ok := b.cache.Get("filename" + f.Name); ok && time.Since(ts.(time.Time)) < time.Second*10 {
|
||||||
|
b.Log.Debugf("Not downloading file name %s which we uploaded", f.Name)
|
||||||
|
return true
|
||||||
|
} else {
|
||||||
|
b.Log.Debugf("Not skipping %s %s", f.Name, time.Now().String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user