More comments

This commit is contained in:
Wyatt Johnson
2017-01-18 11:28:39 -07:00
parent 52ed18a9b8
commit 5b30fd89c8
2 changed files with 48 additions and 16 deletions
+6 -6
View File
@@ -72,18 +72,18 @@ const createComment = (context, {body, asset_id, parent_id = null}, wordlist = {
};
/**
* Filters the comment and outputs the wordlist results.
* @param {[type]} context [description]
* @param {[type]} comment [description]
* @return {[type]} [description]
* Filters the comment object and outputs wordlist results.
* @param {Object} context graphql context
* @param {String} body body of a comment
* @return {Object} resolves to the wordlist results
*/
const filterNewComment = (context, comment) => {
const filterNewComment = (context, {body}) => {
// Create a new instance of the Wordlist.
const wl = new Wordlist();
// Load the wordlist and filter the comment content.
return wl.load().then(() => wl.filter(comment, 'body'));
return wl.load().then(() => wl.scan('body', body));
};
module.exports = (context) => ({
+42 -10
View File
@@ -118,6 +118,42 @@ class Wordlist {
});
}
/**
* Scans a specific field for wordlist violations.
*/
scan(fieldName, phrase) {
let errors = {};
// If the field doesn't exist in the body, then it can't be profane!
if (!phrase) {
// Return that there wasn't a profane word here.
return errors;
}
// Check if the field contains a banned word.
if (this.match(this.lists.banned, phrase)) {
debug(`the field "${fieldName}" contained a phrase "${phrase}" which contained a banned word/phrase`);
errors.banned = Errors.ErrContainsProfanity;
// Stop looping through the fields now, we discovered the worst possible
// situation (a banned word).
return errors;
}
// Check if the field contains a banned word.
if (this.match(this.lists.suspect, phrase)) {
debug(`the field "${fieldName}" contained a phrase "${phrase}" which contained a suspected word/phrase`);
errors.suspect = Errors.ErrContainsProfanity;
// Continue looping through the fields now, we discovered a possible bad
// word (suspect).
return errors;
}
}
/**
* Perform the filtering based on the loaded wordlists.
*/
@@ -129,9 +165,9 @@ class Wordlist {
// Loop over all the fields from the body that we want to check.
for (let i = 0; i < fields.length; i++) {
let field = fields[i];
let fieldName = fields[i];
let phrase = _.get(body, field, false);
let phrase = _.get(body, fieldName, false);
// If the field doesn't exist in the body, then it can't be profane!
if (!phrase) {
@@ -140,11 +176,10 @@ class Wordlist {
continue;
}
// Check if the field contains a banned word.
if (this.match(this.lists.banned, phrase)) {
debug(`the field "${field}" contained a phrase "${phrase}" which contained a banned word/phrase`);
errors = Object.assign(errors, this.scan(fieldName, phrase));
errors.banned = Errors.ErrContainsProfanity;
// Check if the field contains a banned word.
if (errors.banned) {
// Stop looping through the fields now, we discovered the worst possible
// situation (a banned word).
@@ -152,10 +187,7 @@ class Wordlist {
}
// Check if the field contains a banned word.
if (this.match(this.lists.suspect, phrase)) {
debug(`the field "${field}" contained a phrase "${phrase}" which contained a suspected word/phrase`);
errors.suspect = Errors.ErrContainsProfanity;
if (errors.suspect) {
// Continue looping through the fields now, we discovered a possible bad
// word (suspect).