mirror of
https://github.com/mattermost/mattermost.git
synced 2025-02-25 18:55:24 -06:00
adding different commands
This commit is contained in:
272
api/command.go
272
api/command.go
@@ -4,12 +4,8 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
//"io"
|
||||
"net/http"
|
||||
// "path"
|
||||
// "strconv"
|
||||
"strings"
|
||||
// "time"
|
||||
|
||||
l4g "code.google.com/p/log4go"
|
||||
"github.com/gorilla/mux"
|
||||
@@ -122,6 +118,13 @@ func executeCommand(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
if _, err := CreatePost(c, post, true); err != nil {
|
||||
c.Err = model.NewAppError("command", "An error while saving the command response to the channel", "")
|
||||
}
|
||||
} else if response.ResponseType == model.COMMAND_RESPONSE_TYPE_EPHEMERAL {
|
||||
post := &model.Post{}
|
||||
post.ChannelId = channelId
|
||||
post.Message = "TODO_EPHEMERAL: " + response.Text
|
||||
if _, err := CreatePost(c, post, true); err != nil {
|
||||
c.Err = model.NewAppError("command", "An error while saving the command response to the channel", "")
|
||||
}
|
||||
}
|
||||
|
||||
w.Write([]byte(response.ToJson()))
|
||||
@@ -285,267 +288,6 @@ func deleteCommand(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte(model.MapToJson(props)))
|
||||
}
|
||||
|
||||
// func command(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// props := model.MapFromJson(r.Body)
|
||||
|
||||
// command := &model.Command{
|
||||
// Command: strings.TrimSpace(props["command"]),
|
||||
// ChannelId: strings.TrimSpace(props["channelId"]),
|
||||
// Suggest: props["suggest"] == "true",
|
||||
// Suggestions: make([]*model.SuggestCommand, 0, 128),
|
||||
// }
|
||||
|
||||
// checkCommand(c, command)
|
||||
// if c.Err != nil {
|
||||
// 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()))
|
||||
// }
|
||||
// }
|
||||
|
||||
// func checkCommand(c *Context, command *model.Command) bool {
|
||||
|
||||
// if len(command.Command) == 0 || strings.Index(command.Command, "/") != 0 {
|
||||
// c.Err = model.NewAppError("checkCommand", "Command must start with /", "")
|
||||
// return false
|
||||
// }
|
||||
|
||||
// if len(command.ChannelId) > 0 {
|
||||
// cchan := Srv.Store.Channel().CheckPermissionsTo(c.Session.TeamId, command.ChannelId, c.Session.UserId)
|
||||
|
||||
// if !c.HasPermissionsToChannel(cchan, "checkCommand") {
|
||||
// return true
|
||||
// }
|
||||
// }
|
||||
|
||||
// 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 {
|
||||
// return true
|
||||
// }
|
||||
// }
|
||||
|
||||
// return false
|
||||
// }
|
||||
|
||||
// func logoutCommand(c *Context, command *model.Command) bool {
|
||||
|
||||
// cmd := cmds["logoutCommand"]
|
||||
|
||||
// if strings.Index(command.Command, cmd) == 0 {
|
||||
// command.AddSuggestion(&model.SuggestCommand{Suggestion: cmd, Description: "Logout"})
|
||||
|
||||
// if !command.Suggest {
|
||||
// command.GotoLocation = "/logout"
|
||||
// command.Response = model.RESP_EXECUTED
|
||||
// return true
|
||||
// }
|
||||
|
||||
// } else if strings.Index(cmd, command.Command) == 0 {
|
||||
// command.AddSuggestion(&model.SuggestCommand{Suggestion: cmd, Description: "Logout"})
|
||||
// }
|
||||
|
||||
// return false
|
||||
// }
|
||||
|
||||
// func echoCommand(c *Context, command *model.Command) bool {
|
||||
// cmd := cmds["echoCommand"]
|
||||
// maxThreads := 100
|
||||
|
||||
// if !command.Suggest && strings.Index(command.Command, cmd) == 0 {
|
||||
// parameters := strings.SplitN(command.Command, " ", 2)
|
||||
// if len(parameters) != 2 || len(parameters[1]) == 0 {
|
||||
// return false
|
||||
// }
|
||||
// message := strings.Trim(parameters[1], " ")
|
||||
// delay := 0
|
||||
// if endMsg := strings.LastIndex(message, "\""); string(message[0]) == "\"" && endMsg > 1 {
|
||||
// if checkDelay, err := strconv.Atoi(strings.Trim(message[endMsg:], " \"")); err == nil {
|
||||
// delay = checkDelay
|
||||
// }
|
||||
// message = message[1:endMsg]
|
||||
// } else if strings.Index(message, " ") > -1 {
|
||||
// delayIdx := strings.LastIndex(message, " ")
|
||||
// delayStr := strings.Trim(message[delayIdx:], " ")
|
||||
|
||||
// if checkDelay, err := strconv.Atoi(delayStr); err == nil {
|
||||
// delay = checkDelay
|
||||
// message = message[:delayIdx]
|
||||
// }
|
||||
// }
|
||||
|
||||
// if delay > 10000 {
|
||||
// c.Err = model.NewAppError("echoCommand", "Delays must be under 10000 seconds", "")
|
||||
// return false
|
||||
// }
|
||||
|
||||
// if echoSem == nil {
|
||||
// // We want one additional thread allowed so we never reach channel lockup
|
||||
// echoSem = make(chan bool, maxThreads+1)
|
||||
// }
|
||||
|
||||
// if len(echoSem) >= maxThreads {
|
||||
// c.Err = model.NewAppError("echoCommand", "High volume of echo request, cannot process request", "")
|
||||
// return false
|
||||
// }
|
||||
|
||||
// echoSem <- true
|
||||
// go func() {
|
||||
// defer func() { <-echoSem }()
|
||||
// post := &model.Post{}
|
||||
// post.ChannelId = command.ChannelId
|
||||
// post.Message = message
|
||||
|
||||
// time.Sleep(time.Duration(delay) * time.Second)
|
||||
|
||||
// if _, err := CreatePost(c, post, true); err != nil {
|
||||
// l4g.Error("Unable to create /echo post, err=%v", err)
|
||||
// }
|
||||
// }()
|
||||
|
||||
// command.Response = model.RESP_EXECUTED
|
||||
// return true
|
||||
|
||||
// } else if strings.Index(cmd, command.Command) == 0 {
|
||||
// command.AddSuggestion(&model.SuggestCommand{Suggestion: cmd, Description: "Echo back text from your account, /echo \"message\" [delay in seconds]"})
|
||||
// }
|
||||
|
||||
// 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 := cmds["shrugCommand"]
|
||||
|
||||
// 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 /shrug 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: "Adds ¯\\_(ツ)_/¯ to your message, /shrug [message]"})
|
||||
// }
|
||||
|
||||
// return false
|
||||
// }
|
||||
|
||||
// func joinCommand(c *Context, command *model.Command) bool {
|
||||
|
||||
// // looks for "/join channel-name"
|
||||
// cmd := cmds["joinCommand"]
|
||||
|
||||
// if strings.Index(command.Command, cmd) == 0 {
|
||||
|
||||
// parts := strings.Split(command.Command, " ")
|
||||
|
||||
// startsWith := ""
|
||||
|
||||
// if len(parts) == 2 {
|
||||
// startsWith = parts[1]
|
||||
// }
|
||||
|
||||
// if result := <-Srv.Store.Channel().GetMoreChannels(c.Session.TeamId, c.Session.UserId); result.Err != nil {
|
||||
// c.Err = result.Err
|
||||
// return false
|
||||
// } else {
|
||||
// channels := result.Data.(*model.ChannelList)
|
||||
|
||||
// for _, v := range channels.Channels {
|
||||
|
||||
// if v.Name == startsWith && !command.Suggest {
|
||||
|
||||
// if v.Type == model.CHANNEL_DIRECT {
|
||||
// return false
|
||||
// }
|
||||
|
||||
// JoinChannel(c, v.Id, "")
|
||||
|
||||
// if c.Err != nil {
|
||||
// return false
|
||||
// }
|
||||
|
||||
// command.GotoLocation = c.GetTeamURL() + "/channels/" + v.Name
|
||||
// command.Response = model.RESP_EXECUTED
|
||||
// return true
|
||||
// }
|
||||
|
||||
// if len(startsWith) == 0 || strings.Index(v.Name, startsWith) == 0 {
|
||||
// command.AddSuggestion(&model.SuggestCommand{Suggestion: cmd + " " + v.Name, Description: "Join the open channel"})
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// } else if strings.Index(cmd, command.Command) == 0 {
|
||||
// command.AddSuggestion(&model.SuggestCommand{Suggestion: cmd, Description: "Join an open channel"})
|
||||
// }
|
||||
|
||||
// return false
|
||||
// }
|
||||
|
||||
// func loadTestCommand(c *Context, command *model.Command) bool {
|
||||
// cmd := cmds["loadTestCommand"]
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
|
||||
// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved.
|
||||
// See License.txt for license information.
|
||||
|
||||
package api
|
||||
|
||||
54
api/command_join.go
Normal file
54
api/command_join.go
Normal file
@@ -0,0 +1,54 @@
|
||||
// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved.
|
||||
// See License.txt for license information.
|
||||
|
||||
package api
|
||||
|
||||
import (
|
||||
"github.com/mattermost/platform/model"
|
||||
)
|
||||
|
||||
type JoinProvider struct {
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterCommandProvider(&JoinProvider{})
|
||||
}
|
||||
|
||||
func (me *JoinProvider) GetCommand() *model.Command {
|
||||
return &model.Command{
|
||||
Trigger: "join",
|
||||
AutoComplete: true,
|
||||
AutoCompleteDesc: "Join the open channel",
|
||||
AutoCompleteHint: "[channel-name]",
|
||||
DisplayName: "join",
|
||||
}
|
||||
}
|
||||
|
||||
func (me *JoinProvider) DoCommand(c *Context, channelId string, message string) *model.CommandResponse {
|
||||
if result := <-Srv.Store.Channel().GetMoreChannels(c.Session.TeamId, c.Session.UserId); result.Err != nil {
|
||||
return &model.CommandResponse{Text: "An error occured while listing channels.", ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
|
||||
} else {
|
||||
channels := result.Data.(*model.ChannelList)
|
||||
|
||||
for _, v := range channels.Channels {
|
||||
|
||||
if v.Name == message {
|
||||
|
||||
if v.Type == model.CHANNEL_DIRECT {
|
||||
return &model.CommandResponse{Text: "An error occured while joining the channel.", ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
|
||||
}
|
||||
|
||||
JoinChannel(c, v.Id, "")
|
||||
|
||||
if c.Err != nil {
|
||||
c.Err = nil
|
||||
return &model.CommandResponse{Text: "An error occured while joining the channel.", ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
|
||||
}
|
||||
|
||||
return &model.CommandResponse{GotoLocation: c.GetTeamURL() + "/channels/" + v.Name, Text: "Joined channel.", ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return &model.CommandResponse{ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, Text: "We couldn't find the channel"}
|
||||
}
|
||||
71
api/command_join_test.go
Normal file
71
api/command_join_test.go
Normal file
@@ -0,0 +1,71 @@
|
||||
// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved.
|
||||
// See License.txt for license information.
|
||||
|
||||
package api
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/mattermost/platform/model"
|
||||
"github.com/mattermost/platform/store"
|
||||
)
|
||||
|
||||
func TestJoinCommands(t *testing.T) {
|
||||
Setup()
|
||||
|
||||
team := &model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "test@nowhere.com", Type: model.TEAM_OPEN}
|
||||
team = Client.Must(Client.CreateTeam(team)).Data.(*model.Team)
|
||||
|
||||
user1 := &model.User{TeamId: team.Id, Email: model.NewId() + "corey+test@test.com", Nickname: "Corey Hulen", Password: "pwd"}
|
||||
user1 = Client.Must(Client.CreateUser(user1, "")).Data.(*model.User)
|
||||
store.Must(Srv.Store.User().VerifyEmail(user1.Id))
|
||||
|
||||
Client.LoginByEmail(team.Name, user1.Email, "pwd")
|
||||
|
||||
channel0 := &model.Channel{DisplayName: "00", Name: "00" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
|
||||
channel0 = Client.Must(Client.CreateChannel(channel0)).Data.(*model.Channel)
|
||||
|
||||
channel1 := &model.Channel{DisplayName: "AA", Name: "aa" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
|
||||
channel1 = Client.Must(Client.CreateChannel(channel1)).Data.(*model.Channel)
|
||||
Client.Must(Client.LeaveChannel(channel1.Id))
|
||||
|
||||
channel2 := &model.Channel{DisplayName: "BB", Name: "bb" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
|
||||
channel2 = Client.Must(Client.CreateChannel(channel2)).Data.(*model.Channel)
|
||||
Client.Must(Client.LeaveChannel(channel2.Id))
|
||||
|
||||
user2 := &model.User{TeamId: team.Id, Email: model.NewId() + "corey+test@test.com", Nickname: "Corey Hulen", Password: "pwd"}
|
||||
user2 = Client.Must(Client.CreateUser(user2, "")).Data.(*model.User)
|
||||
store.Must(Srv.Store.User().VerifyEmail(user1.Id))
|
||||
|
||||
data := make(map[string]string)
|
||||
data["user_id"] = user2.Id
|
||||
channel3 := Client.Must(Client.CreateDirectChannel(data)).Data.(*model.Channel)
|
||||
|
||||
rs5 := Client.Must(Client.Command(channel0.Id, "/join "+channel2.Name, false)).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)
|
||||
if strings.HasSuffix(rs6.GotoLocation, "/"+team.Name+"/channels/"+channel3.Name) {
|
||||
t.Fatal("should not have joined direct message channel")
|
||||
}
|
||||
|
||||
c1 := Client.Must(Client.GetChannels("")).Data.(*model.ChannelList)
|
||||
|
||||
if len(c1.Channels) != 5 { // 4 because of town-square, off-topic and direct
|
||||
t.Fatal("didn't join channel")
|
||||
}
|
||||
|
||||
found := false
|
||||
for _, c := range c1.Channels {
|
||||
if c.Name == channel2.Name {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Fatal("didn't join channel")
|
||||
}
|
||||
}
|
||||
29
api/command_logout.go
Normal file
29
api/command_logout.go
Normal file
@@ -0,0 +1,29 @@
|
||||
// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved.
|
||||
// See License.txt for license information.
|
||||
|
||||
package api
|
||||
|
||||
import (
|
||||
"github.com/mattermost/platform/model"
|
||||
)
|
||||
|
||||
type LogoutProvider struct {
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterCommandProvider(&LogoutProvider{})
|
||||
}
|
||||
|
||||
func (me *LogoutProvider) GetCommand() *model.Command {
|
||||
return &model.Command{
|
||||
Trigger: "logout",
|
||||
AutoComplete: true,
|
||||
AutoCompleteDesc: "Logout",
|
||||
AutoCompleteHint: "",
|
||||
DisplayName: "logout",
|
||||
}
|
||||
}
|
||||
|
||||
func (me *LogoutProvider) DoCommand(c *Context, channelId string, message string) *model.CommandResponse {
|
||||
return &model.CommandResponse{GotoLocation: "/logout", ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, Text: "Logging out..."}
|
||||
}
|
||||
32
api/command_logout_test.go
Normal file
32
api/command_logout_test.go
Normal file
@@ -0,0 +1,32 @@
|
||||
// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved.
|
||||
// See License.txt for license information.
|
||||
|
||||
package api
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/mattermost/platform/model"
|
||||
"github.com/mattermost/platform/store"
|
||||
)
|
||||
|
||||
func TestLogoutCommand(t *testing.T) {
|
||||
Setup()
|
||||
|
||||
team := &model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "test@nowhere.com", Type: model.TEAM_OPEN}
|
||||
team = Client.Must(Client.CreateTeam(team)).Data.(*model.Team)
|
||||
|
||||
user1 := &model.User{TeamId: team.Id, Email: model.NewId() + "corey+test@test.com", Nickname: "Corey Hulen", Password: "pwd"}
|
||||
user1 = Client.Must(Client.CreateUser(user1, "")).Data.(*model.User)
|
||||
store.Must(Srv.Store.User().VerifyEmail(user1.Id))
|
||||
|
||||
Client.LoginByEmail(team.Name, user1.Email, "pwd")
|
||||
|
||||
channel1 := &model.Channel{DisplayName: "AA", Name: "aa" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
|
||||
channel1 = Client.Must(Client.CreateChannel(channel1)).Data.(*model.Channel)
|
||||
|
||||
rs1 := Client.Must(Client.Command(channel1.Id, "/logout", false)).Data.(*model.CommandResponse)
|
||||
if rs1.GotoLocation != "/logout" {
|
||||
t.Fatal("failed to logout")
|
||||
}
|
||||
}
|
||||
29
api/command_me.go
Normal file
29
api/command_me.go
Normal file
@@ -0,0 +1,29 @@
|
||||
// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved.
|
||||
// See License.txt for license information.
|
||||
|
||||
package api
|
||||
|
||||
import (
|
||||
"github.com/mattermost/platform/model"
|
||||
)
|
||||
|
||||
type MeProvider struct {
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterCommandProvider(&MeProvider{})
|
||||
}
|
||||
|
||||
func (me *MeProvider) GetCommand() *model.Command {
|
||||
return &model.Command{
|
||||
Trigger: "me",
|
||||
AutoComplete: true,
|
||||
AutoCompleteDesc: "Do an action",
|
||||
AutoCompleteHint: "[message]",
|
||||
DisplayName: "me",
|
||||
}
|
||||
}
|
||||
|
||||
func (me *MeProvider) DoCommand(c *Context, channelId string, message string) *model.CommandResponse {
|
||||
return &model.CommandResponse{ResponseType: model.COMMAND_RESPONSE_TYPE_IN_CHANNEL, Text: "*" + message + "*"}
|
||||
}
|
||||
47
api/command_me_test.go
Normal file
47
api/command_me_test.go
Normal file
@@ -0,0 +1,47 @@
|
||||
// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved.
|
||||
// See License.txt for license information.
|
||||
|
||||
package api
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/mattermost/platform/model"
|
||||
"github.com/mattermost/platform/store"
|
||||
)
|
||||
|
||||
func TestMeCommand(t *testing.T) {
|
||||
Setup()
|
||||
|
||||
team := &model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "test@nowhere.com", Type: model.TEAM_OPEN}
|
||||
team = Client.Must(Client.CreateTeam(team)).Data.(*model.Team)
|
||||
|
||||
user1 := &model.User{TeamId: team.Id, Email: model.NewId() + "corey+test@test.com", Nickname: "Corey Hulen", Password: "pwd"}
|
||||
user1 = Client.Must(Client.CreateUser(user1, "")).Data.(*model.User)
|
||||
store.Must(Srv.Store.User().VerifyEmail(user1.Id))
|
||||
|
||||
Client.LoginByEmail(team.Name, user1.Email, "pwd")
|
||||
|
||||
channel1 := &model.Channel{DisplayName: "AA", Name: "aa" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
|
||||
channel1 = Client.Must(Client.CreateChannel(channel1)).Data.(*model.Channel)
|
||||
|
||||
testString := "/me hello"
|
||||
|
||||
r1 := Client.Must(Client.Command(channel1.Id, testString, false)).Data.(*model.CommandResponse)
|
||||
if r1 == nil {
|
||||
t.Fatal("Command failed to execute")
|
||||
}
|
||||
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
|
||||
p1 := Client.Must(Client.GetPosts(channel1.Id, 0, 2, "")).Data.(*model.PostList)
|
||||
if len(p1.Order) != 1 {
|
||||
t.Fatal("Command failed to send")
|
||||
} else {
|
||||
if p1.Posts[p1.Order[0]].Message != `*hello*` {
|
||||
t.Log(p1.Posts[p1.Order[0]].Message)
|
||||
t.Fatal("invalid shrug reponse")
|
||||
}
|
||||
}
|
||||
}
|
||||
29
api/command_shrug.go
Normal file
29
api/command_shrug.go
Normal file
@@ -0,0 +1,29 @@
|
||||
// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved.
|
||||
// See License.txt for license information.
|
||||
|
||||
package api
|
||||
|
||||
import (
|
||||
"github.com/mattermost/platform/model"
|
||||
)
|
||||
|
||||
type ShrugProvider struct {
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterCommandProvider(&ShrugProvider{})
|
||||
}
|
||||
|
||||
func (me *ShrugProvider) GetCommand() *model.Command {
|
||||
return &model.Command{
|
||||
Trigger: "shrug",
|
||||
AutoComplete: true,
|
||||
AutoCompleteDesc: `Adds ¯\_(ツ)_/¯ to your message`,
|
||||
AutoCompleteHint: "[message]",
|
||||
DisplayName: "shrug",
|
||||
}
|
||||
}
|
||||
|
||||
func (me *ShrugProvider) DoCommand(c *Context, channelId string, message string) *model.CommandResponse {
|
||||
return &model.CommandResponse{ResponseType: model.COMMAND_RESPONSE_TYPE_IN_CHANNEL, Text: `¯\_(ツ)_/¯`}
|
||||
}
|
||||
47
api/command_shrug_test.go
Normal file
47
api/command_shrug_test.go
Normal file
@@ -0,0 +1,47 @@
|
||||
// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved.
|
||||
// See License.txt for license information.
|
||||
|
||||
package api
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/mattermost/platform/model"
|
||||
"github.com/mattermost/platform/store"
|
||||
)
|
||||
|
||||
func TestShrugCommand(t *testing.T) {
|
||||
Setup()
|
||||
|
||||
team := &model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "test@nowhere.com", Type: model.TEAM_OPEN}
|
||||
team = Client.Must(Client.CreateTeam(team)).Data.(*model.Team)
|
||||
|
||||
user1 := &model.User{TeamId: team.Id, Email: model.NewId() + "corey+test@test.com", Nickname: "Corey Hulen", Password: "pwd"}
|
||||
user1 = Client.Must(Client.CreateUser(user1, "")).Data.(*model.User)
|
||||
store.Must(Srv.Store.User().VerifyEmail(user1.Id))
|
||||
|
||||
Client.LoginByEmail(team.Name, user1.Email, "pwd")
|
||||
|
||||
channel1 := &model.Channel{DisplayName: "AA", Name: "aa" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
|
||||
channel1 = Client.Must(Client.CreateChannel(channel1)).Data.(*model.Channel)
|
||||
|
||||
testString := "/shrug"
|
||||
|
||||
r1 := Client.Must(Client.Command(channel1.Id, testString, false)).Data.(*model.CommandResponse)
|
||||
if r1 == nil {
|
||||
t.Fatal("Command failed to execute")
|
||||
}
|
||||
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
|
||||
p1 := Client.Must(Client.GetPosts(channel1.Id, 0, 2, "")).Data.(*model.PostList)
|
||||
if len(p1.Order) != 1 {
|
||||
t.Fatal("Command failed to send")
|
||||
} else {
|
||||
if p1.Posts[p1.Order[0]].Message != `¯\_(ツ)_/¯` {
|
||||
t.Log(p1.Posts[p1.Order[0]].Message)
|
||||
t.Fatal("invalid shrug reponse")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -198,179 +198,6 @@ func TestDeleteCommand(t *testing.T) {
|
||||
*utils.Cfg.ServiceSettings.EnableCommands = false
|
||||
}
|
||||
|
||||
// func TestSuggestRootCommands(t *testing.T) {
|
||||
// Setup()
|
||||
|
||||
// team := &model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "test@nowhere.com", Type: model.TEAM_OPEN}
|
||||
// team = Client.Must(Client.CreateTeam(team)).Data.(*model.Team)
|
||||
|
||||
// user1 := &model.User{TeamId: team.Id, Email: model.NewId() + "corey+test@test.com", Nickname: "Corey Hulen", Password: "pwd"}
|
||||
// user1 = Client.Must(Client.CreateUser(user1, "")).Data.(*model.User)
|
||||
// store.Must(Srv.Store.User().VerifyEmail(user1.Id))
|
||||
|
||||
// Client.LoginByEmail(team.Name, user1.Email, "pwd")
|
||||
|
||||
// if _, err := Client.Command("", "", true); err == nil {
|
||||
// t.Fatal("Should fail")
|
||||
// }
|
||||
|
||||
// rs1 := Client.Must(Client.Command("", "/", true)).Data.(*model.Command)
|
||||
|
||||
// hasLogout := false
|
||||
// for _, v := range rs1.Suggestions {
|
||||
// if v.Suggestion == "/logout" {
|
||||
// hasLogout = true
|
||||
// }
|
||||
// }
|
||||
|
||||
// if !hasLogout {
|
||||
// t.Log(rs1.Suggestions)
|
||||
// t.Fatal("should have logout cmd")
|
||||
// }
|
||||
|
||||
// rs2 := Client.Must(Client.Command("", "/log", true)).Data.(*model.Command)
|
||||
|
||||
// if rs2.Suggestions[0].Suggestion != "/logout" {
|
||||
// t.Fatal("should have logout cmd")
|
||||
// }
|
||||
|
||||
// rs3 := Client.Must(Client.Command("", "/joi", true)).Data.(*model.Command)
|
||||
|
||||
// if rs3.Suggestions[0].Suggestion != "/join" {
|
||||
// t.Fatal("should have join cmd")
|
||||
// }
|
||||
|
||||
// rs4 := Client.Must(Client.Command("", "/ech", true)).Data.(*model.Command)
|
||||
|
||||
// if rs4.Suggestions[0].Suggestion != "/echo" {
|
||||
// t.Fatal("should have echo cmd")
|
||||
// }
|
||||
// }
|
||||
|
||||
// func TestLogoutCommands(t *testing.T) {
|
||||
// Setup()
|
||||
|
||||
// team := &model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "test@nowhere.com", Type: model.TEAM_OPEN}
|
||||
// team = Client.Must(Client.CreateTeam(team)).Data.(*model.Team)
|
||||
|
||||
// user1 := &model.User{TeamId: team.Id, Email: model.NewId() + "corey+test@test.com", Nickname: "Corey Hulen", Password: "pwd"}
|
||||
// user1 = Client.Must(Client.CreateUser(user1, "")).Data.(*model.User)
|
||||
// store.Must(Srv.Store.User().VerifyEmail(user1.Id))
|
||||
|
||||
// Client.LoginByEmail(team.Name, user1.Email, "pwd")
|
||||
|
||||
// rs1 := Client.Must(Client.Command("", "/logout", false)).Data.(*model.Command)
|
||||
// if rs1.GotoLocation != "/logout" {
|
||||
// t.Fatal("failed to logout")
|
||||
// }
|
||||
// }
|
||||
|
||||
// func TestJoinCommands(t *testing.T) {
|
||||
// Setup()
|
||||
|
||||
// team := &model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "test@nowhere.com", Type: model.TEAM_OPEN}
|
||||
// team = Client.Must(Client.CreateTeam(team)).Data.(*model.Team)
|
||||
|
||||
// user1 := &model.User{TeamId: team.Id, Email: model.NewId() + "corey+test@test.com", Nickname: "Corey Hulen", Password: "pwd"}
|
||||
// user1 = Client.Must(Client.CreateUser(user1, "")).Data.(*model.User)
|
||||
// store.Must(Srv.Store.User().VerifyEmail(user1.Id))
|
||||
|
||||
// Client.LoginByEmail(team.Name, user1.Email, "pwd")
|
||||
|
||||
// channel1 := &model.Channel{DisplayName: "AA", Name: "aa" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
|
||||
// channel1 = Client.Must(Client.CreateChannel(channel1)).Data.(*model.Channel)
|
||||
// Client.Must(Client.LeaveChannel(channel1.Id))
|
||||
|
||||
// channel2 := &model.Channel{DisplayName: "BB", Name: "bb" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
|
||||
// channel2 = Client.Must(Client.CreateChannel(channel2)).Data.(*model.Channel)
|
||||
// Client.Must(Client.LeaveChannel(channel2.Id))
|
||||
|
||||
// user2 := &model.User{TeamId: team.Id, Email: model.NewId() + "corey+test@test.com", Nickname: "Corey Hulen", Password: "pwd"}
|
||||
// user2 = Client.Must(Client.CreateUser(user2, "")).Data.(*model.User)
|
||||
// store.Must(Srv.Store.User().VerifyEmail(user1.Id))
|
||||
|
||||
// data := make(map[string]string)
|
||||
// data["user_id"] = user2.Id
|
||||
// channel3 := Client.Must(Client.CreateDirectChannel(data)).Data.(*model.Channel)
|
||||
|
||||
// rs1 := Client.Must(Client.Command("", "/join aa", true)).Data.(*model.Command)
|
||||
// if rs1.Suggestions[0].Suggestion != "/join "+channel1.Name {
|
||||
// t.Fatal("should have join cmd")
|
||||
// }
|
||||
|
||||
// rs2 := Client.Must(Client.Command("", "/join bb", true)).Data.(*model.Command)
|
||||
// if rs2.Suggestions[0].Suggestion != "/join "+channel2.Name {
|
||||
// t.Fatal("should have join cmd")
|
||||
// }
|
||||
|
||||
// rs3 := Client.Must(Client.Command("", "/join", true)).Data.(*model.Command)
|
||||
// if len(rs3.Suggestions) != 2 {
|
||||
// t.Fatal("should have 2 join cmd")
|
||||
// }
|
||||
|
||||
// rs4 := Client.Must(Client.Command("", "/join ", true)).Data.(*model.Command)
|
||||
// if len(rs4.Suggestions) != 2 {
|
||||
// t.Fatal("should have 2 join cmd")
|
||||
// }
|
||||
|
||||
// rs5 := Client.Must(Client.Command("", "/join "+channel2.Name, false)).Data.(*model.Command)
|
||||
// if !strings.HasSuffix(rs5.GotoLocation, "/"+team.Name+"/channels/"+channel2.Name) {
|
||||
// t.Fatal("failed to join channel")
|
||||
// }
|
||||
|
||||
// rs6 := Client.Must(Client.Command("", "/join "+channel3.Name, false)).Data.(*model.Command)
|
||||
// if strings.HasSuffix(rs6.GotoLocation, "/"+team.Name+"/channels/"+channel3.Name) {
|
||||
// t.Fatal("should not have joined direct message channel")
|
||||
// }
|
||||
|
||||
// c1 := Client.Must(Client.GetChannels("")).Data.(*model.ChannelList)
|
||||
|
||||
// if len(c1.Channels) != 4 { // 4 because of town-square, off-topic and direct
|
||||
// t.Fatal("didn't join channel")
|
||||
// }
|
||||
|
||||
// found := false
|
||||
// for _, c := range c1.Channels {
|
||||
// if c.Name == channel2.Name {
|
||||
// found = true
|
||||
// break
|
||||
// }
|
||||
// }
|
||||
// if !found {
|
||||
// t.Fatal("didn't join channel")
|
||||
// }
|
||||
// }
|
||||
|
||||
// func TestEchoCommand(t *testing.T) {
|
||||
// Setup()
|
||||
|
||||
// team := &model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "test@nowhere.com", Type: model.TEAM_OPEN}
|
||||
// team = Client.Must(Client.CreateTeam(team)).Data.(*model.Team)
|
||||
|
||||
// user1 := &model.User{TeamId: team.Id, Email: model.NewId() + "corey+test@test.com", Nickname: "Corey Hulen", Password: "pwd"}
|
||||
// user1 = Client.Must(Client.CreateUser(user1, "")).Data.(*model.User)
|
||||
// store.Must(Srv.Store.User().VerifyEmail(user1.Id))
|
||||
|
||||
// Client.LoginByEmail(team.Name, user1.Email, "pwd")
|
||||
|
||||
// channel1 := &model.Channel{DisplayName: "AA", Name: "aa" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
|
||||
// channel1 = Client.Must(Client.CreateChannel(channel1)).Data.(*model.Channel)
|
||||
|
||||
// echoTestString := "/echo test"
|
||||
|
||||
// r1 := Client.Must(Client.Command(channel1.Id, echoTestString, false)).Data.(*model.Command)
|
||||
// if r1.Response != model.RESP_EXECUTED {
|
||||
// t.Fatal("Echo command failed to execute")
|
||||
// }
|
||||
|
||||
// time.Sleep(100 * time.Millisecond)
|
||||
|
||||
// p1 := Client.Must(Client.GetPosts(channel1.Id, 0, 2, "")).Data.(*model.PostList)
|
||||
// if len(p1.Order) != 1 {
|
||||
// t.Fatal("Echo command failed to send")
|
||||
// }
|
||||
// }
|
||||
|
||||
// func TestLoadTestUrlCommand(t *testing.T) {
|
||||
// Setup()
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@ const (
|
||||
type CommandResponse struct {
|
||||
ResponseType string `json:"response_type"`
|
||||
Text string `json:"text"`
|
||||
GotoLocation string `json:"goto_location"`
|
||||
Attachments interface{} `json:"attachments"`
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user