From b742923897fa563629e477d81242a60d6b379c04 Mon Sep 17 00:00:00 2001 From: Nat Welch Date: Fri, 5 Oct 2018 13:40:32 -0400 Subject: [PATCH] Toxic Comments Plugin logging (#1970) * Add debug as a dep to toxic-comments * Add debug logging to toxic-comments plugin * fix: cleaned up a bit --- .../talk-plugin-toxic-comments/package.json | 1 + .../server/hooks.js | 3 ++- .../server/perspective.js | 19 ++++++++++++++++++- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/plugins/talk-plugin-toxic-comments/package.json b/plugins/talk-plugin-toxic-comments/package.json index f941e1b6c..8dbd2ed98 100644 --- a/plugins/talk-plugin-toxic-comments/package.json +++ b/plugins/talk-plugin-toxic-comments/package.json @@ -7,6 +7,7 @@ "author": "The Coral Project Team ", "license": "Apache-2.0", "dependencies": { + "debug": "^4.0.1", "ms": "^2.0.0" } } diff --git a/plugins/talk-plugin-toxic-comments/server/hooks.js b/plugins/talk-plugin-toxic-comments/server/hooks.js index be9fbb68a..610e7ad77 100644 --- a/plugins/talk-plugin-toxic-comments/server/hooks.js +++ b/plugins/talk-plugin-toxic-comments/server/hooks.js @@ -1,5 +1,6 @@ const { getScores, isToxic } = require('./perspective'); const { ErrToxic } = require('./errors'); +const debug = require('debug')('talk:plugin:toxic-comments'); function handlePositiveToxic(input) { input.status = 'SYSTEM_WITHHELD'; @@ -20,7 +21,7 @@ async function getScore(body) { scores = await getScores(body); } catch (err) { // Warn and let mutation pass. - console.trace(err); // TODO: log/handle this differently? + debug('Error sending to API: %o', err); return; } diff --git a/plugins/talk-plugin-toxic-comments/server/perspective.js b/plugins/talk-plugin-toxic-comments/server/perspective.js index 4b7fcd0c0..424543b2b 100644 --- a/plugins/talk-plugin-toxic-comments/server/perspective.js +++ b/plugins/talk-plugin-toxic-comments/server/perspective.js @@ -6,6 +6,7 @@ const { API_TIMEOUT, DO_NOT_STORE, } = require('./config'); +const debug = require('debug')('talk:plugin:toxic-comments'); /** * Get scores from the perspective api @@ -13,6 +14,8 @@ const { * @return {object} object containing toxicity scores */ async function getScores(text) { + debug('Sending to Perspective: %o', text); + const response = await fetch( `${API_ENDPOINT}/comments:analyze?key=${API_KEY}`, { @@ -25,7 +28,6 @@ async function getScores(text) { comment: { text, }, - // TODO: support other languages. languages: ['en'], doNotStore: DO_NOT_STORE, @@ -36,7 +38,22 @@ async function getScores(text) { }), } ); + const data = await response.json(); + + // If we get an error, just say it's not a toxic comment. + if (data.error) { + debug('Recieved Error when submitting: %o', data.error); + return { + TOXICITY: { + summaryScore: 0.0, + }, + SEVERE_TOXICITY: { + summaryScore: 0.0, + }, + }; + } + return { TOXICITY: { summaryScore: data.attributeScores.TOXICITY.summaryScore.value,