(feature) New MessagesWillBeConsumed Server Plugin Hook (#23305)

* feature: implemented basic MessageWillBeConsumed hook and applied on GetSinglePost method.

* use hook in get methods

* chore: refactored hook usage and created utils functions to apply hook

* bugfix: single post not updating

* chore: adjusted hook to return post

* chore: reverted some uneeded  changes

* chore: updated hook to accept slice of posts

* bugfix: slice filled with niil values

* chore: MessageWillBeConsumed ranamed to MessagesWillBeConsumed

* Update plugin/hooks.go

Co-authored-by: Jesse Hallam <jesse@thehallams.ca>

* Add feature flag

* Update min version

* update tests to account for feature flag

* fix linting issues

---------

Co-authored-by: Matej Topolovac <>
Co-authored-by: mtopolovac <43346061+mtopolovac@users.noreply.github.com>
Co-authored-by: Jesse Hallam <jesse@thehallams.ca>
Co-authored-by: Kevin Hsieh <kevinh@qrypt.com>
Co-authored-by: Jesse Hallam <jesse.hallam@gmail.com>
Этот коммит содержится в:
Qrypt
2023-10-23 10:12:46 -04:00
коммит произвёл GitHub
родитель bdacc97454
Коммит a46ad3169c
9 изменённых файлов: 237 добавлений и 0 удалений

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

@@ -1550,3 +1550,82 @@ func TestHookNotificationWillBePushed(t *testing.T) {
})
}
}
func TestHookMessagesWillBeConsumed(t *testing.T) {
setupPlugin := func(t *testing.T, th *TestHelper) {
var mockAPI plugintest.API
mockAPI.On("LoadPluginConfiguration", mock.Anything).Return(nil)
mockAPI.On("LogDebug", "message").Return(nil)
tearDown, _, _ := SetAppEnvironmentWithPlugins(t, []string{`
package main
import (
"github.com/mattermost/mattermost/server/public/plugin"
"github.com/mattermost/mattermost/server/public/model"
)
type MyPlugin struct {
plugin.MattermostPlugin
}
func (p *MyPlugin) MessagesWillBeConsumed(posts []*model.Post) []*model.Post {
for _, post := range posts {
post.Message = "mwbc_plugin:" + post.Message
}
return posts
}
func main() {
plugin.ClientMain(&MyPlugin{})
}
`}, th.App, func(*model.Manifest) plugin.API { return &mockAPI })
t.Cleanup(tearDown)
}
t.Run("feature flag disabled", func(t *testing.T) {
os.Setenv("MM_FEATUREFLAGS_CONSUMEPOSTHOOK", "false")
defer os.Unsetenv("MM_FEATUREFLAGS_CONSUMEPOSTHOOK")
th := Setup(t).InitBasic()
t.Cleanup(th.TearDown)
setupPlugin(t, th)
newPost := &model.Post{
UserId: th.BasicUser.Id,
ChannelId: th.BasicChannel.Id,
Message: "message",
CreateAt: model.GetMillis() - 10000,
}
_, err := th.App.CreatePost(th.Context, newPost, th.BasicChannel, false, true)
require.Nil(t, err)
post, err := th.App.GetSinglePost(newPost.Id, true)
require.Nil(t, err)
assert.Equal(t, "message", post.Message)
})
t.Run("feature flag enabled", func(t *testing.T) {
os.Setenv("MM_FEATUREFLAGS_CONSUMEPOSTHOOK", "true")
defer os.Unsetenv("MM_FEATUREFLAGS_CONSUMEPOSTHOOK")
th := Setup(t).InitBasic()
t.Cleanup(th.TearDown)
setupPlugin(t, th)
newPost := &model.Post{
UserId: th.BasicUser.Id,
ChannelId: th.BasicChannel.Id,
Message: "message",
CreateAt: model.GetMillis() - 10000,
}
_, err := th.App.CreatePost(th.Context, newPost, th.BasicChannel, false, true)
require.Nil(t, err)
post, err := th.App.GetSinglePost(newPost.Id, true)
require.Nil(t, err)
assert.Equal(t, "mwbc_plugin:message", post.Message)
})
}