add UI for testing word mutes

This commit is contained in:
Hazelnoot 2025-05-10 22:32:19 -04:00
parent 9dbdb97bb5
commit 32b860c352
3 changed files with 102 additions and 34 deletions

24
locales/index.d.ts vendored
View file

@ -12969,6 +12969,30 @@ export interface Locale extends ILocale {
*/ */
"text": string; "text": string;
}; };
/**
* Test word mutes
*/
"wordMuteTestLabel": string;
/**
* Enter some text here to test your word mute 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 word mutes.
*/
"wordMuteTestNoMatch": string;
} }
declare const locales: { declare const locales: {
[lang: string]: Locale; [lang: string]: Locale;

View file

@ -11,16 +11,33 @@ SPDX-License-Identifier: AGPL-3.0-only
<template #caption>{{ i18n.ts._wordMute.muteWordsDescription }}<br>{{ i18n.ts._wordMute.muteWordsDescription2 }}</template> <template #caption>{{ i18n.ts._wordMute.muteWordsDescription }}<br>{{ i18n.ts._wordMute.muteWordsDescription2 }}</template>
</MkTextarea> </MkTextarea>
</div> </div>
<MkFolder>
<template #label>{{ i18n.ts.wordMuteTestLabel }}</template>
<div class="_gaps">
<MkTextarea v-model="testWords">
<template #caption>{{ i18n.ts.wordMuteTestDescription }}</template>
</MkTextarea>
<div><MkButton :disabled="!testWords" @click="testWordMutes">{{ i18n.ts.wordMuteTestTest }}</MkButton></div>
<div v-if="testMatches == null">{{ i18n.ts.wordMuteTestNoResults}}</div>
<div v-else-if="testMatches === ''">{{ i18n.ts.wordMuteTestNoMatch }}</div>
<div v-else>{{ i18n.tsx.wordMuteTestMatch({ words: testMatches }) }}</div>
</div>
</MkFolder>
<MkButton primary inline :disabled="!changed" @click="save()"><i class="ti ti-device-floppy"></i> {{ i18n.ts.save }}</MkButton> <MkButton primary inline :disabled="!changed" @click="save()"><i class="ti ti-device-floppy"></i> {{ i18n.ts.save }}</MkButton>
</div> </div>
</template> </template>
<script lang="ts" setup> <script lang="ts" setup>
import { ref, watch } from 'vue'; import { computed, ref, watch } from 'vue';
import MkTextarea from '@/components/MkTextarea.vue'; import MkTextarea from '@/components/MkTextarea.vue';
import MkButton from '@/components/MkButton.vue'; import MkButton from '@/components/MkButton.vue';
import * as os from '@/os.js'; import * as os from '@/os.js';
import { i18n } from '@/i18n.js'; import { i18n } from '@/i18n.js';
import MkFolder from '@/components/MkFolder.vue';
import { checkWordMute } from '@/utility/check-word-mute';
const props = defineProps<{ const props = defineProps<{
muted: (string[] | string)[]; muted: (string[] | string)[];
@ -30,7 +47,7 @@ const emit = defineEmits<{
(ev: 'save', value: (string[] | string)[]): void; (ev: 'save', value: (string[] | string)[]): void;
}>(); }>();
const render = (mutedWords) => mutedWords.map(x => { const render = (mutedWords: (string | string[])[]) => mutedWords.map(x => {
if (Array.isArray(x)) { if (Array.isArray(x)) {
return x.join(' '); return x.join(' ');
} else { } else {
@ -40,15 +57,19 @@ const render = (mutedWords) => mutedWords.map(x => {
const mutedWords = ref(render(props.muted)); const mutedWords = ref(render(props.muted));
const changed = ref(false); const changed = ref(false);
const testWords = ref<string | null>(null);
const testMatches = ref<string | null>(null); computed(() => {
});
watch(mutedWords, () => { watch(mutedWords, () => {
changed.value = true; changed.value = true;
}); });
async function save() { function parseMutes(mutes: string) {
const parseMutes = (mutes) => {
// split into lines, remove empty lines and unnecessary whitespace // split into lines, remove empty lines and unnecessary whitespace
let lines = mutes.trim().split('\n').map(line => line.trim()).filter(line => line !== ''); 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 // check each line if it is a RegExp or not
for (let i = 0; i < lines.length; i++) { for (let i = 0; i < lines.length; i++) {
@ -70,23 +91,39 @@ async function save() {
throw err; throw err;
} }
} else { } else {
lines[i] = line.split(' '); outLines[i] = line.split(' ');
} }
} }
return lines; return outLines;
}; }
let parsed; async function save() {
try { try {
parsed = parseMutes(mutedWords.value); const parsed = parseMutes(mutedWords.value);
} catch (err) {
// already displayed error message in parseMutes
return;
}
emit('save', parsed); emit('save', parsed);
changed.value = false; changed.value = false;
} catch {
// already displayed error message in parseMutes
return;
}
}
function testWordMutes() {
if (!testWords.value || !mutedWords.value) {
testMatches.value = null;
return;
}
try {
const mutes = parseMutes(mutedWords.value);
const matches = checkWordMute(testWords.value, null, mutes);
testMatches.value = matches ? matches.flat(2).join(', ') : '';
} catch {
// Error is displayed by above function
testMatches.value = null;
}
} }
</script> </script>

View file

@ -543,3 +543,10 @@ enableProxyAccountDescription: "If disabled, then the proxy account will not be
_confirmPollEdit: _confirmPollEdit:
title: Are you sure you want to edit this poll title: Are you sure you want to edit this poll
text: Editing this poll will cause it to lose all previous votes text: Editing this poll will cause it to lose all previous votes
wordMuteTestLabel: "Test word mutes"
wordMuteTestDescription: "Enter some text here to test your word mute 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 word mutes."