revamped configuration for moderation phases

This commit is contained in:
Wyatt Johnson
2017-09-15 14:23:18 -06:00
parent f2e06d6d27
commit f97094177a
5 changed files with 230 additions and 114 deletions
@@ -1,6 +1,5 @@
const {getScores, isToxic} = require('./perspective');
const {ErrToxic} = require('./errors');
const ActionsService = require('../../../services/actions');
// We don't add the hooks during _test_ as the perspective API is not available.
if (process.env.NODE_ENV === 'test') {
@@ -12,53 +11,36 @@ module.exports = {
createComment: {
async pre(_, {input}, _context, _info) {
let scores;
// Try getting scores.
let scores;
try {
scores = await getScores(input.body);
}
catch(err) {
} catch(err) {
// Warn and let mutation pass.
console.trace(err);
return;
}
const commentIsToxic = isToxic(scores);
if (input.checkToxicity && commentIsToxic) {
throw ErrToxic;
}
// attach scores to metadata.
// Attach scores to metadata.
input.metadata = Object.assign({}, input.metadata, {
perspective: scores,
});
if (commentIsToxic) {
if (isToxic(scores)) {
if (input.checkToxicity) {
throw ErrToxic;
}
input.status = 'SYSTEM_WITHHELD';
}
},
async post(_, _input, _context, _info, result) {
const metadata = result.comment.metadata;
if (metadata.perspective && isToxic(metadata.perspective)) {
// TODO: this is kind of fragile, we should refactor this to resolve
// all these const's that we're using like 'COMMENTS', 'FLAG' to be
// defined in a checkable schema.
// Add a flag to the comment.
await ActionsService.create({
item_id: result.comment.id,
item_type: 'COMMENTS',
input.actions = input.actions && input.actions.length >= 0 ? input.actions : [];
input.actions.push({
action_type: 'FLAG',
user_id: null,
group_id: 'Comment contains toxic language',
metadata: {}
});
}
return result;
},
},
},