Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for using ID in channel config (mattermost) #1715

Merged
merged 1 commit into from
Feb 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions bridge/mattermost/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,17 +140,22 @@ 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)

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
Expand Down Expand Up @@ -197,17 +202,22 @@ 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)

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
Expand Down Expand Up @@ -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,
Expand All @@ -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)
Expand All @@ -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)
Expand Down
43 changes: 41 additions & 2 deletions bridge/mattermost/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down Expand Up @@ -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,
}
Expand Down Expand Up @@ -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 ""
}
32 changes: 20 additions & 12 deletions bridge/mattermost/mattermost.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"strings"
"sync"

"github.com/42wim/matterbridge/bridge"
"github.com/42wim/matterbridge/bridge/config"
Expand All @@ -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()
Expand Down Expand Up @@ -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)
}
Expand All @@ -131,6 +138,7 @@ func (b *Bmattermost) JoinChannel(channel config.ChannelInfo) error {

return b.mc.JoinChannel(id)
}

return nil
}

Expand Down Expand Up @@ -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)
}
}
Expand All @@ -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)
}
3 changes: 2 additions & 1 deletion matterbridge.toml.sample
Original file line number Diff line number Diff line change
Expand Up @@ -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
# -------------------------------------------------------------------------------------------------------------------------------------
Expand Down