diff --git a/server/channels/app/app_iface.go b/server/channels/app/app_iface.go index c192dc571c..cf749c3fc9 100644 --- a/server/channels/app/app_iface.go +++ b/server/channels/app/app_iface.go @@ -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 diff --git a/server/channels/app/channels.go b/server/channels/app/channels.go index ed3fc12fcd..52ed6d07fb 100644 --- a/server/channels/app/channels.go +++ b/server/channels/app/channels.go @@ -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` diff --git a/server/channels/app/command.go b/server/channels/app/command.go index bdad88e727..acf4e496c2 100644 --- a/server/channels/app/command.go +++ b/server/channels/app/command.go @@ -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 { diff --git a/server/channels/app/opentracing/opentracing_layer.go b/server/channels/app/opentracing/opentracing_layer.go index 5994816d2d..4a4cd72441 100644 --- a/server/channels/app/opentracing/opentracing_layer.go +++ b/server/channels/app/opentracing/opentracing_layer.go @@ -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") diff --git a/server/channels/app/plugin_api.go b/server/channels/app/plugin_api.go index 1db6623233..6e202efb16 100644 --- a/server/channels/app/plugin_api.go +++ b/server/channels/app/plugin_api.go @@ -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) diff --git a/server/channels/app/plugin_commands.go b/server/channels/app/plugin_commands.go index 56feb27537..ffb4f00d9d 100644 --- a/server/channels/app/plugin_commands.go +++ b/server/channels/app/plugin_commands.go @@ -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 +} diff --git a/server/channels/app/plugin_commands_test.go b/server/channels/app/plugin_commands_test.go index 1e76aad832..c494756e25 100644 --- a/server/channels/app/plugin_commands_test.go +++ b/server/channels/app/plugin_commands_test.go @@ -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) + + }) +} diff --git a/server/channels/app/server.go b/server/channels/app/server.go index aafa71cdea..c9dd6a9752 100644 --- a/server/channels/app/server.go +++ b/server/channels/app/server.go @@ -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. diff --git a/server/channels/product/api.go b/server/channels/product/api.go index c553e7aa12..663bab9487 100644 --- a/server/channels/product/api.go +++ b/server/channels/product/api.go @@ -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.