mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-17 14:47:53 +01:00
Added block when user types a swear word listed in banned words (#8197)
* Added block when user types a swear word listed in banned words * Moved banned words check to server * Removed unused code * Moved banned words to separate file and fixed grammar. * Updated chat test * Changed error to BadRequest * Fixed regex matching * Updated test banned word * Moved banned words and cached regex * Updated banned word message * Add ban filter only for tavern * Added tavern id constant * Added more tests for banned words * Added warning to banned words * Added alert * Added new regex to capture markdown * Fixed lint, spelling and importing
This commit is contained in:
@@ -2,6 +2,7 @@ import { authWithHeaders } from '../../middlewares/auth';
|
||||
import { model as Group } from '../../models/group';
|
||||
import { model as User } from '../../models/user';
|
||||
import {
|
||||
BadRequest,
|
||||
NotFound,
|
||||
NotAuthorized,
|
||||
} from '../../libs/errors';
|
||||
@@ -12,6 +13,8 @@ import slack from '../../libs/slack';
|
||||
import pusher from '../../libs/pusher';
|
||||
import nconf from 'nconf';
|
||||
import Bluebird from 'bluebird';
|
||||
import bannedWords from '../../libs/bannedWords';
|
||||
import { TAVERN_ID } from '../../models/group';
|
||||
|
||||
const FLAG_REPORT_EMAILS = nconf.get('FLAG_REPORT_EMAIL').split(',').map((email) => {
|
||||
return { email, canSend: true };
|
||||
@@ -82,6 +85,28 @@ api.getChat = {
|
||||
},
|
||||
};
|
||||
|
||||
// @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;
|
||||
}
|
||||
|
||||
/**
|
||||
* @api {post} /api/v3/groups/:groupId/chat Post chat message to a group
|
||||
* @apiName PostChat
|
||||
@@ -121,6 +146,10 @@ api.postChat = {
|
||||
throw new NotFound('Your chat privileges have been revoked.');
|
||||
}
|
||||
|
||||
if (group._id === TAVERN_ID && textContainsBannedWords(req.body.message)) {
|
||||
throw new BadRequest(res.t('bannedWordUsed'));
|
||||
}
|
||||
|
||||
let lastClientMsg = req.query.previousMsg;
|
||||
chatUpdated = lastClientMsg && group.chat && group.chat[0] && group.chat[0].id !== lastClientMsg ? true : false;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user