From 9b572be0df688535bd58b2c1c7dbae377300ef82 Mon Sep 17 00:00:00 2001 From: Hazel K Date: Thu, 3 Oct 2024 11:32:23 -0400 Subject: [PATCH] use standard mute logic --- .../frontend/src/pages/following-feed.vue | 74 ++++--------------- .../frontend/src/scripts/check-word-mute.ts | 2 +- 2 files changed, 14 insertions(+), 62 deletions(-) diff --git a/packages/frontend/src/pages/following-feed.vue b/packages/frontend/src/pages/following-feed.vue index e8ca76ef8a..55e226884e 100644 --- a/packages/frontend/src/pages/following-feed.vue +++ b/packages/frontend/src/pages/following-feed.vue @@ -70,7 +70,7 @@ import * as os from '@/os.js'; import MkPageHeader from '@/components/global/MkPageHeader.vue'; import { $i } from '@/account.js'; import MkLoading from '@/components/global/MkLoading.vue'; -import { getNoteText } from '@/scripts/check-word-mute.js'; +import { checkWordMute } from '@/scripts/check-word-mute.js'; const props = withDefaults(defineProps<{ initialTab?: FollowingFeedTab, @@ -160,83 +160,35 @@ async function onChangeTab(): Promise { await showUserNotes(''); } -const softMutePatterns = ref(buildMutePatterns($i?.mutedWords)); -const hardMutePatterns = ref(buildMutePatterns($i?.hardMutedWords)); - -function buildMutePatterns(mutedWords: (string | string[])[] | undefined): RegExp[] { - if (!mutedWords || mutedWords.length < 1) { - return []; - } - - // flags -> pattern[] - const patternMap = new Map>(); - for (const mute of mutedWords) { - let flags: string; - let patterns: string[]; - - // Translate the pattern, which can be a raw string or a regular expression. - // For unknown reasons, raw strings are expressed as an array and expressions are plain strings. - if (!mute) { - continue; - } else if (Array.isArray(mute)) { - patterns = mute; - flags = 'i'; - } else { - const match = mute.match(/^\/(.+)\/(.*)$/); - if (!match) { - continue; - } else { - patterns = [match[1]]; - flags = match[2]; - } - } - - // Group the patterns based on shared flags - let flagPatterns = patternMap.get(flags); - if (!flagPatterns) { - flagPatterns = new Set(); - patternMap.set(flags, flagPatterns); - } - - for (const pattern of patterns) { - flagPatterns.add(pattern); - } - } - - // Parse all the patterns into regular expressions - return Array - .from(patternMap) - .map(([flag, patterns]) => { - const pattern = Array.from(patterns).map(p => `(${p})`).join('|'); - return new RegExp(pattern, flag); - }); -} - -// Adapted from MkNote.ts function isSoftMuted(note: Misskey.entities.Note): boolean { - return isMuted(note, softMutePatterns.value); + return isMuted(note, $i?.mutedWords); } function isHardMuted(note: Misskey.entities.Note): boolean { - return isMuted(note, hardMutePatterns.value); + return isMuted(note, $i?.hardMutedWords); } -function isMuted(note: Misskey.entities.Note, mutes: RegExp[]): boolean { - if (mutes.length < 1) return false; +// Match the typing used by Misskey +type Mutes = (string | string[])[] | null | undefined; +// Adapted from MkNote.ts +function isMuted(note: Misskey.entities.Note, mutes: Mutes): boolean { return checkMute(note, mutes) || checkMute(note.reply, mutes) || checkMute(note.renote, mutes); } // Adapted from check-word-mute.ts -function checkMute(note: Misskey.entities.Note | undefined | null, mutes: RegExp[]): boolean { +function checkMute(note: Misskey.entities.Note | undefined | null, mutes: Mutes): boolean { if (!note) { return false; } - const noteText = getNoteText(note); - return mutes.some(p => p.test(noteText)); + if (!mutes || mutes.length < 1) { + return false; + } + + return checkWordMute(note, $i, mutes); } const latestNotesPaging = shallowRef>(); diff --git a/packages/frontend/src/scripts/check-word-mute.ts b/packages/frontend/src/scripts/check-word-mute.ts index 45af7374e8..e65c327ffe 100644 --- a/packages/frontend/src/scripts/check-word-mute.ts +++ b/packages/frontend/src/scripts/check-word-mute.ts @@ -43,7 +43,7 @@ export function checkWordMute(note: Note, me: MeDetailed | null | undefined, mut return false; } -export function getNoteText(note: Note): string { +function getNoteText(note: Note): string { const textParts: string[] = []; if (note.cw) textParts.push(note.cw);