Skip to content

Commit

Permalink
[FIX] Bad words falling if message is empty (#22930)
Browse files Browse the repository at this point in the history
Co-authored-by: Diego Sampaio <[email protected]>
  • Loading branch information
ggazzo and sampaiodiego authored Aug 20, 2021
1 parent 717461d commit ac7ae55
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 29 deletions.
29 changes: 0 additions & 29 deletions app/lib/server/methods/filterBadWords.js

This file was deleted.

56 changes: 56 additions & 0 deletions app/lib/server/methods/filterBadWords.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { Meteor } from 'meteor/meteor';
import { Tracker } from 'meteor/tracker';
import Filter from 'bad-words';

import { settings } from '../../../settings/server';
import { callbacks } from '../../../callbacks/server';
import { IMessage } from '../../../../definition/IMessage';

const Dep = new Tracker.Dependency();
Meteor.startup(() => {
settings.get(/Message_AllowBadWordsFilter|Message_BadWordsFilterList|Message_BadWordsWhitelist/, () => {
Dep.changed();
});
Tracker.autorun(() => {
Dep.depend();
const allowBadWordsFilter = settings.get('Message_AllowBadWordsFilter');

callbacks.remove('beforeSaveMessage', 'filterBadWords');

if (!allowBadWordsFilter) {
return;
}

const badWordsList = settings.get('Message_BadWordsFilterList') as string | undefined;
const whiteList = settings.get('Message_BadWordsWhitelist') as string | undefined;

const options = {
list: badWordsList?.split(',').map((word) => word.trim()).filter(Boolean) || [],
// library definition does not allow optional definition
exclude: undefined,
splitRegex: undefined,
placeHolder: undefined,
regex: undefined,
replaceRegex: undefined,
emptyList: undefined,
};

const filter = new Filter(options);

if (whiteList?.length) {
filter.removeWords(...whiteList.split(',').map((word) => word.trim()));
}

callbacks.add('beforeSaveMessage', function(message: IMessage) {
if (!message.msg) {
return message;
}
try {
message.msg = filter.clean(message.msg);
} finally {
// eslint-disable-next-line no-unsafe-finally
return message;
}
}, callbacks.priority.HIGH, 'filterBadWords');
});
});
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"@storybook/addons": "^6.3.6",
"@storybook/react": "^6.3.6",
"@types/agenda": "^2.0.9",
"@types/bad-words": "^3.0.1",
"@types/bcrypt": "^5.0.0",
"@types/body-parser": "^1.19.0",
"@types/chai": "^4.2.19",
Expand Down

0 comments on commit ac7ae55

Please sign in to comment.