mirror of
https://github.com/cwinfo/matterbridge.git
synced 2024-11-25 20:11:36 +00:00
Make goconst linter happy
This commit is contained in:
parent
1e0bb3da95
commit
b2a07aba3a
@ -10,6 +10,7 @@ import (
|
|||||||
"github.com/42wim/matterbridge/bridge/helper"
|
"github.com/42wim/matterbridge/bridge/helper"
|
||||||
"github.com/42wim/matterbridge/matterclient"
|
"github.com/42wim/matterbridge/matterclient"
|
||||||
"github.com/42wim/matterbridge/matterhook"
|
"github.com/42wim/matterbridge/matterhook"
|
||||||
|
"github.com/mattermost/platform/model"
|
||||||
"github.com/rs/xid"
|
"github.com/rs/xid"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -239,11 +240,11 @@ func (b *Bmattermost) handleMatterClient(messages chan *config.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 == "post_edited" && !b.GetBool("EditDisable") {
|
if message.Raw.Event == model.WEBSOCKET_EVENT_POST_EDITED && !b.GetBool("EditDisable") {
|
||||||
rmsg.Text = message.Text + b.GetString("EditSuffix")
|
rmsg.Text = message.Text + b.GetString("EditSuffix")
|
||||||
}
|
}
|
||||||
|
|
||||||
if message.Raw.Event == "post_deleted" {
|
if message.Raw.Event == model.WEBSOCKET_EVENT_POST_DELETED {
|
||||||
rmsg.Event = config.EVENT_MSG_DELETE
|
rmsg.Event = config.EVENT_MSG_DELETE
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -434,7 +435,7 @@ func (b *Bmattermost) skipMessage(message *matterclient.Message) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Handle edited messages
|
// Handle edited messages
|
||||||
if (message.Raw.Event == "post_edited") && b.GetBool("EditDisable") {
|
if (message.Raw.Event == model.WEBSOCKET_EVENT_POST_EDITED) && b.GetBool("EditDisable") {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -462,7 +463,7 @@ func (b *Bmattermost) skipMessage(message *matterclient.Message) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// only handle posted, edited or deleted events
|
// only handle posted, edited or deleted events
|
||||||
if !(message.Raw.Event == "posted" || message.Raw.Event == "post_edited" || message.Raw.Event == "post_deleted") {
|
if !(message.Raw.Event == "posted" || message.Raw.Event == model.WEBSOCKET_EVENT_POST_EDITED || message.Raw.Event == model.WEBSOCKET_EVENT_POST_DELETED) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
|
@ -12,6 +12,12 @@ import (
|
|||||||
"github.com/go-telegram-bot-api/telegram-bot-api"
|
"github.com/go-telegram-bot-api/telegram-bot-api"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
unknownUser = "unknown"
|
||||||
|
HTMLFormat = "HTML"
|
||||||
|
HTMLNick = "htmlnick"
|
||||||
|
)
|
||||||
|
|
||||||
type Btelegram struct {
|
type Btelegram struct {
|
||||||
c *tgbotapi.BotAPI
|
c *tgbotapi.BotAPI
|
||||||
*bridge.Config
|
*bridge.Config
|
||||||
@ -64,7 +70,7 @@ func (b *Btelegram) Send(msg config.Message) (string, error) {
|
|||||||
return b.cacheAvatar(&msg)
|
return b.cacheAvatar(&msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
if b.GetString("MessageFormat") == "HTML" {
|
if b.GetString("MessageFormat") == HTMLFormat {
|
||||||
msg.Text = makeHTML(msg.Text)
|
msg.Text = makeHTML(msg.Text)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -98,12 +104,12 @@ func (b *Btelegram) Send(msg config.Message) (string, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
if strings.ToLower(b.GetString("MessageFormat")) == "htmlnick" {
|
if strings.ToLower(b.GetString("MessageFormat")) == HTMLNick {
|
||||||
b.Log.Debug("Using mode HTML - nick only")
|
b.Log.Debug("Using mode HTML - nick only")
|
||||||
msg.Text = html.EscapeString(msg.Text)
|
msg.Text = html.EscapeString(msg.Text)
|
||||||
}
|
}
|
||||||
m := tgbotapi.NewEditMessageText(chatid, msgid, msg.Username+msg.Text)
|
m := tgbotapi.NewEditMessageText(chatid, msgid, msg.Username+msg.Text)
|
||||||
if b.GetString("MessageFormat") == "HTML" {
|
if b.GetString("MessageFormat") == HTMLFormat {
|
||||||
b.Log.Debug("Using mode HTML")
|
b.Log.Debug("Using mode HTML")
|
||||||
m.ParseMode = tgbotapi.ModeHTML
|
m.ParseMode = tgbotapi.ModeHTML
|
||||||
}
|
}
|
||||||
@ -111,7 +117,7 @@ func (b *Btelegram) Send(msg config.Message) (string, error) {
|
|||||||
b.Log.Debug("Using mode markdown")
|
b.Log.Debug("Using mode markdown")
|
||||||
m.ParseMode = tgbotapi.ModeMarkdown
|
m.ParseMode = tgbotapi.ModeMarkdown
|
||||||
}
|
}
|
||||||
if strings.ToLower(b.GetString("MessageFormat")) == "htmlnick" {
|
if strings.ToLower(b.GetString("MessageFormat")) == HTMLNick {
|
||||||
b.Log.Debug("Using mode HTML - nick only")
|
b.Log.Debug("Using mode HTML - nick only")
|
||||||
m.ParseMode = tgbotapi.ModeHTML
|
m.ParseMode = tgbotapi.ModeHTML
|
||||||
}
|
}
|
||||||
@ -187,7 +193,7 @@ func (b *Btelegram) handleRecv(updates <-chan tgbotapi.Update) {
|
|||||||
|
|
||||||
// if we really didn't find a username, set it to unknown
|
// if we really didn't find a username, set it to unknown
|
||||||
if rmsg.Username == "" {
|
if rmsg.Username == "" {
|
||||||
rmsg.Username = "unknown"
|
rmsg.Username = unknownUser
|
||||||
}
|
}
|
||||||
|
|
||||||
// handle any downloads
|
// handle any downloads
|
||||||
@ -209,7 +215,7 @@ func (b *Btelegram) handleRecv(updates <-chan tgbotapi.Update) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if usernameForward == "" {
|
if usernameForward == "" {
|
||||||
usernameForward = "unknown"
|
usernameForward = unknownUser
|
||||||
}
|
}
|
||||||
rmsg.Text = "Forwarded from " + usernameForward + ": " + rmsg.Text
|
rmsg.Text = "Forwarded from " + usernameForward + ": " + rmsg.Text
|
||||||
}
|
}
|
||||||
@ -229,7 +235,7 @@ func (b *Btelegram) handleRecv(updates <-chan tgbotapi.Update) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if usernameReply == "" {
|
if usernameReply == "" {
|
||||||
usernameReply = "unknown"
|
usernameReply = unknownUser
|
||||||
}
|
}
|
||||||
if !b.GetBool("QuoteDisable") {
|
if !b.GetBool("QuoteDisable") {
|
||||||
rmsg.Text = b.handleQuote(rmsg.Text, usernameReply, message.ReplyToMessage.Text)
|
rmsg.Text = b.handleQuote(rmsg.Text, usernameReply, message.ReplyToMessage.Text)
|
||||||
@ -401,7 +407,7 @@ func (b *Btelegram) handleUploadFile(msg *config.Message, chatid int64) (string,
|
|||||||
func (b *Btelegram) sendMessage(chatid int64, username, text string) (string, error) {
|
func (b *Btelegram) sendMessage(chatid int64, username, text string) (string, error) {
|
||||||
m := tgbotapi.NewMessage(chatid, "")
|
m := tgbotapi.NewMessage(chatid, "")
|
||||||
m.Text = username + text
|
m.Text = username + text
|
||||||
if b.GetString("MessageFormat") == "HTML" {
|
if b.GetString("MessageFormat") == HTMLFormat {
|
||||||
b.Log.Debug("Using mode HTML")
|
b.Log.Debug("Using mode HTML")
|
||||||
m.ParseMode = tgbotapi.ModeHTML
|
m.ParseMode = tgbotapi.ModeHTML
|
||||||
}
|
}
|
||||||
@ -409,7 +415,7 @@ func (b *Btelegram) sendMessage(chatid int64, username, text string) (string, er
|
|||||||
b.Log.Debug("Using mode markdown")
|
b.Log.Debug("Using mode markdown")
|
||||||
m.ParseMode = tgbotapi.ModeMarkdown
|
m.ParseMode = tgbotapi.ModeMarkdown
|
||||||
}
|
}
|
||||||
if strings.ToLower(b.GetString("MessageFormat")) == "htmlnick" {
|
if strings.ToLower(b.GetString("MessageFormat")) == HTMLNick {
|
||||||
b.Log.Debug("Using mode HTML - nick only")
|
b.Log.Debug("Using mode HTML - nick only")
|
||||||
m.Text = username + html.EscapeString(text)
|
m.Text = username + html.EscapeString(text)
|
||||||
m.ParseMode = tgbotapi.ModeHTML
|
m.ParseMode = tgbotapi.ModeHTML
|
||||||
|
@ -70,6 +70,10 @@ var bridgeMap = map[string]bridge.Factory{
|
|||||||
"zulip": bzulip.New,
|
"zulip": bzulip.New,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
apiProtocol = "api"
|
||||||
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
flog = log.WithFields(log.Fields{"prefix": "gateway"})
|
flog = log.WithFields(log.Fields{"prefix": "gateway"})
|
||||||
}
|
}
|
||||||
@ -158,7 +162,7 @@ RECONNECT:
|
|||||||
func (gw *Gateway) mapChannelConfig(cfg []config.Bridge, direction string) {
|
func (gw *Gateway) mapChannelConfig(cfg []config.Bridge, direction string) {
|
||||||
for _, br := range cfg {
|
for _, br := range cfg {
|
||||||
if isApi(br.Account) {
|
if isApi(br.Account) {
|
||||||
br.Channel = "api"
|
br.Channel = apiProtocol
|
||||||
}
|
}
|
||||||
// make sure to lowercase irc channels in config #348
|
// make sure to lowercase irc channels in config #348
|
||||||
if strings.HasPrefix(br.Account, "irc.") {
|
if strings.HasPrefix(br.Account, "irc.") {
|
||||||
@ -191,7 +195,7 @@ func (gw *Gateway) getDestChannel(msg *config.Message, dest bridge.Bridge) []con
|
|||||||
var channels []config.ChannelInfo
|
var channels []config.ChannelInfo
|
||||||
|
|
||||||
// for messages received from the api check that the gateway is the specified one
|
// for messages received from the api check that the gateway is the specified one
|
||||||
if msg.Protocol == "api" && gw.Name != msg.Gateway {
|
if msg.Protocol == apiProtocol && gw.Name != msg.Gateway {
|
||||||
return channels
|
return channels
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -310,7 +314,7 @@ func (gw *Gateway) handleMessage(msg config.Message, dest *bridge.Bridge) []*BrM
|
|||||||
msg.ID = gw.getDestMsgID(origmsg.ID, dest, channel)
|
msg.ID = gw.getDestMsgID(origmsg.ID, dest, channel)
|
||||||
|
|
||||||
// for api we need originchannel as channel
|
// for api we need originchannel as channel
|
||||||
if dest.Protocol == "api" {
|
if dest.Protocol == apiProtocol {
|
||||||
msg.Channel = originchannel
|
msg.Channel = originchannel
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -459,7 +463,7 @@ func (gw *Gateway) modifyMessage(msg *config.Message) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// messages from api have Gateway specified, don't overwrite
|
// messages from api have Gateway specified, don't overwrite
|
||||||
if msg.Protocol != "api" {
|
if msg.Protocol != apiProtocol {
|
||||||
msg.Gateway = gw.Name
|
msg.Gateway = gw.Name
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -152,6 +152,12 @@ enable=true
|
|||||||
channel="--333333333333"
|
channel="--333333333333"
|
||||||
`)
|
`)
|
||||||
|
|
||||||
|
const (
|
||||||
|
ircTestAccount = "irc.zzz"
|
||||||
|
tgTestAccount = "telegram.zzz"
|
||||||
|
slackTestAccount = "slack.zzz"
|
||||||
|
)
|
||||||
|
|
||||||
func maketestRouter(input []byte) *Router {
|
func maketestRouter(input []byte) *Router {
|
||||||
cfg := config.NewConfigFromString(input)
|
cfg := config.NewConfigFromString(input)
|
||||||
r, err := NewRouter(cfg)
|
r, err := NewRouter(cfg)
|
||||||
@ -248,31 +254,31 @@ func TestGetDestChannelAdvanced(t *testing.T) {
|
|||||||
switch gw.Name {
|
switch gw.Name {
|
||||||
case "bridge":
|
case "bridge":
|
||||||
if (msg.Channel == "#main" || msg.Channel == "-1111111111111" || msg.Channel == "irc") &&
|
if (msg.Channel == "#main" || msg.Channel == "-1111111111111" || msg.Channel == "irc") &&
|
||||||
(msg.Account == "irc.zzz" || msg.Account == "telegram.zzz" || msg.Account == "slack.zzz") {
|
(msg.Account == ircTestAccount || msg.Account == tgTestAccount || msg.Account == slackTestAccount) {
|
||||||
hits[gw.Name]++
|
hits[gw.Name]++
|
||||||
switch br.Account {
|
switch br.Account {
|
||||||
case "irc.zzz":
|
case ircTestAccount:
|
||||||
assert.Equal(t, []config.ChannelInfo{{
|
assert.Equal(t, []config.ChannelInfo{{
|
||||||
Name: "#main",
|
Name: "#main",
|
||||||
Account: "irc.zzz",
|
Account: ircTestAccount,
|
||||||
Direction: "inout",
|
Direction: "inout",
|
||||||
ID: "#mainirc.zzz",
|
ID: "#mainirc.zzz",
|
||||||
SameChannel: map[string]bool{"bridge": false},
|
SameChannel: map[string]bool{"bridge": false},
|
||||||
Options: config.ChannelOptions{Key: ""},
|
Options: config.ChannelOptions{Key: ""},
|
||||||
}}, channels)
|
}}, channels)
|
||||||
case "telegram.zzz":
|
case tgTestAccount:
|
||||||
assert.Equal(t, []config.ChannelInfo{{
|
assert.Equal(t, []config.ChannelInfo{{
|
||||||
Name: "-1111111111111",
|
Name: "-1111111111111",
|
||||||
Account: "telegram.zzz",
|
Account: tgTestAccount,
|
||||||
Direction: "inout",
|
Direction: "inout",
|
||||||
ID: "-1111111111111telegram.zzz",
|
ID: "-1111111111111telegram.zzz",
|
||||||
SameChannel: map[string]bool{"bridge": false},
|
SameChannel: map[string]bool{"bridge": false},
|
||||||
Options: config.ChannelOptions{Key: ""},
|
Options: config.ChannelOptions{Key: ""},
|
||||||
}}, channels)
|
}}, channels)
|
||||||
case "slack.zzz":
|
case slackTestAccount:
|
||||||
assert.Equal(t, []config.ChannelInfo{{
|
assert.Equal(t, []config.ChannelInfo{{
|
||||||
Name: "irc",
|
Name: "irc",
|
||||||
Account: "slack.zzz",
|
Account: slackTestAccount,
|
||||||
Direction: "inout",
|
Direction: "inout",
|
||||||
ID: "ircslack.zzz",
|
ID: "ircslack.zzz",
|
||||||
SameChannel: map[string]bool{"bridge": false},
|
SameChannel: map[string]bool{"bridge": false},
|
||||||
@ -282,22 +288,22 @@ func TestGetDestChannelAdvanced(t *testing.T) {
|
|||||||
}
|
}
|
||||||
case "bridge2":
|
case "bridge2":
|
||||||
if (msg.Channel == "#main-help" || msg.Channel == "--444444444444") &&
|
if (msg.Channel == "#main-help" || msg.Channel == "--444444444444") &&
|
||||||
(msg.Account == "irc.zzz" || msg.Account == "telegram.zzz") {
|
(msg.Account == ircTestAccount || msg.Account == tgTestAccount) {
|
||||||
hits[gw.Name]++
|
hits[gw.Name]++
|
||||||
switch br.Account {
|
switch br.Account {
|
||||||
case "irc.zzz":
|
case ircTestAccount:
|
||||||
assert.Equal(t, []config.ChannelInfo{{
|
assert.Equal(t, []config.ChannelInfo{{
|
||||||
Name: "#main-help",
|
Name: "#main-help",
|
||||||
Account: "irc.zzz",
|
Account: ircTestAccount,
|
||||||
Direction: "inout",
|
Direction: "inout",
|
||||||
ID: "#main-helpirc.zzz",
|
ID: "#main-helpirc.zzz",
|
||||||
SameChannel: map[string]bool{"bridge2": false},
|
SameChannel: map[string]bool{"bridge2": false},
|
||||||
Options: config.ChannelOptions{Key: ""},
|
Options: config.ChannelOptions{Key: ""},
|
||||||
}}, channels)
|
}}, channels)
|
||||||
case "telegram.zzz":
|
case tgTestAccount:
|
||||||
assert.Equal(t, []config.ChannelInfo{{
|
assert.Equal(t, []config.ChannelInfo{{
|
||||||
Name: "--444444444444",
|
Name: "--444444444444",
|
||||||
Account: "telegram.zzz",
|
Account: tgTestAccount,
|
||||||
Direction: "inout",
|
Direction: "inout",
|
||||||
ID: "--444444444444telegram.zzz",
|
ID: "--444444444444telegram.zzz",
|
||||||
SameChannel: map[string]bool{"bridge2": false},
|
SameChannel: map[string]bool{"bridge2": false},
|
||||||
@ -307,22 +313,22 @@ func TestGetDestChannelAdvanced(t *testing.T) {
|
|||||||
}
|
}
|
||||||
case "bridge3":
|
case "bridge3":
|
||||||
if (msg.Channel == "#main-telegram" || msg.Channel == "--333333333333") &&
|
if (msg.Channel == "#main-telegram" || msg.Channel == "--333333333333") &&
|
||||||
(msg.Account == "irc.zzz" || msg.Account == "telegram.zzz") {
|
(msg.Account == ircTestAccount || msg.Account == tgTestAccount) {
|
||||||
hits[gw.Name]++
|
hits[gw.Name]++
|
||||||
switch br.Account {
|
switch br.Account {
|
||||||
case "irc.zzz":
|
case ircTestAccount:
|
||||||
assert.Equal(t, []config.ChannelInfo{{
|
assert.Equal(t, []config.ChannelInfo{{
|
||||||
Name: "#main-telegram",
|
Name: "#main-telegram",
|
||||||
Account: "irc.zzz",
|
Account: ircTestAccount,
|
||||||
Direction: "inout",
|
Direction: "inout",
|
||||||
ID: "#main-telegramirc.zzz",
|
ID: "#main-telegramirc.zzz",
|
||||||
SameChannel: map[string]bool{"bridge3": false},
|
SameChannel: map[string]bool{"bridge3": false},
|
||||||
Options: config.ChannelOptions{Key: ""},
|
Options: config.ChannelOptions{Key: ""},
|
||||||
}}, channels)
|
}}, channels)
|
||||||
case "telegram.zzz":
|
case tgTestAccount:
|
||||||
assert.Equal(t, []config.ChannelInfo{{
|
assert.Equal(t, []config.ChannelInfo{{
|
||||||
Name: "--333333333333",
|
Name: "--333333333333",
|
||||||
Account: "telegram.zzz",
|
Account: tgTestAccount,
|
||||||
Direction: "inout",
|
Direction: "inout",
|
||||||
ID: "--333333333333telegram.zzz",
|
ID: "--333333333333telegram.zzz",
|
||||||
SameChannel: map[string]bool{"bridge3": false},
|
SameChannel: map[string]bool{"bridge3": false},
|
||||||
@ -337,11 +343,11 @@ func TestGetDestChannelAdvanced(t *testing.T) {
|
|||||||
}
|
}
|
||||||
hits[gw.Name]++
|
hits[gw.Name]++
|
||||||
switch br.Account {
|
switch br.Account {
|
||||||
case "irc.zzz":
|
case ircTestAccount:
|
||||||
assert.Len(t, channels, 2)
|
assert.Len(t, channels, 2)
|
||||||
assert.Contains(t, channels, config.ChannelInfo{
|
assert.Contains(t, channels, config.ChannelInfo{
|
||||||
Name: "#main",
|
Name: "#main",
|
||||||
Account: "irc.zzz",
|
Account: ircTestAccount,
|
||||||
Direction: "out",
|
Direction: "out",
|
||||||
ID: "#mainirc.zzz",
|
ID: "#mainirc.zzz",
|
||||||
SameChannel: map[string]bool{"announcements": false},
|
SameChannel: map[string]bool{"announcements": false},
|
||||||
@ -349,25 +355,25 @@ func TestGetDestChannelAdvanced(t *testing.T) {
|
|||||||
})
|
})
|
||||||
assert.Contains(t, channels, config.ChannelInfo{
|
assert.Contains(t, channels, config.ChannelInfo{
|
||||||
Name: "#main-help",
|
Name: "#main-help",
|
||||||
Account: "irc.zzz",
|
Account: ircTestAccount,
|
||||||
Direction: "out",
|
Direction: "out",
|
||||||
ID: "#main-helpirc.zzz",
|
ID: "#main-helpirc.zzz",
|
||||||
SameChannel: map[string]bool{"announcements": false},
|
SameChannel: map[string]bool{"announcements": false},
|
||||||
Options: config.ChannelOptions{Key: ""},
|
Options: config.ChannelOptions{Key: ""},
|
||||||
})
|
})
|
||||||
case "slack.zzz":
|
case slackTestAccount:
|
||||||
assert.Equal(t, []config.ChannelInfo{{
|
assert.Equal(t, []config.ChannelInfo{{
|
||||||
Name: "general",
|
Name: "general",
|
||||||
Account: "slack.zzz",
|
Account: slackTestAccount,
|
||||||
Direction: "out",
|
Direction: "out",
|
||||||
ID: "generalslack.zzz",
|
ID: "generalslack.zzz",
|
||||||
SameChannel: map[string]bool{"announcements": false},
|
SameChannel: map[string]bool{"announcements": false},
|
||||||
Options: config.ChannelOptions{Key: ""},
|
Options: config.ChannelOptions{Key: ""},
|
||||||
}}, channels)
|
}}, channels)
|
||||||
case "telegram.zzz":
|
case tgTestAccount:
|
||||||
assert.Equal(t, []config.ChannelInfo{{
|
assert.Equal(t, []config.ChannelInfo{{
|
||||||
Name: "--333333333333",
|
Name: "--333333333333",
|
||||||
Account: "telegram.zzz",
|
Account: tgTestAccount,
|
||||||
Direction: "out",
|
Direction: "out",
|
||||||
ID: "--333333333333telegram.zzz",
|
ID: "--333333333333telegram.zzz",
|
||||||
SameChannel: map[string]bool{"announcements": false},
|
SameChannel: map[string]bool{"announcements": false},
|
||||||
|
Loading…
Reference in New Issue
Block a user