mirror of
https://github.com/cwinfo/matterbridge.git
synced 2025-06-27 13:29:25 +00:00
@ -166,15 +166,14 @@ func (b *Bslack) handleMessageEvent(ev *slack.MessageEvent) (*config.Message, er
|
||||
}
|
||||
|
||||
rmsg := config.Message{
|
||||
Text: ev.Text,
|
||||
Channel: channelInfo.Name,
|
||||
Account: b.Account,
|
||||
ID: "slack " + ev.Timestamp,
|
||||
Extra: map[string][]interface{}{},
|
||||
Text: ev.Text,
|
||||
Channel: channelInfo.Name,
|
||||
Account: b.Account,
|
||||
ID: "slack " + ev.Timestamp,
|
||||
Extra: map[string][]interface{}{},
|
||||
ParentID: ev.ThreadTimestamp,
|
||||
}
|
||||
|
||||
|
||||
if b.useChannelID {
|
||||
rmsg.Channel = "ID:" + channelInfo.ID
|
||||
}
|
||||
|
@ -4,7 +4,6 @@ import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/nlopes/slack"
|
||||
@ -61,22 +60,17 @@ func (b *Bslack) getChannelByID(ID string) (*slack.Channel, error) {
|
||||
|
||||
const minimumRefreshInterval = 10 * time.Second
|
||||
|
||||
var (
|
||||
refreshMutex sync.Mutex
|
||||
refreshInProgress bool
|
||||
earliestChannelRefresh = time.Now()
|
||||
earliestUserRefresh = time.Now()
|
||||
)
|
||||
|
||||
func (b *Bslack) populateUsers() {
|
||||
refreshMutex.Lock()
|
||||
if time.Now().Before(earliestUserRefresh) || refreshInProgress {
|
||||
b.Log.Debugf("Not refreshing user list as it was done less than %d seconds ago.", int(minimumRefreshInterval.Seconds()))
|
||||
refreshMutex.Unlock()
|
||||
b.refreshMutex.Lock()
|
||||
if time.Now().Before(b.earliestUserRefresh) || b.refreshInProgress {
|
||||
b.Log.Debugf("Not refreshing user list as it was done less than %v ago.",
|
||||
minimumRefreshInterval)
|
||||
b.refreshMutex.Unlock()
|
||||
|
||||
return
|
||||
}
|
||||
refreshInProgress = true
|
||||
refreshMutex.Unlock()
|
||||
b.refreshInProgress = true
|
||||
b.refreshMutex.Unlock()
|
||||
|
||||
users, err := b.sc.GetUsers()
|
||||
if err != nil {
|
||||
@ -95,19 +89,22 @@ func (b *Bslack) populateUsers() {
|
||||
defer b.usersMutex.Unlock()
|
||||
b.users = newUsers
|
||||
|
||||
earliestUserRefresh = time.Now().Add(minimumRefreshInterval)
|
||||
refreshInProgress = false
|
||||
b.refreshMutex.Lock()
|
||||
defer b.refreshMutex.Unlock()
|
||||
b.earliestUserRefresh = time.Now().Add(minimumRefreshInterval)
|
||||
b.refreshInProgress = false
|
||||
}
|
||||
|
||||
func (b *Bslack) populateChannels() {
|
||||
refreshMutex.Lock()
|
||||
if time.Now().Before(earliestChannelRefresh) || refreshInProgress {
|
||||
b.Log.Debugf("Not refreshing channel list as it was done less than %d seconds ago.", int(minimumRefreshInterval.Seconds()))
|
||||
refreshMutex.Unlock()
|
||||
b.refreshMutex.Lock()
|
||||
if time.Now().Before(b.earliestChannelRefresh) || b.refreshInProgress {
|
||||
b.Log.Debugf("Not refreshing channel list as it was done less than %v seconds ago.",
|
||||
minimumRefreshInterval)
|
||||
b.refreshMutex.Unlock()
|
||||
return
|
||||
}
|
||||
refreshInProgress = true
|
||||
refreshMutex.Unlock()
|
||||
b.refreshInProgress = true
|
||||
b.refreshMutex.Unlock()
|
||||
|
||||
newChannelsByID := map[string]*slack.Channel{}
|
||||
newChannelsByName := map[string]*slack.Channel{}
|
||||
@ -139,8 +136,10 @@ func (b *Bslack) populateChannels() {
|
||||
b.channelsByID = newChannelsByID
|
||||
b.channelsByName = newChannelsByName
|
||||
|
||||
earliestChannelRefresh = time.Now().Add(minimumRefreshInterval)
|
||||
refreshInProgress = false
|
||||
b.refreshMutex.Lock()
|
||||
defer b.refreshMutex.Unlock()
|
||||
b.earliestChannelRefresh = time.Now().Add(minimumRefreshInterval)
|
||||
b.refreshInProgress = false
|
||||
}
|
||||
|
||||
var (
|
||||
|
@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/42wim/matterbridge/bridge"
|
||||
"github.com/42wim/matterbridge/bridge/config"
|
||||
@ -34,6 +35,11 @@ type Bslack struct {
|
||||
channelsByID map[string]*slack.Channel
|
||||
channelsByName map[string]*slack.Channel
|
||||
channelsMutex sync.RWMutex
|
||||
|
||||
refreshInProgress bool
|
||||
earliestChannelRefresh time.Time
|
||||
earliestUserRefresh time.Time
|
||||
refreshMutex sync.Mutex
|
||||
}
|
||||
|
||||
const (
|
||||
@ -68,12 +74,14 @@ func New(cfg *bridge.Config) bridge.Bridger {
|
||||
cfg.Log.Fatalf("Could not create LRU cache for Slack bridge: %v", err)
|
||||
}
|
||||
b := &Bslack{
|
||||
Config: cfg,
|
||||
uuid: xid.New().String(),
|
||||
cache: newCache,
|
||||
users: map[string]*slack.User{},
|
||||
channelsByID: map[string]*slack.Channel{},
|
||||
channelsByName: map[string]*slack.Channel{},
|
||||
Config: cfg,
|
||||
uuid: xid.New().String(),
|
||||
cache: newCache,
|
||||
users: map[string]*slack.User{},
|
||||
channelsByID: map[string]*slack.Channel{},
|
||||
channelsByName: map[string]*slack.Channel{},
|
||||
earliestChannelRefresh: time.Now(),
|
||||
earliestUserRefresh: time.Now(),
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
Reference in New Issue
Block a user