2019-03-17 09:03:57 -06:00
|
|
|
import { emojiRegex } from './emoji-regex';
|
2019-04-23 17:11:19 -06:00
|
|
|
import { fetchMeta } from './fetch-meta';
|
2019-04-07 06:50:36 -06:00
|
|
|
import { Emojis } from '../models';
|
2019-03-17 09:03:57 -06:00
|
|
|
|
2020-01-29 12:37:25 -07:00
|
|
|
const legacy10: Record<string, string> = {
|
|
|
|
'like': '👍',
|
|
|
|
'love': '❤', // ここに記述する場合は異体字セレクタを入れない
|
|
|
|
'laugh': '😆',
|
|
|
|
'hmm': '🤔',
|
|
|
|
'surprise': '😮',
|
|
|
|
'congrats': '🎉',
|
|
|
|
'angry': '💢',
|
|
|
|
'confused': '😥',
|
|
|
|
'rip': '😇',
|
|
|
|
'pudding': '🍮',
|
2019-03-17 09:03:57 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
export async function getFallbackReaction(): Promise<string> {
|
2019-03-17 10:03:35 -06:00
|
|
|
const meta = await fetchMeta();
|
2020-01-29 12:37:25 -07:00
|
|
|
return meta.useStarForReactionFallback ? '⭐' : '👍';
|
2019-03-17 09:03:57 -06:00
|
|
|
}
|
|
|
|
|
2020-02-18 14:36:50 -07:00
|
|
|
export function convertLegacyReactions(reactions: Record<string, number>) {
|
|
|
|
const _reactions = {} as Record<string, number>;
|
|
|
|
|
|
|
|
for (const reaction of Object.keys(reactions)) {
|
|
|
|
if (Object.keys(legacy10).includes(reaction)) {
|
|
|
|
if (_reactions[legacy10[reaction]]) {
|
|
|
|
_reactions[legacy10[reaction]] += reactions[reaction];
|
|
|
|
} else {
|
|
|
|
_reactions[legacy10[reaction]] = reactions[reaction];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (_reactions[reaction]) {
|
|
|
|
_reactions[reaction] += reactions[reaction];
|
|
|
|
} else {
|
|
|
|
_reactions[reaction] = reactions[reaction];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return _reactions;
|
|
|
|
}
|
|
|
|
|
2020-01-29 12:37:25 -07:00
|
|
|
export async function toDbReaction(reaction?: string | null): Promise<string> {
|
2019-03-17 09:03:57 -06:00
|
|
|
if (reaction == null) return await getFallbackReaction();
|
|
|
|
|
2020-01-29 12:37:25 -07:00
|
|
|
// 文字列タイプのリアクションを絵文字に変換
|
|
|
|
if (Object.keys(legacy10).includes(reaction)) return legacy10[reaction];
|
2019-03-17 09:03:57 -06:00
|
|
|
|
|
|
|
// Unicode絵文字
|
|
|
|
const match = emojiRegex.exec(reaction);
|
|
|
|
if (match) {
|
|
|
|
// 合字を含む1つの絵文字
|
|
|
|
const unicode = match[0];
|
|
|
|
|
2020-01-29 12:37:25 -07:00
|
|
|
// 異体字セレクタ除去
|
|
|
|
return unicode.match('\u200d') ? unicode : unicode.replace(/\ufe0f/g, '');
|
2019-03-17 09:03:57 -06:00
|
|
|
}
|
|
|
|
|
2019-03-18 05:02:25 -06:00
|
|
|
const custom = reaction.match(/^:([\w+-]+):$/);
|
2019-03-17 09:03:57 -06:00
|
|
|
if (custom) {
|
2019-04-07 06:50:36 -06:00
|
|
|
const emoji = await Emojis.findOne({
|
2019-03-17 09:03:57 -06:00
|
|
|
host: null,
|
|
|
|
name: custom[1],
|
|
|
|
});
|
|
|
|
|
|
|
|
if (emoji) return reaction;
|
|
|
|
}
|
|
|
|
|
|
|
|
return await getFallbackReaction();
|
|
|
|
}
|
2020-01-29 12:37:25 -07:00
|
|
|
|
|
|
|
export function convertLegacyReaction(reaction: string): string {
|
|
|
|
if (Object.keys(legacy10).includes(reaction)) return legacy10[reaction];
|
|
|
|
return reaction;
|
|
|
|
}
|