[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 удалений

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

@@ -1779,6 +1779,38 @@ func (s SqlChannelStore) GetMemberCount(channelId string, allowFromCache bool) (
return count, nil
}
// GetMemberCountsByGroup returns a slice of ChannelMemberCountByGroup for a given channel
// which contains the number of channel members for each group and optionally the number of unique timezones present for each group in the channel
func (s SqlChannelStore) GetMemberCountsByGroup(channelID string, includeTimezones bool) ([]*model.ChannelMemberCountByGroup, *model.AppError) {
selectStr := "GroupMembers.GroupId, COUNT(ChannelMembers.UserId) AS ChannelMemberCount"
if includeTimezones {
selectStr = "GroupMembers.GroupId, COUNT(ChannelMembers.UserId) AS ChannelMemberCount, COUNT( DISTINCT Users.Timezone ) AS ChannelMemberTimezonesCount"
}
query := s.getQueryBuilder().
Select(selectStr).
From("ChannelMembers").
Join("GroupMembers ON GroupMembers.UserId = ChannelMembers.UserId")
if includeTimezones {
query = query.Join("Users ON Users.Id = GroupMembers.UserId")
}
query = query.Where(sq.Eq{"ChannelMembers.ChannelId": channelID}).GroupBy("GroupMembers.GroupId")
queryString, args, err := query.ToSql()
if err != nil {
return nil, model.NewAppError("SqlChannelStore.GetMemberCountsByGroup", "store.sql.build_query.app_error", nil, err.Error(), http.StatusInternalServerError)
}
var data []*model.ChannelMemberCountByGroup
if _, err = s.GetReplica().Select(&data, queryString, args...); err != nil {
return nil, model.NewAppError("SqlChannelStore.GetMemberCountsByGroup", "store.sql_channel.get_member_count.app_error", nil, "channel_id="+channelID+", "+err.Error(), http.StatusInternalServerError)
}
return data, nil
}
func (s SqlChannelStore) InvalidatePinnedPostCount(channelId string) {
}