Thumbnail

rani/matterbridge.git

Clone URL: https://git.buni.party/rani/matterbridge.git

commit b90ddbeee1d1059ee3f712a972d9b25fe195aa32 Author: Wim <wim@42.be> Date: Sun Feb 06 18:26:30 2022 +0000 Add support for using ID in channel config (mattermost) (#1715) diff --git a/bridge/mattermost/handlers.go b/bridge/mattermost/handlers.go index 00c9445..c7b5124 100644 --- a/bridge/mattermost/handlers.go +++ b/bridge/mattermost/handlers.go @@ -1409 +14014 @@ 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) @@ -1507 +1557 @@ 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 @@ -1979 +20214 @@ 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) @@ -2077 +2177 @@ 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 @@ -2486 +2587 @@ 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, @@ -2657 +2767 @@ 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) @@ -2857 +2967 @@ 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 865b872..7bd766b 100644 --- a/bridge/mattermost/helpers.go +++ b/bridge/mattermost/helpers.go @@ -24111 +24117 @@ 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,   } @@ -30411 +31017 @@ 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,   } @@ -3763 +38830 @@ 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 f1d3db6..91324be 100644 --- a/bridge/mattermost/mattermost.go +++ b/bridge/mattermost/mattermost.go @@ -46 +47 @@ import (   "errors"   "fmt"   "strings" + "sync"     "github.com/42wim/matterbridge/bridge"   "github.com/42wim/matterbridge/bridge/config" @@ -2213 +2319 @@ 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() @@ -11314 +12014 @@ 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)   } @@ -1316 +1387 @@ func (b *Bmattermost) JoinChannel(channel config.ChannelInfo) error {     return b.mc.JoinChannel(id)   } +   return nil  }   @@ -19811 +20611 @@ 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)   }   } @@ -2288 +2368 @@ 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 63a614d..7dd5283 100644 --- a/matterbridge.toml.sample +++ b/matterbridge.toml.sample @@ -18977 +18978 @@ 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 # -------------------------------------------------------------------------------------------------------------------------------------