diff options
Diffstat (limited to 'api/command.go')
-rw-r--r-- | api/command.go | 125 |
1 files changed, 94 insertions, 31 deletions
diff --git a/api/command.go b/api/command.go index 54f863c48..50ca41155 100644 --- a/api/command.go +++ b/api/command.go @@ -17,14 +17,25 @@ import ( type commandHandler func(c *Context, command *model.Command) bool -var commands = []commandHandler{ - logoutCommand, - joinCommand, - loadTestCommand, - echoCommand, - shrugCommand, -} - +var ( + cmds = map[string]string{ + "logoutCommand": "/logout", + "joinCommand": "/join", + "loadTestCommand": "/loadtest", + "echoCommand": "/echo", + "shrugCommand": "/shrug", + "meCommand": "/me", + } + commands = []commandHandler{ + logoutCommand, + joinCommand, + loadTestCommand, + echoCommand, + shrugCommand, + meCommand, + } + commandNotImplementedErr = model.NewAppError("checkCommand", "Command not implemented", "") +) var echoSem chan bool func InitCommand(r *mux.Router) { @@ -45,7 +56,14 @@ func command(c *Context, w http.ResponseWriter, r *http.Request) { checkCommand(c, command) if c.Err != nil { - return + if c.Err != commandNotImplementedErr { + return + } else { + c.Err = nil + command.Response = model.RESP_NOT_IMPLEMENTED + w.Write([]byte(command.ToJson())) + return + } } else { w.Write([]byte(command.ToJson())) } @@ -66,6 +84,23 @@ func checkCommand(c *Context, command *model.Command) bool { } } + if !command.Suggest { + implemented := false + for _, cmd := range cmds { + bounds := len(cmd) + if len(command.Command) < bounds { + continue + } + if command.Command[:bounds] == cmd { + implemented = true + } + } + if !implemented { + c.Err = commandNotImplementedErr + return false + } + } + for _, v := range commands { if v(c, command) || c.Err != nil { @@ -78,7 +113,7 @@ func checkCommand(c *Context, command *model.Command) bool { func logoutCommand(c *Context, command *model.Command) bool { - cmd := "/logout" + cmd := cmds["logoutCommand"] if strings.Index(command.Command, cmd) == 0 { command.AddSuggestion(&model.SuggestCommand{Suggestion: cmd, Description: "Logout"}) @@ -97,7 +132,7 @@ func logoutCommand(c *Context, command *model.Command) bool { } func echoCommand(c *Context, command *model.Command) bool { - cmd := "/echo" + cmd := cmds["echoCommand"] maxThreads := 100 if !command.Suggest && strings.Index(command.Command, cmd) == 0 { @@ -161,11 +196,39 @@ func echoCommand(c *Context, command *model.Command) bool { return false } +func meCommand(c *Context, command *model.Command) bool { + cmd := cmds["meCommand"] + + if !command.Suggest && strings.Index(command.Command, cmd) == 0 { + message := "" + + parameters := strings.SplitN(command.Command, " ", 2) + if len(parameters) > 1 { + message += "*" + parameters[1] + "*" + } + + post := &model.Post{} + post.Message = message + post.ChannelId = command.ChannelId + if _, err := CreatePost(c, post, false); err != nil { + l4g.Error("Unable to create /me post post, err=%v", err) + return false + } + command.Response = model.RESP_EXECUTED + return true + + } else if strings.Index(cmd, command.Command) == 0 { + command.AddSuggestion(&model.SuggestCommand{Suggestion: cmd, Description: "Do an action, /me [message]"}) + } + + return false +} + func shrugCommand(c *Context, command *model.Command) bool { - cmd := "/shrug" + cmd := cmds["shrugCommand"] if !command.Suggest && strings.Index(command.Command, cmd) == 0 { - message := "¯\\_(ツ)_/¯" + message := `¯\\\_(ツ)_/¯` parameters := strings.SplitN(command.Command, " ", 2) if len(parameters) > 1 { @@ -192,7 +255,7 @@ func shrugCommand(c *Context, command *model.Command) bool { func joinCommand(c *Context, command *model.Command) bool { // looks for "/join channel-name" - cmd := "/join" + cmd := cmds["joinCommand"] if strings.Index(command.Command, cmd) == 0 { @@ -242,7 +305,7 @@ func joinCommand(c *Context, command *model.Command) bool { } func loadTestCommand(c *Context, command *model.Command) bool { - cmd := "/loadtest" + cmd := cmds["loadTestCommand"] // This command is only available when EnableTesting is true if !utils.Cfg.ServiceSettings.EnableTesting { @@ -304,7 +367,7 @@ func contains(items []string, token string) bool { } func loadTestSetupCommand(c *Context, command *model.Command) bool { - cmd := "/loadtest setup" + cmd := cmds["loadTestCommand"] + " setup" if strings.Index(command.Command, cmd) == 0 && !command.Suggest { tokens := strings.Fields(strings.TrimPrefix(command.Command, cmd)) @@ -348,11 +411,11 @@ func loadTestSetupCommand(c *Context, command *model.Command) bool { if doTeams { if err := CreateBasicUser(client); err != nil { - l4g.Error("Failed to create testing enviroment") + l4g.Error("Failed to create testing environment") return true } client.LoginByEmail(BTEST_TEAM_NAME, BTEST_USER_EMAIL, BTEST_USER_PASSWORD) - enviroment, err := CreateTestEnviromentWithTeams( + environment, err := CreateTestEnvironmentWithTeams( client, utils.Range{numTeams, numTeams}, utils.Range{numChannels, numChannels}, @@ -360,18 +423,18 @@ func loadTestSetupCommand(c *Context, command *model.Command) bool { utils.Range{numPosts, numPosts}, doFuzz) if err != true { - l4g.Error("Failed to create testing enviroment") + l4g.Error("Failed to create testing environment") return true } else { - l4g.Info("Testing enviroment created") - for i := 0; i < len(enviroment.Teams); i++ { - l4g.Info("Team Created: " + enviroment.Teams[i].Name) - l4g.Info("\t User to login: " + enviroment.Enviroments[i].Users[0].Email + ", " + USER_PASSWORD) + l4g.Info("Testing environment created") + for i := 0; i < len(environment.Teams); i++ { + l4g.Info("Team Created: " + environment.Teams[i].Name) + l4g.Info("\t User to login: " + environment.Environments[i].Users[0].Email + ", " + USER_PASSWORD) } } } else { client.MockSession(c.Session.Token) - CreateTestEnviromentInTeam( + CreateTestEnvironmentInTeam( client, c.Session.TeamId, utils.Range{numChannels, numChannels}, @@ -383,15 +446,15 @@ func loadTestSetupCommand(c *Context, command *model.Command) bool { } else if strings.Index(cmd, command.Command) == 0 { command.AddSuggestion(&model.SuggestCommand{ Suggestion: cmd, - Description: "Creates a testing enviroment in current team. [teams] [fuzz] <Num Channels> <Num Users> <NumPosts>"}) + Description: "Creates a testing environment in current team. [teams] [fuzz] <Num Channels> <Num Users> <NumPosts>"}) } return false } func loadTestUsersCommand(c *Context, command *model.Command) bool { - cmd1 := "/loadtest users" - cmd2 := "/loadtest users fuzz" + cmd1 := cmds["loadTestCommand"] + " users" + cmd2 := cmds["loadTestCommand"] + " users fuzz" if strings.Index(command.Command, cmd1) == 0 && !command.Suggest { cmd := cmd1 @@ -420,8 +483,8 @@ func loadTestUsersCommand(c *Context, command *model.Command) bool { } func loadTestChannelsCommand(c *Context, command *model.Command) bool { - cmd1 := "/loadtest channels" - cmd2 := "/loadtest channels fuzz" + cmd1 := cmds["loadTestCommand"] + " channels" + cmd2 := cmds["loadTestCommand"] + " channels fuzz" if strings.Index(command.Command, cmd1) == 0 && !command.Suggest { cmd := cmd1 @@ -451,8 +514,8 @@ func loadTestChannelsCommand(c *Context, command *model.Command) bool { } func loadTestPostsCommand(c *Context, command *model.Command) bool { - cmd1 := "/loadtest posts" - cmd2 := "/loadtest posts fuzz" + cmd1 := cmds["loadTestCommand"] + " posts" + cmd2 := cmds["loadTestCommand"] + " posts fuzz" if strings.Index(command.Command, cmd1) == 0 && !command.Suggest { cmd := cmd1 |