mirror of
https://github.com/cwinfo/matterbridge.git
synced 2024-11-24 17:51:36 +00:00
Remove mattermost 5 support (#1936)
This commit is contained in:
parent
4cf313c4c6
commit
eac2a8c8dc
@ -3,10 +3,8 @@ package bmattermost
|
|||||||
import (
|
import (
|
||||||
"github.com/42wim/matterbridge/bridge/config"
|
"github.com/42wim/matterbridge/bridge/config"
|
||||||
"github.com/42wim/matterbridge/bridge/helper"
|
"github.com/42wim/matterbridge/bridge/helper"
|
||||||
"github.com/42wim/matterbridge/matterclient"
|
"github.com/matterbridge/matterclient"
|
||||||
matterclient6 "github.com/matterbridge/matterclient"
|
"github.com/mattermost/mattermost-server/v6/model"
|
||||||
"github.com/mattermost/mattermost-server/v5/model"
|
|
||||||
model6 "github.com/mattermost/mattermost-server/v6/model"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// handleDownloadAvatar downloads the avatar of userid from channel
|
// handleDownloadAvatar downloads the avatar of userid from channel
|
||||||
@ -26,20 +24,11 @@ func (b *Bmattermost) handleDownloadAvatar(userid string, channel string) {
|
|||||||
var (
|
var (
|
||||||
data []byte
|
data []byte
|
||||||
err error
|
err error
|
||||||
resp *model.Response
|
|
||||||
)
|
)
|
||||||
if b.mc6 != nil {
|
data, _, err = b.mc.Client.GetProfileImage(userid, "")
|
||||||
data, _, err = b.mc6.Client.GetProfileImage(userid, "")
|
if err != nil {
|
||||||
if err != nil {
|
b.Log.Errorf("ProfileImage download failed for %#v %s", userid, err)
|
||||||
b.Log.Errorf("ProfileImage download failed for %#v %s", userid, err)
|
return
|
||||||
return
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
data, resp = b.mc.Client.GetProfileImage(userid, "")
|
|
||||||
if resp.Error != nil {
|
|
||||||
b.Log.Errorf("ProfileImage download failed for %#v %s", userid, resp.Error)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
err = helper.HandleDownloadSize(b.Log, &rmsg, userid+".png", int64(len(data)), b.General)
|
err = helper.HandleDownloadSize(b.Log, &rmsg, userid+".png", int64(len(data)), b.General)
|
||||||
@ -52,33 +41,10 @@ func (b *Bmattermost) handleDownloadAvatar(userid string, channel string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// handleDownloadFile handles file download
|
|
||||||
func (b *Bmattermost) handleDownloadFile(rmsg *config.Message, id string) error {
|
|
||||||
if b.mc6 != nil {
|
|
||||||
return b.handleDownloadFile6(rmsg, id)
|
|
||||||
}
|
|
||||||
|
|
||||||
url, _ := b.mc.Client.GetFileLink(id)
|
|
||||||
finfo, resp := b.mc.Client.GetFileInfo(id)
|
|
||||||
if resp.Error != nil {
|
|
||||||
return resp.Error
|
|
||||||
}
|
|
||||||
err := helper.HandleDownloadSize(b.Log, rmsg, finfo.Name, finfo.Size, b.General)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
data, resp := b.mc.Client.DownloadFile(id, true)
|
|
||||||
if resp.Error != nil {
|
|
||||||
return resp.Error
|
|
||||||
}
|
|
||||||
helper.HandleDownloadData(b.Log, rmsg, finfo.Name, rmsg.Text, url, &data, b.General)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// nolint:wrapcheck
|
// nolint:wrapcheck
|
||||||
func (b *Bmattermost) handleDownloadFile6(rmsg *config.Message, id string) error {
|
func (b *Bmattermost) handleDownloadFile(rmsg *config.Message, id string) error {
|
||||||
url, _, _ := b.mc6.Client.GetFileLink(id)
|
url, _, _ := b.mc.Client.GetFileLink(id)
|
||||||
finfo, _, err := b.mc6.Client.GetFileInfo(id)
|
finfo, _, err := b.mc.Client.GetFileInfo(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -86,7 +52,7 @@ func (b *Bmattermost) handleDownloadFile6(rmsg *config.Message, id string) error
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
data, _, err := b.mc6.Client.DownloadFile(id, true)
|
data, _, err := b.mc.Client.DownloadFile(id, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -125,15 +91,10 @@ func (b *Bmattermost) handleMatter() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// nolint:cyclop
|
||||||
func (b *Bmattermost) handleMatterClient(messages chan *config.Message) {
|
func (b *Bmattermost) handleMatterClient(messages chan *config.Message) {
|
||||||
if b.mc6 != nil {
|
|
||||||
b.Log.Debug("starting matterclient6")
|
|
||||||
b.handleMatterClient6(messages)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
for message := range b.mc.MessageChan {
|
for message := range b.mc.MessageChan {
|
||||||
b.Log.Debugf("%#v", message.Raw.Data)
|
b.Log.Debugf("%#v %#v", message.Raw.GetData(), message.Raw.EventType())
|
||||||
|
|
||||||
if b.skipMessage(message) {
|
if b.skipMessage(message) {
|
||||||
b.Log.Debugf("Skipped message: %#v", message)
|
b.Log.Debugf("Skipped message: %#v", message)
|
||||||
@ -166,11 +127,11 @@ func (b *Bmattermost) handleMatterClient(messages chan *config.Message) {
|
|||||||
b.handleProps(rmsg, message)
|
b.handleProps(rmsg, message)
|
||||||
|
|
||||||
// create a text for bridges that don't support native editing
|
// create a text for bridges that don't support native editing
|
||||||
if message.Raw.Event == model.WEBSOCKET_EVENT_POST_EDITED && !b.GetBool("EditDisable") {
|
if message.Raw.EventType() == model.WebsocketEventPostEdited && !b.GetBool("EditDisable") {
|
||||||
rmsg.Text = message.Text + b.GetString("EditSuffix")
|
rmsg.Text = message.Text + b.GetString("EditSuffix")
|
||||||
}
|
}
|
||||||
|
|
||||||
if message.Raw.Event == model.WEBSOCKET_EVENT_POST_DELETED {
|
if message.Raw.EventType() == model.WebsocketEventPostDeleted {
|
||||||
rmsg.Event = config.EventMsgDelete
|
rmsg.Event = config.EventMsgDelete
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -192,68 +153,6 @@ func (b *Bmattermost) handleMatterClient(messages chan *config.Message) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// nolint:cyclop
|
|
||||||
func (b *Bmattermost) handleMatterClient6(messages chan *config.Message) {
|
|
||||||
for message := range b.mc6.MessageChan {
|
|
||||||
b.Log.Debugf("%#v %#v", message.Raw.GetData(), message.Raw.EventType())
|
|
||||||
|
|
||||||
if b.skipMessage6(message) {
|
|
||||||
b.Log.Debugf("Skipped message: %#v", 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, channelName)
|
|
||||||
}
|
|
||||||
|
|
||||||
b.Log.Debugf("== Receiving event %#v", message)
|
|
||||||
|
|
||||||
rmsg := &config.Message{
|
|
||||||
Username: message.Username,
|
|
||||||
UserID: message.UserID,
|
|
||||||
Channel: channelName,
|
|
||||||
Text: message.Text,
|
|
||||||
ID: message.Post.Id,
|
|
||||||
ParentID: message.Post.RootId, // ParentID is obsolete with mattermost
|
|
||||||
Extra: make(map[string][]interface{}),
|
|
||||||
}
|
|
||||||
|
|
||||||
// handle mattermost post properties (override username and attachments)
|
|
||||||
b.handleProps6(rmsg, message)
|
|
||||||
|
|
||||||
// create a text for bridges that don't support native editing
|
|
||||||
if message.Raw.EventType() == model6.WebsocketEventPostEdited && !b.GetBool("EditDisable") {
|
|
||||||
rmsg.Text = message.Text + b.GetString("EditSuffix")
|
|
||||||
}
|
|
||||||
|
|
||||||
if message.Raw.EventType() == model6.WebsocketEventPostDeleted {
|
|
||||||
rmsg.Event = config.EventMsgDelete
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, id := range message.Post.FileIds {
|
|
||||||
err := b.handleDownloadFile(rmsg, id)
|
|
||||||
if err != nil {
|
|
||||||
b.Log.Errorf("download failed: %s", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Use nickname instead of username if defined
|
|
||||||
if !b.GetBool("useusername") {
|
|
||||||
if nick := b.mc6.GetNickName(rmsg.UserID); nick != "" {
|
|
||||||
rmsg.Username = nick
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
messages <- rmsg
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *Bmattermost) handleMatterHook(messages chan *config.Message) {
|
func (b *Bmattermost) handleMatterHook(messages chan *config.Message) {
|
||||||
for {
|
for {
|
||||||
message := b.mh.Receive()
|
message := b.mh.Receive()
|
||||||
@ -268,12 +167,7 @@ func (b *Bmattermost) handleMatterHook(messages chan *config.Message) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// handleUploadFile handles native upload of files
|
|
||||||
func (b *Bmattermost) handleUploadFile(msg *config.Message) (string, error) {
|
func (b *Bmattermost) handleUploadFile(msg *config.Message) (string, error) {
|
||||||
if b.mc6 != nil {
|
|
||||||
return b.handleUploadFile6(msg)
|
|
||||||
}
|
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
var res, id string
|
var res, id string
|
||||||
channelID := b.getChannelID(msg.Channel)
|
channelID := b.getChannelID(msg.Channel)
|
||||||
@ -292,53 +186,8 @@ func (b *Bmattermost) handleUploadFile(msg *config.Message) (string, error) {
|
|||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// nolint:forcetypeassert,wrapcheck
|
|
||||||
func (b *Bmattermost) handleUploadFile6(msg *config.Message) (string, error) {
|
|
||||||
var err error
|
|
||||||
var res, id string
|
|
||||||
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)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
msg.Text = fi.Comment
|
|
||||||
if b.GetBool("PrefixMessagesWithNick") {
|
|
||||||
msg.Text = msg.Username + msg.Text
|
|
||||||
}
|
|
||||||
res, err = b.mc6.PostMessageWithFiles(channelID, msg.Text, msg.ParentID, []string{id})
|
|
||||||
}
|
|
||||||
return res, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *Bmattermost) handleProps(rmsg *config.Message, message *matterclient.Message) {
|
|
||||||
props := message.Post.Props
|
|
||||||
if props == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if _, ok := props["override_username"].(string); ok {
|
|
||||||
rmsg.Username = props["override_username"].(string)
|
|
||||||
}
|
|
||||||
if _, ok := props["attachments"].([]interface{}); ok {
|
|
||||||
rmsg.Extra["attachments"] = props["attachments"].([]interface{})
|
|
||||||
if rmsg.Text == "" {
|
|
||||||
for _, attachment := range rmsg.Extra["attachments"] {
|
|
||||||
attach := attachment.(map[string]interface{})
|
|
||||||
if attach["text"].(string) != "" {
|
|
||||||
rmsg.Text += attach["text"].(string)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if attach["fallback"].(string) != "" {
|
|
||||||
rmsg.Text += attach["fallback"].(string)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// nolint:forcetypeassert
|
// nolint:forcetypeassert
|
||||||
func (b *Bmattermost) handleProps6(rmsg *config.Message, message *matterclient6.Message) {
|
func (b *Bmattermost) handleProps(rmsg *config.Message, message *matterclient.Message) {
|
||||||
props := message.Post.Props
|
props := message.Post.Props
|
||||||
if props == nil {
|
if props == nil {
|
||||||
return
|
return
|
||||||
|
@ -6,11 +6,9 @@ import (
|
|||||||
|
|
||||||
"github.com/42wim/matterbridge/bridge/config"
|
"github.com/42wim/matterbridge/bridge/config"
|
||||||
"github.com/42wim/matterbridge/bridge/helper"
|
"github.com/42wim/matterbridge/bridge/helper"
|
||||||
"github.com/42wim/matterbridge/matterclient"
|
|
||||||
"github.com/42wim/matterbridge/matterhook"
|
"github.com/42wim/matterbridge/matterhook"
|
||||||
matterclient6 "github.com/matterbridge/matterclient"
|
"github.com/matterbridge/matterclient"
|
||||||
"github.com/mattermost/mattermost-server/v5/model"
|
"github.com/mattermost/mattermost-server/v6/model"
|
||||||
model6 "github.com/mattermost/mattermost-server/v6/model"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func (b *Bmattermost) doConnectWebhookBind() error {
|
func (b *Bmattermost) doConnectWebhookBind() error {
|
||||||
@ -24,33 +22,15 @@ func (b *Bmattermost) doConnectWebhookBind() error {
|
|||||||
})
|
})
|
||||||
case b.GetString("Token") != "":
|
case b.GetString("Token") != "":
|
||||||
b.Log.Info("Connecting using token (sending)")
|
b.Log.Info("Connecting using token (sending)")
|
||||||
b.Log.Infof("Using mattermost v6 methods: %t", b.v6)
|
err := b.apiLogin()
|
||||||
|
if err != nil {
|
||||||
if b.v6 {
|
return err
|
||||||
err := b.apiLogin6()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
err := b.apiLogin()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
case b.GetString("Login") != "":
|
case b.GetString("Login") != "":
|
||||||
b.Log.Info("Connecting using login/password (sending)")
|
b.Log.Info("Connecting using login/password (sending)")
|
||||||
b.Log.Infof("Using mattermost v6 methods: %t", b.v6)
|
err := b.apiLogin()
|
||||||
|
if err != nil {
|
||||||
if b.v6 {
|
return err
|
||||||
err := b.apiLogin6()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
err := b.apiLogin()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
b.Log.Info("Connecting using webhookbindaddress (receiving)")
|
b.Log.Info("Connecting using webhookbindaddress (receiving)")
|
||||||
@ -72,45 +52,28 @@ func (b *Bmattermost) doConnectWebhookURL() error {
|
|||||||
})
|
})
|
||||||
if b.GetString("Token") != "" {
|
if b.GetString("Token") != "" {
|
||||||
b.Log.Info("Connecting using token (receiving)")
|
b.Log.Info("Connecting using token (receiving)")
|
||||||
b.Log.Infof("Using mattermost v6 methods: %t", b.v6)
|
err := b.apiLogin()
|
||||||
|
if err != nil {
|
||||||
if b.v6 {
|
return err
|
||||||
err := b.apiLogin6()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
err := b.apiLogin()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else if b.GetString("Login") != "" {
|
} else if b.GetString("Login") != "" {
|
||||||
b.Log.Info("Connecting using login/password (receiving)")
|
b.Log.Info("Connecting using login/password (receiving)")
|
||||||
b.Log.Infof("Using mattermost v6 methods: %t", b.v6)
|
err := b.apiLogin()
|
||||||
|
if err != nil {
|
||||||
if b.v6 {
|
return err
|
||||||
err := b.apiLogin6()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
err := b.apiLogin()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// nolint:wrapcheck
|
||||||
func (b *Bmattermost) apiLogin() error {
|
func (b *Bmattermost) apiLogin() error {
|
||||||
password := b.GetString("Password")
|
password := b.GetString("Password")
|
||||||
if b.GetString("Token") != "" {
|
if b.GetString("Token") != "" {
|
||||||
password = "token=" + b.GetString("Token")
|
password = "token=" + b.GetString("Token")
|
||||||
}
|
}
|
||||||
|
|
||||||
b.mc = matterclient.New(b.GetString("Login"), password, b.GetString("Team"), b.GetString("Server"))
|
b.mc = matterclient.New(b.GetString("Login"), password, b.GetString("Team"), b.GetString("Server"), "")
|
||||||
if b.GetBool("debug") {
|
if b.GetBool("debug") {
|
||||||
b.mc.SetLogLevel("debug")
|
b.mc.SetLogLevel("debug")
|
||||||
}
|
}
|
||||||
@ -118,39 +81,13 @@ func (b *Bmattermost) apiLogin() error {
|
|||||||
b.mc.SkipVersionCheck = b.GetBool("SkipVersionCheck")
|
b.mc.SkipVersionCheck = b.GetBool("SkipVersionCheck")
|
||||||
b.mc.NoTLS = b.GetBool("NoTLS")
|
b.mc.NoTLS = b.GetBool("NoTLS")
|
||||||
b.Log.Infof("Connecting %s (team: %s) on %s", b.GetString("Login"), b.GetString("Team"), b.GetString("Server"))
|
b.Log.Infof("Connecting %s (team: %s) on %s", b.GetString("Login"), b.GetString("Team"), b.GetString("Server"))
|
||||||
err := b.mc.Login()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
b.Log.Info("Connection succeeded")
|
|
||||||
b.TeamID = b.mc.GetTeamId()
|
|
||||||
go b.mc.WsReceiver()
|
|
||||||
go b.mc.StatusLoop()
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// nolint:wrapcheck
|
if err := b.mc.Login(); err != nil {
|
||||||
func (b *Bmattermost) apiLogin6() error {
|
|
||||||
password := b.GetString("Password")
|
|
||||||
if b.GetString("Token") != "" {
|
|
||||||
password = "token=" + b.GetString("Token")
|
|
||||||
}
|
|
||||||
|
|
||||||
b.mc6 = matterclient6.New(b.GetString("Login"), password, b.GetString("Team"), b.GetString("Server"), "")
|
|
||||||
if b.GetBool("debug") {
|
|
||||||
b.mc6.SetLogLevel("debug")
|
|
||||||
}
|
|
||||||
b.mc6.SkipTLSVerify = b.GetBool("SkipTLSVerify")
|
|
||||||
b.mc6.SkipVersionCheck = b.GetBool("SkipVersionCheck")
|
|
||||||
b.mc6.NoTLS = b.GetBool("NoTLS")
|
|
||||||
b.Log.Infof("Connecting %s (team: %s) on %s", b.GetString("Login"), b.GetString("Team"), b.GetString("Server"))
|
|
||||||
|
|
||||||
if err := b.mc6.Login(); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
b.Log.Info("Connection succeeded")
|
b.Log.Info("Connection succeeded")
|
||||||
b.TeamID = b.mc6.GetTeamID()
|
b.TeamID = b.mc.GetTeamID()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -234,6 +171,7 @@ func (b *Bmattermost) sendWebhook(msg config.Message) (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// skipMessages returns true if this message should not be handled
|
// skipMessages returns true if this message should not be handled
|
||||||
|
// nolint:gocyclo,cyclop
|
||||||
func (b *Bmattermost) skipMessage(message *matterclient.Message) bool {
|
func (b *Bmattermost) skipMessage(message *matterclient.Message) bool {
|
||||||
// Handle join/leave
|
// Handle join/leave
|
||||||
if message.Type == "system_join_leave" ||
|
if message.Type == "system_join_leave" ||
|
||||||
@ -260,76 +198,7 @@ func (b *Bmattermost) skipMessage(message *matterclient.Message) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Handle edited messages
|
// Handle edited messages
|
||||||
if (message.Raw.Event == model.WEBSOCKET_EVENT_POST_EDITED) && b.GetBool("EditDisable") {
|
if (message.Raw.EventType() == model.WebsocketEventPostEdited) && b.GetBool("EditDisable") {
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ignore non-post messages
|
|
||||||
if message.Post == nil {
|
|
||||||
b.Log.Debugf("ignoring nil message.Post: %#v", message)
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ignore messages sent from matterbridge
|
|
||||||
if message.Post.Props != nil {
|
|
||||||
if _, ok := message.Post.Props["matterbridge_"+b.uuid].(bool); ok {
|
|
||||||
b.Log.Debugf("sent by matterbridge, ignoring")
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ignore messages sent from a user logged in as the bot
|
|
||||||
if b.mc.User.Username == message.Username {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
// if the message has reactions don't repost it (for now, until we can correlate reaction with message)
|
|
||||||
if message.Post.HasReactions {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
// ignore messages from other teams than ours
|
|
||||||
if message.Raw.Data["team_id"].(string) != b.TeamID {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
// only handle posted, edited or deleted events
|
|
||||||
if !(message.Raw.Event == "posted" || message.Raw.Event == model.WEBSOCKET_EVENT_POST_EDITED ||
|
|
||||||
message.Raw.Event == model.WEBSOCKET_EVENT_POST_DELETED) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// skipMessages returns true if this message should not be handled
|
|
||||||
// nolint:gocyclo,cyclop
|
|
||||||
func (b *Bmattermost) skipMessage6(message *matterclient6.Message) bool {
|
|
||||||
// Handle join/leave
|
|
||||||
if message.Type == "system_join_leave" ||
|
|
||||||
message.Type == "system_join_channel" ||
|
|
||||||
message.Type == "system_leave_channel" {
|
|
||||||
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: channelName,
|
|
||||||
Account: b.Account,
|
|
||||||
Event: config.EventJoinLeave,
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle edited messages
|
|
||||||
if (message.Raw.EventType() == model6.WebsocketEventPostEdited) && b.GetBool("EditDisable") {
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -348,7 +217,7 @@ func (b *Bmattermost) skipMessage6(message *matterclient6.Message) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Ignore messages sent from a user logged in as the bot
|
// Ignore messages sent from a user logged in as the bot
|
||||||
if b.mc6.User.Username == message.Username {
|
if b.mc.User.Username == message.Username {
|
||||||
b.Log.Debug("message from same user as bot, ignoring")
|
b.Log.Debug("message from same user as bot, ignoring")
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
@ -365,8 +234,8 @@ func (b *Bmattermost) skipMessage6(message *matterclient6.Message) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// only handle posted, edited or deleted events
|
// only handle posted, edited or deleted events
|
||||||
if !(message.Raw.EventType() == "posted" || message.Raw.EventType() == model6.WebsocketEventPostEdited ||
|
if !(message.Raw.EventType() == "posted" || message.Raw.EventType() == model.WebsocketEventPostEdited ||
|
||||||
message.Raw.EventType() == model6.WebsocketEventPostDeleted) {
|
message.Raw.EventType() == model.WebsocketEventPostDeleted) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
@ -396,11 +265,7 @@ func (b *Bmattermost) getChannelID(name string) string {
|
|||||||
return idcheck[1]
|
return idcheck[1]
|
||||||
}
|
}
|
||||||
|
|
||||||
if b.mc6 != nil {
|
return b.mc.GetChannelID(name, b.TeamID)
|
||||||
return b.mc6.GetChannelID(name, b.TeamID)
|
|
||||||
}
|
|
||||||
|
|
||||||
return b.mc.GetChannelId(name, b.TeamID)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bmattermost) getChannelName(id string) string {
|
func (b *Bmattermost) getChannelName(id string) string {
|
||||||
|
@ -9,16 +9,14 @@ import (
|
|||||||
"github.com/42wim/matterbridge/bridge"
|
"github.com/42wim/matterbridge/bridge"
|
||||||
"github.com/42wim/matterbridge/bridge/config"
|
"github.com/42wim/matterbridge/bridge/config"
|
||||||
"github.com/42wim/matterbridge/bridge/helper"
|
"github.com/42wim/matterbridge/bridge/helper"
|
||||||
"github.com/42wim/matterbridge/matterclient"
|
|
||||||
"github.com/42wim/matterbridge/matterhook"
|
"github.com/42wim/matterbridge/matterhook"
|
||||||
matterclient6 "github.com/matterbridge/matterclient"
|
"github.com/matterbridge/matterclient"
|
||||||
"github.com/rs/xid"
|
"github.com/rs/xid"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Bmattermost struct {
|
type Bmattermost struct {
|
||||||
mh *matterhook.Client
|
mh *matterhook.Client
|
||||||
mc *matterclient.MMClient
|
mc *matterclient.Client
|
||||||
mc6 *matterclient6.Client
|
|
||||||
v6 bool
|
v6 bool
|
||||||
uuid string
|
uuid string
|
||||||
TeamID string
|
TeamID string
|
||||||
@ -74,34 +72,17 @@ func (b *Bmattermost) Connect() error {
|
|||||||
return nil
|
return nil
|
||||||
case b.GetString("Token") != "":
|
case b.GetString("Token") != "":
|
||||||
b.Log.Info("Connecting using token (sending and receiving)")
|
b.Log.Info("Connecting using token (sending and receiving)")
|
||||||
b.Log.Infof("Using mattermost v6 methods: %t", b.v6)
|
err := b.apiLogin()
|
||||||
|
if err != nil {
|
||||||
if b.v6 {
|
return err
|
||||||
err := b.apiLogin6()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
err := b.apiLogin()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
go b.handleMatter()
|
go b.handleMatter()
|
||||||
case b.GetString("Login") != "":
|
case b.GetString("Login") != "":
|
||||||
b.Log.Info("Connecting using login/password (sending and receiving)")
|
b.Log.Info("Connecting using login/password (sending and receiving)")
|
||||||
b.Log.Infof("Using mattermost v6 methods: %t", b.v6)
|
b.Log.Infof("Using mattermost v6 methods: %t", b.v6)
|
||||||
|
err := b.apiLogin()
|
||||||
if b.v6 {
|
if err != nil {
|
||||||
err := b.apiLogin6()
|
return err
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
err := b.apiLogin()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
go b.handleMatter()
|
go b.handleMatter()
|
||||||
}
|
}
|
||||||
@ -132,10 +113,6 @@ func (b *Bmattermost) JoinChannel(channel config.ChannelInfo) error {
|
|||||||
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
if b.mc6 != nil {
|
|
||||||
return b.mc6.JoinChannel(id) // nolint:wrapcheck
|
|
||||||
}
|
|
||||||
|
|
||||||
return b.mc.JoinChannel(id)
|
return b.mc.JoinChannel(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -168,9 +145,6 @@ func (b *Bmattermost) Send(msg config.Message) (string, error) {
|
|||||||
if msg.ID == "" {
|
if msg.ID == "" {
|
||||||
return "", nil
|
return "", nil
|
||||||
}
|
}
|
||||||
if b.mc6 != nil {
|
|
||||||
return msg.ID, b.mc6.DeleteMessage(msg.ID) // nolint:wrapcheck
|
|
||||||
}
|
|
||||||
|
|
||||||
return msg.ID, b.mc.DeleteMessage(msg.ID)
|
return msg.ID, b.mc.DeleteMessage(msg.ID)
|
||||||
}
|
}
|
||||||
@ -183,36 +157,20 @@ 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)
|
// we only can reply to the root of the thread, not to a specific ID (like discord for example does)
|
||||||
if msg.ParentID != "" {
|
if msg.ParentID != "" {
|
||||||
if b.mc6 != nil {
|
post, _, err := b.mc.Client.GetPost(msg.ParentID, "")
|
||||||
post, _, err := b.mc6.Client.GetPost(msg.ParentID, "")
|
if err != nil {
|
||||||
if err != nil {
|
b.Log.Errorf("getting post %s failed: %s", msg.ParentID, err)
|
||||||
b.Log.Errorf("getting post %s failed: %s", msg.ParentID, err)
|
}
|
||||||
}
|
if post.RootId != "" {
|
||||||
if post.RootId != "" {
|
msg.ParentID = post.RootId
|
||||||
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)
|
|
||||||
}
|
|
||||||
if post.RootId != "" {
|
|
||||||
msg.ParentID = post.RootId
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Upload a file if it exists
|
// Upload a file if it exists
|
||||||
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 _, err := b.mc.PostMessage(b.getChannelID(rmsg.Channel), 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 {
|
|
||||||
if _, err := b.mc.PostMessage(b.getChannelID(rmsg.Channel), rmsg.Username+rmsg.Text, msg.ParentID); err != nil {
|
|
||||||
b.Log.Errorf("PostMessage failed: %s", err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if len(msg.Extra["file"]) > 0 {
|
if len(msg.Extra["file"]) > 0 {
|
||||||
@ -227,17 +185,9 @@ func (b *Bmattermost) Send(msg config.Message) (string, error) {
|
|||||||
|
|
||||||
// Edit message if we have an ID
|
// Edit message if we have an ID
|
||||||
if msg.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)
|
return b.mc.EditMessage(msg.ID, msg.Text)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Post normal message
|
// Post normal message
|
||||||
if b.mc6 != nil {
|
|
||||||
return b.mc6.PostMessage(b.getChannelID(msg.Channel), msg.Text, msg.ParentID) // nolint:wrapcheck
|
|
||||||
}
|
|
||||||
|
|
||||||
return b.mc.PostMessage(b.getChannelID(msg.Channel), msg.Text, msg.ParentID)
|
return b.mc.PostMessage(b.getChannelID(msg.Channel), msg.Text, msg.ParentID)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user