MM-19508 Extend ShouldProcessMessage to filter out from_webhoo… (#13270)

Extend ShouldProcessMessage to filter out from_webhook posts
Этот коммит содержится в:
Vladimir Lebedev
2019-12-19 22:52:41 +03:00
коммит произвёл Ben Schumacher
родитель 7274f4c6c2
Коммит 722924b910
2 изменённых файлов: 67 добавлений и 0 удалений

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

@@ -66,6 +66,7 @@ type ShouldProcessMessageOption func(*shouldProcessMessageOptions)
type shouldProcessMessageOptions struct {
AllowSystemMessages bool
AllowBots bool
AllowWebhook bool
FilterChannelIDs []string
FilterUserIDs []string
OnlyBotDMs bool
@@ -89,6 +90,15 @@ func AllowBots() ShouldProcessMessageOption {
}
}
// AllowWebhook configures a call to ShouldProcessMessage to return true for posts from webhook.
//
// As it is typically desirable only to consume messages from human users of the system, ShouldProcessMessage ignores webhook messages by default.
func AllowWebhook() ShouldProcessMessageOption {
return func(options *shouldProcessMessageOptions) {
options.AllowWebhook = true
}
}
// FilterChannelIDs configures a call to ShouldProcessMessage to return true only for the given channels.
//
// By default, posts from all channels are allowed to be processed.
@@ -138,6 +148,10 @@ func (p *HelpersImpl) ShouldProcessMessage(post *model.Post, options ...ShouldPr
return false, nil
}
if !messageProcessOptions.AllowWebhook && post.Props["from_webhook"] == "true" {
return false, nil
}
if !messageProcessOptions.AllowBots {
user, appErr := p.API.GetUser(post.UserId)
if appErr != nil {

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

@@ -447,4 +447,57 @@ func TestShouldProcessMessage(t *testing.T) {
assert.True(t, shouldProcessMessage)
})
t.Run("should not process the message which have from_webhook", func(t *testing.T) {
channelID := "1"
api := setupAPI()
api.On("GetChannel", channelID).Return(&model.Channel{Id: channelID, Type: model.CHANNEL_GROUP}, nil)
p.API = api
api.On("KVGet", plugin.BOT_USER_KEY).Return([]byte(expectedBotID), nil)
shouldProcessMessage, err := p.ShouldProcessMessage(&model.Post{ChannelId: channelID, Props: model.StringInterface{"from_webhook": "true"}}, plugin.AllowBots())
assert.False(t, shouldProcessMessage)
assert.Nil(t, err)
})
t.Run("should process the message which have from_webhook with allow webhook plugin", func(t *testing.T) {
channelID := "1"
api := setupAPI()
api.On("GetChannel", channelID).Return(&model.Channel{Id: channelID, Type: model.CHANNEL_GROUP}, nil)
p.API = api
api.On("KVGet", plugin.BOT_USER_KEY).Return([]byte(expectedBotID), nil)
shouldProcessMessage, err := p.ShouldProcessMessage(&model.Post{ChannelId: channelID, Props: model.StringInterface{"from_webhook": "true"}}, plugin.AllowBots(), plugin.AllowWebhook())
assert.Nil(t, err)
assert.True(t, shouldProcessMessage)
})
t.Run("should process the message where from_webhook is not set", func(t *testing.T) {
channelID := "1"
api := setupAPI()
api.On("GetChannel", channelID).Return(&model.Channel{Id: channelID, Type: model.CHANNEL_GROUP}, nil)
p.API = api
api.On("KVGet", plugin.BOT_USER_KEY).Return([]byte(expectedBotID), nil)
shouldProcessMessage, err := p.ShouldProcessMessage(&model.Post{ChannelId: channelID}, plugin.AllowBots())
assert.Nil(t, err)
assert.True(t, shouldProcessMessage)
})
t.Run("should process the message which have from_webhook false", func(t *testing.T) {
channelID := "1"
api := setupAPI()
api.On("GetChannel", channelID).Return(&model.Channel{Id: channelID, Type: model.CHANNEL_GROUP}, nil)
p.API = api
api.On("KVGet", plugin.BOT_USER_KEY).Return([]byte(expectedBotID), nil)
shouldProcessMessage, err := p.ShouldProcessMessage(&model.Post{ChannelId: channelID, Props: model.StringInterface{"from_webhook": "false"}}, plugin.AllowBots())
assert.Nil(t, err)
assert.True(t, shouldProcessMessage)
})
}