2023-06-05 06:42:55 -04:00
|
|
|
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
|
|
|
// See LICENSE.txt for license information.
|
|
|
|
|
|
|
|
|
|
package commands
|
|
|
|
|
|
|
|
|
|
import (
|
2023-06-06 17:29:29 -04:00
|
|
|
"context"
|
2023-06-05 06:42:55 -04:00
|
|
|
"strings"
|
|
|
|
|
|
2023-06-11 01:24:35 -04:00
|
|
|
"github.com/mattermost/mattermost/server/v8/cmd/mmctl/client"
|
2023-06-05 06:42:55 -04:00
|
|
|
|
2023-06-11 01:24:35 -04:00
|
|
|
"github.com/mattermost/mattermost/server/public/model"
|
2023-06-05 06:42:55 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// getCommandFromCommandArg retrieves a Command by command id or team:trigger.
|
|
|
|
|
func getCommandFromCommandArg(c client.Client, commandArg string) *model.Command {
|
|
|
|
|
if checkSlash(commandArg) {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cmd := getCommandFromTeamTrigger(c, commandArg)
|
|
|
|
|
if cmd == nil {
|
2023-06-06 17:29:29 -04:00
|
|
|
cmd, _, _ = c.GetCommandById(context.TODO(), commandArg)
|
2023-06-05 06:42:55 -04:00
|
|
|
}
|
|
|
|
|
return cmd
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// getCommandFromTeamTrigger retrieves a Command via team:trigger syntax.
|
|
|
|
|
func getCommandFromTeamTrigger(c client.Client, teamTrigger string) *model.Command {
|
|
|
|
|
arr := strings.Split(teamTrigger, ":")
|
|
|
|
|
if len(arr) != 2 {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-06 17:29:29 -04:00
|
|
|
team, _, _ := c.GetTeamByName(context.TODO(), arr[0], "")
|
2023-06-05 06:42:55 -04:00
|
|
|
if team == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
trigger := arr[1]
|
|
|
|
|
if trigger == "" {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-06 17:29:29 -04:00
|
|
|
list, _, _ := c.ListCommands(context.TODO(), team.Id, false)
|
2023-06-05 06:42:55 -04:00
|
|
|
if list == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, cmd := range list {
|
|
|
|
|
if cmd.Trigger == trigger {
|
|
|
|
|
return cmd
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|