-
Notifications
You must be signed in to change notification settings - Fork 10.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FIX] Bad words falling if message is empty (#22930)
Co-authored-by: Diego Sampaio <[email protected]>
- Loading branch information
1 parent
717461d
commit ac7ae55
Showing
4 changed files
with
62 additions
and
29 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters