Migrate to stateless app.App (#17542)
* add request context * move initialialization to server * use app interface instead of global app functions * remove app context from webconn * cleanup * remove duplicated services * move context to separate package * remove finalize init method and move content to NewServer function * restart workers and schedulers after adding license for tests * reflect review comments Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
c09369f14a
Коммит
5ea06e51d0
@@ -10,6 +10,7 @@ import (
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/mattermost/mattermost-server/v5/app/request"
|
||||
"github.com/mattermost/mattermost-server/v5/model"
|
||||
"github.com/mattermost/mattermost-server/v5/shared/mlog"
|
||||
)
|
||||
@@ -20,7 +21,7 @@ type AutocompleteDynamicArgProvider interface {
|
||||
}
|
||||
|
||||
// GetSuggestions returns suggestions for user input.
|
||||
func (a *App) GetSuggestions(commandArgs *model.CommandArgs, commands []*model.Command, roleID string) []model.AutocompleteSuggestion {
|
||||
func (a *App) GetSuggestions(c *request.Context, commandArgs *model.CommandArgs, commands []*model.Command, roleID string) []model.AutocompleteSuggestion {
|
||||
sort.Slice(commands, func(i, j int) bool {
|
||||
return strings.Compare(strings.ToLower(commands[i].Trigger), strings.ToLower(commands[j].Trigger)) < 0
|
||||
})
|
||||
@@ -34,7 +35,7 @@ func (a *App) GetSuggestions(commandArgs *model.CommandArgs, commands []*model.C
|
||||
}
|
||||
|
||||
userInput := commandArgs.Command
|
||||
suggestions := a.getSuggestions(commandArgs, autocompleteData, "", userInput, roleID)
|
||||
suggestions := a.getSuggestions(c, commandArgs, autocompleteData, "", userInput, roleID)
|
||||
for i, suggestion := range suggestions {
|
||||
for _, command := range commands {
|
||||
if strings.HasPrefix(suggestion.Complete, command.Trigger) {
|
||||
@@ -47,7 +48,7 @@ func (a *App) GetSuggestions(commandArgs *model.CommandArgs, commands []*model.C
|
||||
return suggestions
|
||||
}
|
||||
|
||||
func (a *App) getSuggestions(commandArgs *model.CommandArgs, commands []*model.AutocompleteData, inputParsed, inputToBeParsed, roleID string) []model.AutocompleteSuggestion {
|
||||
func (a *App) getSuggestions(c *request.Context, commandArgs *model.CommandArgs, commands []*model.AutocompleteData, inputParsed, inputToBeParsed, roleID string) []model.AutocompleteSuggestion {
|
||||
suggestions := []model.AutocompleteSuggestion{}
|
||||
index := strings.Index(inputToBeParsed, " ")
|
||||
|
||||
@@ -78,12 +79,12 @@ func (a *App) getSuggestions(commandArgs *model.CommandArgs, commands []*model.A
|
||||
|
||||
if len(command.Arguments) == 0 {
|
||||
// Seek recursively in subcommands
|
||||
subSuggestions := a.getSuggestions(commandArgs, command.SubCommands, parsed, toBeParsed, roleID)
|
||||
subSuggestions := a.getSuggestions(c, commandArgs, command.SubCommands, parsed, toBeParsed, roleID)
|
||||
suggestions = append(suggestions, subSuggestions...)
|
||||
continue
|
||||
}
|
||||
|
||||
found, _, _, suggestion := a.parseArguments(commandArgs, command.Arguments, parsed, toBeParsed)
|
||||
found, _, _, suggestion := a.parseArguments(c, commandArgs, command.Arguments, parsed, toBeParsed)
|
||||
if found {
|
||||
suggestions = append(suggestions, suggestion...)
|
||||
}
|
||||
@@ -92,27 +93,27 @@ func (a *App) getSuggestions(commandArgs *model.CommandArgs, commands []*model.A
|
||||
return suggestions
|
||||
}
|
||||
|
||||
func (a *App) parseArguments(commandArgs *model.CommandArgs, args []*model.AutocompleteArg, parsed, toBeParsed string) (found bool, alreadyParsed string, yetToBeParsed string, suggestions []model.AutocompleteSuggestion) {
|
||||
func (a *App) parseArguments(c *request.Context, commandArgs *model.CommandArgs, args []*model.AutocompleteArg, parsed, toBeParsed string) (found bool, alreadyParsed string, yetToBeParsed string, suggestions []model.AutocompleteSuggestion) {
|
||||
if len(args) == 0 {
|
||||
return false, parsed, toBeParsed, suggestions
|
||||
}
|
||||
|
||||
if args[0].Required {
|
||||
found, changedParsed, changedToBeParsed, suggestion := a.parseArgument(commandArgs, args[0], parsed, toBeParsed)
|
||||
found, changedParsed, changedToBeParsed, suggestion := a.parseArgument(c, commandArgs, args[0], parsed, toBeParsed)
|
||||
if found {
|
||||
suggestions = append(suggestions, suggestion...)
|
||||
return true, changedParsed, changedToBeParsed, suggestions
|
||||
}
|
||||
return a.parseArguments(commandArgs, args[1:], changedParsed, changedToBeParsed)
|
||||
return a.parseArguments(c, commandArgs, args[1:], changedParsed, changedToBeParsed)
|
||||
}
|
||||
|
||||
// Handling optional arguments. Optional argument can be inputted or not,
|
||||
// so we have to pase both cases recursively and output combined suggestions.
|
||||
foundWithOptional, changedParsedWithOptional, changedToBeParsedWithOptional, suggestionsWithOptional := a.parseArgument(commandArgs, args[0], parsed, toBeParsed)
|
||||
foundWithOptional, changedParsedWithOptional, changedToBeParsedWithOptional, suggestionsWithOptional := a.parseArgument(c, commandArgs, args[0], parsed, toBeParsed)
|
||||
if foundWithOptional {
|
||||
suggestions = append(suggestions, suggestionsWithOptional...)
|
||||
} else {
|
||||
foundWithOptionalRest, changedParsedWithOptionalRest, changedToBeParsedWithOptionalRest, suggestionsWithOptionalRest := a.parseArguments(commandArgs, args[1:], changedParsedWithOptional, changedToBeParsedWithOptional)
|
||||
foundWithOptionalRest, changedParsedWithOptionalRest, changedToBeParsedWithOptionalRest, suggestionsWithOptionalRest := a.parseArguments(c, commandArgs, args[1:], changedParsedWithOptional, changedToBeParsedWithOptional)
|
||||
if foundWithOptionalRest {
|
||||
suggestions = append(suggestions, suggestionsWithOptionalRest...)
|
||||
}
|
||||
@@ -121,7 +122,7 @@ func (a *App) parseArguments(commandArgs *model.CommandArgs, args []*model.Autoc
|
||||
changedToBeParsedWithOptional = changedToBeParsedWithOptionalRest
|
||||
}
|
||||
|
||||
foundWithoutOptional, changedParsedWithoutOptional, changedToBeParsedWithoutOptional, suggestionsWithoutOptional := a.parseArguments(commandArgs, args[1:], parsed, toBeParsed)
|
||||
foundWithoutOptional, changedParsedWithoutOptional, changedToBeParsedWithoutOptional, suggestionsWithoutOptional := a.parseArguments(c, commandArgs, args[1:], parsed, toBeParsed)
|
||||
if foundWithoutOptional {
|
||||
suggestions = append(suggestions, suggestionsWithoutOptional...)
|
||||
}
|
||||
@@ -140,7 +141,7 @@ func (a *App) parseArguments(commandArgs *model.CommandArgs, args []*model.Autoc
|
||||
return foundWithoutOptional, changedParsedWithoutOptional, changedToBeParsedWithoutOptional, suggestions
|
||||
}
|
||||
|
||||
func (a *App) parseArgument(commandArgs *model.CommandArgs, arg *model.AutocompleteArg, parsed, toBeParsed string) (found bool, alreadyParsed string, yetToBeParsed string, suggestions []model.AutocompleteSuggestion) {
|
||||
func (a *App) parseArgument(c *request.Context, commandArgs *model.CommandArgs, arg *model.AutocompleteArg, parsed, toBeParsed string) (found bool, alreadyParsed string, yetToBeParsed string, suggestions []model.AutocompleteSuggestion) {
|
||||
if arg.Name != "" { //Parse the --name first
|
||||
found, changedParsed, changedToBeParsed, suggestion := parseNamedArgument(arg, parsed, toBeParsed)
|
||||
if found {
|
||||
@@ -174,7 +175,7 @@ func (a *App) parseArgument(commandArgs *model.CommandArgs, arg *model.Autocompl
|
||||
parsed = changedParsed
|
||||
toBeParsed = changedToBeParsed
|
||||
} else if arg.Type == model.AutocompleteArgTypeDynamicList {
|
||||
found, changedParsed, changedToBeParsed, dynamicListSuggestions := a.getDynamicListArgument(commandArgs, arg, parsed, toBeParsed)
|
||||
found, changedParsed, changedToBeParsed, dynamicListSuggestions := a.getDynamicListArgument(c, commandArgs, arg, parsed, toBeParsed)
|
||||
if found {
|
||||
suggestions = append(suggestions, dynamicListSuggestions...)
|
||||
return true, changedParsed, changedToBeParsed, suggestions
|
||||
@@ -237,7 +238,7 @@ func parseStaticListArgument(arg *model.AutocompleteArg, parsed, toBeParsed stri
|
||||
return parseListItems(a.PossibleArguments, parsed, toBeParsed)
|
||||
}
|
||||
|
||||
func (a *App) getDynamicListArgument(commandArgs *model.CommandArgs, arg *model.AutocompleteArg, parsed, toBeParsed string) (found bool, alreadyParsed string, yetToBeParsed string, suggestions []model.AutocompleteSuggestion) {
|
||||
func (a *App) getDynamicListArgument(c *request.Context, commandArgs *model.CommandArgs, arg *model.AutocompleteArg, parsed, toBeParsed string) (found bool, alreadyParsed string, yetToBeParsed string, suggestions []model.AutocompleteSuggestion) {
|
||||
dynamicArg := arg.Data.(*model.AutocompleteDynamicListArg)
|
||||
|
||||
if strings.HasPrefix(dynamicArg.FetchURL, "builtin:") {
|
||||
@@ -255,7 +256,7 @@ func (a *App) getDynamicListArgument(commandArgs *model.CommandArgs, arg *model.
|
||||
|
||||
// Encode the information normally provided to a plugin slash command handler into the request parameters
|
||||
// Encode PluginContext:
|
||||
pluginContext := a.PluginContext()
|
||||
pluginContext := pluginContext(c)
|
||||
params.Add("request_id", pluginContext.RequestId)
|
||||
params.Add("session_id", pluginContext.SessionId)
|
||||
params.Add("ip_address", pluginContext.IpAddress)
|
||||
@@ -270,7 +271,7 @@ func (a *App) getDynamicListArgument(commandArgs *model.CommandArgs, arg *model.
|
||||
params.Add("user_id", commandArgs.UserId)
|
||||
params.Add("site_url", commandArgs.SiteURL)
|
||||
|
||||
resp, err := a.doPluginRequest("GET", dynamicArg.FetchURL, params, nil)
|
||||
resp, err := a.doPluginRequest(c, "GET", dynamicArg.FetchURL, params, nil)
|
||||
|
||||
if err != nil {
|
||||
a.Log().Error("Can't fetch dynamic list arguments for", mlog.String("url", dynamicArg.FetchURL), mlog.Err(err))
|
||||
|
||||
Ссылка в новой задаче
Block a user