fix: fixes for comment counting

This commit is contained in:
Wyatt Johnson
2018-09-21 23:52:28 -06:00
parent 65156c0528
commit 0fbd27cdcb
6 changed files with 82 additions and 31 deletions
@@ -1,3 +1,6 @@
import striptags from "striptags";
import { isNil } from "lodash";
import {
GQLACTION_GROUP,
GQLACTION_TYPE,
@@ -9,28 +12,40 @@ import {
IntermediatePhaseResult,
} from "talk-server/services/comments/moderation";
const testCharCount = (settings: Partial<ModerationSettings>, length: number) =>
settings.charCountEnable && settings.charCount && length > settings.charCount;
const testCharCount = (
settings: Partial<ModerationSettings>,
length: number
) => {
// settings.charCount.enable && settings.charCount && length > settings.charCount;
if (settings.charCount && settings.charCount.enabled) {
if (!isNil(settings.charCount.min)) {
if (length < settings.charCount.min) {
return true;
}
}
if (!isNil(settings.charCount.max)) {
if (length > settings.charCount.max) {
return true;
}
}
}
return false;
};
export const commentLength: IntermediateModerationPhase = ({
asset,
tenant,
comment,
}): IntermediatePhaseResult | void => {
const length = comment.body ? comment.body.length : 0;
const length = comment.body ? striptags(comment.body).length : 0;
// Check to see if the body is too short, if it is, then complain about it!
if (length < 2) {
// TODO: (wyattjoh) return better error.
throw new Error("comment body too short");
}
// Reject if the comment is too long
// Reject if the comment is too long or too short.
if (
testCharCount(tenant, length) ||
(asset.settings && testCharCount(asset.settings, length))
) {
// Add the flag related to Trust to the comment.
return {
status: GQLCOMMENT_STATUS.REJECTED,
actions: [