From 58fda0534999ffe629ed6afb7e92c06a7868cb23 Mon Sep 17 00:00:00 2001 From: Ashish Bhate Date: Sat, 28 Jan 2023 07:33:33 +0530 Subject: [PATCH] MM-49486: Option to exclude file count in channel stats (#22096) Co-authored-by: Mattermost Build --- api4/channel.go | 14 ++++++++++---- api4/channel_test.go | 21 +++++++++++++-------- model/client4.go | 5 +++-- 3 files changed, 26 insertions(+), 14 deletions(-) diff --git a/api4/channel.go b/api4/channel.go index 63eefb58af..0edcd0eaf9 100644 --- a/api4/channel.go +++ b/api4/channel.go @@ -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{ diff --git a/api4/channel_test.go b/api4/channel_test.go index 6f8f5aeabe..882289216f 100644 --- a/api4/channel_test.go +++ b/api4/channel_test.go @@ -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) } diff --git a/model/client4.go b/model/client4.go index 64bf163312..a06a89abb7 100644 --- a/model/client4.go +++ b/model/client4.go @@ -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 }