[CORL-1013] Count Reset (#2960)

* feat: added reset option for count.js

* fix: adjust reset beheviour

* fix: switched to bundlesize2

* fix: added flag to enable github checks

Co-authored-by: Vinh <vinh@vinh.tech>
This commit is contained in:
Wyatt Johnson
2020-05-13 16:47:45 +00:00
committed by GitHub
co-authored by Vinh
parent 79e0da2c2f
commit f73597d7d1
8 changed files with 356 additions and 85 deletions
+44 -48
View File
@@ -1,5 +1,5 @@
import createDOMPurify from "dompurify";
import { JSDOM } from "jsdom";
// import createDOMPurify from "dompurify";
// import { JSDOM } from "jsdom";
import { AppOptions } from "coral-server/app";
import { calculateTotalPublishedCommentCount } from "coral-server/models/comment";
@@ -15,54 +15,50 @@ export type CountOptions = Pick<AppOptions, "mongo" | "tenantCache" | "i18n">;
/**
* countHandler returns translated comment counts using JSONP.
*/
export const countHandler = ({ mongo, i18n }: CountOptions): RequestHandler => {
const window = new JSDOM("").window;
const DOMPurify = createDOMPurify(window as any);
export const countHandler = ({
mongo,
i18n,
}: CountOptions): RequestHandler => async (req, res, next) => {
try {
// Tenant is guaranteed at this point.
const coral = req.coral!;
const tenant = coral.tenant!;
return async (req, res, next) => {
try {
// Tenant is guaranteed at this point.
const coral = req.coral!;
const tenant = coral.tenant!;
const story = await find(mongo, tenant, {
id: req.query.id,
url: req.query.url,
});
const story = await find(mongo, tenant, {
id: req.query.id,
url: req.query.url,
});
const count = story
? calculateTotalPublishedCommentCount(story.commentCounts.status)
: 0;
const count = story
? calculateTotalPublishedCommentCount(story.commentCounts.status)
: 0;
let html = "";
if (req.query.notext === "true") {
// We only need the count without the text.
html = `<span class="${NUMBER_CLASSNAME}">${count}</span>`;
} else {
// Use translated string.
const bundle = i18n.getBundle(tenant.locale);
html = translate(
bundle,
`<span class="${NUMBER_CLASSNAME}">${count}</span> <span class="${TEXT_CLASSNAME}">Comments</span>`,
"comment-count",
{
number: count,
numberClass: NUMBER_CLASSNAME,
textClass: TEXT_CLASSNAME,
}
);
// Strip dangerous html from translation.
html = DOMPurify.sanitize(html);
}
// Respond using jsonp.
res.jsonp({
// Reference from the client that we'll just send back as it is.
ref: req.query.ref,
html,
});
} catch (err) {
return next(err);
let html = "";
if (req.query.notext === "true") {
// We only need the count without the text.
html = `<span class="${NUMBER_CLASSNAME}">${count}</span>`;
} else {
// Use translated string.
const bundle = i18n.getBundle(tenant.locale);
html = translate(
bundle,
`<span class="${NUMBER_CLASSNAME}">${count}</span> <span class="${TEXT_CLASSNAME}">Comments</span>`,
"comment-count",
{
number: count,
numberClass: NUMBER_CLASSNAME,
textClass: TEXT_CLASSNAME,
}
);
}
};
// Respond using jsonp.
res.jsonp({
// Reference from the client that we'll just send back as it is.
ref: req.query.ref,
html,
});
} catch (err) {
return next(err);
}
};