Skip to content

Commit

Permalink
feat(/api/v1/contents): add return results for comments
Browse files Browse the repository at this point in the history
add with_children and with_root query params to filter content between posts and comments.
  • Loading branch information
mthmcalixto committed Jan 12, 2024
1 parent d03462c commit 04b4f75
Show file tree
Hide file tree
Showing 2 changed files with 442 additions and 2 deletions.
16 changes: 14 additions & 2 deletions pages/api/v1/contents/index.public.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import controller from 'models/controller.js';
import event from 'models/event.js';
import firewall from 'models/firewall.js';
import notification from 'models/notification.js';
import removeMarkdown from 'models/remove-markdown';
import user from 'models/user.js';
import validator from 'models/validator.js';

Expand All @@ -35,6 +36,8 @@ function getValidationHandler(request, response, next) {
page: 'optional',
per_page: 'optional',
strategy: 'optional',
with_root: 'optional',
with_children: 'optional',
});

request.query = cleanValues;
Expand All @@ -48,11 +51,12 @@ async function getHandler(request, response) {
const results = await content.findWithStrategy({
strategy: request.query.strategy,
where: {
parent_id: null,
parent_id: request.query.with_children ? undefined : null,
status: 'published',
$not_null: request.query.with_root === false ? ['parent_id'] : undefined,
},
attributes: {
exclude: ['body'],
exclude: request.query.with_children ? undefined : ['body'],
},
page: request.query.page,
per_page: request.query.per_page,
Expand All @@ -62,6 +66,14 @@ async function getHandler(request, response) {

const secureOutputValues = authorization.filterOutput(userTryingToList, 'read:content:list', contentList);

for (const content of secureOutputValues) {
if (content.parent_id) {
content.body = removeMarkdown(content.body, { maxLength: 255 });
} else {
delete content.body;
}
}

controller.injectPaginationHeaders(results.pagination, '/api/v1/contents', response);

return response.status(200).json(secureOutputValues);
Expand Down
Loading

0 comments on commit 04b4f75

Please sign in to comment.