From 9c43eff753ec4976f1bd879732915e29c933c5b9 Mon Sep 17 00:00:00 2001 From: Wim Date: Sun, 6 Feb 2022 18:26:30 +0100 Subject: [PATCH] Add support for using ID in channel config (mattermost) (#1715) --- bridge/mattermost/handlers.go | 23 +++++++++++++----- bridge/mattermost/helpers.go | 43 +++++++++++++++++++++++++++++++-- bridge/mattermost/mattermost.go | 32 +++++++++++++++--------- matterbridge.toml.sample | 3 ++- 4 files changed, 80 insertions(+), 21 deletions(-) diff --git a/bridge/mattermost/handlers.go b/bridge/mattermost/handlers.go index 00c9445d..c7b51243 100644 --- a/bridge/mattermost/handlers.go +++ b/bridge/mattermost/handlers.go @@ -140,9 +140,14 @@ func (b *Bmattermost) handleMatterClient(messages chan *config.Message) { continue } + channelName := b.getChannelName(message.Post.ChannelId) + if channelName == "" { + channelName = message.Channel + } + // only download avatars if we have a place to upload them (configured mediaserver) if b.General.MediaServerUpload != "" || b.General.MediaDownloadPath != "" { - b.handleDownloadAvatar(message.UserID, message.Channel) + b.handleDownloadAvatar(message.UserID, channelName) } b.Log.Debugf("== Receiving event %#v", message) @@ -150,7 +155,7 @@ func (b *Bmattermost) handleMatterClient(messages chan *config.Message) { rmsg := &config.Message{ Username: message.Username, UserID: message.UserID, - Channel: message.Channel, + Channel: channelName, Text: message.Text, ID: message.Post.Id, ParentID: message.Post.RootId, // ParentID is obsolete with mattermost @@ -197,9 +202,14 @@ func (b *Bmattermost) handleMatterClient6(messages chan *config.Message) { continue } + channelName := b.getChannelName(message.Post.ChannelId) + if channelName == "" { + channelName = message.Channel + } + // only download avatars if we have a place to upload them (configured mediaserver) if b.General.MediaServerUpload != "" || b.General.MediaDownloadPath != "" { - b.handleDownloadAvatar(message.UserID, message.Channel) + b.handleDownloadAvatar(message.UserID, channelName) } b.Log.Debugf("== Receiving event %#v", message) @@ -207,7 +217,7 @@ func (b *Bmattermost) handleMatterClient6(messages chan *config.Message) { rmsg := &config.Message{ Username: message.Username, UserID: message.UserID, - Channel: message.Channel, + Channel: channelName, Text: message.Text, ID: message.Post.Id, ParentID: message.Post.RootId, // ParentID is obsolete with mattermost @@ -248,6 +258,7 @@ func (b *Bmattermost) handleMatterHook(messages chan *config.Message) { for { message := b.mh.Receive() b.Log.Debugf("Receiving from matterhook %#v", message) + messages <- &config.Message{ UserID: message.UserID, Username: message.UserName, @@ -265,7 +276,7 @@ func (b *Bmattermost) handleUploadFile(msg *config.Message) (string, error) { var err error var res, id string - channelID := b.mc.GetChannelId(msg.Channel, b.TeamID) + channelID := b.getChannelID(msg.Channel) for _, f := range msg.Extra["file"] { fi := f.(config.FileInfo) id, err = b.mc.UploadFile(*fi.Data, channelID, fi.Name) @@ -285,7 +296,7 @@ func (b *Bmattermost) handleUploadFile(msg *config.Message) (string, error) { func (b *Bmattermost) handleUploadFile6(msg *config.Message) (string, error) { var err error var res, id string - channelID := b.mc6.GetChannelID(msg.Channel, b.TeamID) + channelID := b.getChannelID(msg.Channel) for _, f := range msg.Extra["file"] { fi := f.(config.FileInfo) id, err = b.mc6.UploadFile(*fi.Data, channelID, fi.Name) diff --git a/bridge/mattermost/helpers.go b/bridge/mattermost/helpers.go index 865b8722..7bd766b0 100644 --- a/bridge/mattermost/helpers.go +++ b/bridge/mattermost/helpers.go @@ -241,11 +241,17 @@ func (b *Bmattermost) skipMessage(message *matterclient.Message) bool { if b.GetBool("nosendjoinpart") { return true } + + channelName := b.getChannelName(message.Post.ChannelId) + if channelName == "" { + channelName = message.Channel + } + b.Log.Debugf("Sending JOIN_LEAVE event from %s to gateway", b.Account) b.Remote <- config.Message{ Username: "system", Text: message.Text, - Channel: message.Channel, + Channel: channelName, Account: b.Account, Event: config.EventJoinLeave, } @@ -304,11 +310,17 @@ func (b *Bmattermost) skipMessage6(message *matterclient6.Message) bool { if b.GetBool("nosendjoinpart") { return true } + + channelName := b.getChannelName(message.Post.ChannelId) + if channelName == "" { + channelName = message.Channel + } + b.Log.Debugf("Sending JOIN_LEAVE event from %s to gateway", b.Account) b.Remote <- config.Message{ Username: "system", Text: message.Text, - Channel: message.Channel, + Channel: channelName, Account: b.Account, Event: config.EventJoinLeave, } @@ -376,3 +388,30 @@ func (b *Bmattermost) getVersion() string { return resp.Header.Get("X-Version-Id") } + +func (b *Bmattermost) getChannelID(name string) string { + idcheck := strings.Split(name, "ID:") + if len(idcheck) > 1 { + return idcheck[1] + } + + if b.mc6 != nil { + return b.mc6.GetChannelID(name, b.TeamID) + } + + return b.mc.GetChannelId(name, b.TeamID) +} + +func (b *Bmattermost) getChannelName(id string) string { + b.channelsMutex.RLock() + defer b.channelsMutex.RUnlock() + + for _, c := range b.channelInfoMap { + if c.Name == "ID:"+id { + // if we have ID: specified in our gateway configuration return this + return c.Name + } + } + + return "" +} diff --git a/bridge/mattermost/mattermost.go b/bridge/mattermost/mattermost.go index f1d3db64..91324be1 100644 --- a/bridge/mattermost/mattermost.go +++ b/bridge/mattermost/mattermost.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "strings" + "sync" "github.com/42wim/matterbridge/bridge" "github.com/42wim/matterbridge/bridge/config" @@ -22,13 +23,19 @@ type Bmattermost struct { uuid string TeamID string *bridge.Config - avatarMap map[string]string + avatarMap map[string]string + channelsMutex sync.RWMutex + channelInfoMap map[string]*config.ChannelInfo } const mattermostPlugin = "mattermost.plugin" func New(cfg *bridge.Config) bridge.Bridger { - b := &Bmattermost{Config: cfg, avatarMap: make(map[string]string)} + b := &Bmattermost{ + Config: cfg, + avatarMap: make(map[string]string), + channelInfoMap: make(map[string]*config.ChannelInfo), + } b.v6 = b.GetBool("v6") b.uuid = xid.New().String() @@ -113,14 +120,14 @@ func (b *Bmattermost) JoinChannel(channel config.ChannelInfo) error { if b.Account == mattermostPlugin { return nil } + + b.channelsMutex.Lock() + b.channelInfoMap[channel.ID] = &channel + b.channelsMutex.Unlock() + // we can only join channels using the API if b.GetString("WebhookURL") == "" && b.GetString("WebhookBindAddress") == "" { - var id string - if b.mc6 != nil { - id = b.mc6.GetChannelID(channel.Name, b.TeamID) - } else { - id = b.mc.GetChannelId(channel.Name, b.TeamID) - } + id := b.getChannelID(channel.Name) if id == "" { return fmt.Errorf("Could not find channel ID for channel %s", channel.Name) } @@ -131,6 +138,7 @@ func (b *Bmattermost) JoinChannel(channel config.ChannelInfo) error { return b.mc.JoinChannel(id) } + return nil } @@ -198,11 +206,11 @@ func (b *Bmattermost) Send(msg config.Message) (string, error) { if msg.Extra != nil { for _, rmsg := range helper.HandleExtra(&msg, b.General) { if b.mc6 != nil { - if _, err := b.mc6.PostMessage(b.mc.GetChannelId(rmsg.Channel, b.TeamID), rmsg.Username+rmsg.Text, msg.ParentID); err != nil { + if _, err := b.mc6.PostMessage(b.getChannelID(rmsg.Channel), rmsg.Username+rmsg.Text, msg.ParentID); err != nil { b.Log.Errorf("PostMessage failed: %s", err) } } else { - if _, err := b.mc.PostMessage(b.mc.GetChannelId(rmsg.Channel, b.TeamID), rmsg.Username+rmsg.Text, msg.ParentID); err != nil { + if _, err := b.mc.PostMessage(b.getChannelID(rmsg.Channel), rmsg.Username+rmsg.Text, msg.ParentID); err != nil { b.Log.Errorf("PostMessage failed: %s", err) } } @@ -228,8 +236,8 @@ func (b *Bmattermost) Send(msg config.Message) (string, error) { // Post normal message if b.mc6 != nil { - return b.mc6.PostMessage(b.mc6.GetChannelID(msg.Channel, b.TeamID), msg.Text, msg.ParentID) // nolint:wrapcheck + return b.mc6.PostMessage(b.getChannelID(msg.Channel), msg.Text, msg.ParentID) // nolint:wrapcheck } - return b.mc.PostMessage(b.mc.GetChannelId(msg.Channel, b.TeamID), msg.Text, msg.ParentID) + return b.mc.PostMessage(b.getChannelID(msg.Channel), msg.Text, msg.ParentID) } diff --git a/matterbridge.toml.sample b/matterbridge.toml.sample index 63a614d1..7dd52834 100644 --- a/matterbridge.toml.sample +++ b/matterbridge.toml.sample @@ -1897,7 +1897,8 @@ enable=true # ------------------------------------------------------------------------------------------------------------------------------------- # irc | channel | #general | The # symbol is required and should be lowercase! # ------------------------------------------------------------------------------------------------------------------------------------- - # mattermost | channel | general | This is the channel name as seen in the URL, not the display name + # | channel | general | This is the channel name as seen in the URL, not the display name + # mattermost | channel id | ID:oc4wifyuojgw5f3nsuweesmz8w | This is the channel ID (only use if you know what you're doing) # ------------------------------------------------------------------------------------------------------------------------------------- # matrix | #channel:server | #yourchannel:matrix.org | Encrypted rooms are not supported in matrix # -------------------------------------------------------------------------------------------------------------------------------------