Files
mattermost/app/plugin_commands.go

173 lines
5.5 KiB
Go
Raw Normal View History

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package app
import (
"fmt"
"net/http"
"net/url"
"strings"
"github.com/pkg/errors"
"github.com/mattermost/mattermost-server/v6/app/request"
"github.com/mattermost/mattermost-server/v6/model"
)
type PluginCommand struct {
Command *model.Command
PluginId string
}
func (a *App) RegisterPluginCommand(pluginID 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/" + pluginID)
if err != nil {
return errors.Wrapf(err, "Can't parse url %s", "/plugins/"+pluginID)
}
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.Srv().pluginCommandsLock.Lock()
defer a.Srv().pluginCommandsLock.Unlock()
for _, pc := range a.Srv().pluginCommands {
if pc.Command.Trigger == command.Trigger && pc.Command.TeamId == command.TeamId {
if pc.PluginId == pluginID {
pc.Command = command
return nil
}
}
}
a.Srv().pluginCommands = append(a.Srv().pluginCommands, &PluginCommand{
Command: command,
PluginId: pluginID,
})
return nil
}
func (a *App) UnregisterPluginCommand(pluginID, teamID, trigger string) {
trigger = strings.ToLower(trigger)
a.Srv().pluginCommandsLock.Lock()
defer a.Srv().pluginCommandsLock.Unlock()
var remaining []*PluginCommand
for _, pc := range a.Srv().pluginCommands {
if pc.Command.TeamId != teamID || pc.Command.Trigger != trigger {
remaining = append(remaining, pc)
}
}
a.Srv().pluginCommands = remaining
}
func (a *App) UnregisterPluginCommands(pluginID string) {
a.Srv().unregisterPluginCommands(pluginID)
}
func (s *Server) unregisterPluginCommands(pluginID string) {
s.pluginCommandsLock.Lock()
defer s.pluginCommandsLock.Unlock()
var remaining []*PluginCommand
for _, pc := range s.pluginCommands {
if pc.PluginId != pluginID {
remaining = append(remaining, pc)
}
}
s.pluginCommands = remaining
}
func (a *App) PluginCommandsForTeam(teamID string) []*model.Command {
a.Srv().pluginCommandsLock.RLock()
defer a.Srv().pluginCommandsLock.RUnlock()
var commands []*model.Command
for _, pc := range a.Srv().pluginCommands {
if pc.Command.TeamId == "" || pc.Command.TeamId == teamID {
commands = append(commands, pc.Command)
}
}
return commands
}
// tryExecutePluginCommand attempts to run a command provided by a plugin based on the given arguments. If no such
// command can be found, returns nil for all arguments.
func (a *App) tryExecutePluginCommand(c *request.Context, args *model.CommandArgs) (*model.Command, *model.CommandResponse, *model.AppError) {
parts := strings.Split(args.Command, " ")
trigger := parts[0][1:]
trigger = strings.ToLower(trigger)
var matched *PluginCommand
a.Srv().pluginCommandsLock.RLock()
for _, pc := range a.Srv().pluginCommands {
if (pc.Command.TeamId == "" || pc.Command.TeamId == args.TeamId) && pc.Command.Trigger == trigger {
matched = pc
break
}
}
a.Srv().pluginCommandsLock.RUnlock()
if matched == nil {
return nil, nil, nil
}
pluginsEnvironment := a.GetPluginsEnvironment()
if pluginsEnvironment == nil {
return nil, nil, nil
}
// Checking if plugin is working or not
if err := pluginsEnvironment.PerformHealthCheck(matched.PluginId); err != nil {
return matched.Command, nil, model.NewAppError("ExecutePluginCommand", "model.plugin_command_error.error.app_error", map[string]interface{}{"Command": trigger}, "err= Plugin has recently crashed: "+matched.PluginId, http.StatusInternalServerError)
}
pluginHooks, err := pluginsEnvironment.HooksForPlugin(matched.PluginId)
if err != nil {
return matched.Command, nil, model.NewAppError("ExecutePluginCommand", "model.plugin_command.error.app_error", nil, "err="+err.Error(), http.StatusInternalServerError)
}
for username, userID := range a.MentionsToTeamMembers(args.Command, args.TeamId) {
args.AddUserMention(username, userID)
MM-21987 Resolve mentions in slash commands (#13762) * Create infrastructure to manage mentions Two new files have been added (along with their tests); namely: - model/at_mentions.go: utilities to parse and manage mentions; for the moment, it just contains a regex and a couple of functions to parse possible mentions and to post-process them, but it can be extended in the future. - model/mention_map.go: it contains two new types (UserMentionMap and ChannelMentionMap) that both have FromURLValues and ToURLValues. These types can be used when adding the mentions to the payload of the plugin slash commands. * Extend custom commands payload with mentions Two couples of new fields are added to the payload; namely: - user_mentions and user_mentions_ids: two aligned arrays of the same length containing all the different @-mentions found in the command: the i-th element of user_mentions_ids is the user identifier of the i-th element of user_mentions. - channel_mentions and channel_mentions_ids: two aligned arrays of the same length containing all the different ~-mentions found in the command: the i-th element of channel_mentions_ids is the channel identifier of the i-th element of channel_mentions. * Fix shadowing of variables and redundant return * Fix shadowing of variable * Address review comments (HT @lieut-data) - Improvements in mentionsToTeamMembers and mentionsToPublicChannels: - Scope implementation details inside the functions. - Improve goroutines synchronization by using a sync.WaitGroup. - Retry lookup of username only if the returned error is http.StatusCode, so we can return early if the error is more severe. - Invert check in PossibleAtMentions to improve readability. - Make user and channel mention keys private to the module. - Allow the specification of an empty map of mentions in (Channel|User)MentionsFromURLValues when both mentions keys are absent. - Replace custom functions in tests with require.Equal on maps. * Test functions to parse mentions from messages * Extend plugin commands payload with mentions * Add functions to CommandArgs to add mentions The functions make sure that the maps are initialized before adding any value. * Address review comments (HT @lieut-data) - Adds a mlog.Warn to avoid burying the error when the user is not found. - Improve readability in loop populating the mention map by moving the initialization of the map closer to the loop and by iterating over the channel itself, not over its length. * File was not gofmt-ed with -s * Close channel when all goroutines are finished * Again, all code should be checked with gofmt -s * Refactor code out of a goroutine This change helps improve the readability of the code and does not affect its overall performance. Less complexity is always better. * Close channel and iterate over its range Adapt mentionsToPublicChannels to have the same structure in the management of the mentions channel as in mentionsToTeamMembers. * Adapt mentionsToTeamMembers to new App Commit 17523fa changed the App structure, making the *Server field private, which is now accessed through the Srv() function. Co-authored-by: mattermod <mattermod@users.noreply.github.com>
2020-03-11 11:50:12 +01:00
}
for channelName, channelID := range a.MentionsToPublicChannels(args.Command, args.TeamId) {
args.AddChannelMention(channelName, channelID)
MM-21987 Resolve mentions in slash commands (#13762) * Create infrastructure to manage mentions Two new files have been added (along with their tests); namely: - model/at_mentions.go: utilities to parse and manage mentions; for the moment, it just contains a regex and a couple of functions to parse possible mentions and to post-process them, but it can be extended in the future. - model/mention_map.go: it contains two new types (UserMentionMap and ChannelMentionMap) that both have FromURLValues and ToURLValues. These types can be used when adding the mentions to the payload of the plugin slash commands. * Extend custom commands payload with mentions Two couples of new fields are added to the payload; namely: - user_mentions and user_mentions_ids: two aligned arrays of the same length containing all the different @-mentions found in the command: the i-th element of user_mentions_ids is the user identifier of the i-th element of user_mentions. - channel_mentions and channel_mentions_ids: two aligned arrays of the same length containing all the different ~-mentions found in the command: the i-th element of channel_mentions_ids is the channel identifier of the i-th element of channel_mentions. * Fix shadowing of variables and redundant return * Fix shadowing of variable * Address review comments (HT @lieut-data) - Improvements in mentionsToTeamMembers and mentionsToPublicChannels: - Scope implementation details inside the functions. - Improve goroutines synchronization by using a sync.WaitGroup. - Retry lookup of username only if the returned error is http.StatusCode, so we can return early if the error is more severe. - Invert check in PossibleAtMentions to improve readability. - Make user and channel mention keys private to the module. - Allow the specification of an empty map of mentions in (Channel|User)MentionsFromURLValues when both mentions keys are absent. - Replace custom functions in tests with require.Equal on maps. * Test functions to parse mentions from messages * Extend plugin commands payload with mentions * Add functions to CommandArgs to add mentions The functions make sure that the maps are initialized before adding any value. * Address review comments (HT @lieut-data) - Adds a mlog.Warn to avoid burying the error when the user is not found. - Improve readability in loop populating the mention map by moving the initialization of the map closer to the loop and by iterating over the channel itself, not over its length. * File was not gofmt-ed with -s * Close channel when all goroutines are finished * Again, all code should be checked with gofmt -s * Refactor code out of a goroutine This change helps improve the readability of the code and does not affect its overall performance. Less complexity is always better. * Close channel and iterate over its range Adapt mentionsToPublicChannels to have the same structure in the management of the mentions channel as in mentionsToTeamMembers. * Adapt mentionsToTeamMembers to new App Commit 17523fa changed the App structure, making the *Server field private, which is now accessed through the Srv() function. Co-authored-by: mattermod <mattermod@users.noreply.github.com>
2020-03-11 11:50:12 +01:00
}
response, appErr := pluginHooks.ExecuteCommand(pluginContext(c), args)
// Checking if plugin crashed after running the command
if err := pluginsEnvironment.PerformHealthCheck(matched.PluginId); err != nil {
errMessage := fmt.Sprintf("err= Plugin %s crashed due to /%s command", matched.PluginId, trigger)
return matched.Command, nil, model.NewAppError("ExecutePluginCommand", "model.plugin_command_crash.error.app_error", map[string]interface{}{"Command": trigger, "PluginId": matched.PluginId}, errMessage, http.StatusInternalServerError)
}
return matched.Command, response, appErr
}