Initial commit from mattermost-plugin-starter-template

This commit is contained in:
Felipe M 2025-07-30 13:12:52 +02:00
commit acbc69f7eb
No known key found for this signature in database
GPG key ID: 52E5D65FCF99808A
57 changed files with 27772 additions and 0 deletions

64
server/command/command.go Normal file
View file

@ -0,0 +1,64 @@
package command
import (
"fmt"
"strings"
"github.com/mattermost/mattermost/server/public/model"
"github.com/mattermost/mattermost/server/public/pluginapi"
)
type Handler struct {
client *pluginapi.Client
}
type Command interface {
Handle(args *model.CommandArgs) (*model.CommandResponse, error)
executeHelloCommand(args *model.CommandArgs) *model.CommandResponse
}
const helloCommandTrigger = "hello"
// Register all your slash commands in the NewCommandHandler function.
func NewCommandHandler(client *pluginapi.Client) Command {
err := client.SlashCommand.Register(&model.Command{
Trigger: helloCommandTrigger,
AutoComplete: true,
AutoCompleteDesc: "Say hello to someone",
AutoCompleteHint: "[@username]",
AutocompleteData: model.NewAutocompleteData(helloCommandTrigger, "[@username]", "Username to say hello to"),
})
if err != nil {
client.Log.Error("Failed to register command", "error", err)
}
return &Handler{
client: client,
}
}
// ExecuteCommand hook calls this method to execute the commands that were registered in the NewCommandHandler function.
func (c *Handler) Handle(args *model.CommandArgs) (*model.CommandResponse, error) {
trigger := strings.TrimPrefix(strings.Fields(args.Command)[0], "/")
switch trigger {
case helloCommandTrigger:
return c.executeHelloCommand(args), nil
default:
return &model.CommandResponse{
ResponseType: model.CommandResponseTypeEphemeral,
Text: fmt.Sprintf("Unknown command: %s", args.Command),
}, nil
}
}
func (c *Handler) executeHelloCommand(args *model.CommandArgs) *model.CommandResponse {
if len(strings.Fields(args.Command)) < 2 {
return &model.CommandResponse{
ResponseType: model.CommandResponseTypeEphemeral,
Text: "Please specify a username",
}
}
username := strings.Fields(args.Command)[1]
return &model.CommandResponse{
Text: "Hello, " + username,
}
}

View file

@ -0,0 +1,47 @@
package command
import (
"testing"
"github.com/mattermost/mattermost/server/public/model"
"github.com/mattermost/mattermost/server/public/plugin/plugintest"
"github.com/mattermost/mattermost/server/public/pluginapi"
"github.com/stretchr/testify/assert"
)
type env struct {
client *pluginapi.Client
api *plugintest.API
}
func setupTest() *env {
api := &plugintest.API{}
driver := &plugintest.Driver{}
client := pluginapi.NewClient(api, driver)
return &env{
client: client,
api: api,
}
}
func TestHelloCommand(t *testing.T) {
assert := assert.New(t)
env := setupTest()
env.api.On("RegisterCommand", &model.Command{
Trigger: helloCommandTrigger,
AutoComplete: true,
AutoCompleteDesc: "Say hello to someone",
AutoCompleteHint: "[@username]",
AutocompleteData: model.NewAutocompleteData("hello", "[@username]", "Username to say hello to"),
}).Return(nil)
cmdHandler := NewCommandHandler(env.client)
args := &model.CommandArgs{
Command: "/hello world",
}
response, err := cmdHandler.Handle(args)
assert.Nil(err)
assert.Equal("Hello, world", response.Text)
}

View file

@ -0,0 +1,64 @@
// Code generated by MockGen. DO NOT EDIT.
// Source: github.com/mattermost/mattermost-plugin-bridge-xmpp/server/command (interfaces: Command)
// Package mocks is a generated GoMock package.
package mocks
import (
reflect "reflect"
gomock "github.com/golang/mock/gomock"
model "github.com/mattermost/mattermost/server/public/model"
)
// MockCommand is a mock of Command interface.
type MockCommand struct {
ctrl *gomock.Controller
recorder *MockCommandMockRecorder
}
// MockCommandMockRecorder is the mock recorder for MockCommand.
type MockCommandMockRecorder struct {
mock *MockCommand
}
// NewMockCommand creates a new mock instance.
func NewMockCommand(ctrl *gomock.Controller) *MockCommand {
mock := &MockCommand{ctrl: ctrl}
mock.recorder = &MockCommandMockRecorder{mock}
return mock
}
// EXPECT returns an object that allows the caller to indicate expected use.
func (m *MockCommand) EXPECT() *MockCommandMockRecorder {
return m.recorder
}
// Handle mocks base method.
func (m *MockCommand) Handle(arg0 *model.CommandArgs) (*model.CommandResponse, *model.AppError) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Handle", arg0)
ret0, _ := ret[0].(*model.CommandResponse)
ret1, _ := ret[1].(*model.AppError)
return ret0, ret1
}
// Handle indicates an expected call of Handle.
func (mr *MockCommandMockRecorder) Handle(arg0 interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Handle", reflect.TypeOf((*MockCommand)(nil).Handle), arg0)
}
// executeHelloCommand mocks base method.
func (m *MockCommand) executeHelloCommand(arg0 *model.CommandArgs) *model.CommandResponse {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "executeHelloCommand", arg0)
ret0, _ := ret[0].(*model.CommandResponse)
return ret0
}
// executeHelloCommand indicates an expected call of executeHelloCommand.
func (mr *MockCommandMockRecorder) executeHelloCommand(arg0 interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "executeHelloCommand", reflect.TypeOf((*MockCommand)(nil).executeHelloCommand), arg0)
}