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/
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
fbbe1f7cef
Коммит
df6a7f8b19
@@ -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()
|
||||
|
||||
Ссылка в новой задаче
Block a user