[MM-23264] Get channel member counts by group (#14068)

* MM-23264 Add api endpoint for get groups with members in channel

Add store tests

Add tests for api func

Gofmt

Apply changes from code review

* MM-23264 Make store layers

* MM-23264 Check read permission on channel member counts

* Trigger CI
Этот коммит содержится в:
Farhan Munshi
2020-04-24 17:12:54 -04:00
коммит произвёл GitHub
родитель e39569b358
Коммит 036f9384b4
11 изменённых файлов: 446 добавлений и 0 удалений

Просмотреть файл

@@ -44,6 +44,7 @@ func (api *API) InitChannel() {
api.BaseRoutes.Channel.Handle("/pinned", api.ApiSessionRequired(getPinnedPosts)).Methods("GET")
api.BaseRoutes.Channel.Handle("/timezones", api.ApiSessionRequired(getChannelMembersTimezones)).Methods("GET")
api.BaseRoutes.Channel.Handle("/members_minus_group_members", api.ApiSessionRequired(channelMembersMinusGroupMembers)).Methods("GET")
api.BaseRoutes.Channel.Handle("/member_counts_by_group", api.ApiSessionRequired(channelMemberCountsByGroup)).Methods("GET")
api.BaseRoutes.ChannelForUser.Handle("/unread", api.ApiSessionRequired(getChannelUnread)).Methods("GET")
@@ -1639,6 +1640,39 @@ func channelMembersMinusGroupMembers(c *Context, w http.ResponseWriter, r *http.
w.Write(b)
}
func channelMemberCountsByGroup(c *Context, w http.ResponseWriter, r *http.Request) {
if c.App.License() == nil {
c.Err = model.NewAppError("Api4.channelMemberCountsByGroup", "api.channel.channel_member_counts_by_group.license.error", nil, "", http.StatusNotImplemented)
return
}
c.RequireChannelId()
if c.Err != nil {
return
}
if !c.App.SessionHasPermissionToChannel(*c.App.Session(), c.Params.ChannelId, model.PERMISSION_READ_CHANNEL) {
c.SetPermissionError(model.PERMISSION_READ_CHANNEL)
return
}
includeTimezones := r.URL.Query().Get("include_timezones") == "true"
channelMemberCounts, err := c.App.Srv().Store.Channel().GetMemberCountsByGroup(c.Params.ChannelId, includeTimezones)
if err != nil {
c.Err = err
return
}
b, marshalErr := json.Marshal(channelMemberCounts)
if marshalErr != nil {
c.Err = model.NewAppError("Api4.channelMemberCountsByGroup", "api.marshal_error", nil, marshalErr.Error(), http.StatusInternalServerError)
return
}
w.Write(b)
}
func getChannelModerations(c *Context, w http.ResponseWriter, r *http.Request) {
if c.App.License() == nil {
c.Err = model.NewAppError("Api4.GetChannelModerations", "api.channel.get_channel_moderations.license.error", nil, "", http.StatusNotImplemented)

Просмотреть файл

@@ -3542,3 +3542,95 @@ func TestPatchChannelModerations(t *testing.T) {
})
}
func TestGetChannelMemberCountsByGroup(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
channel := th.BasicChannel
t.Run("Errors without a license", func(t *testing.T) {
_, res := th.SystemAdminClient.GetChannelMemberCountsByGroup(channel.Id, false, "")
require.Equal(t, "api.channel.channel_member_counts_by_group.license.error", res.Error.Id)
})
th.App.SetLicense(model.NewTestLicense())
t.Run("Errors without read permission to the channel", func(t *testing.T) {
_, res := th.Client.GetChannelMemberCountsByGroup(model.NewId(), false, "")
require.Equal(t, "api.context.permissions.app_error", res.Error.Id)
})
t.Run("Returns empty for a channel with no members or groups", func(t *testing.T) {
memberCounts, _ := th.SystemAdminClient.GetChannelMemberCountsByGroup(channel.Id, false, "")
require.Equal(t, []*model.ChannelMemberCountByGroup{}, memberCounts)
})
user := th.BasicUser
user.Timezone["useAutomaticTimezone"] = "false"
user.Timezone["manualTimezone"] = "XOXO/BLABLA"
_, err := th.App.UpsertGroupMember(th.Group.Id, user.Id)
require.Nil(t, err)
_, resp := th.SystemAdminClient.UpdateUser(user)
CheckNoError(t, resp)
user2 := th.BasicUser2
user2.Timezone["automaticTimezone"] = "NoWhere/Island"
_, err = th.App.UpsertGroupMember(th.Group.Id, user2.Id)
require.Nil(t, err)
_, resp = th.SystemAdminClient.UpdateUser(user2)
CheckNoError(t, resp)
t.Run("Returns users in group without timezones", func(t *testing.T) {
memberCounts, _ := th.SystemAdminClient.GetChannelMemberCountsByGroup(channel.Id, false, "")
expectedMemberCounts := []*model.ChannelMemberCountByGroup{
{
GroupId: th.Group.Id,
ChannelMemberCount: 2,
ChannelMemberTimezonesCount: 0,
},
}
require.Equal(t, expectedMemberCounts, memberCounts)
})
t.Run("Returns users in group with timezones", func(t *testing.T) {
memberCounts, _ := th.SystemAdminClient.GetChannelMemberCountsByGroup(channel.Id, true, "")
expectedMemberCounts := []*model.ChannelMemberCountByGroup{
{
GroupId: th.Group.Id,
ChannelMemberCount: 2,
ChannelMemberTimezonesCount: 2,
},
}
require.Equal(t, expectedMemberCounts, memberCounts)
})
id := model.NewId()
group := &model.Group{
DisplayName: "dn_" + id,
Name: "name" + id,
Source: model.GroupSourceLdap,
RemoteId: model.NewId(),
}
_, err = th.App.CreateGroup(group)
require.Nil(t, err)
_, err = th.App.UpsertGroupMember(group.Id, user.Id)
require.Nil(t, err)
t.Run("Returns multiple groups with users in group with timezones", func(t *testing.T) {
memberCounts, _ := th.SystemAdminClient.GetChannelMemberCountsByGroup(channel.Id, true, "")
expectedMemberCounts := []*model.ChannelMemberCountByGroup{
{
GroupId: group.Id,
ChannelMemberCount: 1,
ChannelMemberTimezonesCount: 1,
},
{
GroupId: th.Group.Id,
ChannelMemberCount: 2,
ChannelMemberTimezonesCount: 2,
},
}
require.ElementsMatch(t, expectedMemberCounts, memberCounts)
})
}