diff options
| author | MeiMei <30769358+mei23@users.noreply.github.com> | 2020-04-14 00:42:59 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-04-14 00:42:59 +0900 |
| commit | 9b07c5af05a8be8114af860893d68614e4ee5ca2 (patch) | |
| tree | 3ca2587fe8e4d0bb845d2f42f443e21d224c4d6f /src/misc | |
| parent | chore(client): :art: (diff) | |
| download | sharkey-9b07c5af05a8be8114af860893d68614e4ee5ca2.tar.gz sharkey-9b07c5af05a8be8114af860893d68614e4ee5ca2.tar.bz2 sharkey-9b07c5af05a8be8114af860893d68614e4ee5ca2.zip | |
リモートのカスタム絵文字リアクションを表示できるように (#6239)
* リモートのカスタム絵文字リアクションを表示できるように
* AP
* DBマイグレーション
* ローカルのリアクションの.
* fix
* fix
* fix
* space
Diffstat (limited to 'src/misc')
| -rw-r--r-- | src/misc/reaction-lib.ts | 59 |
1 files changed, 54 insertions, 5 deletions
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<string, string> = { 'like': '👍', @@ -40,12 +41,20 @@ export function convertLegacyReactions(reactions: Record<string, number>) { } } - return _reactions; + const _reactions2 = {} as Record<string, number>; + + for (const reaction of Object.keys(_reactions)) { + _reactions2[decodeReaction(reaction).reaction] = _reactions[reaction]; + } + + return _reactions2; } -export async function toDbReaction(reaction?: string | null): Promise<string> { +export async function toDbReaction(reaction?: string | null, reacterHost?: string | null): Promise<string> { 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<string> { 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; } |