PLT-1598 Slash command works in RHS (#4367)

* PLT-1598 Slash command works in RHS

* fix UserProfile in the reply for Slash Command

* fix some problem about the system messages in RHS
* system message in RHS isn't displayed as comment for root message

* remove status indicator for system message in RHS

* system message in RHS is colored to grey

* system messages don't count as commented post

* fix bug about cleaning draft in RHS

* remove unnecessary function

* implement new model for executing command
This commit is contained in:
Yusuke Nemoto
2016-12-10 13:35:16 +09:00
committed by Joram Wilander
parent cb870c83d1
commit ddacfa58ba
35 changed files with 186 additions and 79 deletions

View File

@@ -20,7 +20,7 @@ import (
type CommandProvider interface {
GetTrigger() string
GetCommand(c *Context) *model.Command
DoCommand(c *Context, channelId string, message string) *model.CommandResponse
DoCommand(c *Context, args *model.CommandArgs, message string) *model.CommandResponse
}
var commandProviders = make(map[string]CommandProvider)
@@ -88,30 +88,28 @@ func listCommands(c *Context, w http.ResponseWriter, r *http.Request) {
}
func executeCommand(c *Context, w http.ResponseWriter, r *http.Request) {
props := model.MapFromJson(r.Body)
command := strings.TrimSpace(props["command"])
channelId := strings.TrimSpace(props["channelId"])
commandArgs := model.CommandArgsFromJson(r.Body)
if len(command) <= 1 || strings.Index(command, "/") != 0 {
if len(commandArgs.Command) <= 1 || strings.Index(commandArgs.Command, "/") != 0 {
c.Err = model.NewLocAppError("executeCommand", "api.command.execute_command.start.app_error", nil, "")
return
}
if len(channelId) > 0 {
if !HasPermissionToChannelContext(c, channelId, model.PERMISSION_USE_SLASH_COMMANDS) {
if len(commandArgs.ChannelId) > 0 {
if !HasPermissionToChannelContext(c, commandArgs.ChannelId, model.PERMISSION_USE_SLASH_COMMANDS) {
return
}
}
parts := strings.Split(command, " ")
parts := strings.Split(commandArgs.Command, " ")
trigger := parts[0][1:]
trigger = strings.ToLower(trigger)
message := strings.Join(parts[1:], " ")
provider := GetCommandProvider(trigger)
if provider != nil {
response := provider.DoCommand(c, channelId, message)
handleResponse(c, w, response, channelId, provider.GetCommand(c), true)
response := provider.DoCommand(c, commandArgs, message)
handleResponse(c, w, response, commandArgs, provider.GetCommand(c), true)
return
} else {
@@ -121,7 +119,7 @@ func executeCommand(c *Context, w http.ResponseWriter, r *http.Request) {
return
}
chanChan := Srv.Store.Channel().Get(channelId)
chanChan := Srv.Store.Channel().Get(commandArgs.ChannelId)
teamChan := Srv.Store.Team().Get(c.TeamId)
userChan := Srv.Store.User().Get(c.Session.UserId)
@@ -166,7 +164,7 @@ func executeCommand(c *Context, w http.ResponseWriter, r *http.Request) {
p.Set("team_id", cmd.TeamId)
p.Set("team_domain", team.Name)
p.Set("channel_id", channelId)
p.Set("channel_id", commandArgs.ChannelId)
p.Set("channel_name", channel.Name)
p.Set("user_id", c.Session.UserId)
@@ -200,7 +198,7 @@ func executeCommand(c *Context, w http.ResponseWriter, r *http.Request) {
if response == nil {
c.Err = model.NewLocAppError("command", "api.command.execute_command.failed_empty.app_error", map[string]interface{}{"Trigger": trigger}, "")
} else {
handleResponse(c, w, response, channelId, cmd, false)
handleResponse(c, w, response, commandArgs, cmd, false)
}
} else {
defer resp.Body.Close()
@@ -219,10 +217,11 @@ func executeCommand(c *Context, w http.ResponseWriter, r *http.Request) {
c.Err = model.NewLocAppError("command", "api.command.execute_command.not_found.app_error", map[string]interface{}{"Trigger": trigger}, "")
}
func handleResponse(c *Context, w http.ResponseWriter, response *model.CommandResponse, channelId string, cmd *model.Command, builtIn bool) {
func handleResponse(c *Context, w http.ResponseWriter, response *model.CommandResponse, commandArgs *model.CommandArgs, cmd *model.Command, builtIn bool) {
post := &model.Post{}
post.ChannelId = channelId
post.ChannelId = commandArgs.ChannelId
post.RootId = commandArgs.RootId
post.ParentId = commandArgs.ParentId
if !builtIn {
post.AddProp("from_webhook", "true")
@@ -258,6 +257,7 @@ func handleResponse(c *Context, w http.ResponseWriter, response *model.CommandRe
post.Message = response.Text
post.CreateAt = model.GetMillis()
post.UserId = c.Session.UserId
post.ParentId = ""
SendEphemeralPost(
c.TeamId,
c.Session.UserId,

View File

@@ -31,7 +31,7 @@ func (me *AwayProvider) GetCommand(c *Context) *model.Command {
}
}
func (me *AwayProvider) DoCommand(c *Context, channelId string, message string) *model.CommandResponse {
func (me *AwayProvider) DoCommand(c *Context, args *model.CommandArgs, message string) *model.CommandResponse {
rmsg := c.T("api.command_away.success")
if len(message) > 0 {
rmsg = message + " " + rmsg

View File

@@ -39,7 +39,7 @@ func (me *EchoProvider) GetCommand(c *Context) *model.Command {
}
}
func (me *EchoProvider) DoCommand(c *Context, channelId string, message string) *model.CommandResponse {
func (me *EchoProvider) DoCommand(c *Context, args *model.CommandArgs, message string) *model.CommandResponse {
maxThreads := 100
delay := 0
@@ -75,7 +75,9 @@ func (me *EchoProvider) DoCommand(c *Context, channelId string, message string)
go func() {
defer func() { <-echoSem }()
post := &model.Post{}
post.ChannelId = channelId
post.ChannelId = args.ChannelId
post.RootId = args.RootId
post.ParentId = args.ParentId
post.Message = message
post.UserId = c.Session.UserId

View File

@@ -17,7 +17,7 @@ func TestEchoCommand(t *testing.T) {
echoTestString := "/echo test"
r1 := Client.Must(Client.Command(channel1.Id, echoTestString, false)).Data.(*model.CommandResponse)
r1 := Client.Must(Client.Command(channel1.Id, echoTestString)).Data.(*model.CommandResponse)
if r1 == nil {
t.Fatal("Echo command failed to execute")
}

View File

@@ -49,11 +49,11 @@ func (me *CollapseProvider) GetCommand(c *Context) *model.Command {
}
}
func (me *ExpandProvider) DoCommand(c *Context, channelId string, message string) *model.CommandResponse {
func (me *ExpandProvider) DoCommand(c *Context, args *model.CommandArgs, message string) *model.CommandResponse {
return setCollapsePreference(c, "false")
}
func (me *CollapseProvider) DoCommand(c *Context, channelId string, message string) *model.CommandResponse {
func (me *CollapseProvider) DoCommand(c *Context, args *model.CommandArgs, message string) *model.CommandResponse {
return setCollapsePreference(c, "true")
}

View File

@@ -15,7 +15,7 @@ func TestExpandCommand(t *testing.T) {
Client := th.BasicClient
channel := th.BasicChannel
r1 := Client.Must(Client.Command(channel.Id, "/expand", false)).Data.(*model.CommandResponse)
r1 := Client.Must(Client.Command(channel.Id, "/expand")).Data.(*model.CommandResponse)
if r1 == nil {
t.Fatal("Command failed to execute")
}
@@ -33,7 +33,7 @@ func TestCollapseCommand(t *testing.T) {
Client := th.BasicClient
channel := th.BasicChannel
r1 := Client.Must(Client.Command(channel.Id, "/collapse", false)).Data.(*model.CommandResponse)
r1 := Client.Must(Client.Command(channel.Id, "/collapse")).Data.(*model.CommandResponse)
if r1 == nil {
t.Fatal("Command failed to execute")
}

View File

@@ -35,7 +35,7 @@ func (me *InvitePeopleProvider) GetCommand(c *Context) *model.Command {
}
}
func (me *InvitePeopleProvider) DoCommand(c *Context, channelId string, message string) *model.CommandResponse {
func (me *InvitePeopleProvider) DoCommand(c *Context, args *model.CommandArgs, message string) *model.CommandResponse {
if !utils.Cfg.EmailSettings.SendEmailNotifications {
return &model.CommandResponse{ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, Text: c.T("api.command.invite_people.email_off")}
}

View File

@@ -14,17 +14,17 @@ func TestInvitePeopleCommand(t *testing.T) {
Client := th.BasicClient
channel := th.BasicChannel
r1 := Client.Must(Client.Command(channel.Id, "/invite_people test@example.com", false)).Data.(*model.CommandResponse)
r1 := Client.Must(Client.Command(channel.Id, "/invite_people test@example.com")).Data.(*model.CommandResponse)
if r1 == nil {
t.Fatal("Command failed to execute")
}
r2 := Client.Must(Client.Command(channel.Id, "/invite_people test1@example.com test2@example.com", false)).Data.(*model.CommandResponse)
r2 := Client.Must(Client.Command(channel.Id, "/invite_people test1@example.com test2@example.com")).Data.(*model.CommandResponse)
if r2 == nil {
t.Fatal("Command failed to execute")
}
r3 := Client.Must(Client.Command(channel.Id, "/invite_people", false)).Data.(*model.CommandResponse)
r3 := Client.Must(Client.Command(channel.Id, "/invite_people")).Data.(*model.CommandResponse)
if r3 == nil {
t.Fatal("Command failed to execute")
}

View File

@@ -32,7 +32,7 @@ func (me *JoinProvider) GetCommand(c *Context) *model.Command {
}
}
func (me *JoinProvider) DoCommand(c *Context, channelId string, message string) *model.CommandResponse {
func (me *JoinProvider) DoCommand(c *Context, args *model.CommandArgs, message string) *model.CommandResponse {
if result := <-Srv.Store.Channel().GetByName(c.TeamId, message); result.Err != nil {
return &model.CommandResponse{Text: c.T("api.command_join.list.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
} else {

View File

@@ -29,12 +29,12 @@ func TestJoinCommands(t *testing.T) {
channel3 := Client.Must(Client.CreateDirectChannel(user2.Id)).Data.(*model.Channel)
rs5 := Client.Must(Client.Command(channel0.Id, "/join "+channel2.Name, false)).Data.(*model.CommandResponse)
rs5 := Client.Must(Client.Command(channel0.Id, "/join "+channel2.Name)).Data.(*model.CommandResponse)
if !strings.HasSuffix(rs5.GotoLocation, "/"+team.Name+"/channels/"+channel2.Name) {
t.Fatal("failed to join channel")
}
rs6 := Client.Must(Client.Command(channel0.Id, "/join "+channel3.Name, false)).Data.(*model.CommandResponse)
rs6 := Client.Must(Client.Command(channel0.Id, "/join "+channel3.Name)).Data.(*model.CommandResponse)
if strings.HasSuffix(rs6.GotoLocation, "/"+team.Name+"/channels/"+channel3.Name) {
t.Fatal("should not have joined direct message channel")
}

View File

@@ -84,7 +84,8 @@ func (me *LoadTestProvider) GetCommand(c *Context) *model.Command {
}
}
func (me *LoadTestProvider) DoCommand(c *Context, channelId string, message string) *model.CommandResponse {
func (me *LoadTestProvider) DoCommand(c *Context, args *model.CommandArgs, message string) *model.CommandResponse {
channelId := args.ChannelId
//This command is only available when EnableTesting is true
if !utils.Cfg.ServiceSettings.EnableTesting {

View File

@@ -25,7 +25,7 @@ func TestLoadTestHelpCommands(t *testing.T) {
utils.Cfg.ServiceSettings.EnableTesting = true
rs := Client.Must(Client.Command(channel.Id, "/loadtest help", false)).Data.(*model.CommandResponse)
rs := Client.Must(Client.Command(channel.Id, "/loadtest help")).Data.(*model.CommandResponse)
if !strings.Contains(rs.Text, "Mattermost load testing commands to help") {
t.Fatal(rs.Text)
}
@@ -46,7 +46,7 @@ func TestLoadTestSetupCommands(t *testing.T) {
utils.Cfg.ServiceSettings.EnableTesting = true
rs := Client.Must(Client.Command(channel.Id, "/loadtest setup fuzz 1 1 1", false)).Data.(*model.CommandResponse)
rs := Client.Must(Client.Command(channel.Id, "/loadtest setup fuzz 1 1 1")).Data.(*model.CommandResponse)
if rs.Text != "Created enviroment" {
t.Fatal(rs.Text)
}
@@ -67,7 +67,7 @@ func TestLoadTestUsersCommands(t *testing.T) {
utils.Cfg.ServiceSettings.EnableTesting = true
rs := Client.Must(Client.Command(channel.Id, "/loadtest users fuzz 1 2", false)).Data.(*model.CommandResponse)
rs := Client.Must(Client.Command(channel.Id, "/loadtest users fuzz 1 2")).Data.(*model.CommandResponse)
if rs.Text != "Added users" {
t.Fatal(rs.Text)
}
@@ -88,7 +88,7 @@ func TestLoadTestChannelsCommands(t *testing.T) {
utils.Cfg.ServiceSettings.EnableTesting = true
rs := Client.Must(Client.Command(channel.Id, "/loadtest channels fuzz 1 2", false)).Data.(*model.CommandResponse)
rs := Client.Must(Client.Command(channel.Id, "/loadtest channels fuzz 1 2")).Data.(*model.CommandResponse)
if rs.Text != "Added channels" {
t.Fatal(rs.Text)
}
@@ -109,7 +109,7 @@ func TestLoadTestPostsCommands(t *testing.T) {
utils.Cfg.ServiceSettings.EnableTesting = true
rs := Client.Must(Client.Command(channel.Id, "/loadtest posts fuzz 2 3 2", false)).Data.(*model.CommandResponse)
rs := Client.Must(Client.Command(channel.Id, "/loadtest posts fuzz 2 3 2")).Data.(*model.CommandResponse)
if rs.Text != "Added posts" {
t.Fatal(rs.Text)
}

View File

@@ -32,7 +32,7 @@ func (me *LogoutProvider) GetCommand(c *Context) *model.Command {
}
}
func (me *LogoutProvider) DoCommand(c *Context, channelId string, message string) *model.CommandResponse {
func (me *LogoutProvider) DoCommand(c *Context, args *model.CommandArgs, message string) *model.CommandResponse {
FAIL := &model.CommandResponse{ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, Text: c.T("api.command_logout.fail_message")}
SUCCESS := &model.CommandResponse{GotoLocation: "/", ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, Text: c.T("api.command_logout.success_message")}

View File

@@ -10,5 +10,5 @@ import (
func TestLogoutTestCommand(t *testing.T) {
th := Setup().InitBasic()
th.BasicClient.Must(th.BasicClient.Command(th.BasicChannel.Id, "/logout", false))
th.BasicClient.Must(th.BasicClient.Command(th.BasicChannel.Id, "/logout"))
}

View File

@@ -32,6 +32,6 @@ func (me *MeProvider) GetCommand(c *Context) *model.Command {
}
}
func (me *MeProvider) DoCommand(c *Context, channelId string, message string) *model.CommandResponse {
func (me *MeProvider) DoCommand(c *Context, args *model.CommandArgs, message string) *model.CommandResponse {
return &model.CommandResponse{ResponseType: model.COMMAND_RESPONSE_TYPE_IN_CHANNEL, Text: "*" + message + "*"}
}

View File

@@ -17,7 +17,7 @@ func TestMeCommand(t *testing.T) {
testString := "/me hello"
r1 := Client.Must(Client.Command(channel.Id, testString, false)).Data.(*model.CommandResponse)
r1 := Client.Must(Client.Command(channel.Id, testString)).Data.(*model.CommandResponse)
if r1 == nil {
t.Fatal("Command failed to execute")
}

View File

@@ -34,7 +34,7 @@ func (me *msgProvider) GetCommand(c *Context) *model.Command {
}
}
func (me *msgProvider) DoCommand(c *Context, channelId string, message string) *model.CommandResponse {
func (me *msgProvider) DoCommand(c *Context, args *model.CommandArgs, message string) *model.CommandResponse {
splitMessage := strings.SplitN(message, " ", 2)

View File

@@ -21,12 +21,12 @@ func TestMsgCommands(t *testing.T) {
Client.Must(Client.CreateDirectChannel(user2.Id))
Client.Must(Client.CreateDirectChannel(user3.Id))
rs1 := Client.Must(Client.Command("", "/msg "+user2.Username, false)).Data.(*model.CommandResponse)
rs1 := Client.Must(Client.Command("", "/msg "+user2.Username)).Data.(*model.CommandResponse)
if !strings.HasSuffix(rs1.GotoLocation, "/"+team.Name+"/channels/"+user1.Id+"__"+user2.Id) && !strings.HasSuffix(rs1.GotoLocation, "/"+team.Name+"/channels/"+user2.Id+"__"+user1.Id) {
t.Fatal("failed to create direct channel")
}
rs2 := Client.Must(Client.Command("", "/msg "+user3.Username+" foobar", false)).Data.(*model.CommandResponse)
rs2 := Client.Must(Client.Command("", "/msg "+user3.Username+" foobar")).Data.(*model.CommandResponse)
if !strings.HasSuffix(rs2.GotoLocation, "/"+team.Name+"/channels/"+user1.Id+"__"+user3.Id) && !strings.HasSuffix(rs2.GotoLocation, "/"+team.Name+"/channels/"+user3.Id+"__"+user1.Id) {
t.Fatal("failed to create second direct channel")
}
@@ -34,7 +34,7 @@ func TestMsgCommands(t *testing.T) {
t.Fatalf("post did not get sent to direct message")
}
rs3 := Client.Must(Client.Command("", "/msg "+user2.Username, false)).Data.(*model.CommandResponse)
rs3 := Client.Must(Client.Command("", "/msg "+user2.Username)).Data.(*model.CommandResponse)
if !strings.HasSuffix(rs3.GotoLocation, "/"+team.Name+"/channels/"+user1.Id+"__"+user2.Id) && !strings.HasSuffix(rs3.GotoLocation, "/"+team.Name+"/channels/"+user2.Id+"__"+user1.Id) {
t.Fatal("failed to go back to existing direct channel")
}

View File

@@ -31,7 +31,7 @@ func (me *OfflineProvider) GetCommand(c *Context) *model.Command {
}
}
func (me *OfflineProvider) DoCommand(c *Context, channelId string, message string) *model.CommandResponse {
func (me *OfflineProvider) DoCommand(c *Context, args *model.CommandArgs, message string) *model.CommandResponse {
rmsg := c.T("api.command_offline.success")
if len(message) > 0 {
rmsg = message + " " + rmsg

View File

@@ -31,7 +31,7 @@ func (me *OnlineProvider) GetCommand(c *Context) *model.Command {
}
}
func (me *OnlineProvider) DoCommand(c *Context, channelId string, message string) *model.CommandResponse {
func (me *OnlineProvider) DoCommand(c *Context, args *model.CommandArgs, message string) *model.CommandResponse {
rmsg := c.T("api.command_online.success")
if len(message) > 0 {
rmsg = message + " " + rmsg

View File

@@ -35,7 +35,7 @@ func (me *ShortcutsProvider) GetCommand(c *Context) *model.Command {
}
}
func (me *ShortcutsProvider) DoCommand(c *Context, channelId string, message string) *model.CommandResponse {
func (me *ShortcutsProvider) DoCommand(c *Context, args *model.CommandArgs, message string) *model.CommandResponse {
shortcutIds := [4]string{
"api.command_shortcuts.nav",
"api.command_shortcuts.files",

View File

@@ -14,12 +14,12 @@ func TestShortcutsCommand(t *testing.T) {
Client := th.BasicClient
channel := th.BasicChannel
rs := Client.Must(Client.Command(channel.Id, "/shortcuts ", false)).Data.(*model.CommandResponse)
rs := Client.Must(Client.Command(channel.Id, "/shortcuts ")).Data.(*model.CommandResponse)
if !strings.Contains(rs.Text, "CTRL") {
t.Fatal("failed to display shortcuts")
}
rs = Client.Must(Client.Command(channel.Id, "/shortcuts mac", false)).Data.(*model.CommandResponse)
rs = Client.Must(Client.Command(channel.Id, "/shortcuts mac")).Data.(*model.CommandResponse)
if !strings.Contains(rs.Text, "CMD") {
t.Fatal("failed to display Mac shortcuts")
}

View File

@@ -32,7 +32,7 @@ func (me *ShrugProvider) GetCommand(c *Context) *model.Command {
}
}
func (me *ShrugProvider) DoCommand(c *Context, channelId string, message string) *model.CommandResponse {
func (me *ShrugProvider) DoCommand(c *Context, args *model.CommandArgs, message string) *model.CommandResponse {
rmsg := `¯\\\_(ツ)\_/¯`
if len(message) > 0 {
rmsg = message + " " + rmsg

View File

@@ -17,7 +17,7 @@ func TestShrugCommand(t *testing.T) {
testString := "/shrug"
r1 := Client.Must(Client.Command(channel.Id, testString, false)).Data.(*model.CommandResponse)
r1 := Client.Must(Client.Command(channel.Id, testString)).Data.(*model.CommandResponse)
if r1 == nil {
t.Fatal("Command failed to execute")
}

View File

@@ -22,7 +22,7 @@ func commandAndTest(t *testing.T, th *TestHelper, status string) {
channel := th.BasicChannel
user := th.BasicUser
r1 := Client.Must(Client.Command(channel.Id, "/"+status, false)).Data.(*model.CommandResponse)
r1 := Client.Must(Client.Command(channel.Id, "/"+status)).Data.(*model.CommandResponse)
if r1 == nil {
t.Fatal("Command failed to execute")
}

View File

@@ -246,7 +246,7 @@ func TestTestCommand(t *testing.T) {
cmd1 = Client.Must(Client.CreateCommand(cmd1)).Data.(*model.Command)
r1 := Client.Must(Client.Command(channel1.Id, "/test", false)).Data.(*model.CommandResponse)
r1 := Client.Must(Client.Command(channel1.Id, "/test")).Data.(*model.CommandResponse)
if r1 == nil {
t.Fatal("Test command failed to execute")
}
@@ -266,7 +266,7 @@ func TestTestCommand(t *testing.T) {
cmd2 = Client.Must(Client.CreateCommand(cmd2)).Data.(*model.Command)
r2 := Client.Must(Client.Command(channel1.Id, "/test2", false)).Data.(*model.CommandResponse)
r2 := Client.Must(Client.Command(channel1.Id, "/test2")).Data.(*model.CommandResponse)
if r2 == nil {
t.Fatal("Test2 command failed to execute")
}