First draft of toxic-comments

This commit is contained in:
Chi Vinh Le
2017-09-06 22:11:48 +07:00
parent fea5f75aa6
commit 6f82fd76f5
32 changed files with 298 additions and 491 deletions
@@ -0,0 +1,37 @@
const perspective = require('./perspective');
const {ADD_COMMENT_TAG} = require('../../../perms/constants');
const {ErrToxic} = require('./errors');
const {TOXICITY_THRESHOLD} = require('./constants');
module.exports = {
Comment: {
tags: {
post(comment, input, {user}, _info, result) {
if (comment.metadata.perspective && user && user.can(ADD_COMMENT_TAG)) {
return result.concat({tag: {name: 'TOXIC', created_at: new Date()}});
}
return result;
}
},
},
RootMutation: {
createComment: {
async pre(_, {input}, _context, _info) {
// Don't call out to perspective when running tests.
if (process.env.NODE_ENV === 'test') {
return;
}
const apiKey = require('./apiKey');
const scores = await perspective.getScores(apiKey, input.body);
if (input.checkToxicity && scores.SEVERE_TOXICITY.summaryScore > TOXICITY_THRESHOLD) {
throw ErrToxic;
}
input.metadata = Object.assign({}, input.metadata, {
perspective: scores,
});
},
},
},
};