From ae8cd7c0f41d645289210b95c101cfc61a250158 Mon Sep 17 00:00:00 2001 From: Fabien 'egg' O'Carroll Date: Thu, 11 Aug 2022 17:58:43 +0100 Subject: [PATCH] Fixed comment replies limiting (#15217) The limit applies to the replies relation is applies globally when fetching a collection, which means only 3 replies in total will be fetched across all comments. This patches the findPage method to manually fetch the replies and replies adjacent relations manually on each comment, applying the limit on a comment-by-comment basis. This is not optimised as we currently still make the initial request for relations. --- ghost/core/core/server/models/comment.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/ghost/core/core/server/models/comment.js b/ghost/core/core/server/models/comment.js index d4a8655b3d..07ca87431f 100644 --- a/ghost/core/core/server/models/comment.js +++ b/ghost/core/core/server/models/comment.js @@ -201,6 +201,25 @@ const Comment = ghostBookshelf.Model.extend({ return options; }, + async findPage(options) { + const {withRelated} = this.defaultRelations('findPage', options); + + const relationsToLoadIndividually = [ + 'replies', + 'replies.member', + 'replies.count.likes', + 'replies.count.liked' + ].filter(relation => withRelated.includes(relation)); + + const result = await ghostBookshelf.Model.findPage.call(this, options); + + for (const model of result.data) { + await model.load(relationsToLoadIndividually); + } + + return result; + }, + countRelations() { return { replies(modelOrCollection) {