[MM-16709] Add helper method for plugins using MessageHasBeenPosted (#12539)

The PR introduces a method in helper interface. The method has common code for filtering to be used across plugins which use MessageHasBeenPosted.
Этот коммит содержится в:
Rajat Varyani
2019-11-18 15:17:32 +05:30
коммит произвёл Ben Schumacher
родитель 9b6a0674f7
Коммит f2fcfa8f9d
6 изменённых файлов: 326 добавлений и 13 удалений

Просмотреть файл

@@ -8,6 +8,7 @@ import (
"fmt"
"io"
"net/http"
"strings"
"unicode/utf8"
)
@@ -217,3 +218,15 @@ func (l *BotList) Etag() string {
func MakeBotNotFoundError(userId string) *AppError {
return NewAppError("SqlBotStore.Get", "store.sql_bot.get.missing.app_error", map[string]interface{}{"user_id": userId}, "", http.StatusNotFound)
}
func IsBotDMChannel(channel *Channel, botUserID string) bool {
if channel.Type != CHANNEL_DIRECT {
return false
}
if !strings.HasPrefix(channel.Name, botUserID+"__") && !strings.HasSuffix(channel.Name, "__"+botUserID) {
return false
}
return true
}

Просмотреть файл

@@ -683,3 +683,45 @@ func TestBotListEtag(t *testing.T) {
})
}
}
func TestIsBotChannel(t *testing.T) {
for _, test := range []struct {
Name string
Channel *Channel
Expected bool
}{
{
Name: "not a direct channel",
Channel: &Channel{Type: CHANNEL_OPEN},
Expected: false,
},
{
Name: "a direct channel with another user",
Channel: &Channel{
Name: "user1__user2",
Type: CHANNEL_DIRECT,
},
Expected: false,
},
{
Name: "a direct channel with the name containing the bot's ID first",
Channel: &Channel{
Name: "botUserID__user2",
Type: CHANNEL_DIRECT,
},
Expected: true,
},
{
Name: "a direct channel with the name containing the bot's ID second",
Channel: &Channel{
Name: "user1__botUserID",
Type: CHANNEL_DIRECT,
},
Expected: true,
},
} {
t.Run(test.Name, func(t *testing.T) {
assert.Equal(t, test.Expected, IsBotDMChannel(test.Channel, "botUserID"))
})
}
}