summaryrefslogtreecommitdiff
path: root/packages/frontend/src/utility/emoji-mute.ts
blob: a058bcc24283fada1730ecaf61b159541b9e6aaa (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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
 * SPDX-FileCopyrightText: syuilo and misskey-project
 * SPDX-License-Identifier: AGPL-3.0-only
 */

import { computed } from 'vue';
import { prefer } from '@/preferences.js';

// custom絵文字の情報からキーを作成する
export function makeEmojiMuteKey(props: { name: string; host?: string | null }) {
	return props.name.startsWith(':') ? props.name : `:${props.name}${props.host ? `@${props.host}` : ''}:`;
}

export function extractCustomEmojiName (name:string) {
	return (name[0] === ':' ? name.substring(1, name.length - 1) : name).replace('@.', '').split('@')[0];
}

export function extractCustomEmojiHost (name:string) {
	// nameは:emojiName@host:の形式
	// 取り出したい部分はhostなので、@以降を取り出す
	const index = name.indexOf('@');
	if (index === -1) {
		return null;
	}
	const host = name.substring(index + 1, name.length - 1);
	if (host === '' || host === '.') {
		return null;
	}
	return host;
}

export function mute(emoji: string) {
	const isCustomEmoji = emoji.startsWith(':') && emoji.endsWith(':');
	const emojiMuteKey = isCustomEmoji ?
		makeEmojiMuteKey({ name: extractCustomEmojiName(emoji), host: extractCustomEmojiHost(emoji) }) :
		emoji;
	const mutedEmojis = prefer.r.mutingEmojis.value;
	if (!mutedEmojis.includes(emojiMuteKey)) {
		return prefer.commit('mutingEmojis', [...mutedEmojis, emojiMuteKey]);
	}
	throw new Error('Emoji is already muted', { cause: `${emojiMuteKey} is Already Muted` });
}

export function unmute(emoji:string) {
	const isCustomEmoji = emoji.startsWith(':') && emoji.endsWith(':');
	const emojiMuteKey = isCustomEmoji ?
		makeEmojiMuteKey({ name: extractCustomEmojiName(emoji), host: extractCustomEmojiHost(emoji) }) :
		emoji;
	const mutedEmojis = prefer.r.mutingEmojis.value;
	console.log('unmute', emoji, emojiMuteKey);
	console.log('mutedEmojis', mutedEmojis);
	prefer.commit('mutingEmojis', mutedEmojis.filter((e) => e !== emojiMuteKey));
}

export function checkMuted(emoji: string) {
	const isCustomEmoji = emoji.startsWith(':') && emoji.endsWith(':');
	const emojiMuteKey = isCustomEmoji ?
		makeEmojiMuteKey({ name: extractCustomEmojiName(emoji), host: extractCustomEmojiHost(emoji) }) :
		emoji;
	return computed(() => prefer.r.mutingEmojis.value.includes(emojiMuteKey));
}