From 9dbdb97bb52189fb0c74c9dbdb171b984468619e Mon Sep 17 00:00:00 2001 From: Hazelnoot Date: Sat, 10 May 2025 22:32:06 -0400 Subject: allow checkWordMute to accept raw strings --- packages/frontend/src/utility/check-word-mute.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'packages/frontend/src/utility') diff --git a/packages/frontend/src/utility/check-word-mute.ts b/packages/frontend/src/utility/check-word-mute.ts index 37ad678555..26bf593f7b 100644 --- a/packages/frontend/src/utility/check-word-mute.ts +++ b/packages/frontend/src/utility/check-word-mute.ts @@ -4,12 +4,12 @@ */ import * as Misskey from 'misskey-js'; -export function checkWordMute(note: Misskey.entities.Note, me: Misskey.entities.UserLite | null | undefined, mutedWords: Array): Array | false { +export function checkWordMute(note: string | Misskey.entities.Note, me: Misskey.entities.UserLite | null | undefined, mutedWords: Array): Array | false { // 自分自身 - if (me && (note.userId === me.id)) return false; + if (me && typeof(note) === 'object' && (note.userId === me.id)) return false; if (mutedWords.length > 0) { - const text = getNoteText(note); + const text = typeof(note) === 'object' ? getNoteText(note) : note; if (text === '') return false; -- cgit v1.2.3-freya From b4bc58ae4c264aa10e3ccbdc7272b2bc37f0af3d Mon Sep 17 00:00:00 2001 From: Hazelnoot Date: Sat, 10 May 2025 22:36:49 -0400 Subject: move parseMutes to a utility file --- .../src/pages/settings/mute-block.word-mute.vue | 34 +----------------- packages/frontend/src/utility/parse-mutes.ts | 41 ++++++++++++++++++++++ 2 files changed, 42 insertions(+), 33 deletions(-) create mode 100644 packages/frontend/src/utility/parse-mutes.ts (limited to 'packages/frontend/src/utility') diff --git a/packages/frontend/src/pages/settings/mute-block.word-mute.vue b/packages/frontend/src/pages/settings/mute-block.word-mute.vue index 52aa2ff230..97ddd91d30 100644 --- a/packages/frontend/src/pages/settings/mute-block.word-mute.vue +++ b/packages/frontend/src/pages/settings/mute-block.word-mute.vue @@ -34,10 +34,10 @@ SPDX-License-Identifier: AGPL-3.0-only import { computed, ref, watch } from 'vue'; import MkTextarea from '@/components/MkTextarea.vue'; import MkButton from '@/components/MkButton.vue'; -import * as os from '@/os.js'; import { i18n } from '@/i18n.js'; import MkFolder from '@/components/MkFolder.vue'; import { checkWordMute } from '@/utility/check-word-mute'; +import { parseMutes } from '@/utility/parse-mutes'; const props = defineProps<{ muted: (string[] | string)[]; @@ -66,38 +66,6 @@ watch(mutedWords, () => { changed.value = true; }); -function parseMutes(mutes: string) { - // split into lines, remove empty lines and unnecessary whitespace - const lines = mutes.trim().split('\n').map(line => line.trim()).filter(line => line !== ''); - const outLines = Array.from(lines) as (string | string[])[]; - - // check each line if it is a RegExp or not - for (let i = 0; i < lines.length; i++) { - const line = lines[i]; - const regexp = line.match(/^\/(.+)\/(.*)$/); - if (regexp) { - // check that the RegExp is valid - try { - new RegExp(regexp[1], regexp[2]); - // note that regex lines will not be split by spaces! - } catch (err: any) { - // invalid syntax: do not save, do not reset changed flag - os.alert({ - type: 'error', - title: i18n.ts.regexpError, - text: i18n.tsx.regexpErrorDescription({ tab: 'word mute', line: i + 1 }) + '\n' + err.toString(), - }); - // re-throw error so these invalid settings are not saved - throw err; - } - } else { - outLines[i] = line.split(' '); - } - } - - return outLines; -} - async function save() { try { const parsed = parseMutes(mutedWords.value); diff --git a/packages/frontend/src/utility/parse-mutes.ts b/packages/frontend/src/utility/parse-mutes.ts new file mode 100644 index 0000000000..1ebd5bcf83 --- /dev/null +++ b/packages/frontend/src/utility/parse-mutes.ts @@ -0,0 +1,41 @@ +/* + * SPDX-FileCopyrightText: syuilo and misskey-project + * SPDX-License-Identifier: AGPL-3.0-only + */ + +import * as os from '@/os'; +import { i18n } from '@/i18n'; + +export type Mutes = (string | string[])[]; + +export function parseMutes(mutes: string): Mutes { + // split into lines, remove empty lines and unnecessary whitespace + const lines = mutes.trim().split('\n').map(line => line.trim()).filter(line => line !== ''); + const outLines: Mutes = Array.from(lines); + + // check each line if it is a RegExp or not + for (let i = 0; i < lines.length; i++) { + const line = lines[i]; + const regexp = line.match(/^\/(.+)\/(.*)$/); + if (regexp) { + // check that the RegExp is valid + try { + new RegExp(regexp[1], regexp[2]); + // note that regex lines will not be split by spaces! + } catch (err: any) { + // invalid syntax: do not save, do not reset changed flag + os.alert({ + type: 'error', + title: i18n.ts.regexpError, + text: i18n.tsx.regexpErrorDescription({ tab: 'word mute', line: i + 1 }) + '\n' + err.toString(), + }); + // re-throw error so these invalid settings are not saved + throw err; + } + } else { + outLines[i] = line.split(' '); + } + } + + return outLines; +} -- cgit v1.2.3-freya