commit ff71421877045a5dfc2702886d5ae9c2ac18314e
Author: Wim <wim@42.be>
Date: Thu Nov 08 22:20:03 2018 +0000
diff --git a/bridge/mattermost/mattermost.go b/bridge/mattermost/mattermost.go
index 2e93446..5e04923 100644
--- a/bridge/mattermost/mattermost.go
+++ b/bridge/mattermost/mattermost.go
@@ -106 +107 @@ import (
"github.com/42wim/matterbridge/bridge/helper"
"github.com/42wim/matterbridge/matterclient"
"github.com/42wim/matterbridge/matterhook"
+ "github.com/mattermost/platform/model"
"github.com/rs/xid"
)
@@ -23911 +24011 @@ func (b *Bmattermost) handleMatterClient(messages chan *config.Message) {
}
// 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")
}
- if message.Raw.Event == "post_deleted" {
+ if message.Raw.Event == model.WEBSOCKET_EVENT_POST_DELETED {
rmsg.Event = config.EVENT_MSG_DELETE
}
@@ -4347 +4357 @@ func (b *Bmattermost) skipMessage(message *matterclient.Message) bool {
}
// 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
}
@@ -4627 +4637 @@ func (b *Bmattermost) skipMessage(message *matterclient.Message) bool {
}
// 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 false
diff --git a/bridge/telegram/telegram.go b/bridge/telegram/telegram.go
index 43d68ed..748ee87 100644
--- a/bridge/telegram/telegram.go
+++ b/bridge/telegram/telegram.go
@@ -126 +1212 @@ import (
"github.com/go-telegram-bot-api/telegram-bot-api"
)
+const (
+ unknownUser = "unknown"
+ HTMLFormat = "HTML"
+ HTMLNick = "htmlnick"
+)
+
type Btelegram struct {
c *tgbotapi.BotAPI
*bridge.Config
@@ -647 +707 @@ func (b *Btelegram) Send(msg config.Message) (string, error) {
return b.cacheAvatar(&msg)
}
- if b.GetString("MessageFormat") == "HTML" {
+ if b.GetString("MessageFormat") == HTMLFormat {
msg.Text = makeHTML(msg.Text)
}
@@ -9812 +10412 @@ func (b *Btelegram) Send(msg config.Message) (string, error) {
if err != nil {
return "", err
}
- if strings.ToLower(b.GetString("MessageFormat")) == "htmlnick" {
+ if strings.ToLower(b.GetString("MessageFormat")) == HTMLNick {
b.Log.Debug("Using mode HTML - nick only")
msg.Text = html.EscapeString(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")
m.ParseMode = tgbotapi.ModeHTML
}
@@ -1117 +1177 @@ func (b *Btelegram) Send(msg config.Message) (string, error) {
b.Log.Debug("Using mode markdown")
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")
m.ParseMode = tgbotapi.ModeHTML
}
@@ -1877 +1937 @@ func (b *Btelegram) handleRecv(updates <-chan tgbotapi.Update) {
// if we really didn't find a username, set it to unknown
if rmsg.Username == "" {
- rmsg.Username = "unknown"
+ rmsg.Username = unknownUser
}
// handle any downloads
@@ -2097 +2157 @@ func (b *Btelegram) handleRecv(updates <-chan tgbotapi.Update) {
}
}
if usernameForward == "" {
- usernameForward = "unknown"
+ usernameForward = unknownUser
}
rmsg.Text = "Forwarded from " + usernameForward + ": " + rmsg.Text
}
@@ -2297 +2357 @@ func (b *Btelegram) handleRecv(updates <-chan tgbotapi.Update) {
}
}
if usernameReply == "" {
- usernameReply = "unknown"
+ usernameReply = unknownUser
}
if !b.GetBool("QuoteDisable") {
rmsg.Text = b.handleQuote(rmsg.Text, usernameReply, message.ReplyToMessage.Text)
@@ -4017 +4077 @@ func (b *Btelegram) handleUploadFile(msg *config.Message, chatid int64) (string,
func (b *Btelegram) sendMessage(chatid int64, username, text string) (string, error) {
m := tgbotapi.NewMessage(chatid, "")
m.Text = username + text
- if b.GetString("MessageFormat") == "HTML" {
+ if b.GetString("MessageFormat") == HTMLFormat {
b.Log.Debug("Using mode HTML")
m.ParseMode = tgbotapi.ModeHTML
}
@@ -4097 +4157 @@ func (b *Btelegram) sendMessage(chatid int64, username, text string) (string, er
b.Log.Debug("Using mode markdown")
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")
m.Text = username + html.EscapeString(text)
m.ParseMode = tgbotapi.ModeHTML
diff --git a/gateway/gateway.go b/gateway/gateway.go
index 3a6fa1e..34cdd7f 100644
--- a/gateway/gateway.go
+++ b/gateway/gateway.go
@@ -706 +7010 @@ var bridgeMap = map[string]bridge.Factory{
"zulip": bzulip.New,
}
+const (
+ apiProtocol = "api"
+)
+
func init() {
flog = log.WithFields(log.Fields{"prefix": "gateway"})
}
@@ -1587 +1627 @@ RECONNECT:
func (gw *Gateway) mapChannelConfig(cfg []config.Bridge, direction string) {
for _, br := range cfg {
if isApi(br.Account) {
- br.Channel = "api"
+ br.Channel = apiProtocol
}
// make sure to lowercase irc channels in config #348
if strings.HasPrefix(br.Account, "irc.") {
@@ -1917 +1957 @@ func (gw *Gateway) getDestChannel(msg *config.Message, dest bridge.Bridge) []con
var channels []config.ChannelInfo
// 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
}
@@ -3107 +3147 @@ func (gw *Gateway) handleMessage(msg config.Message, dest *bridge.Bridge) []*BrM
msg.ID = gw.getDestMsgID(origmsg.ID, dest, channel)
// for api we need originchannel as channel
- if dest.Protocol == "api" {
+ if dest.Protocol == apiProtocol {
msg.Channel = originchannel
}
@@ -4597 +4637 @@ func (gw *Gateway) modifyMessage(msg *config.Message) {
}
// messages from api have Gateway specified, don't overwrite
- if msg.Protocol != "api" {
+ if msg.Protocol != apiProtocol {
msg.Gateway = gw.Name
}
}
diff --git a/gateway/gateway_test.go b/gateway/gateway_test.go
index 0a1470f..e7b77bf 100644
--- a/gateway/gateway_test.go
+++ b/gateway/gateway_test.go
@@ -1526 +15212 @@ enable=true
`)
+const (
+ ircTestAccount = "irc.zzz"
+ tgTestAccount = "telegram.zzz"
+ slackTestAccount = "slack.zzz"
+)
+
func maketestRouter(input []byte) *Router {
cfg := config.NewConfigFromString(input)
r, err := NewRouter(cfg)
@@ -24831 +25431 @@ func TestGetDestChannelAdvanced(t *testing.T) {
switch gw.Name {
case "bridge":
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]++
switch br.Account {
- case "irc.zzz":
+ case ircTestAccount:
assert.Equal(t, []config.ChannelInfo{{
Name: "#main",
- Account: "irc.zzz",
+ Account: ircTestAccount,
Direction: "inout",
ID: "#mainirc.zzz",
SameChannel: map[string]bool{"bridge": false},
Options: config.ChannelOptions{Key: ""},
}}, channels)
- case "telegram.zzz":
+ case tgTestAccount:
assert.Equal(t, []config.ChannelInfo{{
Name: "-1111111111111",
- Account: "telegram.zzz",
+ Account: tgTestAccount,
Direction: "inout",
ID: "-1111111111111telegram.zzz",
SameChannel: map[string]bool{"bridge": false},
Options: config.ChannelOptions{Key: ""},
}}, channels)
- case "slack.zzz":
+ case slackTestAccount:
assert.Equal(t, []config.ChannelInfo{{
Name: "irc",
- Account: "slack.zzz",
+ Account: slackTestAccount,
Direction: "inout",
ID: "ircslack.zzz",
SameChannel: map[string]bool{"bridge": false},
@@ -28222 +28822 @@ func TestGetDestChannelAdvanced(t *testing.T) {
}
case "bridge2":
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]++
switch br.Account {
- case "irc.zzz":
+ case ircTestAccount:
assert.Equal(t, []config.ChannelInfo{{
Name: "#main-help",
- Account: "irc.zzz",
+ Account: ircTestAccount,
Direction: "inout",
ID: "#main-helpirc.zzz",
SameChannel: map[string]bool{"bridge2": false},
Options: config.ChannelOptions{Key: ""},
}}, channels)
- case "telegram.zzz":
+ case tgTestAccount:
assert.Equal(t, []config.ChannelInfo{{
Name: "--444444444444",
- Account: "telegram.zzz",
+ Account: tgTestAccount,
Direction: "inout",
ID: "--444444444444telegram.zzz",
SameChannel: map[string]bool{"bridge2": false},
@@ -30722 +31322 @@ func TestGetDestChannelAdvanced(t *testing.T) {
}
case "bridge3":
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]++
switch br.Account {
- case "irc.zzz":
+ case ircTestAccount:
assert.Equal(t, []config.ChannelInfo{{
Name: "#main-telegram",
- Account: "irc.zzz",
+ Account: ircTestAccount,
Direction: "inout",
ID: "#main-telegramirc.zzz",
SameChannel: map[string]bool{"bridge3": false},
Options: config.ChannelOptions{Key: ""},
}}, channels)
- case "telegram.zzz":
+ case tgTestAccount:
assert.Equal(t, []config.ChannelInfo{{
Name: "--333333333333",
- Account: "telegram.zzz",
+ Account: tgTestAccount,
Direction: "inout",
ID: "--333333333333telegram.zzz",
SameChannel: map[string]bool{"bridge3": false},
@@ -33711 +34311 @@ func TestGetDestChannelAdvanced(t *testing.T) {
}
hits[gw.Name]++
switch br.Account {
- case "irc.zzz":
+ case ircTestAccount:
assert.Len(t, channels, 2)
assert.Contains(t, channels, config.ChannelInfo{
Name: "#main",
- Account: "irc.zzz",
+ Account: ircTestAccount,
Direction: "out",
ID: "#mainirc.zzz",
SameChannel: map[string]bool{"announcements": false},
@@ -34925 +35525 @@ func TestGetDestChannelAdvanced(t *testing.T) {
})
assert.Contains(t, channels, config.ChannelInfo{
Name: "#main-help",
- Account: "irc.zzz",
+ Account: ircTestAccount,
Direction: "out",
ID: "#main-helpirc.zzz",
SameChannel: map[string]bool{"announcements": false},
Options: config.ChannelOptions{Key: ""},
})
- case "slack.zzz":
+ case slackTestAccount:
assert.Equal(t, []config.ChannelInfo{{
Name: "general",
- Account: "slack.zzz",
+ Account: slackTestAccount,
Direction: "out",
ID: "generalslack.zzz",
SameChannel: map[string]bool{"announcements": false},
Options: config.ChannelOptions{Key: ""},
}}, channels)
- case "telegram.zzz":
+ case tgTestAccount:
assert.Equal(t, []config.ChannelInfo{{
Name: "--333333333333",
- Account: "telegram.zzz",
+ Account: tgTestAccount,
Direction: "out",
ID: "--333333333333telegram.zzz",
SameChannel: map[string]bool{"announcements": false},