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 --- store/sql_channel_store.go | 50 +++++++++++++++++++++++++ store/sql_channel_store_test.go | 81 +++++++++++++++++++++++++++++++++++++++++ store/store.go | 1 + 3 files changed, 132 insertions(+) (limited to 'store') diff --git a/store/sql_channel_store.go b/store/sql_channel_store.go index d72722f7c..309337e50 100644 --- a/store/sql_channel_store.go +++ b/store/sql_channel_store.go @@ -619,6 +619,56 @@ func (s SqlChannelStore) GetPublicChannelsForTeam(teamId string, offset int, lim return storeChannel } +func (s SqlChannelStore) GetPublicChannelsByIdsForTeam(teamId string, channelIds []string) StoreChannel { + storeChannel := make(StoreChannel, 1) + + go func() { + result := StoreResult{} + + props := make(map[string]interface{}) + props["teamId"] = teamId + + idQuery := "" + + for index, channelId := range channelIds { + if len(idQuery) > 0 { + idQuery += ", " + } + + props["channelId"+strconv.Itoa(index)] = channelId + idQuery += ":channelId" + strconv.Itoa(index) + } + + data := &model.ChannelList{} + _, err := s.GetReplica().Select(data, + `SELECT + * + FROM + Channels + WHERE + TeamId = :teamId + AND Type = 'O' + AND DeleteAt = 0 + AND Id IN (`+idQuery+`) + ORDER BY DisplayName`, + props) + + if err != nil { + result.Err = model.NewLocAppError("SqlChannelStore.GetPublicChannelsByIdsForTeam", "store.sql_channel.get_channels_by_ids.get.app_error", nil, err.Error()) + } + + if len(*data) == 0 { + result.Err = model.NewAppError("SqlChannelStore.GetPublicChannelsByIdsForTeam", "store.sql_channel.get_channels_by_ids.not_found.app_error", nil, "", http.StatusNotFound) + } + + result.Data = data + storeChannel <- result + close(storeChannel) + }() + + return storeChannel +} + type channelIdWithCountAndUpdateAt struct { Id string TotalMsgCount int64 diff --git a/store/sql_channel_store_test.go b/store/sql_channel_store_test.go index f347fa438..3cc6f3bc4 100644 --- a/store/sql_channel_store_test.go +++ b/store/sql_channel_store_test.go @@ -934,6 +934,87 @@ func TestChannelStoreGetPublicChannelsForTeam(t *testing.T) { } } +func TestChannelStoreGetPublicChannelsByIdsForTeam(t *testing.T) { + Setup() + + teamId1 := model.NewId() + + oc1 := model.Channel{} + oc1.TeamId = teamId1 + oc1.DisplayName = "OpenChannel1Team1" + oc1.Name = "a" + model.NewId() + "b" + oc1.Type = model.CHANNEL_OPEN + Must(store.Channel().Save(&oc1)) + + oc2 := model.Channel{} + oc2.TeamId = model.NewId() + oc2.DisplayName = "OpenChannel2TeamOther" + oc2.Name = "a" + model.NewId() + "b" + oc2.Type = model.CHANNEL_OPEN + Must(store.Channel().Save(&oc2)) + + pc3 := model.Channel{} + pc3.TeamId = teamId1 + pc3.DisplayName = "PrivateChannel3Team1" + pc3.Name = "a" + model.NewId() + "b" + pc3.Type = model.CHANNEL_PRIVATE + Must(store.Channel().Save(&pc3)) + + cids := []string{oc1.Id} + cresult := <-store.Channel().GetPublicChannelsByIdsForTeam(teamId1, cids) + list := cresult.Data.(*model.ChannelList) + + if len(*list) != 1 { + t.Fatal("should return 1 channel") + } + + if (*list)[0].Id != oc1.Id { + t.Fatal("missing channel") + } + + cids = append(cids, oc2.Id) + cids = append(cids, model.NewId()) + cids = append(cids, pc3.Id) + cresult = <-store.Channel().GetPublicChannelsByIdsForTeam(teamId1, cids) + list = cresult.Data.(*model.ChannelList) + + if len(*list) != 1 { + t.Fatal("should return 1 channel") + } + + oc4 := model.Channel{} + oc4.TeamId = teamId1 + oc4.DisplayName = "OpenChannel4Team1" + oc4.Name = "a" + model.NewId() + "b" + oc4.Type = model.CHANNEL_OPEN + Must(store.Channel().Save(&oc4)) + + cids = append(cids, oc4.Id) + cresult = <-store.Channel().GetPublicChannelsByIdsForTeam(teamId1, cids) + list = cresult.Data.(*model.ChannelList) + + if len(*list) != 2 { + t.Fatal("should return 2 channels") + } + + if (*list)[0].Id != oc1.Id { + t.Fatal("missing channel") + } + + if (*list)[1].Id != oc4.Id { + t.Fatal("missing channel") + } + + cids = cids[:0] + cids = append(cids, model.NewId()) + cresult = <-store.Channel().GetPublicChannelsByIdsForTeam(teamId1, cids) + list = cresult.Data.(*model.ChannelList) + + if len(*list) != 0 { + t.Fatal("should not return a channel") + } +} + func TestChannelStoreGetChannelCounts(t *testing.T) { Setup() diff --git a/store/store.go b/store/store.go index 323727697..528b26a3f 100644 --- a/store/store.go +++ b/store/store.go @@ -103,6 +103,7 @@ type ChannelStore interface { GetChannels(teamId string, userId string) StoreChannel GetMoreChannels(teamId string, userId string, offset int, limit int) StoreChannel GetPublicChannelsForTeam(teamId string, offset int, limit int) StoreChannel + GetPublicChannelsByIdsForTeam(teamId string, channelIds []string) StoreChannel GetChannelCounts(teamId string, userId string) StoreChannel GetTeamChannels(teamId string) StoreChannel GetAll(teamId string) StoreChannel -- cgit v1.2.3-1-g7c22