summaryrefslogtreecommitdiffstats
path: root/bridge/slack/helpers.go
diff options
context:
space:
mode:
authorDuco van Amstel <duco.vanamstel@gmail.com>2018-10-16 19:34:09 +0100
committerWim <wim@42.be>2018-10-16 20:34:09 +0200
commit6238effdc27e4f439cb49bfa5fb5cd09fd88375e (patch)
treeb820678b1edbddc7b5cb5e88910f1b8c49768a9c /bridge/slack/helpers.go
parent498377a230b8712792a47a61ad63ab746f7379d1 (diff)
downloadmatterbridge-msglm-6238effdc27e4f439cb49bfa5fb5cd09fd88375e.tar.gz
matterbridge-msglm-6238effdc27e4f439cb49bfa5fb5cd09fd88375e.tar.bz2
matterbridge-msglm-6238effdc27e4f439cb49bfa5fb5cd09fd88375e.zip
Clean up user and channel information management (slack) (#521)
Diffstat (limited to 'bridge/slack/helpers.go')
-rw-r--r--bridge/slack/helpers.go71
1 files changed, 58 insertions, 13 deletions
diff --git a/bridge/slack/helpers.go b/bridge/slack/helpers.go
index 9af5dfac..18dded3b 100644
--- a/bridge/slack/helpers.go
+++ b/bridge/slack/helpers.go
@@ -38,27 +38,72 @@ func (b *Bslack) getChannel(channel string) (*slack.Channel, error) {
}
func (b *Bslack) getChannelByName(name string) (*slack.Channel, error) {
- if b.channels == nil {
- return nil, fmt.Errorf("%s: channel %s not found (no channels found)", b.Account, name)
- }
- for _, channel := range b.channels {
- if channel.Name == name {
- return &channel, nil
- }
+ b.channelsMutex.RLock()
+ defer b.channelsMutex.RUnlock()
+
+ if channel, ok := b.channelsByName[name]; ok {
+ return channel, nil
}
return nil, fmt.Errorf("%s: channel %s not found", b.Account, name)
}
func (b *Bslack) getChannelByID(ID string) (*slack.Channel, error) {
- if b.channels == nil {
- return nil, fmt.Errorf("%s: channel %s not found (no channels found)", b.Account, ID)
+ b.channelsMutex.RLock()
+ defer b.channelsMutex.RUnlock()
+
+ if channel, ok := b.channelsByID[ID]; ok {
+ return channel, nil
+ }
+ return nil, fmt.Errorf("%s: channel %s not found", b.Account, ID)
+}
+
+func (b *Bslack) populateUsers() {
+ users, err := b.sc.GetUsers()
+ if err != nil {
+ b.Log.Errorf("Could not reload users: %#v", err)
+ return
+ }
+
+ newUsers := map[string]*slack.User{}
+ for _, user := range users {
+ newUsers[user.ID] = &user
}
- for _, channel := range b.channels {
- if channel.ID == ID {
- return &channel, nil
+
+ b.usersMutex.Lock()
+ defer b.usersMutex.Unlock()
+ b.users = newUsers
+}
+
+func (b *Bslack) populateChannels() {
+ newChannelsByID := map[string]*slack.Channel{}
+ newChannelsByName := map[string]*slack.Channel{}
+
+ // We only retrieve public and private channels, not IMs
+ // and MPIMs as those do not have a channel name.
+ queryParams := &slack.GetConversationsParameters{
+ ExcludeArchived: "true",
+ Types: []string{"public_channel,private_channel"},
+ }
+ for {
+ channels, nextCursor, err := b.sc.GetConversations(queryParams)
+ if err != nil {
+ b.Log.Errorf("Could not reload channels: %#v", err)
+ return
}
+ for i := 0; i < len(channels); i++ {
+ newChannelsByID[channels[i].ID] = &channels[i]
+ newChannelsByName[channels[i].Name] = &channels[i]
+ }
+ if nextCursor == "" {
+ break
+ }
+ queryParams.Cursor = nextCursor
}
- return nil, fmt.Errorf("%s: channel %s not found", b.Account, ID)
+
+ b.channelsMutex.Lock()
+ defer b.channelsMutex.Unlock()
+ b.channelsByID = newChannelsByID
+ b.channelsByName = newChannelsByName
}
var (