MM-10249 Adding plugin ability to intercept posts before they reach the DB. (#8791)

* Adding plugin ability to intercept posts before they reach the DB.

* s/envoked/invoked/
Этот коммит содержится в:
Christopher Speller
2018-05-15 13:33:47 -07:00
коммит произвёл GitHub
родитель fbbe1f7cef
Коммит df6a7f8b19
13 изменённых файлов: 1120 добавлений и 319 удалений

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

@@ -661,3 +661,7 @@ func (a *App) ExecutePluginCommand(args *model.CommandArgs) (*model.Command, *mo
}
return nil, nil, nil
}
func (a *App) PluginsReady() bool {
return a.PluginEnv != nil && *a.Config().PluginSettings.Enable
}

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

@@ -160,6 +160,14 @@ func (a *App) CreatePost(post *model.Post, channel *model.Channel, triggerWebhoo
return nil, err
}
if a.PluginsReady() {
if newPost, rejectionReason := a.PluginEnv.Hooks().MessageWillBePosted(post); newPost == nil {
return nil, model.NewAppError("createPost", "Post rejected by plugin. "+rejectionReason, nil, "", http.StatusBadRequest)
} else {
post = newPost
}
}
var rpost *model.Post
if result := <-a.Srv.Store.Post().Save(post); result.Err != nil {
return nil, result.Err
@@ -167,6 +175,12 @@ func (a *App) CreatePost(post *model.Post, channel *model.Channel, triggerWebhoo
rpost = result.Data.(*model.Post)
}
if a.PluginsReady() {
a.Go(func() {
a.PluginEnv.Hooks().MessageHasBeenPosted(rpost)
})
}
esInterface := a.Elasticsearch
if esInterface != nil && *a.Config().ElasticsearchSettings.EnableIndexing {
a.Go(func() {
@@ -371,11 +385,25 @@ func (a *App) UpdatePost(post *model.Post, safeUpdate bool) (*model.Post, *model
return nil, err
}
if a.PluginsReady() {
if pluginModifiedPost, rejectionReason := a.PluginEnv.Hooks().MessageWillBeUpdated(newPost, oldPost); pluginModifiedPost == nil {
return nil, model.NewAppError("createPost", "Post rejected by plugin. "+rejectionReason, nil, "", http.StatusBadRequest)
} else {
newPost = pluginModifiedPost
}
}
if result := <-a.Srv.Store.Post().Update(newPost, oldPost); result.Err != nil {
return nil, result.Err
} else {
rpost := result.Data.(*model.Post)
if a.PluginsReady() {
a.Go(func() {
a.PluginEnv.Hooks().MessageHasBeenUpdated(newPost, oldPost)
})
}
esInterface := a.Elasticsearch
if esInterface != nil && *a.Config().ElasticsearchSettings.EnableIndexing {
a.Go(func() {