From 722924b910feba982de70981e7c6458488a86c54 Mon Sep 17 00:00:00 2001 From: Vladimir Lebedev Date: Thu, 19 Dec 2019 22:52:41 +0300 Subject: [PATCH] =?UTF-8?q?MM-19508=20Extend=20ShouldProcessMessage=20to?= =?UTF-8?q?=20filter=20out=20from=5Fwebhoo=E2=80=A6=20(#13270)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Extend ShouldProcessMessage to filter out from_webhook posts --- plugin/helpers_bots.go | 14 ++++++++++ plugin/helpers_bots_test.go | 53 +++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/plugin/helpers_bots.go b/plugin/helpers_bots.go index 208b5d9e0b..c25d01ee77 100644 --- a/plugin/helpers_bots.go +++ b/plugin/helpers_bots.go @@ -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 { diff --git a/plugin/helpers_bots_test.go b/plugin/helpers_bots_test.go index db9200f50b..d76291f69e 100644 --- a/plugin/helpers_bots_test.go +++ b/plugin/helpers_bots_test.go @@ -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) + }) + }