summaryrefslogtreecommitdiffstats
path: root/bridge/slack/helpers.go
diff options
context:
space:
mode:
authorWim <wim@42.be>2019-01-18 18:35:31 +0100
committerGitHub <noreply@github.com>2019-01-18 18:35:31 +0100
commitfb713ed91bfb48c0037e489f80be85c54e69953a (patch)
treee82901d1a604b02bbb5e3a2c4712f20e61d0513b /bridge/slack/helpers.go
parentd99eacc2e1b0deadc7368b35adf0818493fd2fb9 (diff)
downloadmatterbridge-msglm-fb713ed91bfb48c0037e489f80be85c54e69953a.tar.gz
matterbridge-msglm-fb713ed91bfb48c0037e489f80be85c54e69953a.tar.bz2
matterbridge-msglm-fb713ed91bfb48c0037e489f80be85c54e69953a.zip
Add initial support for getting ChannelMember info of all bridges (#678)
* Add initial support for getting ChannelMember info of all bridges. Adds an EventGetChannelMembers event, which gets send every x time to all bridges. Bridges should respond on this event with a Message containing ChannelMembers in the EventGetChannelMembers key in the Extra field. handleEventGetChannelMembers will handle this Message and sets the contained ChannelMembers to the Bridge struct. * Add ChannelMembers support to the slack bridge
Diffstat (limited to 'bridge/slack/helpers.go')
-rw-r--r--bridge/slack/helpers.go46
1 files changed, 46 insertions, 0 deletions
diff --git a/bridge/slack/helpers.go b/bridge/slack/helpers.go
index 4e6e5652..d84353f0 100644
--- a/bridge/slack/helpers.go
+++ b/bridge/slack/helpers.go
@@ -93,7 +93,9 @@ func (b *Bslack) populateUsers(wait bool) {
return
}
for b.refreshInProgress {
+ b.refreshMutex.Unlock()
time.Sleep(time.Second)
+ b.refreshMutex.Lock()
}
b.refreshInProgress = true
b.refreshMutex.Unlock()
@@ -139,13 +141,16 @@ func (b *Bslack) populateChannels(wait bool) {
return
}
for b.refreshInProgress {
+ b.refreshMutex.Unlock()
time.Sleep(time.Second)
+ b.refreshMutex.Lock()
}
b.refreshInProgress = true
b.refreshMutex.Unlock()
newChannelsByID := map[string]*slack.Channel{}
newChannelsByName := map[string]*slack.Channel{}
+ newChannelMembers := make(map[string][]string)
// We only retrieve public and private channels, not IMs
// and MPIMs as those do not have a channel name.
@@ -166,7 +171,18 @@ func (b *Bslack) populateChannels(wait bool) {
for i := range channels {
newChannelsByID[channels[i].ID] = &channels[i]
newChannelsByName[channels[i].Name] = &channels[i]
+ // also find all the members in every channel
+ members, err := b.getUsersInConversation(channels[i].ID)
+ if err != nil {
+ if err = b.handleRateLimit(err); err != nil {
+ b.Log.Errorf("Could not retrieve channel members: %#v", err)
+ return
+ }
+ continue
+ }
+ newChannelMembers[channels[i].ID] = members
}
+
if nextCursor == "" {
break
}
@@ -178,6 +194,10 @@ func (b *Bslack) populateChannels(wait bool) {
b.channelsByID = newChannelsByID
b.channelsByName = newChannelsByName
+ b.channelMembersMutex.Lock()
+ defer b.channelMembersMutex.Unlock()
+ b.channelMembers = newChannelMembers
+
b.refreshMutex.Lock()
defer b.refreshMutex.Unlock()
b.earliestChannelRefresh = time.Now().Add(minimumRefreshInterval)
@@ -367,3 +387,29 @@ func (b *Bslack) handleRateLimit(err error) error {
time.Sleep(rateLimit.RetryAfter)
return nil
}
+
+// getUsersInConversation returns an array of userIDs that are members of channelID
+func (b *Bslack) getUsersInConversation(channelID string) ([]string, error) {
+ channelMembers := []string{}
+ for {
+ queryParams := &slack.GetUsersInConversationParameters{
+ ChannelID: channelID,
+ }
+
+ members, nextCursor, err := b.sc.GetUsersInConversation(queryParams)
+ if err != nil {
+ if err = b.handleRateLimit(err); err != nil {
+ return channelMembers, fmt.Errorf("Could not retrieve users in channels: %#v", err)
+ }
+ continue
+ }
+
+ channelMembers = append(channelMembers, members...)
+
+ if nextCursor == "" {
+ break
+ }
+ queryParams.Cursor = nextCursor
+ }
+ return channelMembers, nil
+}