github.com/jlevesy/mattermost-server@v5.3.2-0.20181003190404-7468f35cb0c8+incompatible/app/plugin_commands.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See LICENSE.txt for license information. 3 4 package app 5 6 import ( 7 "fmt" 8 "net/http" 9 "strings" 10 11 "github.com/mattermost/mattermost-server/model" 12 "github.com/mattermost/mattermost-server/plugin" 13 ) 14 15 type PluginCommand struct { 16 Command *model.Command 17 PluginId string 18 } 19 20 func (a *App) RegisterPluginCommand(pluginId string, command *model.Command) error { 21 if command.Trigger == "" { 22 return fmt.Errorf("invalid command") 23 } 24 25 command = &model.Command{ 26 Trigger: strings.ToLower(command.Trigger), 27 TeamId: command.TeamId, 28 AutoComplete: command.AutoComplete, 29 AutoCompleteDesc: command.AutoCompleteDesc, 30 AutoCompleteHint: command.AutoCompleteHint, 31 DisplayName: command.DisplayName, 32 } 33 34 a.pluginCommandsLock.Lock() 35 defer a.pluginCommandsLock.Unlock() 36 37 for _, pc := range a.pluginCommands { 38 if pc.Command.Trigger == command.Trigger && pc.Command.TeamId == command.TeamId { 39 if pc.PluginId == pluginId { 40 pc.Command = command 41 return nil 42 } 43 } 44 } 45 46 a.pluginCommands = append(a.pluginCommands, &PluginCommand{ 47 Command: command, 48 PluginId: pluginId, 49 }) 50 return nil 51 } 52 53 func (a *App) UnregisterPluginCommand(pluginId, teamId, trigger string) { 54 trigger = strings.ToLower(trigger) 55 56 a.pluginCommandsLock.Lock() 57 defer a.pluginCommandsLock.Unlock() 58 59 var remaining []*PluginCommand 60 for _, pc := range a.pluginCommands { 61 if pc.Command.TeamId != teamId || pc.Command.Trigger != trigger { 62 remaining = append(remaining, pc) 63 } 64 } 65 a.pluginCommands = remaining 66 } 67 68 func (a *App) UnregisterPluginCommands(pluginId string) { 69 a.pluginCommandsLock.Lock() 70 defer a.pluginCommandsLock.Unlock() 71 72 var remaining []*PluginCommand 73 for _, pc := range a.pluginCommands { 74 if pc.PluginId != pluginId { 75 remaining = append(remaining, pc) 76 } 77 } 78 a.pluginCommands = remaining 79 } 80 81 func (a *App) PluginCommandsForTeam(teamId string) []*model.Command { 82 a.pluginCommandsLock.RLock() 83 defer a.pluginCommandsLock.RUnlock() 84 85 var commands []*model.Command 86 for _, pc := range a.pluginCommands { 87 if pc.Command.TeamId == "" || pc.Command.TeamId == teamId { 88 commands = append(commands, pc.Command) 89 } 90 } 91 return commands 92 } 93 94 func (a *App) ExecutePluginCommand(args *model.CommandArgs) (*model.Command, *model.CommandResponse, *model.AppError) { 95 parts := strings.Split(args.Command, " ") 96 trigger := parts[0][1:] 97 trigger = strings.ToLower(trigger) 98 99 a.pluginCommandsLock.RLock() 100 defer a.pluginCommandsLock.RUnlock() 101 102 for _, pc := range a.pluginCommands { 103 if (pc.Command.TeamId == "" || pc.Command.TeamId == args.TeamId) && pc.Command.Trigger == trigger { 104 pluginHooks, err := a.Plugins.HooksForPlugin(pc.PluginId) 105 if err != nil { 106 return pc.Command, nil, model.NewAppError("ExecutePluginCommand", "model.plugin_command.error.app_error", nil, "err="+err.Error(), http.StatusInternalServerError) 107 } 108 response, appErr := pluginHooks.ExecuteCommand(&plugin.Context{}, args) 109 return pc.Command, response, appErr 110 } 111 } 112 return nil, nil, nil 113 }