enhance: word mute checks CW (#8873)

This commit is contained in:
Johann150 2022-06-23 13:26:47 +02:00 committed by GitHub
parent d1e151172b
commit ecdaeea94f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 6 deletions

View File

@ -16,11 +16,13 @@ export async function checkWordMute(note: NoteLike, me: UserLike | null | undefi
if (me && (note.userId === me.id)) return false;
if (mutedWords.length > 0) {
if (note.text == null) return false;
const text = ((note.cw ?? '') + '\n' + (note.text ?? '')).trim();
if (text == '') return false;
const matched = mutedWords.some(filter => {
if (Array.isArray(filter)) {
return filter.every(keyword => note.text!.includes(keyword));
return filter.every(keyword => text.includes(keyword));
} else {
// represents RegExp
const regexp = filter.match(/^\/(.+)\/(.*)$/);
@ -29,7 +31,7 @@ export async function checkWordMute(note: NoteLike, me: UserLike | null | undefi
if (!regexp) return false;
try {
return new RE2(regexp[1], regexp[2]).test(note.text!);
return new RE2(regexp[1], regexp[2]).test(text);
} catch (err) {
// This should never happen due to input sanitisation.
return false;

View File

@ -3,7 +3,9 @@ export function checkWordMute(note: Record<string, any>, me: Record<string, any>
if (me && (note.userId === me.id)) return false;
if (mutedWords.length > 0) {
if (note.text == null) return false;
const text = ((note.cw ?? '') + '\n' + (note.text ?? '')).trim();
if (text == '') return false;
const matched = mutedWords.some(filter => {
if (Array.isArray(filter)) {
@ -11,7 +13,7 @@ export function checkWordMute(note: Record<string, any>, me: Record<string, any>
const filteredFilter = filter.filter(keyword => keyword !== '');
if (filteredFilter.length === 0) return false;
return filteredFilter.every(keyword => note.text!.includes(keyword));
return filteredFilter.every(keyword => text.includes(keyword));
} else {
// represents RegExp
const regexp = filter.match(/^\/(.+)\/(.*)$/);
@ -20,7 +22,7 @@ export function checkWordMute(note: Record<string, any>, me: Record<string, any>
if (!regexp) return false;
try {
return new RegExp(regexp[1], regexp[2]).test(note.text!);
return new RegExp(regexp[1], regexp[2]).test(text);
} catch (err) {
// This should never happen due to input sanitisation.
return false;