Commit 2df818b9 authored by George Goldberg's avatar George Goldberg Committed by Carlos Tadeu Panato Junior

MM-11227: Fix group msg slash command. (#9070)

parent 74e5d8ae
......@@ -340,6 +340,30 @@ func (a *App) createGroupChannel(userIds []string, creatorId string) (*model.Cha
}
}
func (a *App) GetGroupChannel(userIds []string) (*model.Channel, *model.AppError) {
if len(userIds) > model.CHANNEL_GROUP_MAX_USERS || len(userIds) < model.CHANNEL_GROUP_MIN_USERS {
return nil, model.NewAppError("GetGroupChannel", "api.channel.create_group.bad_size.app_error", nil, "", http.StatusBadRequest)
}
var users []*model.User
if result := <-a.Srv.Store.User().GetProfileByIds(userIds, true); result.Err != nil {
return nil, result.Err
} else {
users = result.Data.([]*model.User)
}
if len(users) != len(userIds) {
return nil, model.NewAppError("GetGroupChannel", "api.channel.create_group.bad_user.app_error", nil, "user_ids="+model.ArrayToJson(userIds), http.StatusBadRequest)
}
channel, err := a.GetChannelByName(model.GetGroupNameFromUserIds(userIds), "")
if err != nil {
return nil, err
}
return channel, nil
}
func (a *App) UpdateChannel(channel *model.Channel) (*model.Channel, *model.AppError) {
if result := <-a.Srv.Store.Channel().Update(channel); result.Err != nil {
return nil, result.Err
......
......@@ -93,10 +93,20 @@ func (me *groupmsgProvider) DoCommand(a *App, args *model.CommandArgs, message s
}
}
groupChannel, channelErr := a.CreateGroupChannel(targetUsersSlice, args.UserId)
if channelErr != nil {
mlog.Error(channelErr.Error())
return &model.CommandResponse{Text: args.T("api.command_groupmsg.group_fail.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
var groupChannel *model.Channel
var channelErr *model.AppError
if a.SessionHasPermissionTo(args.Session, model.PERMISSION_CREATE_GROUP_CHANNEL) {
groupChannel, channelErr = a.CreateGroupChannel(targetUsersSlice, args.UserId)
if channelErr != nil {
mlog.Error(channelErr.Error())
return &model.CommandResponse{Text: args.T("api.command_groupmsg.group_fail.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
}
} else {
groupChannel, channelErr = a.GetGroupChannel(targetUsersSlice)
if channelErr != nil {
return &model.CommandResponse{Text: args.T("api.command_groupmsg.permission.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
}
}
if len(parsedMessage) > 0 {
......
......@@ -2,6 +2,11 @@ package app
import (
"testing"
"github.com/nicksnyder/go-i18n/i18n"
"github.com/stretchr/testify/assert"
"github.com/mattermost/mattermost-server/model"
)
func TestGroupMsgUsernames(t *testing.T) {
......@@ -35,3 +40,58 @@ func TestGroupMsgUsernames(t *testing.T) {
t.Fatal("error parsing different types of users")
}
}
func TestGroupMsgProvider(t *testing.T) {
th := Setup().InitBasic()
defer th.TearDown()
user3 := th.CreateUser()
targetUsers := "@" + th.BasicUser2.Username + ",@" + user3.Username + " "
team := th.CreateTeam()
th.LinkUserToTeam(th.BasicUser, team)
cmd := &groupmsgProvider{}
// Check without permission to create a GM channel.
resp := cmd.DoCommand(th.App, &model.CommandArgs{
T: i18n.IdentityTfunc(),
SiteURL: "http://test.url",
TeamId: team.Id,
UserId: th.BasicUser.Id,
Session: model.Session{
Roles: "",
},
}, targetUsers+"hello")
channelName := model.GetGroupNameFromUserIds([]string{th.BasicUser.Id, th.BasicUser2.Id, user3.Id})
assert.Equal(t, "api.command_groupmsg.permission.app_error", resp.Text)
assert.Equal(t, "", resp.GotoLocation)
// Check with permission to create a GM channel.
resp = cmd.DoCommand(th.App, &model.CommandArgs{
T: i18n.IdentityTfunc(),
SiteURL: "http://test.url",
TeamId: team.Id,
UserId: th.BasicUser.Id,
Session: model.Session{
Roles: model.SYSTEM_USER_ROLE_ID,
},
}, targetUsers+"hello")
assert.Equal(t, "", resp.Text)
assert.Equal(t, "http://test.url/"+team.Name+"/channels/"+channelName, resp.GotoLocation)
// Check without permission to post to an existing GM channel.
resp = cmd.DoCommand(th.App, &model.CommandArgs{
T: i18n.IdentityTfunc(),
SiteURL: "http://test.url",
TeamId: team.Id,
UserId: th.BasicUser.Id,
Session: model.Session{
Roles: "",
},
}, targetUsers+"hello")
assert.Equal(t, "", resp.Text)
assert.Equal(t, "http://test.url/"+team.Name+"/channels/"+channelName, resp.GotoLocation)
}
......@@ -834,6 +834,10 @@
"id": "api.command_remove.user_not_in_channel",
"translation": "{{.Username}} is not a member of this channel."
},
{
"id": "api.command_groupmsg.permission.app_error",
"translation": "You don't have the appropriate permissions to create a new group message."
},
{
"id": "api.command_search.desc",
"translation": "Search text in messages"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment