summaryrefslogtreecommitdiff
path: root/packages/frontend/src/utility/parse-mutes.ts
blob: 1ebd5bcf83e6686e436e7a229fd5f1ae55d006ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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;
}