Files
talk/plugins/talk-plugin-toxic-comments/server/hooks.js
T
2017-09-06 22:14:58 +07:00

27 lines
758 B
JavaScript

const perspective = require('./perspective');
const {ErrToxic} = require('./errors');
const {TOXICITY_THRESHOLD} = require('./constants');
module.exports = {
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,
});
},
},
},
};