From 609d4f43d9eef504d852fbf02af5473b0d1424c8 Mon Sep 17 00:00:00 2001 From: Joram Wilander Date: Thu, 2 Feb 2017 09:04:36 -0500 Subject: Implement POST /channels endpoint for APIv4 (#5241) --- app/channel.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'app') diff --git a/app/channel.go b/app/channel.go index 818e241b7..aba65143b 100644 --- a/app/channel.go +++ b/app/channel.go @@ -6,6 +6,7 @@ package app import ( "fmt" "net/http" + "strings" l4g "github.com/alecthomas/log4go" "github.com/mattermost/platform/model" @@ -129,6 +130,38 @@ func JoinDefaultChannels(teamId string, user *model.User, channelRole string) *m return err } +func CreateChannelWithUser(channel *model.Channel, userId string) (*model.Channel, *model.AppError) { + if channel.Type == model.CHANNEL_DIRECT { + return nil, model.NewAppError("CreateChannelWithUser", "api.channel.create_channel.direct_channel.app_error", nil, "", http.StatusBadRequest) + } + + if strings.Index(channel.Name, "__") > 0 { + return nil, model.NewAppError("CreateChannelWithUser", "api.channel.create_channel.invalid_character.app_error", nil, "", http.StatusBadRequest) + } + + if len(channel.TeamId) == 0 { + return nil, model.NewAppError("CreateChannelWithUser", "app.channel.create_channel.no_team_id.app_error", nil, "", http.StatusBadRequest) + } + + // Get total number of channels on current team + if count, err := GetNumberOfChannelsOnTeam(channel.TeamId); err != nil { + return nil, err + } else { + if int64(count+1) > *utils.Cfg.TeamSettings.MaxChannelsPerTeam { + return nil, model.NewAppError("CreateChannelWithUser", "api.channel.create_channel.max_channel_limit.app_error", map[string]interface{}{"MaxChannelsPerTeam": *utils.Cfg.TeamSettings.MaxChannelsPerTeam}, "", http.StatusBadRequest) + } + } + + channel.CreatorId = userId + + rchannel, err := CreateChannel(channel, true) + if err != nil { + return nil, err + } + + return rchannel, nil +} + func CreateChannel(channel *model.Channel, addMember bool) (*model.Channel, *model.AppError) { if result := <-Srv.Store.Channel().Save(channel); result.Err != nil { return nil, result.Err -- cgit v1.2.3-1-g7c22