Skip to content

Commit

Permalink
Option to exclude file count in channel stats (#22533)
Browse files Browse the repository at this point in the history
Co-authored-by: Ashish Bhate <ashish.bhate@mattermost.com>
Co-authored-by: Mattermost Build <build@mattermost.com>
  • Loading branch information
3 people committed Mar 16, 2023
1 parent 06fef21 commit cb617a2
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
14 changes: 10 additions & 4 deletions api4/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,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
Expand Down Expand Up @@ -642,10 +645,13 @@ func getChannelStats(c *Context, w http.ResponseWriter, r *http.Request) {
return
}

filesCount, err := c.App.GetChannelFileCount(c.Params.ChannelId)
if err != nil {
c.Err = err
return
filesCount := int64(-1)
if !excludeFilesCountBool {
filesCount, err = c.App.GetChannelFileCount(c.Params.ChannelId)
if err != nil {
c.Err = err
return
}
}

stats := model.ChannelStats{
Expand Down
21 changes: 13 additions & 8 deletions api4/channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2542,7 +2542,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")
Expand All @@ -2551,7 +2551,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")

Expand All @@ -2562,30 +2562,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)
}

Expand Down
5 changes: 3 additions & 2 deletions model/client4.go
Original file line number Diff line number Diff line change
Expand Up @@ -2986,8 +2986,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
}
Expand Down

0 comments on commit cb617a2

Please sign in to comment.