diff options
author | Wim <wim@42.be> | 2021-10-16 23:11:51 +0200 |
---|---|---|
committer | Wim <wim@42.be> | 2021-10-16 23:23:24 +0200 |
commit | b56f80b1b82d58c40919598662ef6184d617976c (patch) | |
tree | 90ff90c63ebc7abb2ca05c6834912a8af629a702 /bridge/mattermost/mattermost.go | |
parent | 20f6c05ec50739d31f4dbe9fde0d223f2c43f6e8 (diff) | |
download | matterbridge-msglm-b56f80b1b82d58c40919598662ef6184d617976c.tar.gz matterbridge-msglm-b56f80b1b82d58c40919598662ef6184d617976c.tar.bz2 matterbridge-msglm-b56f80b1b82d58c40919598662ef6184d617976c.zip |
Add support for mattermost v6
Diffstat (limited to 'bridge/mattermost/mattermost.go')
-rw-r--r-- | bridge/mattermost/mattermost.go | 94 |
1 files changed, 81 insertions, 13 deletions
diff --git a/bridge/mattermost/mattermost.go b/bridge/mattermost/mattermost.go index bb44c48a..4c7c05b5 100644 --- a/bridge/mattermost/mattermost.go +++ b/bridge/mattermost/mattermost.go @@ -3,18 +3,22 @@ package bmattermost import ( "errors" "fmt" + "strings" "github.com/42wim/matterbridge/bridge" "github.com/42wim/matterbridge/bridge/config" "github.com/42wim/matterbridge/bridge/helper" "github.com/42wim/matterbridge/matterclient" "github.com/42wim/matterbridge/matterhook" + matterclient6 "github.com/matterbridge/matterclient" "github.com/rs/xid" ) type Bmattermost struct { mh *matterhook.Client mc *matterclient.MMClient + mc6 *matterclient6.Client + v6 bool uuid string TeamID string *bridge.Config @@ -25,7 +29,10 @@ const mattermostPlugin = "mattermost.plugin" func New(cfg *bridge.Config) bridge.Bridger { b := &Bmattermost{Config: cfg, avatarMap: make(map[string]string)} + + b.v6 = b.GetBool("v6") b.uuid = xid.New().String() + return b } @@ -37,6 +44,13 @@ func (b *Bmattermost) Connect() error { if b.Account == mattermostPlugin { return nil } + + if strings.HasPrefix(b.getVersion(), "6.") { + if !b.v6 { + b.v6 = true + } + } + if b.GetString("WebhookBindAddress") != "" { if err := b.doConnectWebhookBind(); err != nil { return err @@ -53,16 +67,34 @@ func (b *Bmattermost) Connect() error { return nil case b.GetString("Token") != "": b.Log.Info("Connecting using token (sending and receiving)") - err := b.apiLogin() - if err != nil { - return err + b.Log.Infof("Using mattermost v6 methods: %t", b.v6) + + if b.v6 { + err := b.apiLogin6() + if err != nil { + return err + } + } else { + err := b.apiLogin() + if err != nil { + return err + } } go b.handleMatter() case b.GetString("Login") != "": b.Log.Info("Connecting using login/password (sending and receiving)") - err := b.apiLogin() - if err != nil { - return err + b.Log.Infof("Using mattermost v6 methods: %t", b.v6) + + if b.v6 { + err := b.apiLogin6() + if err != nil { + return err + } + } else { + err := b.apiLogin() + if err != nil { + return err + } } go b.handleMatter() } @@ -83,10 +115,20 @@ func (b *Bmattermost) JoinChannel(channel config.ChannelInfo) error { } // we can only join channels using the API if b.GetString("WebhookURL") == "" && b.GetString("WebhookBindAddress") == "" { - id := b.mc.GetChannelId(channel.Name, b.TeamID) + var id string + if b.mc6 != nil { + id = b.mc6.GetChannelID(channel.Name, b.TeamID) + } else { + id = b.mc.GetChannelId(channel.Name, b.TeamID) + } if id == "" { return fmt.Errorf("Could not find channel ID for channel %s", channel.Name) } + + if b.mc6 != nil { + return b.mc6.JoinChannel(id) // nolint:wrapcheck + } + return b.mc.JoinChannel(id) } return nil @@ -118,6 +160,10 @@ func (b *Bmattermost) Send(msg config.Message) (string, error) { if msg.ID == "" { return "", nil } + if b.mc6 != nil { + return msg.ID, b.mc6.DeleteMessage(msg.ID) // nolint:wrapcheck + } + return msg.ID, b.mc.DeleteMessage(msg.ID) } @@ -129,18 +175,32 @@ func (b *Bmattermost) Send(msg config.Message) (string, error) { // we only can reply to the root of the thread, not to a specific ID (like discord for example does) if msg.ParentID != "" { - post, res := b.mc.Client.GetPost(msg.ParentID, "") - if res.Error != nil { - b.Log.Errorf("getting post %s failed: %s", msg.ParentID, res.Error.DetailedError) + if b.mc6 != nil { + post, _, err := b.mc6.Client.GetPost(msg.ParentID, "") + if err != nil { + b.Log.Errorf("getting post %s failed: %s", msg.ParentID, err) + } + msg.ParentID = post.RootId + } else { + post, res := b.mc.Client.GetPost(msg.ParentID, "") + if res.Error != nil { + b.Log.Errorf("getting post %s failed: %s", msg.ParentID, res.Error.DetailedError) + } + msg.ParentID = post.RootId } - msg.ParentID = post.RootId } // Upload a file if it exists if msg.Extra != nil { for _, rmsg := range helper.HandleExtra(&msg, b.General) { - if _, err := b.mc.PostMessage(b.mc.GetChannelId(rmsg.Channel, b.TeamID), rmsg.Username+rmsg.Text, msg.ParentID); err != nil { - b.Log.Errorf("PostMessage failed: %s", err) + if b.mc6 != nil { + if _, err := b.mc6.PostMessage(b.mc.GetChannelId(rmsg.Channel, b.TeamID), 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 { + b.Log.Errorf("PostMessage failed: %s", err) + } } } if len(msg.Extra["file"]) > 0 { @@ -155,9 +215,17 @@ func (b *Bmattermost) Send(msg config.Message) (string, error) { // Edit message if we have an ID if msg.ID != "" { + if b.mc6 != nil { + return b.mc6.EditMessage(msg.ID, msg.Text) // nolint:wrapcheck + } + return b.mc.EditMessage(msg.ID, msg.Text) } // 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.mc.PostMessage(b.mc.GetChannelId(msg.Channel, b.TeamID), msg.Text, msg.ParentID) } |