From 64070b078004a635b916e0d2a2cea8b7721653c6 Mon Sep 17 00:00:00 2001 From: Devin Binnie <52460000+devinbinnie@users.noreply.github.com> Date: Tue, 28 Jan 2025 08:17:11 -0500 Subject: [PATCH] [MM-62541] Exclude files not attached to a post from the file count for a channel (#29976) * [MM-62541] Exclude files not attached to a post from the file count for a channel * Added test to ensure that we only grab files with a post id --- server/channels/app/channel_test.go | 52 +++++++++++++++++++ .../channels/store/sqlstore/channel_store.go | 1 + 2 files changed, 53 insertions(+) diff --git a/server/channels/app/channel_test.go b/server/channels/app/channel_test.go index d439b2777e..02bd703a51 100644 --- a/server/channels/app/channel_test.go +++ b/server/channels/app/channel_test.go @@ -3084,3 +3084,55 @@ func TestPatchChannelMembersNotifyProps(t *testing.T) { assert.NotNil(t, appErr) }) } +func TestGetChannelFileCount(t *testing.T) { + th := Setup(t).InitBasic() + defer th.TearDown() + + channel := th.BasicChannel + + // Create a post with files + post := &model.Post{ + ChannelId: channel.Id, + Message: "This is a test post", + UserId: th.BasicUser.Id, + } + post, appErr := th.App.CreatePost(th.Context, post, channel, model.CreatePostFlags{}) + require.Nil(t, appErr) + + fileInfo1 := &model.FileInfo{ + Name: "file1.txt", + MimeType: "text/plain", + ChannelId: channel.Id, + CreatorId: th.BasicUser.Id, + PostId: post.Id, + Path: "/path/to/file1.txt", + } + _, err := th.App.Srv().Store().FileInfo().Save(th.Context, fileInfo1) + require.NoError(t, err) + + fileInfo2 := &model.FileInfo{ + Name: "file2.txt", + MimeType: "text/plain", + ChannelId: channel.Id, + CreatorId: th.BasicUser.Id, + PostId: post.Id, + Path: "/path/to/file2.txt", + } + _, err = th.App.Srv().Store().FileInfo().Save(th.Context, fileInfo2) + require.NoError(t, err) + + // Create a file without a post + fileInfo3 := &model.FileInfo{ + Name: "file3.txt", + MimeType: "text/plain", + ChannelId: channel.Id, + CreatorId: th.BasicUser.Id, + Path: "/path/to/file3.txt", + } + _, err = th.App.Srv().Store().FileInfo().Save(th.Context, fileInfo3) + require.NoError(t, err) + + count, appErr := th.App.GetChannelFileCount(th.Context, channel.Id) + require.Nil(t, appErr) + require.Equal(t, int64(2), count) +} diff --git a/server/channels/store/sqlstore/channel_store.go b/server/channels/store/sqlstore/channel_store.go index f34cae8b39..5229ed024c 100644 --- a/server/channels/store/sqlstore/channel_store.go +++ b/server/channels/store/sqlstore/channel_store.go @@ -2329,6 +2329,7 @@ func (s SqlChannelStore) GetFileCount(channelId string) (int64, error) { FileInfo WHERE FileInfo.DeleteAt = 0 + AND FileInfo.PostId != '' AND FileInfo.ChannelId = ?`, channelId) if err != nil {