Skip to content

Commit

Permalink
Add option to enable processing on post update (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
grubbins authored and levb committed Jan 23, 2020
1 parent 542fab8 commit dedc5ac
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 7 deletions.
6 changes: 6 additions & 0 deletions plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
"display_name": "Enable administration with /autolink command",
"type": "bool",
"default": false
},
{
"key": "EnableOnUpdate",
"display_name": "Apply plugin to updated posts as well as new posts",
"type": "bool",
"default": false
}
]
}
Expand Down
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, ""
}
}
50 changes: 46 additions & 4 deletions server/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ func TestSpecialCases(t *testing.T) {
})
validConfig := Config{
EnableAdminCommand: false,
EnableOnUpdate: true,
Links: links,
}

Expand Down Expand Up @@ -220,13 +221,54 @@ func TestSpecialCases(t *testing.T) {

for _, tt := range tests {
t.Run(tt.inputMessage, func(t *testing.T) {
post := &model.Post{
Message: tt.inputMessage,
{
// user creates a new 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

post := &model.Post{
Message: tt.expectedMessage,
}

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

assert.Equal(t, tt.expectedMessage, rpost.Message)
}
{
// user updates the modified post and sets it back to the original text

rpost, _ := p.MessageWillBePosted(&plugin.Context{}, post)
originalPost := &model.Post{
Message: tt.expectedMessage,
}
post := &model.Post{
Message: tt.inputMessage,
}

assert.Equal(t, tt.expectedMessage, rpost.Message)
rpost, _ := p.MessageWillBeUpdated(&plugin.Context{}, originalPost, post)

assert.Equal(t, tt.expectedMessage, rpost.Message)
}
{
// user updates an empty post to the original text

emptyPost := &model.Post{}
post := &model.Post{
Message: tt.inputMessage,
}

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

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

0 comments on commit dedc5ac

Please sign in to comment.