From 4b13ccc5c1b394f20dcdb6a8865bfc6fddc4f264 Mon Sep 17 00:00:00 2001 From: Jeff Nelson Date: Mon, 31 Jul 2017 08:25:39 -0400 Subject: [PATCH] enforce API key --- .../server/perspective.js | 6 ----- .../server/router.js | 24 ++++++++++++------- 2 files changed, 15 insertions(+), 15 deletions(-) delete mode 100644 plugins/talk-plugin-toxic-comments/server/perspective.js diff --git a/plugins/talk-plugin-toxic-comments/server/perspective.js b/plugins/talk-plugin-toxic-comments/server/perspective.js deleted file mode 100644 index 0dc73840c..000000000 --- a/plugins/talk-plugin-toxic-comments/server/perspective.js +++ /dev/null @@ -1,6 +0,0 @@ -module.exports = (perspective) => { - console.log("hello world from perpsctive"); - if(!process.env.PERSPECTIVE_API_KEY) { - throw new Error('Please set the PERSPECTIVE_API_KEY environment variable to use the toxic-comments plugin. Visit https://www.perspectiveapi.com/ to request API access.'); - } -} diff --git a/plugins/talk-plugin-toxic-comments/server/router.js b/plugins/talk-plugin-toxic-comments/server/router.js index 16a5e5e0b..2779191c3 100644 --- a/plugins/talk-plugin-toxic-comments/server/router.js +++ b/plugins/talk-plugin-toxic-comments/server/router.js @@ -4,14 +4,22 @@ const bodyParser = require('body-parser'); var count = 0; module.exports = (router) => { + + const key = process.env.TALK_PERSPECTIVE_API_KEY; + if(!key) { + throw new Error('Please set the TALK_PERSPECTIVE_API_KEY environment variable to use the toxic-comments plugin. Visit https://www.perspectiveapi.com/ to request API access.'); + } + + router.use(boom()); router.use(bodyParser.text()); - router.post('/api/v1/toxicity/comments', (req, res) => { - console.log(req.body); - if(req.body) { + + router.get('/api/v1/toxicity', (req, res) => { + var comment = req.query.comment; + if(comment) { var body = { comment: { - text: req.body, + text: comment, languages: ["en"], requestedAttributes: { TOXICITY: {} @@ -20,16 +28,13 @@ module.exports = (router) => { }; var headers = { 'Content-Type': 'application/json', - 'Content-Length': Buffer.byteLength(data) }; - http.post('https://commentanalyzer.googleapis.com/v1alpha1/comments:analyze?key='+process.env.PERSPECTIVE_API_KEY, { - headers: headers, - body: body - }) + http.post('https://commentanalyzer.googleapis.com/v1alpha1/comments:analyze?key='+key, body) .then(function(response) { return res.json(response); }) .catch(function(err) { + console.log(err); return res.json(err); }) } @@ -37,4 +42,5 @@ module.exports = (router) => { res.boom.notFound(); } }); + };