2018-10-12 23:16:34 +02:00
|
|
|
package bslack
|
|
|
|
|
|
|
|
import (
|
2018-11-10 22:09:41 +01:00
|
|
|
"context"
|
2018-10-12 23:16:34 +02:00
|
|
|
"fmt"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
2018-11-01 21:28:22 +01:00
|
|
|
"time"
|
2018-10-12 23:16:34 +02:00
|
|
|
|
2018-11-07 21:35:59 +01:00
|
|
|
"github.com/42wim/matterbridge/bridge/config"
|
2018-10-12 23:16:34 +02:00
|
|
|
"github.com/nlopes/slack"
|
|
|
|
)
|
|
|
|
|
2018-11-07 21:35:59 +01:00
|
|
|
func (b *Bslack) getUser(id string) *slack.User {
|
2019-01-30 23:26:36 +01:00
|
|
|
b.usersMutex.RLock()
|
|
|
|
user, ok := b.users[id]
|
|
|
|
b.usersMutex.RUnlock()
|
|
|
|
if ok {
|
|
|
|
return user
|
|
|
|
}
|
|
|
|
b.populateUser(id)
|
2018-11-07 21:35:59 +01:00
|
|
|
b.usersMutex.RLock()
|
|
|
|
defer b.usersMutex.RUnlock()
|
|
|
|
|
|
|
|
return b.users[id]
|
|
|
|
}
|
|
|
|
|
2018-10-13 01:02:14 +02:00
|
|
|
func (b *Bslack) getUsername(id string) string {
|
2018-11-07 21:35:59 +01:00
|
|
|
if user := b.getUser(id); user != nil {
|
|
|
|
if user.Profile.DisplayName != "" {
|
|
|
|
return user.Profile.DisplayName
|
2018-10-12 23:16:34 +02:00
|
|
|
}
|
2018-11-07 21:35:59 +01:00
|
|
|
return user.Name
|
2018-10-12 23:16:34 +02:00
|
|
|
}
|
2018-10-13 01:02:14 +02:00
|
|
|
b.Log.Warnf("Could not find user with ID '%s'", id)
|
2018-10-12 23:16:34 +02:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2018-11-07 21:35:59 +01:00
|
|
|
func (b *Bslack) getAvatar(id string) string {
|
|
|
|
if user := b.getUser(id); user != nil {
|
|
|
|
return user.Profile.Image48
|
2018-10-12 23:16:34 +02:00
|
|
|
}
|
2018-10-13 01:02:14 +02:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bslack) getChannel(channel string) (*slack.Channel, error) {
|
|
|
|
if strings.HasPrefix(channel, "ID:") {
|
|
|
|
return b.getChannelByID(strings.TrimPrefix(channel, "ID:"))
|
|
|
|
}
|
|
|
|
return b.getChannelByName(channel)
|
2018-10-12 23:16:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bslack) getChannelByName(name string) (*slack.Channel, error) {
|
2018-11-28 11:04:26 +01:00
|
|
|
return b.getChannelBy(name, b.channelsByName)
|
2018-10-12 23:16:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bslack) getChannelByID(ID string) (*slack.Channel, error) {
|
2018-11-28 11:04:26 +01:00
|
|
|
return b.getChannelBy(ID, b.channelsByID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bslack) getChannelBy(lookupKey string, lookupMap map[string]*slack.Channel) (*slack.Channel, error) {
|
2018-10-16 20:34:09 +02:00
|
|
|
b.channelsMutex.RLock()
|
|
|
|
defer b.channelsMutex.RUnlock()
|
|
|
|
|
2018-11-28 11:04:26 +01:00
|
|
|
if channel, ok := lookupMap[lookupKey]; ok {
|
2018-10-16 20:34:09 +02:00
|
|
|
return channel, nil
|
|
|
|
}
|
2018-11-28 11:04:26 +01:00
|
|
|
return nil, fmt.Errorf("%s: channel %s not found", b.Account, lookupKey)
|
2018-10-16 20:34:09 +02:00
|
|
|
}
|
|
|
|
|
2018-11-01 21:28:22 +01:00
|
|
|
const minimumRefreshInterval = 10 * time.Second
|
|
|
|
|
2018-12-15 22:57:54 +01:00
|
|
|
func (b *Bslack) populateUser(userID string) {
|
|
|
|
b.usersMutex.RLock()
|
|
|
|
_, exists := b.users[userID]
|
|
|
|
b.usersMutex.RUnlock()
|
|
|
|
if exists {
|
|
|
|
// already in cache
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := b.sc.GetUserInfo(userID)
|
|
|
|
if err != nil {
|
|
|
|
b.Log.Debugf("GetUserInfo failed for %v: %v", userID, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
b.usersMutex.Lock()
|
|
|
|
b.users[userID] = user
|
|
|
|
b.usersMutex.Unlock()
|
|
|
|
}
|
|
|
|
|
2018-12-15 23:11:03 +01:00
|
|
|
func (b *Bslack) populateUsers(wait bool) {
|
2018-11-07 20:36:50 +01:00
|
|
|
b.refreshMutex.Lock()
|
2018-12-15 23:11:03 +01:00
|
|
|
if !wait && (time.Now().Before(b.earliestUserRefresh) || b.refreshInProgress) {
|
2018-11-07 20:36:50 +01:00
|
|
|
b.Log.Debugf("Not refreshing user list as it was done less than %v ago.",
|
|
|
|
minimumRefreshInterval)
|
|
|
|
b.refreshMutex.Unlock()
|
|
|
|
|
2018-11-01 21:28:22 +01:00
|
|
|
return
|
|
|
|
}
|
2018-12-15 23:11:03 +01:00
|
|
|
for b.refreshInProgress {
|
2019-01-18 18:35:31 +01:00
|
|
|
b.refreshMutex.Unlock()
|
2018-12-15 23:11:03 +01:00
|
|
|
time.Sleep(time.Second)
|
2019-01-18 18:35:31 +01:00
|
|
|
b.refreshMutex.Lock()
|
2018-12-15 23:11:03 +01:00
|
|
|
}
|
2018-11-07 20:36:50 +01:00
|
|
|
b.refreshInProgress = true
|
|
|
|
b.refreshMutex.Unlock()
|
2018-11-01 21:28:22 +01:00
|
|
|
|
2018-10-16 20:34:09 +02:00
|
|
|
newUsers := map[string]*slack.User{}
|
2018-11-10 22:09:41 +01:00
|
|
|
pagination := b.sc.GetUsersPaginated(slack.GetUsersOptionLimit(200))
|
2019-01-30 23:26:36 +01:00
|
|
|
count := 0
|
2018-11-10 22:09:41 +01:00
|
|
|
for {
|
|
|
|
var err error
|
|
|
|
pagination, err = pagination.Next(context.Background())
|
2019-01-30 23:26:36 +01:00
|
|
|
time.Sleep(time.Second)
|
2018-11-10 22:09:41 +01:00
|
|
|
if err != nil {
|
|
|
|
if pagination.Done(err) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = b.handleRateLimit(err); err != nil {
|
|
|
|
b.Log.Errorf("Could not retrieve users: %#v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range pagination.Users {
|
|
|
|
newUsers[pagination.Users[i].ID] = &pagination.Users[i]
|
|
|
|
}
|
2019-01-30 23:26:36 +01:00
|
|
|
b.Log.Debugf("getting %d users", len(pagination.Users))
|
|
|
|
count++
|
|
|
|
// more > 2000 users, slack will complain and ratelimit. break
|
|
|
|
if count > 10 {
|
|
|
|
b.Log.Info("Large slack detected > 2000 users, skipping loading complete userlist.")
|
|
|
|
break
|
|
|
|
}
|
2018-10-12 23:16:34 +02:00
|
|
|
}
|
2018-10-16 20:34:09 +02:00
|
|
|
|
|
|
|
b.usersMutex.Lock()
|
|
|
|
defer b.usersMutex.Unlock()
|
|
|
|
b.users = newUsers
|
2018-11-01 21:28:22 +01:00
|
|
|
|
2018-11-07 20:36:50 +01:00
|
|
|
b.refreshMutex.Lock()
|
|
|
|
defer b.refreshMutex.Unlock()
|
|
|
|
b.earliestUserRefresh = time.Now().Add(minimumRefreshInterval)
|
|
|
|
b.refreshInProgress = false
|
2018-10-16 20:34:09 +02:00
|
|
|
}
|
|
|
|
|
2018-12-15 23:11:03 +01:00
|
|
|
func (b *Bslack) populateChannels(wait bool) {
|
2018-11-07 20:36:50 +01:00
|
|
|
b.refreshMutex.Lock()
|
2018-12-15 23:11:03 +01:00
|
|
|
if !wait && (time.Now().Before(b.earliestChannelRefresh) || b.refreshInProgress) {
|
2018-11-07 20:36:50 +01:00
|
|
|
b.Log.Debugf("Not refreshing channel list as it was done less than %v seconds ago.",
|
|
|
|
minimumRefreshInterval)
|
|
|
|
b.refreshMutex.Unlock()
|
2018-11-01 21:28:22 +01:00
|
|
|
return
|
|
|
|
}
|
2018-12-15 23:11:03 +01:00
|
|
|
for b.refreshInProgress {
|
2019-01-18 18:35:31 +01:00
|
|
|
b.refreshMutex.Unlock()
|
2018-12-15 23:11:03 +01:00
|
|
|
time.Sleep(time.Second)
|
2019-01-18 18:35:31 +01:00
|
|
|
b.refreshMutex.Lock()
|
2018-12-15 23:11:03 +01:00
|
|
|
}
|
2018-11-07 20:36:50 +01:00
|
|
|
b.refreshInProgress = true
|
|
|
|
b.refreshMutex.Unlock()
|
2018-11-01 21:28:22 +01:00
|
|
|
|
2018-10-16 20:34:09 +02:00
|
|
|
newChannelsByID := map[string]*slack.Channel{}
|
|
|
|
newChannelsByName := map[string]*slack.Channel{}
|
2019-01-18 18:35:31 +01:00
|
|
|
newChannelMembers := make(map[string][]string)
|
2018-10-16 20:34:09 +02:00
|
|
|
|
|
|
|
// 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 {
|
2018-11-10 22:09:41 +01:00
|
|
|
if err = b.handleRateLimit(err); err != nil {
|
|
|
|
b.Log.Errorf("Could not retrieve channels: %#v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
continue
|
2018-10-12 23:16:34 +02:00
|
|
|
}
|
2018-11-10 22:09:41 +01:00
|
|
|
|
|
|
|
for i := range channels {
|
2018-10-16 20:34:09 +02:00
|
|
|
newChannelsByID[channels[i].ID] = &channels[i]
|
|
|
|
newChannelsByName[channels[i].Name] = &channels[i]
|
2019-01-18 18:35:31 +01:00
|
|
|
// also find all the members in every channel
|
2019-01-30 23:26:36 +01:00
|
|
|
// comment for now, issues on big slacks
|
|
|
|
/*
|
|
|
|
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
|
2019-01-18 18:35:31 +01:00
|
|
|
}
|
2019-01-30 23:26:36 +01:00
|
|
|
newChannelMembers[channels[i].ID] = members
|
|
|
|
*/
|
2018-10-16 20:34:09 +02:00
|
|
|
}
|
2019-01-18 18:35:31 +01:00
|
|
|
|
2018-10-16 20:34:09 +02:00
|
|
|
if nextCursor == "" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
queryParams.Cursor = nextCursor
|
2018-10-12 23:16:34 +02:00
|
|
|
}
|
2018-10-16 20:34:09 +02:00
|
|
|
|
|
|
|
b.channelsMutex.Lock()
|
|
|
|
defer b.channelsMutex.Unlock()
|
|
|
|
b.channelsByID = newChannelsByID
|
|
|
|
b.channelsByName = newChannelsByName
|
2018-11-01 21:28:22 +01:00
|
|
|
|
2019-01-18 18:35:31 +01:00
|
|
|
b.channelMembersMutex.Lock()
|
|
|
|
defer b.channelMembersMutex.Unlock()
|
|
|
|
b.channelMembers = newChannelMembers
|
|
|
|
|
2018-11-07 20:36:50 +01:00
|
|
|
b.refreshMutex.Lock()
|
|
|
|
defer b.refreshMutex.Unlock()
|
|
|
|
b.earliestChannelRefresh = time.Now().Add(minimumRefreshInterval)
|
|
|
|
b.refreshInProgress = false
|
2018-10-12 23:16:34 +02:00
|
|
|
}
|
|
|
|
|
2018-11-07 21:35:59 +01:00
|
|
|
// populateReceivedMessage shapes the initial Matterbridge message that we will forward to the
|
|
|
|
// router before we apply message-dependent modifications.
|
|
|
|
func (b *Bslack) populateReceivedMessage(ev *slack.MessageEvent) (*config.Message, error) {
|
|
|
|
// Use our own func because rtm.GetChannelInfo doesn't work for private channels.
|
|
|
|
channel, err := b.getChannelByID(ev.Channel)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
rmsg := &config.Message{
|
|
|
|
Text: ev.Text,
|
|
|
|
Channel: channel.Name,
|
|
|
|
Account: b.Account,
|
2018-11-19 21:28:23 +01:00
|
|
|
ID: ev.Timestamp,
|
2018-11-07 21:35:59 +01:00
|
|
|
Extra: make(map[string][]interface{}),
|
|
|
|
ParentID: ev.ThreadTimestamp,
|
2018-11-19 21:28:23 +01:00
|
|
|
Protocol: b.Protocol,
|
2018-11-07 21:35:59 +01:00
|
|
|
}
|
|
|
|
if b.useChannelID {
|
|
|
|
rmsg.Channel = "ID:" + channel.ID
|
|
|
|
}
|
|
|
|
|
2018-11-08 20:07:21 +01:00
|
|
|
// Handle 'edit' messages.
|
|
|
|
if ev.SubMessage != nil && !b.GetBool(editDisableConfig) {
|
2018-11-19 21:28:23 +01:00
|
|
|
rmsg.ID = ev.SubMessage.Timestamp
|
2018-11-08 20:07:21 +01:00
|
|
|
if ev.SubMessage.ThreadTimestamp != ev.SubMessage.Timestamp {
|
|
|
|
b.Log.Debugf("SubMessage %#v", ev.SubMessage)
|
|
|
|
rmsg.Text = ev.SubMessage.Text + b.GetString(editSuffixConfig)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-07 21:35:59 +01:00
|
|
|
if err = b.populateMessageWithUserInfo(ev, rmsg); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return rmsg, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bslack) populateMessageWithUserInfo(ev *slack.MessageEvent, rmsg *config.Message) error {
|
|
|
|
if ev.SubType == sMessageDeleted || ev.SubType == sFileComment {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-11-08 20:07:21 +01:00
|
|
|
// First, deal with bot-originating messages but only do so when not using webhooks: we
|
|
|
|
// would not be able to distinguish which bot would be sending them.
|
2018-11-10 22:09:41 +01:00
|
|
|
if err := b.populateMessageWithBotInfo(ev, rmsg); err != nil {
|
|
|
|
return err
|
2018-11-07 21:35:59 +01:00
|
|
|
}
|
|
|
|
|
2018-11-08 20:07:21 +01:00
|
|
|
// Second, deal with "real" users if we have the necessary information.
|
|
|
|
var userID string
|
2018-11-08 22:01:29 +01:00
|
|
|
switch {
|
|
|
|
case ev.User != "":
|
2018-11-08 20:07:21 +01:00
|
|
|
userID = ev.User
|
2018-11-08 22:01:29 +01:00
|
|
|
case ev.SubMessage != nil && ev.SubMessage.User != "":
|
2018-11-08 20:07:21 +01:00
|
|
|
userID = ev.SubMessage.User
|
2018-11-08 22:01:29 +01:00
|
|
|
default:
|
2018-11-08 20:07:21 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
user := b.getUser(userID)
|
|
|
|
if user == nil {
|
|
|
|
return fmt.Errorf("could not find information for user with id %s", ev.User)
|
|
|
|
}
|
|
|
|
|
|
|
|
rmsg.UserID = user.ID
|
|
|
|
rmsg.Username = user.Name
|
|
|
|
if user.Profile.DisplayName != "" {
|
|
|
|
rmsg.Username = user.Profile.DisplayName
|
2018-11-07 21:35:59 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-11-10 22:09:41 +01:00
|
|
|
func (b *Bslack) populateMessageWithBotInfo(ev *slack.MessageEvent, rmsg *config.Message) error {
|
|
|
|
if ev.BotID == "" || b.GetString(outgoingWebhookConfig) != "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
var bot *slack.Bot
|
|
|
|
for {
|
|
|
|
bot, err = b.rtm.GetBotInfo(ev.BotID)
|
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = b.handleRateLimit(err); err != nil {
|
|
|
|
b.Log.Errorf("Could not retrieve bot information: %#v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2018-12-12 00:27:55 +01:00
|
|
|
b.Log.Debugf("Found bot %#v", bot)
|
2018-11-10 22:09:41 +01:00
|
|
|
|
2018-12-13 20:49:14 +01:00
|
|
|
if bot.Name != "" {
|
2018-11-10 22:09:41 +01:00
|
|
|
rmsg.Username = bot.Name
|
|
|
|
if ev.Username != "" {
|
|
|
|
rmsg.Username = ev.Username
|
|
|
|
}
|
|
|
|
rmsg.UserID = bot.ID
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-13 01:02:14 +02:00
|
|
|
var (
|
2018-11-26 10:47:04 +01:00
|
|
|
mentionRE = regexp.MustCompile(`<@([a-zA-Z0-9]+)>`)
|
|
|
|
channelRE = regexp.MustCompile(`<#[a-zA-Z0-9]+\|(.+?)>`)
|
|
|
|
variableRE = regexp.MustCompile(`<!((?:subteam\^)?[a-zA-Z0-9]+)(?:\|@?(.+?))?>`)
|
|
|
|
urlRE = regexp.MustCompile(`<(.*?)(\|.*?)?>`)
|
2019-01-04 20:32:58 +01:00
|
|
|
codeFenceRE = regexp.MustCompile(`(?m)^` + "```" + `\w+$`)
|
2018-11-26 10:47:04 +01:00
|
|
|
topicOrPurposeRE = regexp.MustCompile(`(?s)(@.+) (cleared|set)(?: the)? channel (topic|purpose)(?:: (.*))?`)
|
2018-10-13 01:02:14 +02:00
|
|
|
)
|
2018-10-12 23:16:34 +02:00
|
|
|
|
2018-11-26 10:47:04 +01:00
|
|
|
func (b *Bslack) extractTopicOrPurpose(text string) (string, string) {
|
|
|
|
r := topicOrPurposeRE.FindStringSubmatch(text)
|
|
|
|
if len(r) == 5 {
|
|
|
|
action, updateType, extracted := r[2], r[3], r[4]
|
|
|
|
switch action {
|
|
|
|
case "set":
|
|
|
|
return updateType, extracted
|
|
|
|
case "cleared":
|
|
|
|
return updateType, ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
b.Log.Warnf("Encountered channel topic or purpose change message with unexpected format: %s", text)
|
|
|
|
return "unknown", ""
|
|
|
|
}
|
|
|
|
|
2018-10-12 23:16:34 +02:00
|
|
|
// @see https://api.slack.com/docs/message-formatting#linking_to_channels_and_users
|
|
|
|
func (b *Bslack) replaceMention(text string) string {
|
2018-10-13 01:02:14 +02:00
|
|
|
replaceFunc := func(match string) string {
|
|
|
|
userID := strings.Trim(match, "@<>")
|
|
|
|
if username := b.getUsername(userID); userID != "" {
|
|
|
|
return "@" + username
|
|
|
|
}
|
|
|
|
return match
|
2018-10-12 23:16:34 +02:00
|
|
|
}
|
2018-10-13 01:02:14 +02:00
|
|
|
return mentionRE.ReplaceAllStringFunc(text, replaceFunc)
|
2018-10-12 23:16:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// @see https://api.slack.com/docs/message-formatting#linking_to_channels_and_users
|
|
|
|
func (b *Bslack) replaceChannel(text string) string {
|
2018-10-13 01:02:14 +02:00
|
|
|
for _, r := range channelRE.FindAllStringSubmatch(text, -1) {
|
|
|
|
text = strings.Replace(text, r[0], "#"+r[1], 1)
|
2018-10-12 23:16:34 +02:00
|
|
|
}
|
|
|
|
return text
|
|
|
|
}
|
|
|
|
|
|
|
|
// @see https://api.slack.com/docs/message-formatting#variables
|
|
|
|
func (b *Bslack) replaceVariable(text string) string {
|
2018-10-13 01:02:14 +02:00
|
|
|
for _, r := range variableRE.FindAllStringSubmatch(text, -1) {
|
2018-10-12 23:16:34 +02:00
|
|
|
if r[2] != "" {
|
2018-10-13 01:02:14 +02:00
|
|
|
text = strings.Replace(text, r[0], "@"+r[2], 1)
|
2018-10-12 23:16:34 +02:00
|
|
|
} else {
|
2018-10-13 01:02:14 +02:00
|
|
|
text = strings.Replace(text, r[0], "@"+r[1], 1)
|
2018-10-12 23:16:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return text
|
|
|
|
}
|
|
|
|
|
|
|
|
// @see https://api.slack.com/docs/message-formatting#linking_to_urls
|
|
|
|
func (b *Bslack) replaceURL(text string) string {
|
2018-10-13 01:02:14 +02:00
|
|
|
for _, r := range urlRE.FindAllStringSubmatch(text, -1) {
|
2018-10-12 23:16:34 +02:00
|
|
|
if len(strings.TrimSpace(r[2])) == 1 { // A display text separator was found, but the text was blank
|
2018-10-13 01:02:14 +02:00
|
|
|
text = strings.Replace(text, r[0], "", 1)
|
2018-10-12 23:16:34 +02:00
|
|
|
} else {
|
2018-10-13 01:02:14 +02:00
|
|
|
text = strings.Replace(text, r[0], r[1], 1)
|
2018-10-12 23:16:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return text
|
|
|
|
}
|
2018-11-10 22:09:41 +01:00
|
|
|
|
2019-01-04 20:32:58 +01:00
|
|
|
func (b *Bslack) replaceCodeFence(text string) string {
|
|
|
|
return codeFenceRE.ReplaceAllString(text, "```")
|
|
|
|
}
|
|
|
|
|
2018-11-10 22:09:41 +01:00
|
|
|
func (b *Bslack) handleRateLimit(err error) error {
|
|
|
|
rateLimit, ok := err.(*slack.RateLimitedError)
|
|
|
|
if !ok {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
b.Log.Infof("Rate-limited by Slack. Sleeping for %v", rateLimit.RetryAfter)
|
|
|
|
time.Sleep(rateLimit.RetryAfter)
|
|
|
|
return nil
|
|
|
|
}
|
2019-01-18 18:35:31 +01:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|