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/ --- plugin/pluginenv/environment.go | 59 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) (limited to 'plugin/pluginenv/environment.go') diff --git a/plugin/pluginenv/environment.go b/plugin/pluginenv/environment.go index adc02e885..947eda86d 100644 --- a/plugin/pluginenv/environment.go +++ b/plugin/pluginenv/environment.go @@ -306,6 +306,65 @@ func (h *MultiPluginHooks) ServeHTTP(w http.ResponseWriter, r *http.Request) { http.NotFound(w, r) } +// MessageWillBePosted invokes the MessageWillBePosted hook for all plugins. Ordering +// is not guaranteed and the next plugin will get the previous one's modifications. +// if a plugin rejects a post, the rest of the plugins will not know that an attempt was made. +// Returns the final result post, or nil if the post was rejected and a string with a reason +// for the user the message was rejected. +func (h *MultiPluginHooks) MessageWillBePosted(post *model.Post) (*model.Post, string) { + h.env.mutex.RLock() + defer h.env.mutex.RUnlock() + + for _, activePlugin := range h.env.activePlugins { + if activePlugin.Supervisor == nil { + continue + } + var rejectionReason string + post, rejectionReason = activePlugin.Supervisor.Hooks().MessageWillBePosted(post) + if post == nil { + return nil, rejectionReason + } + } + return post, "" +} + +// MessageWillBeUpdated invokes the MessageWillBeUpdated hook for all plugins. Ordering +// is not guaranteed and the next plugin will get the previous one's modifications. +// if a plugin rejects a post, the rest of the plugins will not know that an attempt was made. +// Returns the final result post, or nil if the post was rejected and a string with a reason +// for the user the message was rejected. +func (h *MultiPluginHooks) MessageWillBeUpdated(newPost, oldPost *model.Post) (*model.Post, string) { + h.env.mutex.RLock() + defer h.env.mutex.RUnlock() + + post := newPost + for _, activePlugin := range h.env.activePlugins { + if activePlugin.Supervisor == nil { + continue + } + var rejectionReason string + post, rejectionReason = activePlugin.Supervisor.Hooks().MessageWillBeUpdated(post, oldPost) + if post == nil { + return nil, rejectionReason + } + } + return post, "" +} + +func (h *MultiPluginHooks) MessageHasBeenPosted(post *model.Post) { + h.invoke(func(hooks plugin.Hooks) error { + hooks.MessageHasBeenPosted(post) + return nil + }) +} + +func (h *MultiPluginHooks) MessageHasBeenUpdated(newPost, oldPost *model.Post) { + h.invoke(func(hooks plugin.Hooks) error { + hooks.MessageHasBeenUpdated(newPost, oldPost) + return nil + }) +} + func (h *SinglePluginHooks) invoke(f func(plugin.Hooks) error) error { h.env.mutex.RLock() defer h.env.mutex.RUnlock() -- cgit v1.2.3-1-g7c22