mirror of
https://github.com/cwinfo/matterbridge.git
synced 2024-11-09 16:50:30 +00:00
Add support for using ID in channel config (mattermost) (#1715)
This commit is contained in:
parent
c8d7fdeedc
commit
9c43eff753
@ -140,9 +140,14 @@ func (b *Bmattermost) handleMatterClient(messages chan *config.Message) {
|
|||||||
continue
|
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)
|
// only download avatars if we have a place to upload them (configured mediaserver)
|
||||||
if b.General.MediaServerUpload != "" || b.General.MediaDownloadPath != "" {
|
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)
|
b.Log.Debugf("== Receiving event %#v", message)
|
||||||
@ -150,7 +155,7 @@ func (b *Bmattermost) handleMatterClient(messages chan *config.Message) {
|
|||||||
rmsg := &config.Message{
|
rmsg := &config.Message{
|
||||||
Username: message.Username,
|
Username: message.Username,
|
||||||
UserID: message.UserID,
|
UserID: message.UserID,
|
||||||
Channel: message.Channel,
|
Channel: channelName,
|
||||||
Text: message.Text,
|
Text: message.Text,
|
||||||
ID: message.Post.Id,
|
ID: message.Post.Id,
|
||||||
ParentID: message.Post.RootId, // ParentID is obsolete with mattermost
|
ParentID: message.Post.RootId, // ParentID is obsolete with mattermost
|
||||||
@ -197,9 +202,14 @@ func (b *Bmattermost) handleMatterClient6(messages chan *config.Message) {
|
|||||||
continue
|
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)
|
// only download avatars if we have a place to upload them (configured mediaserver)
|
||||||
if b.General.MediaServerUpload != "" || b.General.MediaDownloadPath != "" {
|
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)
|
b.Log.Debugf("== Receiving event %#v", message)
|
||||||
@ -207,7 +217,7 @@ func (b *Bmattermost) handleMatterClient6(messages chan *config.Message) {
|
|||||||
rmsg := &config.Message{
|
rmsg := &config.Message{
|
||||||
Username: message.Username,
|
Username: message.Username,
|
||||||
UserID: message.UserID,
|
UserID: message.UserID,
|
||||||
Channel: message.Channel,
|
Channel: channelName,
|
||||||
Text: message.Text,
|
Text: message.Text,
|
||||||
ID: message.Post.Id,
|
ID: message.Post.Id,
|
||||||
ParentID: message.Post.RootId, // ParentID is obsolete with mattermost
|
ParentID: message.Post.RootId, // ParentID is obsolete with mattermost
|
||||||
@ -248,6 +258,7 @@ func (b *Bmattermost) handleMatterHook(messages chan *config.Message) {
|
|||||||
for {
|
for {
|
||||||
message := b.mh.Receive()
|
message := b.mh.Receive()
|
||||||
b.Log.Debugf("Receiving from matterhook %#v", message)
|
b.Log.Debugf("Receiving from matterhook %#v", message)
|
||||||
|
|
||||||
messages <- &config.Message{
|
messages <- &config.Message{
|
||||||
UserID: message.UserID,
|
UserID: message.UserID,
|
||||||
Username: message.UserName,
|
Username: message.UserName,
|
||||||
@ -265,7 +276,7 @@ func (b *Bmattermost) handleUploadFile(msg *config.Message) (string, error) {
|
|||||||
|
|
||||||
var err error
|
var err error
|
||||||
var res, id string
|
var res, id string
|
||||||
channelID := b.mc.GetChannelId(msg.Channel, b.TeamID)
|
channelID := b.getChannelID(msg.Channel)
|
||||||
for _, f := range msg.Extra["file"] {
|
for _, f := range msg.Extra["file"] {
|
||||||
fi := f.(config.FileInfo)
|
fi := f.(config.FileInfo)
|
||||||
id, err = b.mc.UploadFile(*fi.Data, channelID, fi.Name)
|
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) {
|
func (b *Bmattermost) handleUploadFile6(msg *config.Message) (string, error) {
|
||||||
var err error
|
var err error
|
||||||
var res, id string
|
var res, id string
|
||||||
channelID := b.mc6.GetChannelID(msg.Channel, b.TeamID)
|
channelID := b.getChannelID(msg.Channel)
|
||||||
for _, f := range msg.Extra["file"] {
|
for _, f := range msg.Extra["file"] {
|
||||||
fi := f.(config.FileInfo)
|
fi := f.(config.FileInfo)
|
||||||
id, err = b.mc6.UploadFile(*fi.Data, channelID, fi.Name)
|
id, err = b.mc6.UploadFile(*fi.Data, channelID, fi.Name)
|
||||||
|
@ -241,11 +241,17 @@ func (b *Bmattermost) skipMessage(message *matterclient.Message) bool {
|
|||||||
if b.GetBool("nosendjoinpart") {
|
if b.GetBool("nosendjoinpart") {
|
||||||
return true
|
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.Log.Debugf("Sending JOIN_LEAVE event from %s to gateway", b.Account)
|
||||||
b.Remote <- config.Message{
|
b.Remote <- config.Message{
|
||||||
Username: "system",
|
Username: "system",
|
||||||
Text: message.Text,
|
Text: message.Text,
|
||||||
Channel: message.Channel,
|
Channel: channelName,
|
||||||
Account: b.Account,
|
Account: b.Account,
|
||||||
Event: config.EventJoinLeave,
|
Event: config.EventJoinLeave,
|
||||||
}
|
}
|
||||||
@ -304,11 +310,17 @@ func (b *Bmattermost) skipMessage6(message *matterclient6.Message) bool {
|
|||||||
if b.GetBool("nosendjoinpart") {
|
if b.GetBool("nosendjoinpart") {
|
||||||
return true
|
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.Log.Debugf("Sending JOIN_LEAVE event from %s to gateway", b.Account)
|
||||||
b.Remote <- config.Message{
|
b.Remote <- config.Message{
|
||||||
Username: "system",
|
Username: "system",
|
||||||
Text: message.Text,
|
Text: message.Text,
|
||||||
Channel: message.Channel,
|
Channel: channelName,
|
||||||
Account: b.Account,
|
Account: b.Account,
|
||||||
Event: config.EventJoinLeave,
|
Event: config.EventJoinLeave,
|
||||||
}
|
}
|
||||||
@ -376,3 +388,30 @@ func (b *Bmattermost) getVersion() string {
|
|||||||
|
|
||||||
return resp.Header.Get("X-Version-Id")
|
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 ""
|
||||||
|
}
|
||||||
|
@ -4,6 +4,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/42wim/matterbridge/bridge"
|
"github.com/42wim/matterbridge/bridge"
|
||||||
"github.com/42wim/matterbridge/bridge/config"
|
"github.com/42wim/matterbridge/bridge/config"
|
||||||
@ -22,13 +23,19 @@ type Bmattermost struct {
|
|||||||
uuid string
|
uuid string
|
||||||
TeamID string
|
TeamID string
|
||||||
*bridge.Config
|
*bridge.Config
|
||||||
avatarMap map[string]string
|
avatarMap map[string]string
|
||||||
|
channelsMutex sync.RWMutex
|
||||||
|
channelInfoMap map[string]*config.ChannelInfo
|
||||||
}
|
}
|
||||||
|
|
||||||
const mattermostPlugin = "mattermost.plugin"
|
const mattermostPlugin = "mattermost.plugin"
|
||||||
|
|
||||||
func New(cfg *bridge.Config) bridge.Bridger {
|
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.v6 = b.GetBool("v6")
|
||||||
b.uuid = xid.New().String()
|
b.uuid = xid.New().String()
|
||||||
@ -113,14 +120,14 @@ func (b *Bmattermost) JoinChannel(channel config.ChannelInfo) error {
|
|||||||
if b.Account == mattermostPlugin {
|
if b.Account == mattermostPlugin {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
b.channelsMutex.Lock()
|
||||||
|
b.channelInfoMap[channel.ID] = &channel
|
||||||
|
b.channelsMutex.Unlock()
|
||||||
|
|
||||||
// we can only join channels using the API
|
// we can only join channels using the API
|
||||||
if b.GetString("WebhookURL") == "" && b.GetString("WebhookBindAddress") == "" {
|
if b.GetString("WebhookURL") == "" && b.GetString("WebhookBindAddress") == "" {
|
||||||
var id string
|
id := b.getChannelID(channel.Name)
|
||||||
if b.mc6 != nil {
|
|
||||||
id = b.mc6.GetChannelID(channel.Name, b.TeamID)
|
|
||||||
} else {
|
|
||||||
id = b.mc.GetChannelId(channel.Name, b.TeamID)
|
|
||||||
}
|
|
||||||
if id == "" {
|
if id == "" {
|
||||||
return fmt.Errorf("Could not find channel ID for channel %s", channel.Name)
|
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 b.mc.JoinChannel(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -198,11 +206,11 @@ func (b *Bmattermost) Send(msg config.Message) (string, error) {
|
|||||||
if msg.Extra != nil {
|
if msg.Extra != nil {
|
||||||
for _, rmsg := range helper.HandleExtra(&msg, b.General) {
|
for _, rmsg := range helper.HandleExtra(&msg, b.General) {
|
||||||
if b.mc6 != nil {
|
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)
|
b.Log.Errorf("PostMessage failed: %s", err)
|
||||||
}
|
}
|
||||||
} else {
|
} 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)
|
b.Log.Errorf("PostMessage failed: %s", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -228,8 +236,8 @@ func (b *Bmattermost) Send(msg config.Message) (string, error) {
|
|||||||
|
|
||||||
// Post normal message
|
// Post normal message
|
||||||
if b.mc6 != nil {
|
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)
|
||||||
}
|
}
|
||||||
|
@ -1897,7 +1897,8 @@ enable=true
|
|||||||
# -------------------------------------------------------------------------------------------------------------------------------------
|
# -------------------------------------------------------------------------------------------------------------------------------------
|
||||||
# irc | channel | #general | The # symbol is required and should be lowercase!
|
# 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
|
# matrix | #channel:server | #yourchannel:matrix.org | Encrypted rooms are not supported in matrix
|
||||||
# -------------------------------------------------------------------------------------------------------------------------------------
|
# -------------------------------------------------------------------------------------------------------------------------------------
|
||||||
|
Loading…
Reference in New Issue
Block a user