mirror of
https://github.com/wassname/talk.git
synced 2026-07-29 11:28:24 +08:00
35 lines
890 B
JavaScript
35 lines
890 B
JavaScript
const perspective = require('./perspective');
|
|
const {ErrNoComment} = require('./errors');
|
|
|
|
module.exports = (router) => {
|
|
|
|
/**
|
|
* POST /api/v1/toxicity/score
|
|
* args:
|
|
* - provide the comment in the request body
|
|
*/
|
|
router.post('/api/v1/toxicity/score', async (req, res, next) => {
|
|
const apiKey = process.env.TALK_PERSPECTIVE_API_KEY;
|
|
if(!apiKey) {
|
|
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.');
|
|
}
|
|
|
|
const {comment} = req.body;
|
|
|
|
if(!comment) {
|
|
return next(ErrNoComment);
|
|
}
|
|
|
|
try {
|
|
const scores = await perspective.getScores(apiKey, comment);
|
|
return res.json({
|
|
comment,
|
|
score: scores.SEVERE_TOXICITY.summaryScore,
|
|
});
|
|
} catch(err) {
|
|
return next(err);
|
|
}
|
|
});
|
|
|
|
};
|