From 01aaccb34080ede234602d1ca9acee8373b8560f Mon Sep 17 00:00:00 2001 From: Saturnino Abril Date: Mon, 27 Mar 2017 20:41:40 +0900 Subject: APIv4 post /channels/ids (#5845) * APIv4 post /channels/ids * updated enpoint as /teams/{team_id}/channels/ids --- api4/channel.go | 37 ++++++++++++++++++++++++++++-- api4/channel_test.go | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+), 2 deletions(-) (limited to 'api4') diff --git a/api4/channel.go b/api4/channel.go index a19f7d858..278bf1d2e 100644 --- a/api4/channel.go +++ b/api4/channel.go @@ -19,8 +19,9 @@ func InitChannel() { BaseRoutes.Channels.Handle("/direct", ApiSessionRequired(createDirectChannel)).Methods("POST") BaseRoutes.Channels.Handle("/members/{user_id:[A-Za-z0-9]+}/view", ApiSessionRequired(viewChannel)).Methods("POST") - BaseRoutes.Team.Handle("/channels", ApiSessionRequired(getPublicChannelsForTeam)).Methods("GET") - BaseRoutes.Team.Handle("/channels/search", ApiSessionRequired(searchChannelsForTeam)).Methods("POST") + BaseRoutes.ChannelsForTeam.Handle("", ApiSessionRequired(getPublicChannelsForTeam)).Methods("GET") + BaseRoutes.ChannelsForTeam.Handle("/ids", ApiSessionRequired(getPublicChannelsByIdsForTeam)).Methods("POST") + BaseRoutes.ChannelsForTeam.Handle("/search", ApiSessionRequired(searchChannelsForTeam)).Methods("POST") BaseRoutes.User.Handle("/teams/{team_id:[A-Za-z0-9]+}/channels", ApiSessionRequired(getChannelsForTeamForUser)).Methods("GET") BaseRoutes.Channel.Handle("", ApiSessionRequired(getChannel)).Methods("GET") @@ -322,6 +323,38 @@ func getPublicChannelsForTeam(c *Context, w http.ResponseWriter, r *http.Request } } +func getPublicChannelsByIdsForTeam(c *Context, w http.ResponseWriter, r *http.Request) { + c.RequireTeamId() + if c.Err != nil { + return + } + + channelIds := model.ArrayFromJson(r.Body) + if len(channelIds) == 0 { + c.SetInvalidParam("channel_ids") + return + } + + for _, cid := range channelIds { + if len(cid) != 26 { + c.SetInvalidParam("channel_id") + return + } + } + + if !app.SessionHasPermissionToTeam(c.Session, c.Params.TeamId, model.PERMISSION_VIEW_TEAM) { + c.SetPermissionError(model.PERMISSION_VIEW_TEAM) + return + } + + if channels, err := app.GetPublicChannelsByIdsForTeam(c.Params.TeamId, channelIds); err != nil { + c.Err = err + return + } else { + w.Write([]byte(channels.ToJson())) + } +} + func getChannelsForTeamForUser(c *Context, w http.ResponseWriter, r *http.Request) { c.RequireUserId().RequireTeamId() if c.Err != nil { diff --git a/api4/channel_test.go b/api4/channel_test.go index a208313df..3d501b313 100644 --- a/api4/channel_test.go +++ b/api4/channel_test.go @@ -6,6 +6,7 @@ package api4 import ( "fmt" "net/http" + "sort" "strconv" "testing" @@ -494,6 +495,68 @@ func TestGetPublicChannelsForTeam(t *testing.T) { CheckNoError(t, resp) } +func TestGetPublicChannelsByIdsForTeam(t *testing.T) { + th := Setup().InitBasic().InitSystemAdmin() + defer TearDown() + Client := th.Client + teamId := th.BasicTeam.Id + input := []string{th.BasicChannel.Id} + output := []string{th.BasicChannel.DisplayName} + + channels, resp := Client.GetPublicChannelsByIdsForTeam(teamId, input) + CheckNoError(t, resp) + + if len(*channels) != 1 { + t.Fatal("should return 1 channel") + } + + if (*channels)[0].DisplayName != output[0] { + t.Fatal("missing channel") + } + + input = append(input, GenerateTestId()) + input = append(input, th.BasicChannel2.Id) + input = append(input, th.BasicPrivateChannel.Id) + output = append(output, th.BasicChannel2.DisplayName) + sort.Strings(output) + + channels, resp = Client.GetPublicChannelsByIdsForTeam(teamId, input) + CheckNoError(t, resp) + + if len(*channels) != 2 { + t.Fatal("should return 2 channels") + } + + for i, c := range *channels { + if c.DisplayName != output[i] { + t.Fatal("missing channel") + } + } + + _, resp = Client.GetPublicChannelsByIdsForTeam(GenerateTestId(), input) + CheckForbiddenStatus(t, resp) + + _, resp = Client.GetPublicChannelsByIdsForTeam(teamId, []string{}) + CheckBadRequestStatus(t, resp) + + _, resp = Client.GetPublicChannelsByIdsForTeam(teamId, []string{"junk"}) + CheckBadRequestStatus(t, resp) + + _, resp = Client.GetPublicChannelsByIdsForTeam(teamId, []string{GenerateTestId()}) + CheckNotFoundStatus(t, resp) + + _, resp = Client.GetPublicChannelsByIdsForTeam(teamId, []string{th.BasicPrivateChannel.Id}) + CheckNotFoundStatus(t, resp) + + Client.Logout() + + _, resp = Client.GetPublicChannelsByIdsForTeam(teamId, input) + CheckUnauthorizedStatus(t, resp) + + _, resp = th.SystemAdminClient.GetPublicChannelsByIdsForTeam(teamId, input) + CheckNoError(t, resp) +} + func TestGetChannelsForTeamForUser(t *testing.T) { th := Setup().InitBasic().InitSystemAdmin() defer TearDown() -- cgit v1.2.3-1-g7c22