summaryrefslogtreecommitdiffstats
path: root/bridge/slack/slack.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/slack.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/slack.go')
-rw-r--r--bridge/slack/slack.go76
1 files changed, 37 insertions, 39 deletions
diff --git a/bridge/slack/slack.go b/bridge/slack/slack.go
index 6524f974..6585c9fc 100644
--- a/bridge/slack/slack.go
+++ b/bridge/slack/slack.go
@@ -16,17 +16,24 @@ import (
)
type Bslack struct {
- mh *matterhook.Client
- sc *slack.Client
- rtm *slack.RTM
- users []slack.User
- si *slack.Info
- channels []slack.Channel
+ sync.RWMutex
+ *bridge.Config
+
+ mh *matterhook.Client
+ sc *slack.Client
+ rtm *slack.RTM
+ si *slack.Info
+
cache *lru.Cache
- useChannelID bool
uuid string
- *bridge.Config
- sync.RWMutex
+ useChannelID bool
+
+ users map[string]*slack.User
+ usersMutex sync.RWMutex
+
+ channelsByID map[string]*slack.Channel
+ channelsByName map[string]*slack.Channel
+ channelsMutex sync.RWMutex
}
const (
@@ -61,9 +68,12 @@ 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,
+ Config: cfg,
+ uuid: xid.New().String(),
+ cache: newCache,
+ users: map[string]*slack.User{},
+ channelsByID: map[string]*slack.Channel{},
+ channelsByName: map[string]*slack.Channel{},
}
return b
}
@@ -132,37 +142,25 @@ func (b *Bslack) Disconnect() error {
return b.rtm.Disconnect()
}
+// JoinChannel only acts as a verification method that checks whether Matterbridge's
+// Slack integration is already member of the channel. This is because Slack does not
+// allow apps or bots to join channels themselves and they need to be invited
+// manually by a user.
func (b *Bslack) JoinChannel(channel config.ChannelInfo) error {
- // use ID:channelid and resolve it to the actual name
- idcheck := strings.Split(channel.Name, "ID:")
- if len(idcheck) > 1 {
+ b.populateChannels()
+
+ channelInfo, err := b.getChannel(channel.Name)
+ if err != nil {
+ return fmt.Errorf("could not join channel: %#v", err)
+ }
+
+ if strings.HasPrefix(channel.Name, "ID:") {
b.useChannelID = true
- ch, err := b.sc.GetChannelInfo(idcheck[1])
- if err != nil {
- return err
- }
- channel.Name = ch.Name
- if err != nil {
- return err
- }
+ channel.Name = channelInfo.Name
}
- // we can only join channels using the API
- if b.sc != nil {
- if strings.HasPrefix(b.GetString(tokenConfig), "xoxb") {
- // TODO check if bot has already joined channel
- return nil
- }
- _, err := b.sc.JoinChannel(channel.Name)
- if err != nil {
- switch err.Error() {
- case "name_taken", "restricted_action":
- case "default":
- {
- return err
- }
- }
- }
+ if !channelInfo.IsMember {
+ return fmt.Errorf("slack integration that matterbridge is using is not member of channel '%s', please add it manually", channelInfo.Name)
}
return nil
}