summaryrefslogtreecommitdiff
path: root/packages/frontend/src/utility/parse-mutes.ts
diff options
context:
space:
mode:
authorHazelnoot <acomputerdog@gmail.com>2025-05-12 10:33:25 +0000
committerHazelnoot <acomputerdog@gmail.com>2025-05-12 10:33:25 +0000
commit835e76152e982bc6f8bfc09d7afa1aba4d872367 (patch)
tree37632f56ebf45c7841a9960266fdaebfd5e00417 /packages/frontend/src/utility/parse-mutes.ts
parentmerge: Fix hidden hashtags showing on the explore / trending page (!1014) (diff)
parentrename SkWordMuteTest to SkPatternTest (diff)
downloadsharkey-835e76152e982bc6f8bfc09d7afa1aba4d872367.tar.gz
sharkey-835e76152e982bc6f8bfc09d7afa1aba4d872367.tar.bz2
sharkey-835e76152e982bc6f8bfc09d7afa1aba4d872367.zip
merge: Add pattern checker for word mutes (resolves #1003) (!1020)
View MR for information: https://activitypub.software/TransFem-org/Sharkey/-/merge_requests/1020 Closes #1003 Approved-by: dakkar <dakkar@thenautilus.net> Approved-by: Marie <github@yuugi.dev>
Diffstat (limited to 'packages/frontend/src/utility/parse-mutes.ts')
-rw-r--r--packages/frontend/src/utility/parse-mutes.ts41
1 files changed, 41 insertions, 0 deletions
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;
+}