diff --git a/store/sqlstore/channel_store.go b/store/sqlstore/channel_store.go index cef687c518..9b7af36a61 100644 --- a/store/sqlstore/channel_store.go +++ b/store/sqlstore/channel_store.go @@ -733,7 +733,7 @@ func (s SqlChannelStore) GetPinnedPosts(channelId string) (*model.PostList, *mod pl := model.NewPostList() var posts []*model.Post - if _, err := s.GetReplica().Select(&posts, "SELECT * FROM Posts WHERE IsPinned = true AND ChannelId = :ChannelId AND DeleteAt = 0 ORDER BY CreateAt ASC", map[string]interface{}{"ChannelId": channelId}); err != nil { + if _, err := s.GetReplica().Select(&posts, "SELECT *, (SELECT count(Posts.Id) FROM Posts WHERE Posts.RootId = (CASE WHEN p.RootId = '' THEN p.Id ELSE p.RootId END) AND Posts.DeleteAt = 0) as ReplyCount FROM Posts p WHERE IsPinned = true AND ChannelId = :ChannelId AND DeleteAt = 0 ORDER BY CreateAt ASC", map[string]interface{}{"ChannelId": channelId}); err != nil { return nil, model.NewAppError("SqlPostStore.GetPinnedPosts", "store.sql_channel.pinned_posts.app_error", nil, err.Error(), http.StatusInternalServerError) } for _, post := range posts { diff --git a/store/storetest/channel_store.go b/store/storetest/channel_store.go index f360c7e809..06a2e52d36 100644 --- a/store/storetest/channel_store.go +++ b/store/storetest/channel_store.go @@ -5589,6 +5589,47 @@ func testChannelStoreGetPinnedPosts(t *testing.T, ss store.Store) { pl, errGet = ss.Channel().GetPinnedPosts(o2.Id) require.Nil(t, errGet, errGet) require.Empty(t, pl.Posts, "wasn't supposed to return posts") + + t.Run("with correct ReplyCount", func(t *testing.T) { + channelId := model.NewId() + userId := model.NewId() + + post1, err := ss.Post().Save(&model.Post{ + ChannelId: channelId, + UserId: userId, + Message: "message", + IsPinned: true, + }) + require.Nil(t, err) + time.Sleep(time.Millisecond) + + post2, err := ss.Post().Save(&model.Post{ + ChannelId: channelId, + UserId: userId, + Message: "message", + IsPinned: true, + }) + require.Nil(t, err) + time.Sleep(time.Millisecond) + + post3, err := ss.Post().Save(&model.Post{ + ChannelId: channelId, + UserId: userId, + ParentId: post1.Id, + RootId: post1.Id, + Message: "message", + IsPinned: true, + }) + require.Nil(t, err) + time.Sleep(time.Millisecond) + + posts, err := ss.Channel().GetPinnedPosts(channelId) + require.Nil(t, err) + require.Len(t, posts.Posts, 3) + require.Equal(t, posts.Posts[post1.Id].ReplyCount, int64(1)) + require.Equal(t, posts.Posts[post2.Id].ReplyCount, int64(0)) + require.Equal(t, posts.Posts[post3.Id].ReplyCount, int64(1)) + }) } func testChannelStoreGetPinnedPostCount(t *testing.T, ss store.Store) {