From ba1e96a020b4298916ab9af9c4f80c9cb57e33d7 Mon Sep 17 00:00:00 2001 From: naskya Date: Fri, 5 May 2023 12:23:44 +0900 Subject: [PATCH] Fix non-ASCII word soft mutes --- .../client/src/scripts/check-word-mute.ts | 45 ++++++++++--------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/packages/client/src/scripts/check-word-mute.ts b/packages/client/src/scripts/check-word-mute.ts index 5ff7f24e1..3237a2c87 100644 --- a/packages/client/src/scripts/check-word-mute.ts +++ b/packages/client/src/scripts/check-word-mute.ts @@ -6,10 +6,6 @@ export type Muted = { const NotMuted = { muted: false, matched: [] }; -function escapeRegExp(x: string) { - return x.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); // $& means the whole matched string -} - function checkWordMute( note: NoteLike, mutedWords: Array, @@ -17,38 +13,43 @@ function checkWordMute( const text = ((note.cw ?? "") + " " + (note.text ?? "")).trim(); if (text === "") return NotMuted; + let result = { muted: false, matched: [] }; + for (const mutePattern of mutedWords) { - let mute: RegExp; - let matched: string[]; if (Array.isArray(mutePattern)) { - matched = mutePattern.filter((keyword) => keyword !== ""); - if (matched.length === 0) { - continue; + // Clean up + const keywords = mutePattern.filter((keyword) => keyword !== ""); + + if ( + keywords.length > 0 && + keywords.every((keyword) => text.includes(keyword)) + ) { + result.muted = true; + result.matched.push(...keywords); } - mute = new RegExp( - `\\b${matched.map(escapeRegExp).join("\\b.*\\b")}\\b`, - "g", - ); } else { + // represents RegExp const regexp = mutePattern.match(/^\/(.+)\/(.*)$/); + // This should never happen due to input sanitisation. if (!regexp) { console.warn(`Found invalid regex in word mutes: ${mutePattern}`); continue; } - mute = new RegExp(regexp[1], regexp[2]); - matched = [mutePattern]; - } - try { - if (mute.test(text)) { - return { muted: true, matched }; + + try { + if (new RegExp(regexp[1], regexp[2]).test(text)) { + result.muted = true; + result.matched.push(mutePattern); + } + } catch (err) { + // This should never happen due to input sanitisation. } - } catch (err) { - // This should never happen due to input sanitisation. } } - return NotMuted; + result.matched = [...new Set(result.matched)]; + return result; } export function getWordSoftMute(