Filter on reported comments#10
Conversation
There was a problem hiding this comment.
Pull request overview
This pull request adds filtering functionality for reported comments in the Ghost admin interface. It enables admins to filter comments by report count and displays the report count in the UI.
Changes:
- Added
count.reportsfield to admin API responses and filtering capability - Implemented SQL-based filtering for report counts with various operators (>, >=, =, etc.)
- Added UI components to display report counts and filter by reported status
Reviewed changes
Copilot reviewed 11 out of 11 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| ghost/core/test/e2e-api/members-comments/comments.test.js | Added test to verify reports count is not exposed in public API |
| ghost/core/test/e2e-api/admin/comments.test.js | Added tests for report count filtering with various operators and combinations |
| ghost/core/test/e2e-api/admin/snapshots/comments.test.js.snap | Updated snapshots to include reports count in admin API responses |
| ghost/core/core/server/services/comments/CommentsService.js | Updated service to include count relations and pass through report filtering options |
| ghost/core/core/server/services/comments/CommentsController.js | Added report count filter extraction logic to parse NQL filters |
| ghost/core/core/server/models/comment.js | Added SQL subquery for report count filtering and count relation |
| ghost/core/core/server/api/endpoints/utils/serializers/output/mappers/comments.js | Added logic to include reports in count fields for admin requests |
| apps/posts/src/views/comments/components/comments-list.tsx | Added UI to display report count with icon |
| apps/posts/src/views/comments/components/comments-header.tsx | Added "Reported" filter option in UI |
| apps/posts/src/views/comments/comments.tsx | Added filter building logic for reported/non-reported comments |
| apps/admin-x-framework/src/api/comments.ts | Added reports field to Comment type definition |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
|
||
| if (jsonModel.count) { | ||
| response.count = _.pick(jsonModel.count, countFields); | ||
| response.count = _.pick(jsonModel.count, isPublicRequest ? countFieldsAdmin : countFields); |
There was a problem hiding this comment.
The ternary logic is inverted. When isPublicRequest is true (members API), it uses countFieldsAdmin which includes reports. It should use countFields (without reports) for public requests and countFieldsAdmin for admin requests. Change to !isPublicRequest ? countFieldsAdmin : countFields.
| if (filter.values[0] === 'true') { | ||
| parts.push('count.reports:0'); | ||
| } else if (filter.values[0] === 'false') { | ||
| parts.push('count.reports:>0'); |
There was a problem hiding this comment.
The filter logic is inverted. When the user selects 'reported: Yes' (value='true'), the code filters for count.reports:0 which means zero reports (not reported). Similarly, 'reported: No' (value='false') filters for count.reports:>0 (has reports). The conditions should be swapped: 'true' should map to count.reports:>0 and 'false' should map to count.reports:0.
| const res = await adminApi.get('/comments/'); | ||
| assert.equal(res.body.comments[0].count.reports, 2); |
There was a problem hiding this comment.
This test doesn't guarantee that the first comment in the response is the one with reports, as the API returns comments in created_at desc order by default. Multiple tests create comments, and the order isn't controlled here. Consider either: 1) Filtering the response to find the specific comment by HTML content, or 2) Using the specific comment ID to fetch and verify.
| const res = await adminApi.get('/comments/'); | ||
| assert.equal(res.body.comments[0].count.reports, 0); |
There was a problem hiding this comment.
This test assumes the first comment in the response is the newly created one, but doesn't verify it. The response could include other comments created in previous tests (though there's a beforeEach truncate). Consider filtering the response by HTML content to ensure you're checking the correct comment.
| const res = await adminApi.get('/comments/?filter=' + encodeURIComponent('count.reports:0')); | ||
| assert.equal(res.body.comments.length, 1); | ||
| assert.equal(res.body.comments[0].html, '<p>Non-reported comment</p>'); |
There was a problem hiding this comment.
This test may be fragile because it doesn't account for other non-reported comments that might exist. While beforeEach truncates the tables, if the test suite structure changes or runs in isolation, this could fail. Consider adding an assertion about which specific comment is returned or ensuring stricter test isolation.
| async getAdminAllComments({includeNested, filter, mongoTransformer, reportCount, order, page, limit}) { | ||
| return await this.models.Comment.findPage({ | ||
| withRelated: ['member', 'post'], | ||
| withRelated: ['member', 'post', 'count.replies', 'count.likes'], |
There was a problem hiding this comment.
The count.reports relation is missing from the withRelated array. The tests expect reports count to be included in admin responses, but it's not being explicitly loaded here. This should include 'count.reports' as well.
Benchmark PR from qodo-benchmark#98