Mm 16292 support command overrides (#14951)

* Supporing plugins that override built-in commands.

* Test.

* Fix test.

* Fix override order for command autocomplete.

* Fix test copy paste errors

Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
Этот коммит содержится в:
Christopher Speller
2020-07-02 13:28:21 -07:00
коммит произвёл GitHub
родитель f5be738923
Коммит 3e9ec51890
3 изменённых файлов: 100 добавлений и 23 удалений

Просмотреть файл

@@ -77,16 +77,6 @@ func (a *App) CreateCommandPost(post *model.Post, teamId string, response *model
func (a *App) ListAutocompleteCommands(teamId string, T goi18n.TranslateFunc) ([]*model.Command, *model.AppError) {
commands := make([]*model.Command, 0, 32)
seen := make(map[string]bool)
for _, value := range commandProviders {
if cmd := value.GetCommand(a, T); cmd != nil {
cpy := *cmd
if cpy.AutoComplete && !seen[cpy.Id] {
cpy.Sanitize()
seen[cpy.Trigger] = true
commands = append(commands, &cpy)
}
}
}
for _, cmd := range a.PluginCommandsForTeam(teamId) {
if cmd.AutoComplete && !seen[cmd.Trigger] {
@@ -102,7 +92,7 @@ func (a *App) ListAutocompleteCommands(teamId string, T goi18n.TranslateFunc) ([
}
for _, cmd := range teamCmds {
if cmd.AutoComplete && !seen[cmd.Id] {
if cmd.AutoComplete && !seen[cmd.Trigger] {
cmd.Sanitize()
seen[cmd.Trigger] = true
commands = append(commands, cmd)
@@ -110,6 +100,17 @@ func (a *App) ListAutocompleteCommands(teamId string, T goi18n.TranslateFunc) ([
}
}
for _, value := range commandProviders {
if cmd := value.GetCommand(a, T); cmd != nil {
cpy := *cmd
if cpy.AutoComplete && !seen[cpy.Trigger] {
cpy.Sanitize()
seen[cpy.Trigger] = true
commands = append(commands, &cpy)
}
}
}
return commands, nil
}
@@ -183,12 +184,8 @@ func (a *App) ExecuteCommand(args *model.CommandArgs) (*model.CommandResponse, *
args.TriggerId = triggerId
cmd, response := a.tryExecuteBuiltInCommand(args, trigger, message)
if cmd != nil && response != nil {
return a.HandleCommandResponse(cmd, args, response, true)
}
cmd, response, appErr = a.tryExecutePluginCommand(args)
// Plugins can override built in and custom commands
cmd, response, appErr := a.tryExecutePluginCommand(args)
if appErr != nil {
return nil, appErr
} else if cmd != nil && response != nil {
@@ -196,6 +193,7 @@ func (a *App) ExecuteCommand(args *model.CommandArgs) (*model.CommandResponse, *
return a.HandleCommandResponse(cmd, args, response, true)
}
// Custom commands can override built ins
cmd, response, appErr = a.tryExecuteCustomCommand(args, trigger, message)
if appErr != nil {
return nil, appErr
@@ -204,6 +202,11 @@ func (a *App) ExecuteCommand(args *model.CommandArgs) (*model.CommandResponse, *
return a.HandleCommandResponse(cmd, args, response, false)
}
cmd, response = a.tryExecuteBuiltInCommand(args, trigger, message)
if cmd != nil && response != nil {
return a.HandleCommandResponse(cmd, args, response, true)
}
return nil, model.NewAppError("command", "api.command.execute_command.not_found.app_error", map[string]interface{}{"Trigger": trigger}, "", http.StatusNotFound)
}