Skip to content

Commit

Permalink
[MM-53269] Add config setting to configure request delay (#167)
Browse files Browse the repository at this point in the history
  • Loading branch information
hanzei authored Nov 6, 2023
1 parent 726829b commit f006ddc
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 33 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/mattermost/mattermost-plugin-demo
go 1.19

require (
github.com/gorilla/mux v1.8.0
github.com/mattermost/mattermost/server/public v0.0.8
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.8.4
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+
github.com/googleapis/gax-go v2.0.0+incompatible/go.mod h1:SFVmujtThgffbyetf+mdk2eWhX2bMyUtNHzFKcPA9HY=
github.com/googleapis/gax-go/v2 v2.0.3/go.mod h1:LLvjysVCY1JZeum8Z6l8qUty8fiNwE08qbEPm1M08qg=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/graph-gophers/graphql-go v1.5.1-0.20230110080634-edea822f558a h1:i0+Se9S+2zL5CBxJouqn2Ej6UQMwH1c57ZB6DVnqck4=
Expand Down
8 changes: 8 additions & 0 deletions plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,14 @@
"help_text": "A secret number that the demo plugin will watch for. If the secret number is mentioned in any channel then the demo plugin will publish a special message.",
"placeholder": "Some secret number",
"default": 123
},
{
"key": "integrationRequestDelay",
"display_name": "Integration Request delay",
"type": "number",
"help_text": "A deplay in seconds that is applied to Slash Command responses, Post Actions responses and Interactive Dialog responses. It's useful for testing.",
"placeholder": "A delay in seconds",
"default": 0
}
]
}
Expand Down
2 changes: 2 additions & 0 deletions server/activate_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ func (p *Plugin) OnActivate() error {
return err
}

p.initializeAPI()

configuration := p.getConfiguration()

if err := p.registerCommands(); err != nil {
Expand Down
5 changes: 5 additions & 0 deletions server/command_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,11 @@ func getAutocompleteTestAutocompleteData() *model.AutocompleteData {
// This demo implementation responds to a /demo_plugin command, allowing the user to enable
// or disable the demo plugin's hooks functionality (but leave the command and webapp enabled).
func (p *Plugin) ExecuteCommand(c *plugin.Context, args *model.CommandArgs) (*model.CommandResponse, *model.AppError) {
delay := p.getConfiguration().IntegrationRequestDelay
if delay > 0 {
time.Sleep(time.Duration(delay) * time.Second)
}

trigger := strings.TrimPrefix(strings.Fields(args.Command)[0], "/")
switch trigger {
case commandTriggerCrash:
Expand Down
28 changes: 17 additions & 11 deletions server/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ type configuration struct {
// SecretNumber is an integer that, when mentioned in a message by a user, will trigger the demo user to post a message.
SecretNumber int

// A deplay in seconds that is applied to Slash Command responses, Post Actions responses and Interactive Dialog responses.
// It's useful for testing.
IntegrationRequestDelay int

// disabled tracks whether or not the plugin has been disabled after activation. It always starts enabled.
disabled bool

Expand All @@ -67,17 +71,19 @@ func (c *configuration) Clone() *configuration {
}

return &configuration{
Username: c.Username,
ChannelName: c.ChannelName,
LastName: c.LastName,
TextStyle: c.TextStyle,
RandomSecret: c.RandomSecret,
SecretMessage: c.SecretMessage,
EnableMentionUser: c.EnableMentionUser,
MentionUser: c.MentionUser,
disabled: c.disabled,
demoUserID: c.demoUserID,
demoChannelIDs: demoChannelIDs,
Username: c.Username,
ChannelName: c.ChannelName,
LastName: c.LastName,
TextStyle: c.TextStyle,
RandomSecret: c.RandomSecret,
SecretMessage: c.SecretMessage,
EnableMentionUser: c.EnableMentionUser,
MentionUser: c.MentionUser,
SecretNumber: c.SecretNumber,
IntegrationRequestDelay: c.IntegrationRequestDelay,
disabled: c.disabled,
demoUserID: c.demoUserID,
demoChannelIDs: demoChannelIDs,
}
}

Expand Down
61 changes: 39 additions & 22 deletions server/http_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"encoding/json"
"fmt"
"net/http"
"time"

"github.com/gorilla/mux"
"github.com/mattermost/mattermost/server/public/model"
"github.com/mattermost/mattermost/server/public/plugin"
)
Expand All @@ -19,28 +21,43 @@ import (
// is used by the web app to recover from a network reconnection and synchronize the state of the
// plugin's hooks.
func (p *Plugin) ServeHTTP(c *plugin.Context, w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/status":
p.handleStatus(w, r)
case "/hello":
p.handleHello(w, r)
case "/dialog/1":
p.handleDialog1(w, r)
case "/dialog/2":
p.handleDialog2(w, r)
case "/dialog/error":
p.handleDialogWithError(w, r)
case "/ephemeral/update":
p.handleEphemeralUpdate(w, r)
case "/ephemeral/delete":
p.handleEphemeralDelete(w, r)
case "/interactive/button/1":
p.handleInteractiveAction(w, r)
case "/dynamic_arg_test_url":
p.handleDynamicArgTest(w, r)
default:
http.NotFound(w, r)
}
p.router.ServeHTTP(w, r)
}

func (p *Plugin) initializeAPI() {
router := mux.NewRouter()

router.HandleFunc("/status", p.handleStatus)
router.HandleFunc("/hello", p.handleHello)
router.HandleFunc("/dynamic_arg_test_url", p.handleDynamicArgTest)

interativeRouter := router.PathPrefix("/interactive").Subrouter()
interativeRouter.Use(p.withDelay)
interativeRouter.HandleFunc("/button/1", p.handleInteractiveAction)

dialogRouter := router.PathPrefix("/dialog").Subrouter()
dialogRouter.Use(p.withDelay)
dialogRouter.HandleFunc("/1", p.handleDialog1)
dialogRouter.HandleFunc("/2", p.handleDialog2)
dialogRouter.HandleFunc("/error", p.handleDialogWithError)

ephemeralRouter := router.PathPrefix("/ephemeral").Subrouter()
ephemeralRouter.Use(p.withDelay)
ephemeralRouter.HandleFunc("/update", p.handleEphemeralUpdate)
ephemeralRouter.HandleFunc("/delete", p.handleEphemeralDelete)

p.router = router
}

func (p *Plugin) withDelay(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
delay := p.getConfiguration().IntegrationRequestDelay
if delay > 0 {
time.Sleep(time.Duration(delay) * time.Second)
}

next.ServeHTTP(w, r)
})
}

func (p *Plugin) handleStatus(w http.ResponseWriter, r *http.Request) {
Expand Down
1 change: 1 addition & 0 deletions server/http_hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func TestServeHTTP(t *testing.T) {
assert := assert.New(t)

plugin := &Plugin{}
plugin.initializeAPI()

w := httptest.NewRecorder()
r := httptest.NewRequest("GET", test.RequestURL, nil)
Expand Down
4 changes: 4 additions & 0 deletions server/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package main
import (
"sync"

"github.com/gorilla/mux"

"github.com/mattermost/mattermost/server/public/model"
"github.com/mattermost/mattermost/server/public/plugin"
"github.com/mattermost/mattermost/server/public/pluginapi"
Expand All @@ -26,6 +28,8 @@ type Plugin struct {
// setConfiguration for usage.
configuration *configuration

router *mux.Router

// BotId of the created bot account.
botID string

Expand Down

0 comments on commit f006ddc

Please sign in to comment.