リモートのカスタム絵文字リアクションを表示できるように (#6239)

* リモートのカスタム絵文字リアクションを表示できるように

* AP

* DBマイグレーション

* ローカルのリアクションの.

* fix

* fix

* fix

* space
This commit is contained in:
MeiMei 2020-04-14 00:42:59 +09:00 committed by GitHub
parent cda1803e59
commit 9b07c5af05
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 185 additions and 41 deletions

View file

@ -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;
}