Skip to content

Commit

Permalink
Add option to enable processing on post update
Browse files Browse the repository at this point in the history
  • Loading branch information
Dave Gittins committed Jan 20, 2020
1 parent 542fab8 commit 2feacc6
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
2 changes: 2 additions & 0 deletions server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
// Config from config.json
type Config struct {
EnableAdminCommand bool
EnableOnUpdate bool
Links []Link
}

Expand Down Expand Up @@ -74,6 +75,7 @@ func (conf Config) ToConfig() map[string]interface{} {
}
return map[string]interface{}{
"EnableAdminCommand": conf.EnableAdminCommand,
"EnableOnUpdate": conf.EnableOnUpdate,
"Links": links,
}
}
Expand Down
21 changes: 18 additions & 3 deletions server/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ type Plugin struct {
confLock sync.RWMutex
}

// MessageWillBePosted is invoked when a message is posted by a user before it is committed
// to the database.
func (p *Plugin) MessageWillBePosted(c *plugin.Context, post *model.Post) (*model.Post, string) {
func (p *Plugin) ProcessPost(c *plugin.Context, post *model.Post) (*model.Post, string) {
conf := p.getConfig()
postText := post.Message
offset := 0
Expand Down Expand Up @@ -98,3 +96,20 @@ func (p *Plugin) MessageWillBePosted(c *plugin.Context, post *model.Post) (*mode

return post, ""
}

// MessageWillBePosted is invoked when a message is posted by a user before it is committed
// to the database.
func (p *Plugin) MessageWillBePosted(c *plugin.Context, post *model.Post) (*model.Post, string) {
return p.ProcessPost(c, post)
}

// MessageWillBeUpdated is invoked when a message is updated by a user before it is committed
// to the database.
func (p *Plugin) MessageWillBeUpdated(c *plugin.Context, post *model.Post, _ *model.Post) (*model.Post, string) {
conf := p.getConfig()
if conf.EnableOnUpdate {
return p.ProcessPost(c, post)
} else {
return post, ""
}
}
19 changes: 19 additions & 0 deletions server/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,13 +220,32 @@ func TestSpecialCases(t *testing.T) {

for _, tt := range tests {
t.Run(tt.inputMessage, func(t *testing.T) {
emptyPost := &model.Post{}
post := &model.Post{
Message: tt.inputMessage,
}

rpost, _ := p.MessageWillBePosted(&plugin.Context{}, post)

assert.Equal(t, tt.expectedMessage, rpost.Message)

// user updates the modified post but with no changes

alreadyProcessedUpdatedPost, _ := p.MessageWillBeUpdated(&plugin.Context{}, rpost, rpost)

assert.Equal(t, tt.expectedMessage, alreadyProcessedUpdatedPost.Message)

// user updates the modified post and sets it back to the original text

noChangeUpdatedPost, _ := p.MessageWillBeUpdated(&plugin.Context{}, post, rpost)

assert.Equal(t, tt.expectedMessage, noChangeUpdatedPost.Message)

// user updates an empty post to the original text

initiallyEmptyUpdatedPost, _ := p.MessageWillBeUpdated(&plugin.Context{}, post, emptyPost)

assert.Equal(t, tt.expectedMessage, initiallyEmptyUpdatedPost.Message)
})
}
}
Expand Down

0 comments on commit 2feacc6

Please sign in to comment.