From ecdaeea94f72d1bd6ae7d3cf50e3c128d537e157 Mon Sep 17 00:00:00 2001 From: Johann150 Date: Thu, 23 Jun 2022 13:26:47 +0200 Subject: [PATCH] enhance: word mute checks CW (#8873) --- packages/backend/src/misc/check-word-mute.ts | 8 +++++--- packages/client/src/scripts/check-word-mute.ts | 8 +++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/packages/backend/src/misc/check-word-mute.ts b/packages/backend/src/misc/check-word-mute.ts index 588dc79e5..015a368df 100644 --- a/packages/backend/src/misc/check-word-mute.ts +++ b/packages/backend/src/misc/check-word-mute.ts @@ -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; diff --git a/packages/client/src/scripts/check-word-mute.ts b/packages/client/src/scripts/check-word-mute.ts index fa74c0993..aafdd8920 100644 --- a/packages/client/src/scripts/check-word-mute.ts +++ b/packages/client/src/scripts/check-word-mute.ts @@ -3,7 +3,9 @@ export function checkWordMute(note: Record, me: Record 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, me: Record 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, me: Record 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;