2021-08-19 18:33:41 +09:00
|
|
|
import { emojiRegex } from './emoji-regex.js';
|
|
|
|
import { fetchMeta } from './fetch-meta.js';
|
|
|
|
import { Emojis } from '@/models/index.js';
|
|
|
|
import { toPunyNullable } from './convert-host.js';
|
2019-03-18 00:03:57 +09:00
|
|
|
|
2020-02-19 22:06:54 +09:00
|
|
|
const legacies: Record<string, string> = {
|
2020-01-30 04:37:25 +09:00
|
|
|
'like': '👍',
|
|
|
|
'love': '❤', // ここに記述する場合は異体字セレクタを入れない
|
|
|
|
'laugh': '😆',
|
|
|
|
'hmm': '🤔',
|
|
|
|
'surprise': '😮',
|
|
|
|
'congrats': '🎉',
|
|
|
|
'angry': '💢',
|
|
|
|
'confused': '😥',
|
|
|
|
'rip': '😇',
|
|
|
|
'pudding': '🍮',
|
2020-02-19 22:06:54 +09:00
|
|
|
'star': '⭐',
|
2019-03-18 00:03:57 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
export async function getFallbackReaction(): Promise<string> {
|
2019-03-18 01:03:35 +09:00
|
|
|
const meta = await fetchMeta();
|
2020-01-30 04:37:25 +09:00
|
|
|
return meta.useStarForReactionFallback ? '⭐' : '👍';
|
2019-03-18 00:03:57 +09:00
|
|
|
}
|
|
|
|
|
2020-02-19 06:36:50 +09:00
|
|
|
export function convertLegacyReactions(reactions: Record<string, number>) {
|
|
|
|
const _reactions = {} as Record<string, number>;
|
|
|
|
|
|
|
|
for (const reaction of Object.keys(reactions)) {
|
2020-04-16 00:45:43 +09:00
|
|
|
if (reactions[reaction] <= 0) continue;
|
|
|
|
|
2020-02-19 22:06:54 +09:00
|
|
|
if (Object.keys(legacies).includes(reaction)) {
|
|
|
|
if (_reactions[legacies[reaction]]) {
|
|
|
|
_reactions[legacies[reaction]] += reactions[reaction];
|
2020-02-19 06:36:50 +09:00
|
|
|
} else {
|
2020-02-19 22:06:54 +09:00
|
|
|
_reactions[legacies[reaction]] = reactions[reaction];
|
2020-02-19 06:36:50 +09:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (_reactions[reaction]) {
|
|
|
|
_reactions[reaction] += reactions[reaction];
|
|
|
|
} else {
|
|
|
|
_reactions[reaction] = reactions[reaction];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-14 00:42:59 +09:00
|
|
|
const _reactions2 = {} as Record<string, number>;
|
|
|
|
|
|
|
|
for (const reaction of Object.keys(_reactions)) {
|
|
|
|
_reactions2[decodeReaction(reaction).reaction] = _reactions[reaction];
|
|
|
|
}
|
|
|
|
|
|
|
|
return _reactions2;
|
2020-02-19 06:36:50 +09:00
|
|
|
}
|
|
|
|
|
2020-04-14 00:42:59 +09:00
|
|
|
export async function toDbReaction(reaction?: string | null, reacterHost?: string | null): Promise<string> {
|
2019-03-18 00:03:57 +09:00
|
|
|
if (reaction == null) return await getFallbackReaction();
|
|
|
|
|
2020-04-14 00:42:59 +09:00
|
|
|
reacterHost = toPunyNullable(reacterHost);
|
|
|
|
|
2020-01-30 04:37:25 +09:00
|
|
|
// 文字列タイプのリアクションを絵文字に変換
|
2020-02-19 22:06:54 +09:00
|
|
|
if (Object.keys(legacies).includes(reaction)) return legacies[reaction];
|
2019-03-18 00:03:57 +09:00
|
|
|
|
|
|
|
// Unicode絵文字
|
|
|
|
const match = emojiRegex.exec(reaction);
|
|
|
|
if (match) {
|
|
|
|
// 合字を含む1つの絵文字
|
|
|
|
const unicode = match[0];
|
|
|
|
|
2020-01-30 04:37:25 +09:00
|
|
|
// 異体字セレクタ除去
|
|
|
|
return unicode.match('\u200d') ? unicode : unicode.replace(/\ufe0f/g, '');
|
2019-03-18 00:03:57 +09:00
|
|
|
}
|
|
|
|
|
2020-04-16 00:47:17 +09:00
|
|
|
const custom = reaction.match(/^:([\w+-]+)(?:@\.)?:$/);
|
2019-03-18 00:03:57 +09:00
|
|
|
if (custom) {
|
2020-04-14 00:42:59 +09:00
|
|
|
const name = custom[1];
|
2019-04-07 21:50:36 +09:00
|
|
|
const emoji = await Emojis.findOne({
|
2020-04-14 00:42:59 +09:00
|
|
|
host: reacterHost || null,
|
|
|
|
name,
|
2019-03-18 00:03:57 +09:00
|
|
|
});
|
|
|
|
|
2020-05-10 17:25:16 +09:00
|
|
|
if (emoji) return reacterHost ? `:${name}@${reacterHost}:` : `:${name}:`;
|
2019-03-18 00:03:57 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
return await getFallbackReaction();
|
|
|
|
}
|
2020-01-30 04:37:25 +09:00
|
|
|
|
2020-04-14 00:42:59 +09:00
|
|
|
type DecodedReaction = {
|
|
|
|
/**
|
|
|
|
* リアクション名 (Unicode Emoji or ':name@hostname' or ':name@.')
|
|
|
|
*/
|
|
|
|
reaction: string;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* name (カスタム絵文字の場合name, Emojiクエリに使う)
|
|
|
|
*/
|
|
|
|
name?: string;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* host (カスタム絵文字の場合host, Emojiクエリに使う)
|
|
|
|
*/
|
|
|
|
host?: string | null;
|
|
|
|
};
|
|
|
|
|
|
|
|
export function decodeReaction(str: string): DecodedReaction {
|
|
|
|
const custom = str.match(/^:([\w+-]+)(?:@([\w.-]+))?:$/);
|
|
|
|
|
|
|
|
if (custom) {
|
|
|
|
const name = custom[1];
|
|
|
|
const host = custom[2] || null;
|
|
|
|
|
|
|
|
return {
|
|
|
|
reaction: `:${name}@${host || '.'}:`, // ローカル分は@以降を省略するのではなく.にする
|
|
|
|
name,
|
|
|
|
host
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
reaction: str,
|
|
|
|
name: undefined,
|
|
|
|
host: undefined
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-01-30 04:37:25 +09:00
|
|
|
export function convertLegacyReaction(reaction: string): string {
|
2020-04-14 00:42:59 +09:00
|
|
|
reaction = decodeReaction(reaction).reaction;
|
2020-02-19 22:06:54 +09:00
|
|
|
if (Object.keys(legacies).includes(reaction)) return legacies[reaction];
|
2020-01-30 04:37:25 +09:00
|
|
|
return reaction;
|
|
|
|
}
|