MM-49486: Option to exclude file count in channel stats (#22096)

Co-authored-by: Mattermost Build <build@mattermost.com>
Этот коммит содержится в:
Ashish Bhate
2023-01-28 07:33:33 +05:30
коммит произвёл GitHub
родитель 3522419e89
Коммит 58fda05349
3 изменённых файлов: 26 добавлений и 14 удалений

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

@@ -627,6 +627,9 @@ func getChannelUnread(c *Context, w http.ResponseWriter, r *http.Request) {
}
func getChannelStats(c *Context, w http.ResponseWriter, r *http.Request) {
excludeFilesCount := r.URL.Query().Get("exclude_files_count")
excludeFilesCountBool, _ := strconv.ParseBool(excludeFilesCount)
c.RequireChannelId()
if c.Err != nil {
return
@@ -655,10 +658,13 @@ func getChannelStats(c *Context, w http.ResponseWriter, r *http.Request) {
return
}
filesCount, err := c.App.GetChannelFileCount(c.AppContext, c.Params.ChannelId)
if err != nil {
c.Err = err
return
filesCount := int64(-1)
if !excludeFilesCountBool {
filesCount, err = c.App.GetChannelFileCount(c.AppContext, c.Params.ChannelId)
if err != nil {
c.Err = err
return
}
}
stats := model.ChannelStats{

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

@@ -2543,7 +2543,7 @@ func TestGetChannelStats(t *testing.T) {
client := th.Client
channel := th.CreatePrivateChannel()
stats, _, err := client.GetChannelStats(channel.Id, "")
stats, _, err := client.GetChannelStats(channel.Id, "", false)
require.NoError(t, err)
require.Equal(t, channel.Id, stats.ChannelId, "couldn't get extra info")
@@ -2552,7 +2552,7 @@ func TestGetChannelStats(t *testing.T) {
require.Equal(t, int64(0), stats.FilesCount, "got incorrect file count")
th.CreatePinnedPostWithClient(th.Client, channel)
stats, _, err = client.GetChannelStats(channel.Id, "")
stats, _, err = client.GetChannelStats(channel.Id, "", false)
require.NoError(t, err)
require.Equal(t, int64(1), stats.PinnedPostCount, "should have returned 1 pinned post count")
@@ -2563,30 +2563,35 @@ func TestGetChannelStats(t *testing.T) {
require.NoError(t, err)
th.CreatePostInChannelWithFiles(channel, fileResp.FileInfos...)
// make sure the file count channel stats is updated
stats, _, err = client.GetChannelStats(channel.Id, "")
stats, _, err = client.GetChannelStats(channel.Id, "", false)
require.NoError(t, err)
require.Equal(t, int64(1), stats.FilesCount, "should have returned 1 file count")
_, resp, err := client.GetChannelStats("junk", "")
// exclude file counts
stats, _, err = client.GetChannelStats(channel.Id, "", true)
require.NoError(t, err)
require.Equal(t, int64(-1), stats.FilesCount, "should have returned -1 file count for exclude_files_count=true")
_, resp, err := client.GetChannelStats("junk", "", false)
require.Error(t, err)
CheckBadRequestStatus(t, resp)
_, resp, err = client.GetChannelStats(model.NewId(), "")
_, resp, err = client.GetChannelStats(model.NewId(), "", false)
require.Error(t, err)
CheckForbiddenStatus(t, resp)
client.Logout()
_, resp, err = client.GetChannelStats(channel.Id, "")
_, resp, err = client.GetChannelStats(channel.Id, "", false)
require.Error(t, err)
CheckUnauthorizedStatus(t, resp)
th.LoginBasic2()
_, resp, err = client.GetChannelStats(channel.Id, "")
_, resp, err = client.GetChannelStats(channel.Id, "", false)
require.Error(t, err)
CheckForbiddenStatus(t, resp)
_, _, err = th.SystemAdminClient.GetChannelStats(channel.Id, "")
_, _, err = th.SystemAdminClient.GetChannelStats(channel.Id, "", false)
require.NoError(t, err)
}

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

@@ -3032,8 +3032,9 @@ func (c *Client4) GetChannel(channelId, etag string) (*Channel, *Response, error
}
// GetChannelStats returns statistics for a channel.
func (c *Client4) GetChannelStats(channelId string, etag string) (*ChannelStats, *Response, error) {
r, err := c.DoAPIGet(c.channelRoute(channelId)+"/stats", etag)
func (c *Client4) GetChannelStats(channelId string, etag string, excludeFilesCount bool) (*ChannelStats, *Response, error) {
route := c.channelRoute(channelId) + fmt.Sprintf("/stats?exclude_files_count=%v", excludeFilesCount)
r, err := c.DoAPIGet(route, etag)
if err != nil {
return nil, BuildResponse(r), err
}