mirror of
https://github.com/wassname/talk.git
synced 2026-08-11 11:27:10 +08:00
[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:
@@ -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);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user