Support product-registered commands

Cherry-picked out of the mpa-playbooks and originally authored by Jose Peso.
This commit is contained in:
Jesse Hallam
2023-03-07 15:03:59 -04:00
parent 3676009cd7
commit 68e4a2b945
9 changed files with 375 additions and 28 deletions

View File

@@ -75,6 +75,8 @@ type AppIface interface {
// overriding attributes set by the user's login provider; otherwise, the name of the offending
// field is returned.
CheckProviderAttributes(user *model.User, patch *model.UserPatch) string
// CommandsForTeam returns all the plugin and product commands for the given team.
CommandsForTeam(teamID string) []*model.Command
// ComputeLastAccessibleFileTime updates cache with CreateAt time of the last accessible file as per the cloud plan's limit.
// Use GetLastAccessibleFileTime() to access the result.
ComputeLastAccessibleFileTime() error
@@ -944,7 +946,6 @@ type AppIface interface {
PermanentDeleteTeam(c request.CTX, team *model.Team) *model.AppError
PermanentDeleteTeamId(c request.CTX, teamID string) *model.AppError
PermanentDeleteUser(c *request.Context, user *model.User) *model.AppError
PluginCommandsForTeam(teamID string) []*model.Command
PostActionCookieSecret() []byte
PostAddToChannelMessage(c request.CTX, user *model.User, addedUser *model.User, channel *model.Channel, postRootId string) *model.AppError
PostPatchWithProxyRemovedFromImageURLs(patch *model.PostPatch) *model.PostPatch
@@ -970,6 +971,7 @@ type AppIface interface {
RegenerateTeamInviteId(teamID string) (*model.Team, *model.AppError)
RegisterCollectionAndTopic(pluginID, collectionType, topicType string) error
RegisterPluginCommand(pluginID string, command *model.Command) error
RegisterProductCommand(ProductID string, command *model.Command) error
ReloadConfig() error
RemoveAllDeactivatedMembersFromChannel(c request.CTX, channel *model.Channel) *model.AppError
RemoveChannelsFromRetentionPolicy(policyID string, channelIDs []string) *model.AppError

View File

@@ -48,6 +48,9 @@ type Channels struct {
pluginsEnvironment *plugin.Environment
pluginConfigListenerID string
productCommandsLock sync.RWMutex
productCommands []*ProductCommand
imageProxy *imageproxy.ImageProxy
// cached counts that are used during notice condition validation
@@ -331,7 +334,7 @@ func (ch *Channels) RequestTrialLicense(requesterID string, users int, termsAcce
}
func (a *App) HooksManager() *product.HooksManager {
return a.Srv().hooksManager
return a.ch.srv.hooksManager
}
// Ensure hooksService implements `product.HooksService`

View File

@@ -91,7 +91,7 @@ func (a *App) ListAutocompleteCommands(teamID string, T i18n.TranslateFunc) ([]*
seen[CmdCustomStatusTrigger] = true
}
for _, cmd := range a.PluginCommandsForTeam(teamID) {
for _, cmd := range a.CommandsForTeam(teamID) {
if cmd.AutoComplete && !seen[cmd.Trigger] {
seen[cmd.Trigger] = true
commands = append(commands, cmd)
@@ -154,7 +154,7 @@ func (a *App) ListAllCommands(teamID string, T i18n.TranslateFunc) ([]*model.Com
}
}
for _, cmd := range a.PluginCommandsForTeam(teamID) {
for _, cmd := range a.CommandsForTeam(teamID) {
if !seen[cmd.Trigger] {
seen[cmd.Trigger] = true
commands = append(commands, cmd)
@@ -202,7 +202,7 @@ func (a *App) ExecuteCommand(c request.CTX, args *model.CommandArgs) (*model.Com
args.TriggerId = triggerId
// Plugins can override built in and custom commands
// Plugins can override built in, custom, and product commands
cmd, response, appErr := a.tryExecutePluginCommand(c, args)
if appErr != nil {
return nil, appErr
@@ -211,6 +211,15 @@ func (a *App) ExecuteCommand(c request.CTX, args *model.CommandArgs) (*model.Com
return a.HandleCommandResponse(c, cmd, args, response, true)
}
// Products can override built in and custom commands
cmd, response, appErr = a.tryExecuteProductCommand(c, args)
if appErr != nil {
return nil, appErr
} else if cmd != nil && response != nil {
response.TriggerId = clientTriggerId
return a.HandleCommandResponse(c, cmd, args, response, true)
}
// Custom commands can override built ins
cmd, response, appErr = a.tryExecuteCustomCommand(c, args, trigger, message)
if appErr != nil {

View File

@@ -1592,6 +1592,23 @@ func (a *OpenTracingAppLayer) Cloud() einterfaces.CloudInterface {
return resultVar0
}
func (a *OpenTracingAppLayer) CommandsForTeam(teamID string) []*model.Command {
origCtx := a.ctx
span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.CommandsForTeam")
a.ctx = newCtx
a.app.Srv().Store().SetContext(newCtx)
defer func() {
a.app.Srv().Store().SetContext(origCtx)
a.ctx = origCtx
}()
defer span.Finish()
resultVar0 := a.app.CommandsForTeam(teamID)
return resultVar0
}
func (a *OpenTracingAppLayer) CompareAndDeletePluginKey(pluginID string, key string, oldValue []byte) (bool, *model.AppError) {
origCtx := a.ctx
span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.CompareAndDeletePluginKey")
@@ -13241,23 +13258,6 @@ func (a *OpenTracingAppLayer) PermanentDeleteUser(c *request.Context, user *mode
return resultVar0
}
func (a *OpenTracingAppLayer) PluginCommandsForTeam(teamID string) []*model.Command {
origCtx := a.ctx
span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.PluginCommandsForTeam")
a.ctx = newCtx
a.app.Srv().Store().SetContext(newCtx)
defer func() {
a.app.Srv().Store().SetContext(origCtx)
a.ctx = origCtx
}()
defer span.Finish()
resultVar0 := a.app.PluginCommandsForTeam(teamID)
return resultVar0
}
func (a *OpenTracingAppLayer) PopulateWebConnConfig(s *model.Session, cfg *platform.WebConnConfig, seqVal string) (*platform.WebConnConfig, error) {
origCtx := a.ctx
span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.PopulateWebConnConfig")
@@ -13837,6 +13837,28 @@ func (a *OpenTracingAppLayer) RegisterPluginCommand(pluginID string, command *mo
return resultVar0
}
func (a *OpenTracingAppLayer) RegisterProductCommand(ProductID string, command *model.Command) error {
origCtx := a.ctx
span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.RegisterProductCommand")
a.ctx = newCtx
a.app.Srv().Store().SetContext(newCtx)
defer func() {
a.app.Srv().Store().SetContext(origCtx)
a.ctx = origCtx
}()
defer span.Finish()
resultVar0 := a.app.RegisterProductCommand(ProductID, command)
if resultVar0 != nil {
span.LogFields(spanlog.Error(resultVar0))
ext.Error.Set(span, true)
}
return resultVar0
}
func (a *OpenTracingAppLayer) ReloadConfig() error {
origCtx := a.ctx
span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.ReloadConfig")

View File

@@ -1101,7 +1101,7 @@ func (api *PluginAPI) ListPluginCommands(teamID string) ([]*model.Command, error
commands := make([]*model.Command, 0)
seen := make(map[string]bool)
for _, cmd := range api.app.PluginCommandsForTeam(teamID) {
for _, cmd := range api.app.CommandsForTeam(teamID) {
if !seen[cmd.Trigger] {
seen[cmd.Trigger] = true
commands = append(commands, cmd)

View File

@@ -102,16 +102,25 @@ func (ch *Channels) unregisterPluginCommands(pluginID string) {
ch.pluginCommands = remaining
}
func (a *App) PluginCommandsForTeam(teamID string) []*model.Command {
// CommandsForTeam returns all the plugin and product commands for the given team.
func (a *App) CommandsForTeam(teamID string) []*model.Command {
var commands []*model.Command
a.ch.pluginCommandsLock.RLock()
defer a.ch.pluginCommandsLock.RUnlock()
var commands []*model.Command
for _, pc := range a.ch.pluginCommands {
if pc.Command.TeamId == "" || pc.Command.TeamId == teamID {
commands = append(commands, pc.Command)
}
}
a.ch.productCommandsLock.RLock()
defer a.ch.productCommandsLock.RUnlock()
for _, pc := range a.ch.productCommands {
if pc.Command.TeamId == "" || pc.Command.TeamId == teamID {
commands = append(commands, pc.Command)
}
}
return commands
}
@@ -174,3 +183,115 @@ func (a *App) tryExecutePluginCommand(c request.CTX, args *model.CommandArgs) (*
return matched.Command, response, appErr
}
// Support for slash commands to MPA
//
// Key differences/points with plugin commands:
// - There's no need of health checks or unregisterProductCommands on products, they are compiled and assumed as active server side
// - HooksForProduct still returns a plugin.Hooks struct, it might make sense to improve the name/package
// - Plugin code had a check for a plugin crash after a command was executed, that has been omitted for products
type ProductCommand struct {
Command *model.Command
ProductID string
}
func (a *App) RegisterProductCommand(ProductID string, command *model.Command) error {
if command.Trigger == "" {
return errors.New("invalid command")
}
if command.AutocompleteData != nil {
if err := command.AutocompleteData.IsValid(); err != nil {
return errors.Wrap(err, "invalid autocomplete data in command")
}
}
if command.AutocompleteData == nil {
command.AutocompleteData = model.NewAutocompleteData(command.Trigger, command.AutoCompleteHint, command.AutoCompleteDesc)
} else {
baseURL, err := url.Parse("/plugins/" + ProductID)
if err != nil {
return errors.Wrapf(err, "Can't parse url %s", "/plugins/"+ProductID)
}
err = command.AutocompleteData.UpdateRelativeURLsForPluginCommands(baseURL)
if err != nil {
return errors.Wrap(err, "Can't update relative urls for plugin commands")
}
}
command = &model.Command{
Trigger: strings.ToLower(command.Trigger),
TeamId: command.TeamId,
AutoComplete: command.AutoComplete,
AutoCompleteDesc: command.AutoCompleteDesc,
AutoCompleteHint: command.AutoCompleteHint,
DisplayName: command.DisplayName,
AutocompleteData: command.AutocompleteData,
AutocompleteIconData: command.AutocompleteIconData,
}
a.ch.productCommandsLock.Lock()
defer a.ch.productCommandsLock.Unlock()
for _, pc := range a.ch.productCommands {
if pc.Command.Trigger == command.Trigger && pc.Command.TeamId == command.TeamId {
if pc.ProductID == ProductID {
pc.Command = command
return nil
}
}
}
a.ch.productCommands = append(a.ch.productCommands, &ProductCommand{
Command: command,
ProductID: ProductID,
})
return nil
}
// tryExecuteProductCommand attempts to run a command provided by a product based on the given arguments. If no such
// command can be found, returns nil for all arguments.
func (a *App) tryExecuteProductCommand(c request.CTX, args *model.CommandArgs) (*model.Command, *model.CommandResponse, *model.AppError) {
parts := strings.Split(args.Command, " ")
trigger := parts[0][1:]
trigger = strings.ToLower(trigger)
var matched *ProductCommand
a.ch.productCommandsLock.RLock()
for _, pc := range a.ch.productCommands {
if (pc.Command.TeamId == "" || pc.Command.TeamId == args.TeamId) && pc.Command.Trigger == trigger {
matched = pc
break
}
}
a.ch.productCommandsLock.RUnlock()
if matched == nil {
return nil, nil, nil
}
// The type returned is still plugin.Hooks, could make sense in the future to move Hooks
// to another package or change the abstraction
productHooks := a.HooksManager().HooksForProduct(matched.ProductID)
if productHooks == nil {
return matched.Command, nil, model.NewAppError("ExecutePropductCommand", "model.plugin_command.error.app_error", nil, "", http.StatusInternalServerError)
}
for username, userID := range a.MentionsToTeamMembers(c, args.Command, args.TeamId) {
args.AddUserMention(username, userID)
}
for channelName, channelID := range a.MentionsToPublicChannels(c, args.Command, args.TeamId) {
args.AddChannelMention(channelName, channelID)
}
response, appErr := productHooks.ExecuteCommand(pluginContext(c), args)
// This is a response from the product, which may set an incorrect status code;
// e.g setting a status code of 0 will crash the server. So we always bucket everything under 500.
if appErr != nil && (appErr.StatusCode < 100 || appErr.StatusCode > 999) {
mlog.Warn("Invalid status code returned from plugin. Converting to internal server error.", mlog.String("plugin_id", matched.ProductID), mlog.Int("status_code", appErr.StatusCode))
appErr.StatusCode = http.StatusInternalServerError
}
return matched.Command, response, appErr
}

View File

@@ -7,10 +7,14 @@ import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/mattermost/mattermost-server/v6/channels/app/request"
"github.com/mattermost/mattermost-server/v6/channels/product"
"github.com/mattermost/mattermost-server/v6/model"
"github.com/mattermost/mattermost-server/v6/server/platform/shared/i18n"
"github.com/mattermost/mattermost-server/v6/platform/shared/i18n"
"github.com/mattermost/mattermost-server/v6/plugin"
)
func TestPluginCommand(t *testing.T) {
@@ -439,3 +443,188 @@ func TestPluginCommand(t *testing.T) {
})
}
// Test Product with the minimum code needed to handle
// hooksmanager and slash commands
type TProduct struct {
hooksService product.HooksService
}
func newTProduct(m map[product.ServiceKey]any) (product.Product, error) {
return &TProduct{
hooksService: m[product.HooksKey].(product.HooksService),
}, nil
}
func (p *TProduct) Start() error {
p.hooksService.RegisterHooks("productT", p)
return nil
}
func (p *TProduct) Stop() error { return nil }
func (p *TProduct) ExecuteCommand(c *plugin.Context, args *model.CommandArgs) (*model.CommandResponse, *model.AppError) {
return &model.CommandResponse{Text: "product slash command called"}, nil
}
func TestProductCommands(t *testing.T) {
products := map[string]product.Manifest{
"productT": {
Initializer: newTProduct,
Dependencies: map[product.ServiceKey]struct{}{},
},
}
t.Run("Execute product command", func(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
// Server hijack.
// This must be done in a cleaner way.
th.Server.initializeProducts(products, th.Server.services)
th.Server.products["productT"].Start()
require.Len(t, th.Server.products, 2) // 1 product + channels
err := th.App.RegisterProductCommand("productT", &model.Command{
TeamId: th.BasicTeam.Id,
Trigger: "product",
DisplayName: "Product Command",
AutoComplete: true,
AutoCompleteDesc: "autocomplete",
})
require.NoError(t, err)
ctx := request.EmptyContext(th.TestLogger)
resp, err2 := th.App.ExecuteCommand(ctx, &model.CommandArgs{
TeamId: th.BasicTeam.Id,
ChannelId: th.BasicChannel.Id,
UserId: th.BasicUser.Id,
Command: "/product",
})
require.Nil(t, err2)
require.NotNil(t, resp)
assert.Equal(t, "product slash command called", resp.Text)
})
t.Run("Product commands can override builtin commands", func(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
// Server hijack.
// This must be done in a cleaner way.
th.Server.initializeProducts(products, th.Server.services)
th.Server.products["productT"].Start()
require.Len(t, th.Server.products, 2) // 1 product + channels
err := th.App.RegisterProductCommand("productT", &model.Command{
TeamId: th.BasicTeam.Id,
Trigger: "away",
DisplayName: "Product Command",
AutoComplete: true,
AutoCompleteDesc: "autocomplete",
})
require.NoError(t, err)
ctx := request.EmptyContext(th.TestLogger)
resp, err2 := th.App.ExecuteCommand(ctx, &model.CommandArgs{
TeamId: th.BasicTeam.Id,
ChannelId: th.BasicChannel.Id,
UserId: th.BasicUser.Id,
Command: "/away",
})
require.Nil(t, err2)
require.NotNil(t, resp)
assert.Equal(t, "product slash command called", resp.Text)
})
t.Run("Plugin commands can override product commands", func(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
th.App.UpdateConfig(func(cfg *model.Config) {
cfg.PluginSettings.Plugins["testloadpluginconfig"] = map[string]any{
"TeamId": th.BasicTeam.Id,
}
})
tearDown, _, activationErrors := SetAppEnvironmentWithPlugins(t, []string{`
package main
import (
"github.com/mattermost/mattermost-server/server/v7/plugin"
"github.com/mattermost/mattermost-server/server/v7/model"
)
type configuration struct {
TeamId string
}
type MyPlugin struct {
plugin.MattermostPlugin
configuration configuration
}
func (p *MyPlugin) OnConfigurationChange() error {
if err := p.API.LoadPluginConfiguration(&p.configuration); err != nil {
return err
}
return nil
}
func (p *MyPlugin) OnActivate() error {
err := p.API.RegisterCommand(&model.Command{
TeamId: p.configuration.TeamId,
Trigger: "triggername",
DisplayName: "Plugin Command",
AutoComplete: true,
AutoCompleteDesc: "autocomplete",
})
if err != nil {
p.API.LogError("error", "err", err)
}
return err
}
func (p *MyPlugin) ExecuteCommand(c *plugin.Context, args *model.CommandArgs) (*model.CommandResponse, *model.AppError) {
return &model.CommandResponse{
Text: "plugin slash command called",
}, nil
}
func main() {
plugin.ClientMain(&MyPlugin{})
}
`}, th.App, th.NewPluginAPI)
defer tearDown()
require.Len(t, activationErrors, 1)
require.Nil(t, nil, activationErrors[0])
// Server hijack.
// This must be done in a cleaner way.
th.Server.initializeProducts(products, th.Server.services)
th.Server.products["productT"].Start()
require.Len(t, th.Server.products, 2) // 1 product + channels
err := th.App.RegisterProductCommand("productT", &model.Command{
TeamId: th.BasicTeam.Id,
Trigger: "triggername",
DisplayName: "Product Command",
AutoComplete: true,
AutoCompleteDesc: "autocomplete",
})
require.NoError(t, err)
ctx := request.EmptyContext(th.TestLogger)
resp, err2 := th.App.ExecuteCommand(ctx, &model.CommandArgs{
TeamId: th.BasicTeam.Id,
ChannelId: th.BasicChannel.Id,
UserId: th.BasicUser.Id,
Command: "/triggername",
})
require.Nil(t, err2)
require.NotNil(t, resp)
assert.Equal(t, "plugin slash command called", resp.Text)
})
}

View File

@@ -257,6 +257,7 @@ func NewServer(options ...Option) (*Server, error) {
product.SystemKey: &systemServiceAdapter{server: s},
product.SessionKey: app,
product.FrontendKey: app,
product.CommandKey: app,
}
// Step 4: Initialize products.

View File

@@ -253,7 +253,7 @@ type FrontendService interface {
// The service shall be registered via app.CommandKey service key.
type CommandService interface {
ExecuteCommand(c request.CTX, args *model.CommandArgs) (*model.CommandResponse, *model.AppError)
RegisterPluginCommand(pluginID string, command *model.Command) error
RegisterProductCommand(productID string, command *model.Command) error
}
// ThreadsService is the API for interacting with threads anywhere.