From df6a7f8b19e2381ee57f946d5b184185653b2ee1 Mon Sep 17 00:00:00 2001 From: Christopher Speller Date: Tue, 15 May 2018 13:33:47 -0700 Subject: 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/ --- app/post.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'app/post.go') diff --git a/app/post.go b/app/post.go index bc31aee44..2efa4c90e 100644 --- a/app/post.go +++ b/app/post.go @@ -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() { -- cgit v1.2.3-1-g7c22