summaryrefslogtreecommitdiff
path: root/src/client/scripts/check-word-mute.ts
blob: 3b1fa75b1e2febab08c1854342cf1815654b2ada (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
export async function checkWordMute(note: Record<string, any>, me: Record<string, any> | null | undefined, mutedWords: string[][]): Promise<boolean> {
	// 自分自身
	if (me && (note.userId === me.id)) return false;

	const words = mutedWords
		// Clean up
		.map(xs => xs.filter(x => x !== ''))
		.filter(xs => xs.length > 0);

	if (words.length > 0) {
		if (note.text == null) return false;

		const matched = words.some(and =>
			and.every(keyword => {
				const regexp = keyword.match(/^\/(.+)\/(.*)$/);
				if (regexp) {
					return new RegExp(regexp[1], regexp[2]).test(note.text!);
				}
				return note.text!.includes(keyword);
			}));

		if (matched) return true;
	}

	return false;
}