Add plugin slash command support (#7941)
* add plugin slash command support * remove unused string * rebase
Этот коммит содержится в:
@@ -32,6 +32,14 @@ func (api *LocalAPI) LoadPluginConfiguration(args struct{}, reply *[]byte) error
|
||||
return nil
|
||||
}
|
||||
|
||||
func (api *LocalAPI) RegisterCommand(args *model.Command, reply *APITeamReply) error {
|
||||
return api.api.RegisterCommand(args)
|
||||
}
|
||||
|
||||
func (api *LocalAPI) UnregisterCommand(args *APIUnregisterCommandArgs, reply *APITeamReply) error {
|
||||
return api.api.UnregisterCommand(args.TeamId, args.Trigger)
|
||||
}
|
||||
|
||||
type APIErrorReply struct {
|
||||
Error *model.AppError
|
||||
}
|
||||
@@ -344,6 +352,22 @@ func (api *RemoteAPI) LoadPluginConfiguration(dest interface{}) error {
|
||||
return json.Unmarshal(config, dest)
|
||||
}
|
||||
|
||||
func (api *RemoteAPI) RegisterCommand(command *model.Command) error {
|
||||
return api.client.Call("LocalAPI.RegisterCommand", command, nil)
|
||||
}
|
||||
|
||||
type APIUnregisterCommandArgs struct {
|
||||
TeamId string
|
||||
Trigger string
|
||||
}
|
||||
|
||||
func (api *RemoteAPI) UnregisterCommand(teamId, trigger string) error {
|
||||
return api.client.Call("LocalAPI.UnregisterCommand", &APIUnregisterCommandArgs{
|
||||
TeamId: teamId,
|
||||
Trigger: trigger,
|
||||
}, nil)
|
||||
}
|
||||
|
||||
func (api *RemoteAPI) CreateUser(user *model.User) (*model.User, *model.AppError) {
|
||||
var reply APIUserReply
|
||||
if err := api.client.Call("LocalAPI.CreateUser", user, &reply); err != nil {
|
||||
|
||||
@@ -2,6 +2,7 @@ package rpcplugin
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"testing"
|
||||
@@ -84,6 +85,16 @@ func TestAPI(t *testing.T) {
|
||||
assert.Equal(t, "foo", config.Foo)
|
||||
assert.Equal(t, "baz", config.Bar.Baz)
|
||||
|
||||
api.On("RegisterCommand", mock.AnythingOfType("*model.Command")).Return(fmt.Errorf("foo")).Once()
|
||||
assert.Error(t, remote.RegisterCommand(&model.Command{}))
|
||||
api.On("RegisterCommand", mock.AnythingOfType("*model.Command")).Return(nil).Once()
|
||||
assert.NoError(t, remote.RegisterCommand(&model.Command{}))
|
||||
|
||||
api.On("UnregisterCommand", "team", "trigger").Return(fmt.Errorf("foo")).Once()
|
||||
assert.Error(t, remote.UnregisterCommand("team", "trigger"))
|
||||
api.On("UnregisterCommand", "team", "trigger").Return(nil).Once()
|
||||
assert.NoError(t, remote.UnregisterCommand("team", "trigger"))
|
||||
|
||||
api.On("CreateChannel", mock.AnythingOfType("*model.Channel")).Return(func(c *model.Channel) (*model.Channel, *model.AppError) {
|
||||
c.Id = "thechannelid"
|
||||
return c, nil
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"net/rpc"
|
||||
"reflect"
|
||||
|
||||
"github.com/mattermost/mattermost-server/model"
|
||||
"github.com/mattermost/mattermost-server/plugin"
|
||||
)
|
||||
|
||||
@@ -125,6 +126,20 @@ func (h *LocalHooks) ServeHTTP(args ServeHTTPArgs, reply *struct{}) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
type HooksExecuteCommandReply struct {
|
||||
Response *model.CommandResponse
|
||||
Error *model.AppError
|
||||
}
|
||||
|
||||
func (h *LocalHooks) ExecuteCommand(args *model.CommandArgs, reply *HooksExecuteCommandReply) error {
|
||||
if hook, ok := h.hooks.(interface {
|
||||
ExecuteCommand(*model.CommandArgs) (*model.CommandResponse, *model.AppError)
|
||||
}); ok {
|
||||
reply.Response, reply.Error = hook.ExecuteCommand(args)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func ServeHooks(hooks interface{}, conn io.ReadWriteCloser, muxer *Muxer) {
|
||||
server := rpc.NewServer()
|
||||
server.Register(&LocalHooks{
|
||||
@@ -141,6 +156,7 @@ const (
|
||||
remoteOnDeactivate = 1
|
||||
remoteServeHTTP = 2
|
||||
remoteOnConfigurationChange = 3
|
||||
remoteExecuteCommand = 4
|
||||
maxRemoteHookCount = iota
|
||||
)
|
||||
|
||||
@@ -225,6 +241,17 @@ func (h *RemoteHooks) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
func (h *RemoteHooks) ExecuteCommand(args *model.CommandArgs) (*model.CommandResponse, *model.AppError) {
|
||||
if !h.implemented[remoteExecuteCommand] {
|
||||
return nil, model.NewAppError("RemoteHooks.ExecuteCommand", "plugin.rpcplugin.invocation.error", nil, "err=ExecuteCommand hook not implemented", http.StatusInternalServerError)
|
||||
}
|
||||
var reply HooksExecuteCommandReply
|
||||
if err := h.client.Call("LocalHooks.ExecuteCommand", args, &reply); err != nil {
|
||||
return nil, model.NewAppError("RemoteHooks.ExecuteCommand", "plugin.rpcplugin.invocation.error", nil, "err="+err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
return reply.Response, reply.Error
|
||||
}
|
||||
|
||||
func (h *RemoteHooks) Close() error {
|
||||
if h.apiCloser != nil {
|
||||
h.apiCloser.Close()
|
||||
@@ -253,6 +280,8 @@ func ConnectHooks(conn io.ReadWriteCloser, muxer *Muxer) (*RemoteHooks, error) {
|
||||
remote.implemented[remoteOnConfigurationChange] = true
|
||||
case "ServeHTTP":
|
||||
remote.implemented[remoteServeHTTP] = true
|
||||
case "ExecuteCommand":
|
||||
remote.implemented[remoteExecuteCommand] = true
|
||||
}
|
||||
}
|
||||
return remote, nil
|
||||
|
||||
@@ -13,6 +13,7 @@ import (
|
||||
"github.com/stretchr/testify/mock"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/mattermost/mattermost-server/model"
|
||||
"github.com/mattermost/mattermost-server/plugin"
|
||||
"github.com/mattermost/mattermost-server/plugin/plugintest"
|
||||
)
|
||||
@@ -79,6 +80,17 @@ func TestHooks(t *testing.T) {
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "bar", string(body))
|
||||
|
||||
hooks.On("ExecuteCommand", &model.CommandArgs{
|
||||
Command: "/foo",
|
||||
}).Return(&model.CommandResponse{
|
||||
Text: "bar",
|
||||
}, nil)
|
||||
commandResponse, appErr := hooks.ExecuteCommand(&model.CommandArgs{
|
||||
Command: "/foo",
|
||||
})
|
||||
assert.Equal(t, "bar", commandResponse.Text)
|
||||
assert.Nil(t, appErr)
|
||||
}))
|
||||
}
|
||||
|
||||
|
||||
Ссылка в новой задаче
Block a user