MM-29584: Make apps plugin hook invocation/registration conditional (#16769)
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
3a3ec001bf
Коммит
e37e902ddf
@@ -70,6 +70,7 @@ type shouldProcessMessageOptions struct {
|
|||||||
FilterChannelIDs []string
|
FilterChannelIDs []string
|
||||||
FilterUserIDs []string
|
FilterUserIDs []string
|
||||||
OnlyBotDMs bool
|
OnlyBotDMs bool
|
||||||
|
BotID string
|
||||||
}
|
}
|
||||||
|
|
||||||
// AllowSystemMessages configures a call to ShouldProcessMessage to return true for system messages.
|
// AllowSystemMessages configures a call to ShouldProcessMessage to return true for system messages.
|
||||||
@@ -126,6 +127,15 @@ func OnlyBotDMs() ShouldProcessMessageOption {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If provided, BotID configures ShouldProcessMessage to skip its retrieval from the store.
|
||||||
|
//
|
||||||
|
// By default, posts from all non-bot users are allowed.
|
||||||
|
func BotID(botID string) ShouldProcessMessageOption {
|
||||||
|
return func(options *shouldProcessMessageOptions) {
|
||||||
|
options.BotID = botID
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ShouldProcessMessage implements Helpers.ShouldProcessMessage
|
// ShouldProcessMessage implements Helpers.ShouldProcessMessage
|
||||||
func (p *HelpersImpl) ShouldProcessMessage(post *model.Post, options ...ShouldProcessMessageOption) (bool, error) {
|
func (p *HelpersImpl) ShouldProcessMessage(post *model.Post, options ...ShouldProcessMessageOption) (bool, error) {
|
||||||
messageProcessOptions := &shouldProcessMessageOptions{}
|
messageProcessOptions := &shouldProcessMessageOptions{}
|
||||||
@@ -133,10 +143,18 @@ func (p *HelpersImpl) ShouldProcessMessage(post *model.Post, options ...ShouldPr
|
|||||||
option(messageProcessOptions)
|
option(messageProcessOptions)
|
||||||
}
|
}
|
||||||
|
|
||||||
botIDBytes, kvGetErr := p.API.KVGet(BotUserKey)
|
var botIDBytes []byte
|
||||||
|
var kvGetErr *model.AppError
|
||||||
|
|
||||||
|
if messageProcessOptions.BotID != "" {
|
||||||
|
botIDBytes = []byte(messageProcessOptions.BotID)
|
||||||
|
} else {
|
||||||
|
botIDBytes, kvGetErr = p.API.KVGet(BotUserKey)
|
||||||
|
|
||||||
if kvGetErr != nil {
|
if kvGetErr != nil {
|
||||||
return false, errors.Wrap(kvGetErr, "failed to get bot")
|
return false, errors.Wrap(kvGetErr, "failed to get bot")
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if botIDBytes != nil {
|
if botIDBytes != nil {
|
||||||
if post.UserId == string(botIDBytes) {
|
if post.UserId == string(botIDBytes) {
|
||||||
|
|||||||
@@ -560,4 +560,20 @@ func TestShouldProcessMessage(t *testing.T) {
|
|||||||
assert.True(t, shouldProcessMessage)
|
assert.True(t, shouldProcessMessage)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
t.Run("should process the message when we pass the botId as input", func(t *testing.T) {
|
||||||
|
userID := "user-id"
|
||||||
|
channelID := "1"
|
||||||
|
api := setupAPI()
|
||||||
|
api.On("GetChannel", channelID).Return(&model.Channel{Id: channelID, Type: model.CHANNEL_GROUP}, nil)
|
||||||
|
p.API = api
|
||||||
|
api.On("GetUser", userID).Return(&model.User{IsBot: false}, nil)
|
||||||
|
|
||||||
|
// we should skip the store Get
|
||||||
|
api.On("KVGet", plugin.BotUserKey).Return(nil, nil)
|
||||||
|
|
||||||
|
shouldProcessMessage, err := p.ShouldProcessMessage(&model.Post{ChannelId: channelID, UserId: userID}, plugin.BotID(expectedBotID))
|
||||||
|
assert.Nil(t, err)
|
||||||
|
|
||||||
|
assert.True(t, shouldProcessMessage)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user