diff --git a/routes/api/graph/mutators.js b/routes/api/graph/mutators.js index 3d97cc1bd..2a9f9bc64 100644 --- a/routes/api/graph/mutators.js +++ b/routes/api/graph/mutators.js @@ -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) => ({ diff --git a/services/wordlist.js b/services/wordlist.js index e912abc43..6cb8449a6 100644 --- a/services/wordlist.js +++ b/services/wordlist.js @@ -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).