Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Add ability to cache comments count endpoint #15734

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,12 @@
return;
}

const rawRes = await fetch(api, {
method: 'POST',
const rawRes = await fetch(`${api}?ids=${ids.join(',')}`, {
method: 'GET',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({ids})
}
});

if (rawRes.status !== 200) {
Expand Down
3 changes: 3 additions & 0 deletions ghost/core/core/server/api/endpoints/comments-members.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ module.exports = {

counts: {
permissions: false,
options: [
'ids'
],
async query(frame) {
return commentsService.controller.count(frame);
}
Expand Down
16 changes: 9 additions & 7 deletions ghost/core/core/server/services/comments/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,13 @@ module.exports = class CommentsController {
}

async count(frame) {
if (!frame?.data?.ids) {
if (!frame?.options?.ids) {
return await this.stats.getAllCounts();
}

return await this.stats.getCountsByPost(frame.data.ids);
const ids = frame?.options?.ids.split(',');

return await this.stats.getCountsByPost(ids);
}

/**
Expand All @@ -119,8 +121,8 @@ module.exports = class CommentsController {
this.#checkMember(frame);

return await this.service.likeComment(
frame.options.id,
frame.options?.context?.member,
frame.options.id,
frame.options?.context?.member,
frame.options
);
}
Expand All @@ -132,8 +134,8 @@ module.exports = class CommentsController {
this.#checkMember(frame);

return await this.service.unlikeComment(
frame.options.id,
frame.options?.context?.member,
frame.options.id,
frame.options?.context?.member,
frame.options
);
}
Expand All @@ -145,7 +147,7 @@ module.exports = class CommentsController {
this.#checkMember(frame);

return await this.service.reportComment(
frame.options.id,
frame.options.id,
frame.options?.context?.member
);
}
Expand Down
9 changes: 7 additions & 2 deletions ghost/core/core/server/web/comments/routes.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
const express = require('../../../shared/express');
const config = require('../../../shared/config');
const api = require('../../api').endpoints;
const {http} = require('@tryghost/api-framework');
const shared = require('../shared');

const bodyParser = require('body-parser');
const membersService = require('../../../server/services/members');

module.exports = function apiRoutes() {
const router = express.Router('comment api');

router.use(bodyParser.json({limit: '50mb'}));

// Global handling for member session, ensures a member is logged in to the frontend
router.use(membersService.middleware.loadMemberSession);

router.post('/counts', http(api.commentsMembers.counts));
const countsCache = shared.middleware.cacheControl(
'public',
{maxAge: config.get('caching:commentsCountAPI:maxAge')}
);
router.get('/counts', countsCache, http(api.commentsMembers.counts));

router.get('/', http(api.commentsMembers.browse));
router.get('/:id', http(api.commentsMembers.read));
Expand Down
5 changes: 4 additions & 1 deletion ghost/core/core/shared/config/defaults.json
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@
},
"contentAPI": {
"maxAge": 0
},
"commentsCountAPI": {
"maxAge": 0
naz marked this conversation as resolved.
Show resolved Hide resolved
}
},
"imageOptimization": {
Expand Down Expand Up @@ -168,7 +171,7 @@
"comments": {
"url": "https://cdn.jsdelivr.net/ghost/comments-ui@~{version}/umd/comments-ui.min.js",
"styles": "https://cdn.jsdelivr.net/ghost/comments-ui@~{version}/umd/main.css",
"version": "0.10"
"version": "0.11"
},
"editor": {
"url": "https://unpkg.com/@tryghost/koenig-lexical@~{version}/dist/koenig-lexical.umd.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1863,7 +1863,7 @@ Object {
exports[`Comments API when commenting enabled for all when authenticated Can fetch counts 2: [headers] 1`] = `
Object {
"access-control-allow-origin": "*",
"cache-control": "no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0",
"cache-control": "public, max-age=0",
"content-length": "89",
"content-type": "application/json; charset=utf-8",
"etag": StringMatching /\\(\\?:W\\\\/\\)\\?"\\(\\?:\\[ !#-\\\\x7E\\\\x80-\\\\xFF\\]\\*\\|\\\\r\\\\n\\[\\\\t \\]\\|\\\\\\\\\\.\\)\\*"/,
Expand Down
Loading