Merge pull request #2320 from mozilla/msg-command
PLT-2231 /msg command and tests
Этот коммит содержится в:
95
api/command_msg.go
Обычный файл
95
api/command_msg.go
Обычный файл
@@ -0,0 +1,95 @@
|
|||||||
|
// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved.
|
||||||
|
// See License.txt for license information.
|
||||||
|
|
||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/mattermost/platform/model"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type msgProvider struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
CMD_MSG = "msg"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
RegisterCommandProvider(&msgProvider{})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (me *msgProvider) GetTrigger() string {
|
||||||
|
return CMD_MSG
|
||||||
|
}
|
||||||
|
|
||||||
|
func (me *msgProvider) GetCommand(c *Context) *model.Command {
|
||||||
|
return &model.Command{
|
||||||
|
Trigger: CMD_MSG,
|
||||||
|
AutoComplete: true,
|
||||||
|
AutoCompleteDesc: c.T("api.command_msg.desc"),
|
||||||
|
AutoCompleteHint: c.T("api.command_msg.hint"),
|
||||||
|
DisplayName: c.T("api.command_msg.name"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (me *msgProvider) DoCommand(c *Context, channelId string, message string) *model.CommandResponse {
|
||||||
|
|
||||||
|
splitMessage := strings.SplitN(message, " ", 2)
|
||||||
|
|
||||||
|
parsedMessage := ""
|
||||||
|
targetUser := ""
|
||||||
|
|
||||||
|
if len(splitMessage) > 1 {
|
||||||
|
parsedMessage = strings.SplitN(message, " ", 2)[1]
|
||||||
|
}
|
||||||
|
targetUser = strings.SplitN(message, " ", 2)[0]
|
||||||
|
targetUser = strings.TrimPrefix(targetUser, "@")
|
||||||
|
|
||||||
|
if profileList := <-Srv.Store.User().GetProfiles(c.Session.TeamId); profileList.Err != nil {
|
||||||
|
c.Err = profileList.Err
|
||||||
|
return &model.CommandResponse{Text: c.T("api.command_msg.list.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
|
||||||
|
} else {
|
||||||
|
profileUsers := profileList.Data.(map[string]*model.User)
|
||||||
|
for _, userProfile := range profileUsers {
|
||||||
|
//Don't let users open DMs with themselves. It probably won't work out well.
|
||||||
|
if userProfile.Id == c.Session.UserId {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if userProfile.Username == targetUser {
|
||||||
|
targetChannelId := ""
|
||||||
|
|
||||||
|
//Find the channel based on this user
|
||||||
|
channelName := model.GetDMNameFromIds(c.Session.UserId, userProfile.Id)
|
||||||
|
|
||||||
|
if channel := <-Srv.Store.Channel().GetByName(c.Session.TeamId, channelName); channel.Err != nil {
|
||||||
|
if channel.Err.Id == "store.sql_channel.get_by_name.missing.app_error" {
|
||||||
|
if directChannel, err := CreateDirectChannel(c, userProfile.Id); err != nil {
|
||||||
|
c.Err = err
|
||||||
|
return &model.CommandResponse{Text: c.T("api.command_msg.dm_fail.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
|
||||||
|
} else {
|
||||||
|
targetChannelId = directChannel.Id
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
c.Err = channel.Err
|
||||||
|
return &model.CommandResponse{Text: c.T("api.command_msg.dm_fail.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
targetChannelId = channel.Data.(*model.Channel).Id
|
||||||
|
}
|
||||||
|
|
||||||
|
makeDirectChannelVisible(c.Session.TeamId, targetChannelId)
|
||||||
|
if len(parsedMessage) > 0 {
|
||||||
|
post := &model.Post{}
|
||||||
|
post.Message = parsedMessage
|
||||||
|
post.ChannelId = targetChannelId
|
||||||
|
if _, err := CreatePost(c, post, true); err != nil {
|
||||||
|
return &model.CommandResponse{Text: c.T("api.command_msg.fail.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return &model.CommandResponse{GotoLocation: c.GetTeamURL() + "/channels/" + channelName, Text: c.T("api.command_msg.success"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return &model.CommandResponse{Text: c.T("api.command_msg.missing.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
|
||||||
|
}
|
||||||
51
api/command_msg_test.go
Обычный файл
51
api/command_msg_test.go
Обычный файл
@@ -0,0 +1,51 @@
|
|||||||
|
// 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 TestMsgCommands(t *testing.T) {
|
||||||
|
Setup()
|
||||||
|
|
||||||
|
team := &model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "success+test@simulator.amazonses.com", Type: model.TEAM_OPEN}
|
||||||
|
team = Client.Must(Client.CreateTeam(team)).Data.(*model.Team)
|
||||||
|
|
||||||
|
user1 := &model.User{TeamId: team.Id, Email: model.NewId() + "success+test@simulator.amazonses.com", Nickname: "Corey Hulen", Username: "user1", 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")
|
||||||
|
|
||||||
|
user2 := &model.User{TeamId: team.Id, Email: model.NewId() + "success+test2@simulator.amazonses.com", Nickname: "Corey Hulen 2", Username: "user2", Password: "pwd"}
|
||||||
|
user2 = Client.Must(Client.CreateUser(user2, "")).Data.(*model.User)
|
||||||
|
store.Must(Srv.Store.User().VerifyEmail(user2.Id))
|
||||||
|
|
||||||
|
user3 := &model.User{TeamId: team.Id, Email: model.NewId() + "success+test3@simulator.amazonses.com", Nickname: "Corey Hulen 3", Username: "user3", Password: "pwd"}
|
||||||
|
user3 = Client.Must(Client.CreateUser(user3, "")).Data.(*model.User)
|
||||||
|
store.Must(Srv.Store.User().VerifyEmail(user3.Id))
|
||||||
|
|
||||||
|
rs1 := Client.Must(Client.Command("", "/msg user2", false)).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 foobar", false)).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")
|
||||||
|
}
|
||||||
|
if result := Client.Must(Client.SearchPosts("foobar")).Data.(*model.PostList); len(result.Order) == 0 {
|
||||||
|
t.Fatalf("post did not get sent to direct message")
|
||||||
|
}
|
||||||
|
|
||||||
|
rs3 := Client.Must(Client.Command("", "/msg user2", false)).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")
|
||||||
|
}
|
||||||
|
}
|
||||||
36
i18n/en.json
36
i18n/en.json
@@ -316,6 +316,38 @@
|
|||||||
"translation": "echo"
|
"translation": "echo"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
"id": "api.command_msg.desc",
|
||||||
|
"translation": "Send Direct Message to a user"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "api.command_msg.fail.app_error",
|
||||||
|
"translation": "An error occured while messaging the user."
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "api.command_msg.dm_fail.app_error",
|
||||||
|
"translation": "An error occured while creating the direct message."
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "api.command_msg.hint",
|
||||||
|
"translation": "@[username] 'message'"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "api.command_msg.list.app_error",
|
||||||
|
"translation": "An error occured while listing users."
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "api.command_msg.missing.app_error",
|
||||||
|
"translation": "We couldn't find the user"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "api.command_msg.name",
|
||||||
|
"translation": "message"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "api.command_msg.success",
|
||||||
|
"translation": "Messaged user."
|
||||||
|
},
|
||||||
|
{
|
||||||
"id": "api.command_join.desc",
|
"id": "api.command_join.desc",
|
||||||
"translation": "Join the open channel"
|
"translation": "Join the open channel"
|
||||||
},
|
},
|
||||||
@@ -2643,6 +2675,10 @@
|
|||||||
"id": "store.sql_channel.get_by_name.existing.app_error",
|
"id": "store.sql_channel.get_by_name.existing.app_error",
|
||||||
"translation": "We couldn't find the existing channel"
|
"translation": "We couldn't find the existing channel"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"id": "store.sql_channel.get_by_name.missing.app_error",
|
||||||
|
"translation": "Channel does not exist"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"id": "store.sql_channel.get_channel_counts.get.app_error",
|
"id": "store.sql_channel.get_channel_counts.get.app_error",
|
||||||
"translation": "We couldn't get the channel counts"
|
"translation": "We couldn't get the channel counts"
|
||||||
|
|||||||
@@ -4,11 +4,16 @@
|
|||||||
package store
|
package store
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"database/sql"
|
||||||
"github.com/go-gorp/gorp"
|
"github.com/go-gorp/gorp"
|
||||||
"github.com/mattermost/platform/model"
|
"github.com/mattermost/platform/model"
|
||||||
"github.com/mattermost/platform/utils"
|
"github.com/mattermost/platform/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
MISSING_CHANNEL_ERROR = "store.sql_channel.get_by_name.missing.app_error"
|
||||||
|
)
|
||||||
|
|
||||||
type SqlChannelStore struct {
|
type SqlChannelStore struct {
|
||||||
*SqlStore
|
*SqlStore
|
||||||
}
|
}
|
||||||
@@ -437,7 +442,11 @@ func (s SqlChannelStore) GetByName(teamId string, name string) StoreChannel {
|
|||||||
channel := model.Channel{}
|
channel := model.Channel{}
|
||||||
|
|
||||||
if err := s.GetReplica().SelectOne(&channel, "SELECT * FROM Channels WHERE TeamId = :TeamId AND Name= :Name AND DeleteAt = 0", map[string]interface{}{"TeamId": teamId, "Name": name}); err != nil {
|
if err := s.GetReplica().SelectOne(&channel, "SELECT * FROM Channels WHERE TeamId = :TeamId AND Name= :Name AND DeleteAt = 0", map[string]interface{}{"TeamId": teamId, "Name": name}); err != nil {
|
||||||
result.Err = model.NewLocAppError("SqlChannelStore.GetByName", "store.sql_channel.get_by_name.existing.app_error", nil, "teamId="+teamId+", "+"name="+name+", "+err.Error())
|
if err == sql.ErrNoRows {
|
||||||
|
result.Err = model.NewLocAppError("SqlChannelStore.GetByName", MISSING_CHANNEL_ERROR, nil, "teamId="+teamId+", "+"name="+name+", "+err.Error())
|
||||||
|
} else {
|
||||||
|
result.Err = model.NewLocAppError("SqlChannelStore.GetByName", "store.sql_channel.get_by_name.existing.app_error", nil, "teamId="+teamId+", "+"name="+name+", "+err.Error())
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
result.Data = &channel
|
result.Data = &channel
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -100,13 +100,16 @@ export default class AtMentionProvider {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// add dummy users to represent the @all and @channel special mentions
|
//Don't imply that @all and @channel can be direct messaged
|
||||||
if ('all'.startsWith(usernamePrefix)) {
|
if (!pretext.startsWith('/msg')) {
|
||||||
filtered.push({username: 'all'});
|
// add dummy users to represent the @all and @channel special mentions
|
||||||
}
|
if ('all'.startsWith(usernamePrefix)) {
|
||||||
|
filtered.push({username: 'all'});
|
||||||
|
}
|
||||||
|
|
||||||
if ('channel'.startsWith(usernamePrefix)) {
|
if ('channel'.startsWith(usernamePrefix)) {
|
||||||
filtered.push({username: 'channel'});
|
filtered.push({username: 'channel'});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
filtered = filtered.sort((a, b) => a.username.localeCompare(b.username));
|
filtered = filtered.sort((a, b) => a.username.localeCompare(b.username));
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user