From 53847af2c4e84e6dc81b12fb6481cb8dfbf701b9 Mon Sep 17 00:00:00 2001 From: George Goldberg Date: Thu, 22 Dec 2016 18:21:05 +0000 Subject: API for getting channel members by IDs. (#4877) --- api/channel.go | 25 +++++++++++++++++++++++++ api/channel_test.go | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) (limited to 'api') diff --git a/api/channel.go b/api/channel.go index feebf6981..7ccf5e2b6 100644 --- a/api/channel.go +++ b/api/channel.go @@ -39,6 +39,7 @@ func InitChannel() { BaseRoutes.NeedChannel.Handle("/", ApiUserRequired(getChannel)).Methods("GET") BaseRoutes.NeedChannel.Handle("/stats", ApiUserRequired(getChannelStats)).Methods("GET") BaseRoutes.NeedChannel.Handle("/members/{user_id:[A-Za-z0-9]+}", ApiUserRequired(getChannelMember)).Methods("GET") + BaseRoutes.NeedChannel.Handle("/members/ids", ApiUserRequired(getChannelMembersByIds)).Methods("POST") BaseRoutes.NeedChannel.Handle("/join", ApiUserRequired(join)).Methods("POST") BaseRoutes.NeedChannel.Handle("/leave", ApiUserRequired(leave)).Methods("POST") BaseRoutes.NeedChannel.Handle("/delete", ApiUserRequired(deleteChannel)).Methods("POST") @@ -1300,3 +1301,27 @@ func viewChannel(c *Context, w http.ResponseWriter, r *http.Request) { ReturnStatusOK(w) } + +func getChannelMembersByIds(c *Context, w http.ResponseWriter, r *http.Request) { + params := mux.Vars(r) + channelId := params["channel_id"] + + userIds := model.ArrayFromJson(r.Body) + if len(userIds) == 0 { + c.SetInvalidParam("getChannelMembersByIds", "user_ids") + return + } + + if !HasPermissionToChannelContext(c, channelId, model.PERMISSION_READ_CHANNEL) { + return + } + + if result := <-Srv.Store.Channel().GetMembersByIds(channelId, userIds); result.Err != nil { + c.Err = result.Err + return + } else { + members := result.Data.(model.ChannelMembers) + w.Write([]byte(members.ToJson())) + return + } +} diff --git a/api/channel_test.go b/api/channel_test.go index d24a6d603..d1bd66a02 100644 --- a/api/channel_test.go +++ b/api/channel_test.go @@ -1777,3 +1777,40 @@ func TestViewChannel(t *testing.T) { t.Fatal("message counts don't match") } } + +func TestGetChannelMembersByIds(t *testing.T) { + th := Setup().InitBasic() + + if _, err := AddUserToChannel(th.BasicUser2, th.BasicChannel); err != nil { + t.Fatal("Could not add second user to channel") + } + + if result, err := th.BasicClient.GetChannelMembersByIds(th.BasicChannel.Id, []string{th.BasicUser.Id}); err != nil { + t.Fatal(err) + } else { + member := (*result.Data.(*model.ChannelMembers))[0] + if member.UserId != th.BasicUser.Id { + t.Fatal("user id did not match") + } + if member.ChannelId != th.BasicChannel.Id { + t.Fatal("team id did not match") + } + } + + if result, err := th.BasicClient.GetChannelMembersByIds(th.BasicChannel.Id, []string{th.BasicUser.Id, th.BasicUser2.Id, model.NewId()}); err != nil { + t.Fatal(err) + } else { + members := result.Data.(*model.ChannelMembers) + if len(*members) != 2 { + t.Fatal("length should have been 2, got ", len(*members)) + } + } + + if _, err := th.BasicClient.GetChannelMembersByIds("junk", []string{th.BasicUser.Id}); err == nil { + t.Fatal("should have errored - bad team id") + } + + if _, err := th.BasicClient.GetChannelMembersByIds(th.BasicChannel.Id, []string{}); err == nil { + t.Fatal("should have errored - empty user ids") + } +} -- cgit v1.2.3-1-g7c22