Unverified Commit 04749027 authored by Jesse Hallam's avatar Jesse Hallam Committed by GitHub

MM-11575: change plugin nil semantics (#9212)

* change MessageWillBePosted nil return semantics

* change FileWillBeUploaded nil return semantics

* use LogDebug to verify plugin inputs vs. the confusing Delete(User|Team)
parent 1ecb98d9
......@@ -435,20 +435,27 @@ func (a *App) DoUploadFileExpectModification(now time.Time, rawTeamId string, ra
}
if a.PluginsReady() {
var rejectionError *model.AppError
pluginContext := &plugin.Context{}
var rejectionReason string
a.Plugins.RunMultiPluginHook(func(hooks plugin.Hooks) bool {
var newBytes bytes.Buffer
info, rejectionReason = hooks.FileWillBeUploaded(pluginContext, info, bytes.NewReader(data), &newBytes)
rejected := info == nil
if !rejected && newBytes.Len() != 0 {
replacementInfo, rejectionReason := hooks.FileWillBeUploaded(pluginContext, info, bytes.NewReader(data), &newBytes)
if rejectionReason != "" {
rejectionError = model.NewAppError("DoUploadFile", "File rejected by plugin. "+rejectionReason, nil, "", http.StatusBadRequest)
return false
}
if replacementInfo != nil {
info = replacementInfo
}
if newBytes.Len() != 0 {
data = newBytes.Bytes()
info.Size = int64(len(data))
}
return !rejected
return true
}, plugin.FileWillBeUploadedId)
if info == nil {
return nil, data, model.NewAppError("DoUploadFile", "File rejected by plugin. "+rejectionReason, nil, "", http.StatusBadRequest)
if rejectionError != nil {
return nil, data, rejectionError
}
}
......
......@@ -45,7 +45,7 @@ func TestPluginDeadlock(t *testing.T) {
func (p *MyPlugin) MessageWillBePosted(c *plugin.Context, post *model.Post) (*model.Post, string) {
if _, from_plugin := post.Props["from_plugin"]; from_plugin {
return post, ""
return nil, ""
}
p.API.CreatePost(&model.Post{
......@@ -57,7 +57,7 @@ func TestPluginDeadlock(t *testing.T) {
},
})
return post, ""
return nil, ""
}
func main() {
......@@ -120,7 +120,7 @@ func TestPluginDeadlock(t *testing.T) {
func (p *MyPlugin) MessageWillBePosted(c *plugin.Context, post *model.Post) (*model.Post, string) {
if _, from_plugin := post.Props["from_plugin"]; from_plugin {
return post, ""
return nil, ""
}
p.API.CreatePost(&model.Post{
......@@ -132,7 +132,7 @@ func TestPluginDeadlock(t *testing.T) {
},
})
return post, ""
return nil, ""
}
func main() {
......
This diff is collapsed.
......@@ -162,14 +162,22 @@ func (a *App) CreatePost(post *model.Post, channel *model.Channel, triggerWebhoo
}
if a.PluginsReady() {
var rejectionReason string
var rejectionError *model.AppError
pluginContext := &plugin.Context{}
a.Plugins.RunMultiPluginHook(func(hooks plugin.Hooks) bool {
post, rejectionReason = hooks.MessageWillBePosted(pluginContext, post)
return post != nil
replacementPost, rejectionReason := hooks.MessageWillBePosted(pluginContext, post)
if rejectionReason != "" {
rejectionError = model.NewAppError("createPost", "Post rejected by plugin. "+rejectionReason, nil, "", http.StatusBadRequest)
return false
}
if replacementPost != nil {
post = replacementPost
}
return true
}, plugin.MessageWillBePostedId)
if post == nil {
return nil, model.NewAppError("createPost", "Post rejected by plugin. "+rejectionReason, nil, "", http.StatusBadRequest)
if rejectionError != nil {
return nil, rejectionError
}
}
......
......@@ -71,7 +71,10 @@ type Hooks interface {
// MessageWillBePosted is invoked when a message is posted by a user before it is committed
// to the database. If you also want to act on edited posts, see MessageWillBeUpdated.
// Return values should be the modified post or nil if rejected and an explanation for the user.
//
// To reject a post, return an non-empty string describing why the post was rejected.
// To modify the post, return the replacement, non-nil *model.Post and an empty string.
// To allow the post without modification, return a nil *model.Post and an empty string.
//
// If you don't need to modify or reject posts, use MessageHasBeenPosted instead.
//
......@@ -129,8 +132,12 @@ type Hooks interface {
UserHasLoggedIn(c *Context, user *model.User)
// FileWillBeUploaded is invoked when a file is uploaded, but before it is committed to backing store.
// Read from file to retrieve the body of the uploaded file. You may modify the body of the file by writing to output.
// Returned FileInfo will be used instead of input FileInfo. Return nil to reject the file upload and include a text reason as the second argument.
// Read from file to retrieve the body of the uploaded file.
//
// To reject a file upload, return an non-empty string describing why the file was rejected.
// To modify the file, write to the output and/or return a non-nil *model.FileInfo, as well as an empty string.
// To allow the file without modification, do not write to the output and return a nil *model.FileInfo and an empty string.
//
// Note that this method will be called for files uploaded by plugins, including the plugin that uploaded the post.
// FileInfo.Size will be automatically set properly if you modify the file.
FileWillBeUploaded(c *Context, info *model.FileInfo, file io.Reader, output io.Writer) (*model.FileInfo, string)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment