diff --git a/locales/index.d.ts b/locales/index.d.ts index da1e522756..dc41c96a96 100644 --- a/locales/index.d.ts +++ b/locales/index.d.ts @@ -13005,6 +13005,30 @@ export interface Locale extends ILocale { */ "text": string; }; + /** + * Test patterns + */ + "wordMuteTestLabel": string; + /** + * Enter some text here to test your word patterns. The matched words, if any, will be displayed below. + */ + "wordMuteTestDescription": string; + /** + * Test + */ + "wordMuteTestTest": string; + /** + * Matched words: {words} + */ + "wordMuteTestMatch": ParameterizedString<"words">; + /** + * No results yet, enter some text and click "Test" to check it. + */ + "wordMuteTestNoResults": string; + /** + * Text does not match any patterns. + */ + "wordMuteTestNoMatch": string; } declare const locales: { [lang: string]: Locale; diff --git a/packages/frontend/src/components/SkPatternTest.vue b/packages/frontend/src/components/SkPatternTest.vue new file mode 100644 index 0000000000..2ed2b3fdc3 --- /dev/null +++ b/packages/frontend/src/components/SkPatternTest.vue @@ -0,0 +1,57 @@ + + + + + {{ i18n.ts.wordMuteTestLabel }} + + + + {{ i18n.ts.wordMuteTestDescription }} + + {{ i18n.ts.wordMuteTestTest }} + {{ i18n.ts.wordMuteTestNoResults}} + {{ i18n.ts.wordMuteTestNoMatch }} + {{ i18n.tsx.wordMuteTestMatch({ words: testMatches }) }} + + + + + + + diff --git a/packages/frontend/src/pages/admin/moderation.vue b/packages/frontend/src/pages/admin/moderation.vue index 9675bdc21a..6845bd7ad0 100644 --- a/packages/frontend/src/pages/admin/moderation.vue +++ b/packages/frontend/src/pages/admin/moderation.vue @@ -47,6 +47,9 @@ SPDX-License-Identifier: AGPL-3.0-only {{ i18n.ts.trustedLinkUrlPatternsDescription }} + + + {{ i18n.ts.save }} @@ -71,6 +74,9 @@ SPDX-License-Identifier: AGPL-3.0-only {{ i18n.ts.sensitiveWordsDescription }}{{ i18n.ts.sensitiveWordsDescription2 }} + + + {{ i18n.ts.save }} @@ -83,6 +89,9 @@ SPDX-License-Identifier: AGPL-3.0-only {{ i18n.ts.prohibitedWordsDescription }}{{ i18n.ts.prohibitedWordsDescription2 }} + + + {{ i18n.ts.save }} @@ -95,6 +104,9 @@ SPDX-License-Identifier: AGPL-3.0-only {{ i18n.ts.prohibitedWordsForNameOfUserDescription }}{{ i18n.ts.prohibitedWordsDescription2 }} + + + {{ i18n.ts.save }} @@ -166,6 +178,7 @@ import { definePage } from '@/page.js'; import MkButton from '@/components/MkButton.vue'; import FormLink from '@/components/form/link.vue'; import MkFolder from '@/components/MkFolder.vue'; +import SkPatternTest from '@/components/SkPatternTest.vue'; const enableRegistration = ref(false); const emailRequiredForSignup = ref(false); 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 f5837abe98..36b388b848 100644 --- a/packages/frontend/src/pages/settings/mute-block.word-mute.vue +++ b/packages/frontend/src/pages/settings/mute-block.word-mute.vue @@ -11,6 +11,9 @@ SPDX-License-Identifier: AGPL-3.0-only {{ i18n.ts._wordMute.muteWordsDescription }}{{ i18n.ts._wordMute.muteWordsDescription2 }} + + + {{ i18n.ts.save }} @@ -19,8 +22,9 @@ SPDX-License-Identifier: AGPL-3.0-only import { 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 { parseMutes } from '@/utility/parse-mutes'; +import SkPatternTest from '@/components/SkPatternTest.vue'; const props = defineProps<{ muted: (string[] | string)[]; @@ -30,7 +34,7 @@ const emit = defineEmits<{ (ev: 'save', value: (string[] | string)[]): void; }>(); -const render = (mutedWords) => mutedWords.map(x => { +const render = (mutedWords: (string | string[])[]) => mutedWords.map(x => { if (Array.isArray(x)) { return x.join(' '); } else { @@ -46,47 +50,15 @@ watch(mutedWords, () => { }); async function save() { - const parseMutes = (mutes) => { - // split into lines, remove empty lines and unnecessary whitespace - let lines = mutes.trim().split('\n').map(line => line.trim()).filter(line => line !== ''); - - // 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 { - lines[i] = line.split(' '); - } - } - - return lines; - }; - - let parsed; try { - parsed = parseMutes(mutedWords.value); - } catch (err) { + const parsed = parseMutes(mutedWords.value); + + emit('save', parsed); + + changed.value = false; + } catch { // already displayed error message in parseMutes return; } - - emit('save', parsed); - - changed.value = false; } 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; 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; +} diff --git a/sharkey-locales/en-US.yml b/sharkey-locales/en-US.yml index 4869c1ce84..9232a7706c 100644 --- a/sharkey-locales/en-US.yml +++ b/sharkey-locales/en-US.yml @@ -552,3 +552,10 @@ enableProxyAccountDescription: "If disabled, then the proxy account will not be _confirmPollEdit: title: Are you sure you want to edit this poll text: Editing this poll will cause it to lose all previous votes + +wordMuteTestLabel: "Test patterns" +wordMuteTestDescription: "Enter some text here to test your word patterns. The matched words, if any, will be displayed below." +wordMuteTestTest: "Test" +wordMuteTestMatch: "Matched words: {words}" +wordMuteTestNoResults: "No results yet, enter some text and click \"Test\" to check it." +wordMuteTestNoMatch: "Text does not match any patterns."