iceshrimp-legacy/src/misc/reaction-lib.ts
MeiMei 2684541693 Custom reaction (#4517)
* Custom reaction

* increase limit of reactions/delete

* リアクションの場合は OS標準の絵文字を使用 を迂回する

* カスタムリアクションを無効にする設定

* fix

* disableCustomReaction --> enableEmojiReaction

* Avoid MFM rendering

* 🎨

* 🎨

* Auto accept

* custom emoji reaction

* Improve usability

* Extract emojiRegex

* Fix

* Clean up

* 🎨

* 🎨

* toDbReaction で reaction は必須に

あとフォールバックは like に

* Clean up

* Make required

* 3eb08748fe (r266241728)

* Refactor

* Allow null
2019-03-18 00:03:57 +09:00

60 lines
1.6 KiB
TypeScript

import Emoji from '../models/emoji';
import { emojiRegex } from './emoji-regex';
const basic10: Record<string, string> = {
'👍': 'like',
'❤': 'love', // ここに記述する場合は異体字セレクタを入れない
'😆': 'laugh',
'🤔': 'hmm',
'😮': 'surprise',
'🎉': 'congrats',
'💢': 'angry',
'😥': 'confused',
'😇': 'rip',
'🍮': 'pudding',
};
export async function getFallbackReaction(): Promise<string> {
return 'like';
}
export async function toDbReaction(reaction: string, enableEmoji = true): Promise<string> {
if (reaction == null) return await getFallbackReaction();
// 既存の文字列リアクションはそのまま
if (Object.values(basic10).includes(reaction)) return reaction;
if (!enableEmoji) return await getFallbackReaction();
// Unicode絵文字
const match = emojiRegex.exec(reaction);
if (match) {
// 合字を含む1つの絵文字
const unicode = match[0];
// 異体字セレクタ除去後の絵文字
const normalized = unicode.match('\u200d') ? unicode : unicode.replace(/\ufe0f/g, '');
// Unicodeプリンは寿司化不能とするため文字列化しない
if (normalized === '🍮') return normalized;
// プリン以外の既存のリアクションは文字列化する
if (basic10[normalized]) return basic10[normalized];
// それ以外はUnicodeのまま
return normalized;
}
const custom = reaction.match(/:([\w+-]+):/);
if (custom) {
const emoji = await Emoji.findOne({
host: null,
name: custom[1],
});
if (emoji) return reaction;
}
return await getFallbackReaction();
}