From 59a53d8f64d116e98ac4d9d586bc4d191ece48d0 Mon Sep 17 00:00:00 2001 From: naskya Date: Fri, 5 May 2023 12:36:17 +0900 Subject: [PATCH] Fix regex hard mutes --- packages/backend/src/misc/check-word-mute.ts | 26 ++++++++++++-------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/packages/backend/src/misc/check-word-mute.ts b/packages/backend/src/misc/check-word-mute.ts index b1d2e1bff..c561c7e0f 100644 --- a/packages/backend/src/misc/check-word-mute.ts +++ b/packages/backend/src/misc/check-word-mute.ts @@ -21,29 +21,35 @@ function checkWordMute( const text = ((note.cw ?? "") + " " + (note.text ?? "")).trim(); if (text === "") return false; - const matched = mutedWords.some((filter) => { - if (Array.isArray(filter)) { - return filter.every((keyword) => text.includes(keyword)); + for (const mutePattern of mutedWords) { + if (Array.isArray(mutePattern)) { + // Clean up + const keywords = mutePattern.filter((keyword) => keyword !== ""); + + if ( + keywords.length > 0 && + keywords.every((keyword) => text.includes(keyword)) + ) + return true; } else { // represents RegExp - const regexp = filter.match(/^\/(.+)\/(.*)$/); + const regexp = mutePattern.match(/^\/(.+)\/(.*)$/); // This should never happen due to input sanitisation. if (!regexp) { - console.warn(`Found invalid regex in word mutes: ${filter}`); - return false; + console.warn(`Found invalid regex in word mutes: ${mutePattern}`); + continue; } try { - return new RE2(regexp[1], regexp[2]).test(text); + if (new RegExp(regexp[1], regexp[2]).test(text)) return true; } catch (err) { // This should never happen due to input sanitisation. - return false; } } - }); + } - return matched; + return false; } export async function getWordHardMute(