From 5eaa29057faa8d0e05eb4631dd0dba0755e63a4e Mon Sep 17 00:00:00 2001 From: sup39 Date: Wed, 27 Dec 2023 21:11:53 +0900 Subject: [PATCH] fix: properly check post language in checkLangMute() --- packages/client/src/scripts/check-word-mute.ts | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/packages/client/src/scripts/check-word-mute.ts b/packages/client/src/scripts/check-word-mute.ts index 7a39950c..acdaf438 100644 --- a/packages/client/src/scripts/check-word-mute.ts +++ b/packages/client/src/scripts/check-word-mute.ts @@ -12,11 +12,15 @@ function checkLangMute( note: firefish.entities.Note, mutedLangs: Array, ): Muted { - const mutedLangList = new Set( - mutedLangs.reduce((arr, x) => [...arr, ...(Array.isArray(x) ? x : [x])]), - ); - if (mutedLangList.has((note.lang?.[0]?.lang || "").split("-")[0])) { - return { muted: true, matched: [note.lang?.[0]?.lang] }; + const mutedLangList = new Set(mutedLangs.flatMap((e) => e)); + // handle subtags + // e.g. if lang = "zh-hant-tw", check ["zh", "zh-hant", "zh-hant-tw"] + const langChunks: string[] = (note.lang || "").split("-"); + for (let i = 0; i < langChunks.length; i++) { + const lang = langChunks.slice(0, i + 1).join("-"); + if (mutedLangList.has(lang)) { + return { muted: true, matched: [lang] }; + } } return NotMuted; }