diff options
Diffstat (limited to 'api')
-rw-r--r-- | api/channel.go | 37 | ||||
-rw-r--r-- | api/channel_test.go | 40 |
2 files changed, 75 insertions, 2 deletions
diff --git a/api/channel.go b/api/channel.go index 6eda72dd9..2604cc5ce 100644 --- a/api/channel.go +++ b/api/channel.go @@ -25,6 +25,7 @@ func InitChannel() { BaseRoutes.Channels.Handle("/create", ApiUserRequired(createChannel)).Methods("POST") BaseRoutes.Channels.Handle("/view", ApiUserRequired(viewChannel)).Methods("POST") BaseRoutes.Channels.Handle("/create_direct", ApiUserRequired(createDirectChannel)).Methods("POST") + BaseRoutes.Channels.Handle("/create_group", ApiUserRequired(createGroupChannel)).Methods("POST") BaseRoutes.Channels.Handle("/update", ApiUserRequired(updateChannel)).Methods("POST") BaseRoutes.Channels.Handle("/update_header", ApiUserRequired(updateChannelHeader)).Methods("POST") BaseRoutes.Channels.Handle("/update_purpose", ApiUserRequired(updateChannelPurpose)).Methods("POST") @@ -98,6 +99,38 @@ func createDirectChannel(c *Context, w http.ResponseWriter, r *http.Request) { } } +func createGroupChannel(c *Context, w http.ResponseWriter, r *http.Request) { + if !app.SessionHasPermissionTo(c.Session, model.PERMISSION_CREATE_GROUP_CHANNEL) { + c.SetPermissionError(model.PERMISSION_CREATE_GROUP_CHANNEL) + return + } + + userIds := model.ArrayFromJson(r.Body) + if len(userIds) == 0 { + c.SetInvalidParam("createGroupChannel", "user_ids") + return + } + + found := false + for _, id := range userIds { + if id == c.Session.UserId { + found = true + break + } + } + + if !found { + userIds = append(userIds, c.Session.UserId) + } + + if sc, err := app.CreateGroupChannel(userIds); err != nil { + c.Err = err + return + } else { + w.Write([]byte(sc.ToJson())) + } +} + func CanManageChannel(c *Context, channel *model.Channel) bool { if channel.Type == model.CHANNEL_OPEN && !app.SessionHasPermissionToChannel(c.Session, channel.Id, model.PERMISSION_MANAGE_PUBLIC_CHANNEL_PROPERTIES) { c.SetPermissionError(model.PERMISSION_MANAGE_PUBLIC_CHANNEL_PROPERTIES) @@ -457,7 +490,7 @@ func getChannel(c *Context, w http.ResponseWriter, r *http.Request) { return } - if channel.TeamId != c.TeamId && channel.Type != model.CHANNEL_DIRECT { + if channel.TeamId != c.TeamId && !channel.IsGroupOrDirect() { c.Err = model.NewLocAppError("getChannel", "api.channel.get_channel.wrong_team.app_error", map[string]interface{}{"ChannelId": id, "TeamId": c.TeamId}, "") return } @@ -493,7 +526,7 @@ func getChannelByName(c *Context, w http.ResponseWriter, r *http.Request) { return } - if channel.TeamId != c.TeamId && channel.Type != model.CHANNEL_DIRECT { + if channel.TeamId != c.TeamId && !channel.IsGroupOrDirect() { c.Err = model.NewLocAppError("getChannel", "api.channel.get_channel.wrong_team.app_error", map[string]interface{}{"ChannelName": channelName, "TeamId": c.TeamId}, "") return } diff --git a/api/channel_test.go b/api/channel_test.go index 2e6484dfa..d8da3374b 100644 --- a/api/channel_test.go +++ b/api/channel_test.go @@ -219,6 +219,46 @@ func TestCreateDirectChannel(t *testing.T) { } } +func TestCreateGroupChannel(t *testing.T) { + th := Setup().InitBasic() + Client := th.BasicClient + user := th.BasicUser + user2 := th.BasicUser2 + user3 := th.CreateUser(Client) + + userIds := []string{user.Id, user2.Id, user3.Id} + + var channel *model.Channel + if result, err := Client.CreateGroupChannel(userIds); err != nil { + t.Fatal(err) + } else { + channel = result.Data.(*model.Channel) + } + + if channel.Type != model.CHANNEL_GROUP { + t.Fatal("channel type was not group") + } + + // Don't fail on group channels already existing and return the original channel again + if result, err := Client.CreateGroupChannel(userIds); err != nil { + t.Fatal(err) + } else if result.Data.(*model.Channel).Id != channel.Id { + t.Fatal("didn't return original group channel when saving a duplicate") + } + + if _, err := Client.CreateGroupChannel([]string{user.Id}); err == nil { + t.Fatal("should have failed with not enough users") + } + + if _, err := Client.CreateGroupChannel([]string{}); err == nil { + t.Fatal("should have failed with not enough users") + } + + if _, err := Client.CreateGroupChannel([]string{user.Id, user2.Id, user3.Id, "junk"}); err == nil { + t.Fatal("should have failed with non-existent user") + } +} + func TestUpdateChannel(t *testing.T) { th := Setup().InitBasic().InitSystemAdmin() Client := th.SystemAdminClient |