Skip to content

Commit

Permalink
Include hidden comments for admins in pagination count (#21675)
Browse files Browse the repository at this point in the history
ref PLG-227

- Ensure hidden comments are included in the replies count for admin
users to ensure pagination works as expected.
  • Loading branch information
ronaldlangeveld authored Nov 21, 2024
1 parent 1f501c1 commit f0dab9d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
15 changes: 13 additions & 2 deletions ghost/core/core/server/models/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ const Comment = ghostBookshelf.Model.extend({
// Note: this limit is not working
.query('limit', 3);
},

customQuery(qb) {
qb.where(function () {
this.whereNotIn('comments.status', ['hidden', 'deleted'])
Expand Down Expand Up @@ -271,8 +272,8 @@ const Comment = ghostBookshelf.Model.extend({

countRelations() {
return {
replies(modelOrCollection) {
if (labs.isSet('commentImprovements')) {
replies(modelOrCollection, options) {
if (labs.isSet('commentImprovements') && !options.isAdmin) {
modelOrCollection.query('columns', 'comments.*', (qb) => {
qb.count('replies.id')
.from('comments AS replies')
Expand All @@ -288,6 +289,16 @@ const Comment = ghostBookshelf.Model.extend({
.as('count__replies');
});
}

if (options.isAdmin && labs.isSet('commentImprovements')) {
modelOrCollection.query('columns', 'comments.*', (qb) => {
qb.count('replies.id')
.from('comments AS replies')
.whereRaw('replies.parent_id = comments.id')
.whereNotIn('replies.status', ['deleted'])
.as('count__replies');
});
}
},
likes(modelOrCollection) {
modelOrCollection.query('columns', 'comments.*', (qb) => {
Expand Down
33 changes: 30 additions & 3 deletions ghost/core/test/e2e-api/admin/comments.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,37 @@ describe('Admin Comments API', function () {

const hiddenComment = res.body.comments.find(comment => comment.status === 'hidden');
assert.equal(hiddenComment.html, 'Comment 2');
// console.log(res.body);
// assert.equal(res.body.comments[0].html, 'Comment 2');
});

// assert.equal(res.body.comments[1].html, 'Comment 1');
it('includes hidden replies but not deleted replies in count', async function () {
const post = fixtureManager.get('posts', 1);
const {parent} = await dbFns.addCommentWithReplies({
member_id: fixtureManager.get('members', 0).id,
post_id: post.id,
html: 'Comment 1',
status: 'published',
replies: [
{
member_id: fixtureManager.get('members', 0).id,
html: 'Reply 1',
status: 'hidden'
},
{
member_id: fixtureManager.get('members', 0).id,
html: 'Reply 2',
status: 'deleted'
},
{
member_id: fixtureManager.get('members', 0).id,
html: 'Reply 3',
status: 'published'
}
]
});

const res = await adminApi.get('/comments/post/' + post.id + '/');
const item = res.body.comments.find(cmt => parent.id === cmt.id);
assert.equal(item.count.replies, 2);
});
});
});

0 comments on commit f0dab9d

Please sign in to comment.