Skip to content

Commit

Permalink
updated enpoint as /teams/{team_id}/channels/ids
Browse files Browse the repository at this point in the history
  • Loading branch information
saturninoabril committed Mar 24, 2017
1 parent 5f063d2 commit a0566b6
Show file tree
Hide file tree
Showing 7 changed files with 241 additions and 232 deletions.
58 changes: 34 additions & 24 deletions api4/channel.go
Expand Up @@ -18,9 +18,9 @@ func InitChannel() {
BaseRoutes.Channels.Handle("", ApiSessionRequired(createChannel)).Methods("POST")
BaseRoutes.Channels.Handle("/direct", ApiSessionRequired(createDirectChannel)).Methods("POST")
BaseRoutes.Channels.Handle("/members/{user_id:[A-Za-z0-9]+}/view", ApiSessionRequired(viewChannel)).Methods("POST")
BaseRoutes.Channels.Handle("/ids", ApiSessionRequired(getChannelsByIds)).Methods("POST")

BaseRoutes.Team.Handle("/channels", ApiSessionRequired(getPublicChannelsForTeam)).Methods("GET")
BaseRoutes.ChannelsForTeam.Handle("", ApiSessionRequired(getPublicChannelsForTeam)).Methods("GET")
BaseRoutes.ChannelsForTeam.Handle("/ids", ApiSessionRequired(getPublicChannelsByIdsForTeam)).Methods("POST")

BaseRoutes.Channel.Handle("", ApiSessionRequired(getChannel)).Methods("GET")
BaseRoutes.Channel.Handle("", ApiSessionRequired(updateChannel)).Methods("PUT")
Expand Down Expand Up @@ -289,6 +289,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 deleteChannel(c *Context, w http.ResponseWriter, r *http.Request) {
c.RequireChannelId()
if c.Err != nil {
Expand Down Expand Up @@ -487,28 +519,6 @@ func viewChannel(c *Context, w http.ResponseWriter, r *http.Request) {
ReturnStatusOK(w)
}

func getChannelsByIds(c *Context, w http.ResponseWriter, r *http.Request) {
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 channels, err := app.GetChannelsByIds(channelIds, c.Session.UserId); err != nil {
c.Err = err
return
} else {
w.Write([]byte(channels.ToJson()))
}
}

func updateChannelMemberRoles(c *Context, w http.ResponseWriter, r *http.Request) {
c.RequireChannelId().RequireUserId()
if c.Err != nil {
Expand Down
113 changes: 62 additions & 51 deletions api4/channel_test.go
Expand Up @@ -439,6 +439,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 TestDeleteChannel(t *testing.T) {
th := Setup().InitBasic().InitSystemAdmin()
defer TearDown()
Expand Down Expand Up @@ -1023,57 +1085,6 @@ func TestViewChannel(t *testing.T) {
CheckNoError(t, resp)
}

func TestGetChannelsByIds(t *testing.T) {
th := Setup().InitBasic()
defer TearDown()
Client := th.Client
input := []string{th.BasicChannel.Id}
output := []string{th.BasicChannel.DisplayName}

channels, resp := Client.GetChannelsByIds(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)
output = append(output, th.BasicChannel2.DisplayName)
sort.Strings(output)

channels, resp = Client.GetChannelsByIds(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.GetChannelsByIds([]string{})
CheckBadRequestStatus(t, resp)

_, resp = Client.GetChannelsByIds([]string{"junk"})
CheckBadRequestStatus(t, resp)

_, resp = Client.GetChannelsByIds([]string{GenerateTestId()})
CheckNotFoundStatus(t, resp)

Client.Logout()

_, resp = Client.GetChannelsByIds(input)
CheckUnauthorizedStatus(t, resp)
}

func TestGetChannelUnread(t *testing.T) {
th := Setup().InitBasic().InitSystemAdmin()
defer TearDown()
Expand Down
4 changes: 2 additions & 2 deletions app/channel.go
Expand Up @@ -671,8 +671,8 @@ func GetChannelsUserNotIn(teamId string, userId string, offset int, limit int) (
}
}

func GetChannelsByIds(channelIds []string, userId string) (*model.ChannelList, *model.AppError) {
if result := <-Srv.Store.Channel().GetChannelsByIds(channelIds, userId); result.Err != nil {
func GetPublicChannelsByIdsForTeam(teamId string, channelIds []string) (*model.ChannelList, *model.AppError) {
if result := <-Srv.Store.Channel().GetPublicChannelsByIdsForTeam(teamId, channelIds); result.Err != nil {
return nil, result.Err
} else {
return result.Data.(*model.ChannelList), nil
Expand Down
22 changes: 11 additions & 11 deletions model/client4.go
Expand Up @@ -916,7 +916,7 @@ func (c *Client4) GetChannelStats(channelId string, etag string) (*ChannelStats,
}
}

// GetPublicChannelsForTeam returns a channel based on the provided team id string.
// GetPublicChannelsForTeam returns a list of channels based on the provided team id string.
func (c *Client4) GetPublicChannelsForTeam(teamId string, page int, perPage int, etag string) (*ChannelList, *Response) {
query := fmt.Sprintf("?page=%v&per_page=%v", page, perPage)
if r, err := c.DoApiGet(c.GetPublicChannelsForTeamRoute(teamId)+query, etag); err != nil {
Expand All @@ -927,6 +927,16 @@ func (c *Client4) GetPublicChannelsForTeam(teamId string, page int, perPage int,
}
}

// GetPublicChannelsByIdsForTeam returns a list of channels based on provided team id string
func (c *Client4) GetPublicChannelsByIdsForTeam(teamId string, channelIds []string) (*ChannelList, *Response) {
if r, err := c.DoApiPost(c.GetPublicChannelsForTeamRoute(teamId)+"/ids", ArrayToJson(channelIds)); err != nil {
return nil, &Response{StatusCode: r.StatusCode, Error: err}
} else {
defer closeBody(r)
return ChannelListFromJson(r.Body), BuildResponse(r)
}
}

// DeleteChannel deletes channel based on the provided channel id string.
func (c *Client4) DeleteChannel(channelId string) (bool, *Response) {
if r, err := c.DoApiDelete(c.GetChannelRoute(channelId)); err != nil {
Expand Down Expand Up @@ -1010,16 +1020,6 @@ func (c *Client4) ViewChannel(userId string, view *ChannelView) (bool, *Response
}
}

// GetChannelsByIds gets a list of channels that the user is member of
func (c *Client4) GetChannelsByIds(channelIds []string) (*ChannelList, *Response) {
if r, err := c.DoApiPost(c.GetChannelsRoute()+"/ids", ArrayToJson(channelIds)); err != nil {
return nil, &Response{StatusCode: r.StatusCode, Error: err}
} else {
defer closeBody(r)
return ChannelListFromJson(r.Body), BuildResponse(r)
}
}

// GetChannelUnread will return a ChannelUnread object that contains the number of
// unread messages and mentions for a user.
func (c *Client4) GetChannelUnread(channelId, userId string) (*ChannelUnread, *Response) {
Expand Down
89 changes: 50 additions & 39 deletions store/sql_channel_store.go
Expand Up @@ -541,45 +541,6 @@ func (s SqlChannelStore) GetChannels(teamId string, userId string) StoreChannel
return storeChannel
}

func (s SqlChannelStore) GetChannelsByIds(channelIds []string, userId string) StoreChannel {
storeChannel := make(StoreChannel, 1)

go func() {
result := StoreResult{}

props := make(map[string]interface{})
props["userId"] = userId

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 Channels.* FROM Channels, ChannelMembers WHERE Id = ChannelId AND UserId = :userId AND DeleteAt = 0 AND ChannelId IN ("+idQuery+") ORDER BY DisplayName", props)

if err != nil {
result.Err = model.NewLocAppError("SqlChannelStore.GetChannelsByIds", "store.sql_channel.get_channels_by_ids.get.app_error", nil, err.Error())
}

if len(*data) == 0 {
result.Err = model.NewAppError("SqlChannelStore.GetChannelsByIds", "store.sql_channel.get_channels_by_ids.not_found.app_error", nil, "", http.StatusNotFound)
}

result.Data = data
storeChannel <- result
close(storeChannel)
}()

return storeChannel
}

func (s SqlChannelStore) GetMoreChannels(teamId string, userId string, offset int, limit int) StoreChannel {
storeChannel := make(StoreChannel, 1)

Expand Down Expand Up @@ -658,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
Expand Down

0 comments on commit a0566b6

Please sign in to comment.