2023-01-12 21:40:33 -07:00
|
|
|
import { emojiRegex } from "./emoji-regex.js";
|
|
|
|
import { fetchMeta } from "./fetch-meta.js";
|
|
|
|
import { Emojis } from "@/models/index.js";
|
|
|
|
import { toPunyNullable } from "./convert-host.js";
|
|
|
|
import { IsNull } from "typeorm";
|
2019-03-17 09:03:57 -06:00
|
|
|
|
2023-02-17 01:01:22 -07:00
|
|
|
const legacies = new Map([
|
2023-02-17 14:01:14 -07:00
|
|
|
["like", "👍"],
|
|
|
|
["love", "❤️"],
|
|
|
|
["laugh", "😆"],
|
|
|
|
["hmm", "🤔"],
|
|
|
|
["surprise", "😮"],
|
|
|
|
["congrats", "🎉"],
|
|
|
|
["angry", "💢"],
|
|
|
|
["confused", "😥"],
|
|
|
|
["rip", "😇"],
|
|
|
|
["pudding", "🍮"],
|
|
|
|
["star", "⭐"],
|
2023-02-17 01:01:22 -07:00
|
|
|
]);
|
|
|
|
|
|
|
|
export async function getFallbackReaction() {
|
2019-03-17 10:03:35 -06:00
|
|
|
const meta = await fetchMeta();
|
2023-01-04 21:06:48 -07:00
|
|
|
return meta.defaultReaction;
|
2019-03-17 09:03:57 -06:00
|
|
|
}
|
|
|
|
|
2020-02-18 14:36:50 -07:00
|
|
|
export function convertLegacyReactions(reactions: Record<string, number>) {
|
2023-02-17 01:01:22 -07:00
|
|
|
const _reactions = new Map();
|
|
|
|
const decodedReactions = new Map();
|
2020-02-18 14:36:50 -07:00
|
|
|
|
2023-02-17 01:01:22 -07:00
|
|
|
for (const reaction in reactions) {
|
2020-04-15 09:45:43 -06:00
|
|
|
if (reactions[reaction] <= 0) continue;
|
|
|
|
|
2023-02-17 01:01:22 -07:00
|
|
|
let decodedReaction;
|
|
|
|
if (decodedReactions.has(reaction)) {
|
|
|
|
decodedReaction = decodedReactions.get(reaction);
|
2020-02-18 14:36:50 -07:00
|
|
|
} else {
|
2023-02-17 01:01:22 -07:00
|
|
|
decodedReaction = decodeReaction(reaction);
|
|
|
|
decodedReactions.set(reaction, decodedReaction);
|
2020-02-18 14:36:50 -07:00
|
|
|
}
|
|
|
|
|
2023-02-17 01:01:22 -07:00
|
|
|
let emoji = legacies.get(decodedReaction.reaction);
|
|
|
|
if (emoji) {
|
|
|
|
_reactions.set(emoji, (_reactions.get(emoji) || 0) + reactions[reaction]);
|
|
|
|
} else {
|
2023-02-17 14:01:14 -07:00
|
|
|
_reactions.set(
|
|
|
|
reaction,
|
|
|
|
(_reactions.get(reaction) || 0) + reactions[reaction],
|
|
|
|
);
|
2023-02-17 01:01:22 -07:00
|
|
|
}
|
|
|
|
}
|
2020-04-13 09:42:59 -06:00
|
|
|
|
2023-02-17 01:01:22 -07:00
|
|
|
const _reactions2 = new Map();
|
|
|
|
for (const [reaction, count] of _reactions) {
|
|
|
|
const decodedReaction = decodedReactions.get(reaction);
|
|
|
|
_reactions2.set(decodedReaction.reaction, count);
|
2020-04-13 09:42:59 -06:00
|
|
|
}
|
|
|
|
|
2023-02-17 01:01:22 -07:00
|
|
|
return Object.fromEntries(_reactions2);
|
2020-02-18 14:36:50 -07:00
|
|
|
}
|
|
|
|
|
2023-01-12 21:40:33 -07:00
|
|
|
export async function toDbReaction(
|
|
|
|
reaction?: string | null,
|
|
|
|
reacterHost?: string | null,
|
|
|
|
): Promise<string> {
|
2023-02-17 01:01:22 -07:00
|
|
|
if (!reaction) return await getFallbackReaction();
|
2019-03-17 09:03:57 -06:00
|
|
|
|
2020-04-13 09:42:59 -06:00
|
|
|
reacterHost = toPunyNullable(reacterHost);
|
|
|
|
|
2023-02-03 18:30:43 -07:00
|
|
|
// Convert string-type reactions to unicode
|
2023-02-17 01:01:22 -07:00
|
|
|
const emoji = legacies.get(reaction) || (reaction === "♥️" ? "❤️" : null);
|
|
|
|
if (emoji) return emoji;
|
2019-03-17 09:03:57 -06:00
|
|
|
|
2023-02-05 04:36:50 -07:00
|
|
|
// Allow unicode reactions
|
|
|
|
const match = emojiRegex.exec(reaction);
|
|
|
|
if (match) {
|
|
|
|
const unicode = match[0];
|
|
|
|
return unicode;
|
|
|
|
}
|
|
|
|
|
2020-04-15 09:47:17 -06:00
|
|
|
const custom = reaction.match(/^:([\w+-]+)(?:@\.)?:$/);
|
2019-03-17 09:03:57 -06:00
|
|
|
if (custom) {
|
2020-04-13 09:42:59 -06:00
|
|
|
const name = custom[1];
|
2022-03-26 00:34:00 -06:00
|
|
|
const emoji = await Emojis.findOneBy({
|
2023-02-17 01:01:22 -07:00
|
|
|
host: reacterHost || IsNull(),
|
2020-04-13 09:42:59 -06:00
|
|
|
name,
|
2019-03-17 09:03:57 -06:00
|
|
|
});
|
|
|
|
|
2020-05-10 02:25:16 -06:00
|
|
|
if (emoji) return reacterHost ? `:${name}@${reacterHost}:` : `:${name}:`;
|
2019-03-17 09:03:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return await getFallbackReaction();
|
|
|
|
}
|
2020-01-29 12:37:25 -07:00
|
|
|
|
2020-04-13 09:42:59 -06:00
|
|
|
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 {
|
2023-01-12 21:40:33 -07:00
|
|
|
reaction: `:${name}@${host || "."}:`, // ローカル分は@以降を省略するのではなく.にする
|
2020-04-13 09:42:59 -06:00
|
|
|
name,
|
2021-12-09 07:58:30 -07:00
|
|
|
host,
|
2020-04-13 09:42:59 -06:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
reaction: str,
|
|
|
|
name: undefined,
|
2021-12-09 07:58:30 -07:00
|
|
|
host: undefined,
|
2020-04-13 09:42:59 -06:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-01-29 12:37:25 -07:00
|
|
|
export function convertLegacyReaction(reaction: string): string {
|
2023-02-17 14:01:14 -07:00
|
|
|
const decoded = decodeReaction(reaction).reaction;
|
|
|
|
if (legacies.has(decoded)) return legacies.get(decoded)!;
|
|
|
|
return decoded;
|
2020-01-29 12:37:25 -07:00
|
|
|
}
|