summaryrefslogtreecommitdiffstats
path: root/plugin/pluginenv/environment.go
diff options
context:
space:
mode:
authorChristopher Speller <crspeller@gmail.com>2018-05-15 13:33:47 -0700
committerGitHub <noreply@github.com>2018-05-15 13:33:47 -0700
commitdf6a7f8b19e2381ee57f946d5b184185653b2ee1 (patch)
tree622ff6b13b23bf4506ea41eb010141930e143815 /plugin/pluginenv/environment.go
parentfbbe1f7cefd52a27fd52893509b5365d275f9bee (diff)
downloadchat-df6a7f8b19e2381ee57f946d5b184185653b2ee1.tar.gz
chat-df6a7f8b19e2381ee57f946d5b184185653b2ee1.tar.bz2
chat-df6a7f8b19e2381ee57f946d5b184185653b2ee1.zip
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/
Diffstat (limited to 'plugin/pluginenv/environment.go')
-rw-r--r--plugin/pluginenv/environment.go59
1 files changed, 59 insertions, 0 deletions
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()