firefish/packages/backend/src/misc/populate-emojis.ts

190 lines
5.6 KiB
TypeScript
Raw Normal View History

import { In, IsNull } from "typeorm";
import { Emojis } from "@/models/index.js";
2023-01-13 04:40:33 +00:00
import type { Emoji } from "@/models/entities/emoji.js";
import type { Note } from "@/models/entities/note.js";
import { Cache } from "./cache.js";
import { decodeReaction, isSelfHost, toPuny } from "backend-rs";
import config from "@/config/index.js";
import { query } from "@/prelude/url.js";
import { redisClient } from "@/db/redis.js";
2024-04-01 03:01:59 +00:00
import type { NoteEdit } from "@/models/entities/note-edit.js";
2023-07-03 02:10:33 +00:00
const cache = new Cache<Emoji | null>("populateEmojis", 60 * 60 * 12);
/**
*
*/
type PopulatedEmoji = {
name: string;
url: string;
2023-05-20 02:26:13 +00:00
width: number | null;
height: number | null;
};
2023-01-13 04:40:33 +00:00
function normalizeHost(
src: string | undefined,
noteUserHost: string | null,
): string | null {
2021-03-22 03:36:57 +00:00
// クエリに使うホスト
const host =
2023-01-13 04:40:33 +00:00
src === "."
? null // .はローカルホスト (ここがマッチするのはリアクションのみ)
: src === undefined
? noteUserHost // ノートなどでホスト省略表記の場合はローカルホスト (ここがリアクションにマッチすることはない)
: isSelfHost(src)
? null // 自ホスト指定
: src || noteUserHost; // 指定されたホスト || ノートなどの所有者のホスト (こっちがリアクションにマッチすることはない)
2021-03-22 03:36:57 +00:00
return host == null ? null : toPuny(host);
2021-03-22 03:36:57 +00:00
}
function parseEmojiStr(emojiName: string, noteUserHost: string | null) {
const match = emojiName.match(/^(\w+)(?:@([\w.-]+))?$/);
if (!match) return { name: null, host: null };
const name = match[1];
return { name, host: normalizeHost(match[2], noteUserHost) };
2021-03-22 03:36:57 +00:00
}
/**
*
* @param emojiName (:, @. (decodeReactionで可能))
2021-03-21 15:45:14 +00:00
* @param noteUserHost
* @returns , nullは未マッチを意味する
*/
2023-01-13 04:40:33 +00:00
export async function populateEmoji(
emojiName: string,
noteUserHost: string | null,
): Promise<PopulatedEmoji | null> {
2021-03-22 03:36:57 +00:00
const { name, host } = parseEmojiStr(emojiName, noteUserHost);
if (name == null) return null;
2023-01-13 04:40:33 +00:00
const queryOrNull = async () =>
(await Emojis.findOneBy({
name,
host: host ?? IsNull(),
})) || null;
2023-05-20 02:26:13 +00:00
const cacheKey = `${name} ${host}`;
let emoji = await cache.fetch(cacheKey, queryOrNull);
if (emoji && !(emoji.width && emoji.height)) {
emoji = await queryOrNull();
2023-07-03 00:37:46 +00:00
await cache.set(cacheKey, emoji);
2023-05-20 02:26:13 +00:00
}
if (emoji == null) return null;
const isLocal = emoji.host == null;
const emojiUrl = emoji.publicUrl || emoji.originalUrl; // || emoji.originalUrl してるのは後方互換性のため
2023-01-13 04:40:33 +00:00
const url = isLocal
? emojiUrl
: `${config.url}/proxy/${encodeURIComponent(
new URL(emojiUrl).pathname,
)}?${query({ url: emojiUrl })}`;
return {
name: emojiName,
url,
2023-05-20 02:26:13 +00:00
width: emoji.width,
height: emoji.height,
};
}
/**
* (, )
*/
2023-01-13 04:40:33 +00:00
export async function populateEmojis(
emojiNames: string[],
noteUserHost: string | null,
): Promise<PopulatedEmoji[]> {
const emojis = await Promise.all(
emojiNames.map((x) => populateEmoji(x, noteUserHost)),
);
return emojis.filter((x): x is PopulatedEmoji => x != null);
}
2024-04-01 03:01:59 +00:00
export function aggregateNoteEditEmojis(
noteEdits: NoteEdit[],
sourceHost: string | null,
) {
let emojis: string[] = [];
for (const noteEdit of noteEdits) {
emojis = emojis.concat(noteEdit.emojis);
}
emojis = Array.from(new Set(emojis));
return emojis
.map((e) => parseEmojiStr(e, sourceHost))
.filter((x) => x.name != null) as {
name: string;
host: string | null;
}[];
}
2021-03-22 03:41:33 +00:00
export function aggregateNoteEmojis(notes: Note[]) {
2023-01-13 04:40:33 +00:00
let emojis: { name: string | null; host: string | null }[] = [];
2021-03-22 03:41:33 +00:00
for (const note of notes) {
2023-01-13 04:40:33 +00:00
emojis = emojis.concat(
note.emojis.map((e) => parseEmojiStr(e, note.userHost)),
);
2021-03-22 03:41:33 +00:00
if (note.renote) {
2023-01-13 04:40:33 +00:00
emojis = emojis.concat(
note.renote.emojis.map((e) => parseEmojiStr(e, note.renote!.userHost)),
);
2021-03-22 03:41:33 +00:00
if (note.renote.user) {
2023-01-13 04:40:33 +00:00
emojis = emojis.concat(
note.renote.user.emojis.map((e) =>
parseEmojiStr(e, note.renote!.userHost),
),
);
2021-03-22 03:41:33 +00:00
}
}
2023-01-13 04:40:33 +00:00
const customReactions = Object.keys(note.reactions)
.map((x) => decodeReaction(x))
.filter((x) => x.name != null) as typeof emojis;
2021-03-22 03:41:33 +00:00
emojis = emojis.concat(customReactions);
if (note.user) {
2023-01-13 04:40:33 +00:00
emojis = emojis.concat(
note.user.emojis.map((e) => parseEmojiStr(e, note.userHost)),
);
2021-03-22 03:41:33 +00:00
}
}
2023-01-13 04:40:33 +00:00
return emojis.filter((x) => x.name != null) as {
name: string;
host: string | null;
}[];
2021-03-22 03:41:33 +00:00
}
/**
2024-04-01 03:01:59 +00:00
* Get the given list of emojis from the database and adds them to the cache
2021-03-22 03:41:33 +00:00
*/
2023-01-13 04:40:33 +00:00
export async function prefetchEmojis(
emojis: { name: string; host: string | null }[],
): Promise<void> {
const notCachedEmojis = emojis.filter(
2023-07-03 00:37:46 +00:00
async (emoji) => !(await cache.get(`${emoji.name} ${emoji.host}`)),
2023-01-13 04:40:33 +00:00
);
2021-03-22 03:41:33 +00:00
const emojisQuery: any[] = [];
2023-01-13 04:40:33 +00:00
const hosts = new Set(notCachedEmojis.map((e) => e.host));
2021-03-22 03:41:33 +00:00
for (const host of hosts) {
emojisQuery.push({
2023-01-13 04:40:33 +00:00
name: In(
notCachedEmojis.filter((e) => e.host === host).map((e) => e.name),
),
host: host ?? IsNull(),
2021-03-22 03:41:33 +00:00
});
}
2023-01-13 04:40:33 +00:00
const _emojis =
emojisQuery.length > 0
? await Emojis.find({
where: emojisQuery,
select: ["name", "host", "originalUrl", "publicUrl"],
})
2023-01-13 04:40:33 +00:00
: [];
2023-07-03 00:37:46 +00:00
const trans = redisClient.multi();
2021-03-22 03:41:33 +00:00
for (const emoji of _emojis) {
2023-07-03 00:37:46 +00:00
cache.set(`${emoji.name} ${emoji.host}`, emoji, trans);
2021-03-22 03:41:33 +00:00
}
2023-07-03 00:37:46 +00:00
await trans.exec();
2021-03-22 03:41:33 +00:00
}