lists banned words in the chat error message - fixes https://github.com/HabitRPG/habitica/issues/8812 (#8858)

* issue 8812 - added the list of bad words matched to the postChat error message.

* issue 8812 - added the list of bad words matched to the postChat error message.

* issue 8812 - some refactoring, fixed relevant tests, and lint rules refactor

* small fix for unnecessary empty array

* added test and did some small refactoring

* lint error fix

* issue 8812 - added the list of bad words matched to the postChat error message.

* issue 8812 - some refactoring, fixed relevant tests, and lint rules refactor

* small fix for unnecessary empty array

* added test and did some small refactoring

* lint error fix

* add test to check the error message contains the banned words used

* improve banned words test

* issue 8812 - added the list of bad words matched to the postChat error message.

* issue 8812 - some refactoring, fixed relevant tests, and lint rules refactor

* small fix for unnecessary empty array

* added test and did some small refactoring

* lint error fix

* issue 8812 - added the list of bad words matched to the postChat error message.

* issue 8812 - some refactoring, fixed relevant tests, and lint rules refactor

* add test to check the error message contains the banned words used

* improve banned words test

* merge with develop - aligned banned slurs check with banned words check
This commit is contained in:
borisabramovich86
2017-08-02 22:43:22 +03:00
committed by Sabe Jones
parent 014a7197f0
commit 026014b8d6
3 changed files with 60 additions and 45 deletions

View File

@@ -14,6 +14,7 @@ import pusher from '../../libs/pusher';
import nconf from 'nconf';
import Bluebird from 'bluebird';
import bannedWords from '../../libs/bannedWords';
import { getMatchesByWordArray } from '../../libs/stringUtils';
import { TAVERN_ID } from '../../models/group';
import bannedSlurs from '../../libs/bannedSlurs';
@@ -54,42 +55,9 @@ async function getAuthorEmailFromMessage (message) {
}
}
// @TODO: Probably move this to a library
function matchExact (r, str) {
let match = str.match(r);
return match !== null && match[0] !== null;
}
let bannedWordRegexs = [];
for (let i = 0; i < bannedWords.length; i += 1) {
let word = bannedWords[i];
let regEx = new RegExp(`\\b([^a-z]+)?${word.toLowerCase()}([^a-z]+)?\\b`);
bannedWordRegexs.push(regEx);
}
function textContainsBannedWords (message) {
for (let i = 0; i < bannedWordRegexs.length; i += 1) {
let regEx = bannedWordRegexs[i];
if (matchExact(regEx, message.toLowerCase())) return true;
}
return false;
}
let bannedSlurRegexs = [];
for (let i = 0; i < bannedSlurs.length; i += 1) {
let word = bannedSlurs[i];
let regEx = new RegExp(`\\b([^a-z]+)?${word.toLowerCase()}([^a-z]+)?\\b`);
bannedSlurRegexs.push(regEx);
}
function textContainsBannedSlur (message) {
for (let i = 0; i < bannedSlurRegexs.length; i += 1) {
let regEx = bannedSlurRegexs[i];
if (matchExact(regEx, message.toLowerCase())) return true;
}
return false;
let bannedSlursMatched = getMatchesByWordArray(message, bannedSlurs);
return bannedSlursMatched.length > 0;
}
/**
@@ -124,6 +92,10 @@ api.getChat = {
},
};
function getBannedWordsFromText (message) {
return getMatchesByWordArray(message, bannedWords);
}
/**
* @api {post} /api/v3/groups/:groupId/chat Post chat message to a group
* @apiName PostChat
@@ -199,8 +171,13 @@ api.postChat = {
throw new NotAuthorized(res.t('chatPrivilegesRevoked'));
}
if (group._id === TAVERN_ID && textContainsBannedWords(req.body.message)) {
throw new BadRequest(res.t('bannedWordUsed'));
if (group._id === TAVERN_ID) {
let matchedBadWords = getBannedWordsFromText(req.body.message);
if (matchedBadWords.length > 0) {
let message = res.t('bannedWordUsed').split('.');
message[0] += ` (${matchedBadWords.join(', ')})`;
throw new BadRequest(message.join('.'));
}
}
let lastClientMsg = req.query.previousMsg;