From 9b07c5af05a8be8114af860893d68614e4ee5ca2 Mon Sep 17 00:00:00 2001 From: MeiMei <30769358+mei23@users.noreply.github.com> Date: Tue, 14 Apr 2020 00:42:59 +0900 Subject: リモートのカスタム絵文字リアクションを表示できるように (#6239) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * リモートのカスタム絵文字リアクションを表示できるように * AP * DBマイグレーション * ローカルのリアクションの. * fix * fix * fix * space --- src/misc/reaction-lib.ts | 59 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 54 insertions(+), 5 deletions(-) (limited to 'src/misc') diff --git a/src/misc/reaction-lib.ts b/src/misc/reaction-lib.ts index 43dbe1cc2c..e5da5ca4aa 100644 --- a/src/misc/reaction-lib.ts +++ b/src/misc/reaction-lib.ts @@ -1,6 +1,7 @@ import { emojiRegex } from './emoji-regex'; import { fetchMeta } from './fetch-meta'; import { Emojis } from '../models'; +import { toPunyNullable } from './convert-host'; const legacies: Record = { 'like': '👍', @@ -40,12 +41,20 @@ export function convertLegacyReactions(reactions: Record) { } } - return _reactions; + const _reactions2 = {} as Record; + + for (const reaction of Object.keys(_reactions)) { + _reactions2[decodeReaction(reaction).reaction] = _reactions[reaction]; + } + + return _reactions2; } -export async function toDbReaction(reaction?: string | null): Promise { +export async function toDbReaction(reaction?: string | null, reacterHost?: string | null): Promise { if (reaction == null) return await getFallbackReaction(); + reacterHost = toPunyNullable(reacterHost); + // 文字列タイプのリアクションを絵文字に変換 if (Object.keys(legacies).includes(reaction)) return legacies[reaction]; @@ -61,18 +70,58 @@ export async function toDbReaction(reaction?: string | null): Promise { const custom = reaction.match(/^:([\w+-]+):$/); if (custom) { + const name = custom[1]; const emoji = await Emojis.findOne({ - host: null, - name: custom[1], + host: reacterHost || null, + name, }); - if (emoji) return reaction; + if (emoji) return reacterHost ? `:${name}@${reacterHost}:` : `:${name}:` } return await getFallbackReaction(); } +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 + }; +} + export function convertLegacyReaction(reaction: string): string { + reaction = decodeReaction(reaction).reaction; if (Object.keys(legacies).includes(reaction)) return legacies[reaction]; return reaction; } -- cgit v1.2.3-freya