2022-09-18 03:27:08 +09:00
|
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
|
import { DataSource, In, IsNull } from 'typeorm';
|
|
|
|
|
import { DI } from '@/di-symbols.js';
|
|
|
|
|
import { IdService } from '@/core/IdService.js';
|
2023-01-22 23:53:24 +09:00
|
|
|
|
import { EmojiEntityService } from '@/core/entities/EmojiEntityService.js';
|
|
|
|
|
import { GlobalEventService } from '@/core/GlobalEventService.js';
|
2022-09-18 03:27:08 +09:00
|
|
|
|
import type { DriveFile } from '@/models/entities/DriveFile.js';
|
|
|
|
|
import type { Emoji } from '@/models/entities/Emoji.js';
|
2023-01-26 15:48:12 +09:00
|
|
|
|
import type { EmojisRepository, Note } from '@/models/index.js';
|
2022-12-04 17:05:32 +09:00
|
|
|
|
import { bindThis } from '@/decorators.js';
|
2023-04-04 15:56:47 +09:00
|
|
|
|
import { MemoryKVCache } from '@/misc/cache.js';
|
2023-01-26 15:48:12 +09:00
|
|
|
|
import { UtilityService } from '@/core/UtilityService.js';
|
|
|
|
|
import type { Config } from '@/config.js';
|
|
|
|
|
import { ReactionService } from '@/core/ReactionService.js';
|
|
|
|
|
import { query } from '@/misc/prelude/url.js';
|
2022-09-18 03:27:08 +09:00
|
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class CustomEmojiService {
|
2023-04-04 15:56:47 +09:00
|
|
|
|
private cache: MemoryKVCache<Emoji | null>;
|
2023-01-26 15:48:12 +09:00
|
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
|
constructor(
|
2023-01-26 15:48:12 +09:00
|
|
|
|
@Inject(DI.config)
|
|
|
|
|
private config: Config,
|
|
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
|
@Inject(DI.db)
|
|
|
|
|
private db: DataSource,
|
|
|
|
|
|
|
|
|
|
@Inject(DI.emojisRepository)
|
|
|
|
|
private emojisRepository: EmojisRepository,
|
|
|
|
|
|
2023-01-26 15:48:12 +09:00
|
|
|
|
private utilityService: UtilityService,
|
2022-09-18 03:27:08 +09:00
|
|
|
|
private idService: IdService,
|
2023-01-22 23:53:24 +09:00
|
|
|
|
private emojiEntityService: EmojiEntityService,
|
|
|
|
|
private globalEventService: GlobalEventService,
|
2023-01-26 15:48:12 +09:00
|
|
|
|
private reactionService: ReactionService,
|
2022-09-18 03:27:08 +09:00
|
|
|
|
) {
|
2023-04-04 15:56:47 +09:00
|
|
|
|
this.cache = new MemoryKVCache<Emoji | null>(1000 * 60 * 60 * 12);
|
2022-09-18 03:27:08 +09:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-04 15:03:09 +09:00
|
|
|
|
@bindThis
|
2022-09-18 03:27:08 +09:00
|
|
|
|
public async add(data: {
|
|
|
|
|
driveFile: DriveFile;
|
|
|
|
|
name: string;
|
|
|
|
|
category: string | null;
|
|
|
|
|
aliases: string[];
|
|
|
|
|
host: string | null;
|
2023-03-16 15:08:48 +09:00
|
|
|
|
license: string | null;
|
2022-09-18 03:27:08 +09:00
|
|
|
|
}): Promise<Emoji> {
|
|
|
|
|
const emoji = await this.emojisRepository.insert({
|
|
|
|
|
id: this.idService.genId(),
|
|
|
|
|
updatedAt: new Date(),
|
|
|
|
|
name: data.name,
|
|
|
|
|
category: data.category,
|
|
|
|
|
host: data.host,
|
|
|
|
|
aliases: data.aliases,
|
|
|
|
|
originalUrl: data.driveFile.url,
|
|
|
|
|
publicUrl: data.driveFile.webpublicUrl ?? data.driveFile.url,
|
|
|
|
|
type: data.driveFile.webpublicType ?? data.driveFile.type,
|
2023-03-16 15:08:48 +09:00
|
|
|
|
license: data.license,
|
2022-09-18 03:27:08 +09:00
|
|
|
|
}).then(x => this.emojisRepository.findOneByOrFail(x.identifiers[0]));
|
|
|
|
|
|
2023-01-26 14:29:28 +09:00
|
|
|
|
if (data.host == null) {
|
2023-03-14 09:37:14 +09:00
|
|
|
|
await this.db.queryResultCache?.remove(['meta_emojis']);
|
2022-09-18 03:27:08 +09:00
|
|
|
|
|
2023-01-26 14:29:28 +09:00
|
|
|
|
this.globalEventService.publishBroadcastStream('emojiAdded', {
|
2023-02-17 15:36:36 +09:00
|
|
|
|
emoji: await this.emojiEntityService.packDetailed(emoji.id),
|
2023-01-26 14:29:28 +09:00
|
|
|
|
});
|
|
|
|
|
}
|
2023-01-22 23:53:24 +09:00
|
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
|
return emoji;
|
|
|
|
|
}
|
2023-01-26 15:48:12 +09:00
|
|
|
|
|
|
|
|
|
@bindThis
|
|
|
|
|
private normalizeHost(src: string | undefined, noteUserHost: string | null): string | null {
|
|
|
|
|
// クエリに使うホスト
|
|
|
|
|
let host = src === '.' ? null // .はローカルホスト (ここがマッチするのはリアクションのみ)
|
|
|
|
|
: src === undefined ? noteUserHost // ノートなどでホスト省略表記の場合はローカルホスト (ここがリアクションにマッチすることはない)
|
|
|
|
|
: this.utilityService.isSelfHost(src) ? null // 自ホスト指定
|
|
|
|
|
: (src || noteUserHost); // 指定されたホスト || ノートなどの所有者のホスト (こっちがリアクションにマッチすることはない)
|
|
|
|
|
|
|
|
|
|
host = this.utilityService.toPunyNullable(host);
|
|
|
|
|
|
|
|
|
|
return host;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@bindThis
|
|
|
|
|
private parseEmojiStr(emojiName: string, noteUserHost: string | null) {
|
|
|
|
|
const match = emojiName.match(/^(\w+)(?:@([\w.-]+))?$/);
|
|
|
|
|
if (!match) return { name: null, host: null };
|
|
|
|
|
|
|
|
|
|
const name = match[1];
|
|
|
|
|
|
|
|
|
|
// ホスト正規化
|
|
|
|
|
const host = this.utilityService.toPunyNullable(this.normalizeHost(match[2], noteUserHost));
|
|
|
|
|
|
|
|
|
|
return { name, host };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 添付用(リモート)カスタム絵文字URLを解決する
|
|
|
|
|
* @param emojiName ノートやユーザープロフィールに添付された、またはリアクションのカスタム絵文字名 (:は含めない, リアクションでローカルホストの場合は@.を付ける (これはdecodeReactionで可能))
|
|
|
|
|
* @param noteUserHost ノートやユーザープロフィールの所有者のホスト
|
|
|
|
|
* @returns URL, nullは未マッチを意味する
|
|
|
|
|
*/
|
|
|
|
|
@bindThis
|
|
|
|
|
public async populateEmoji(emojiName: string, noteUserHost: string | null): Promise<string | null> {
|
|
|
|
|
const { name, host } = this.parseEmojiStr(emojiName, noteUserHost);
|
|
|
|
|
if (name == null) return null;
|
|
|
|
|
if (host == null) return null;
|
|
|
|
|
|
|
|
|
|
const queryOrNull = async () => (await this.emojisRepository.findOneBy({
|
|
|
|
|
name,
|
|
|
|
|
host: host ?? IsNull(),
|
|
|
|
|
})) ?? null;
|
|
|
|
|
|
|
|
|
|
const emoji = await this.cache.fetch(`${name} ${host}`, queryOrNull);
|
|
|
|
|
|
|
|
|
|
if (emoji == null) return null;
|
|
|
|
|
|
|
|
|
|
const isLocal = emoji.host == null;
|
|
|
|
|
const emojiUrl = emoji.publicUrl || emoji.originalUrl; // || emoji.originalUrl してるのは後方互換性のため(publicUrlはstringなので??はだめ)
|
2023-01-26 18:44:43 +09:00
|
|
|
|
const url = isLocal
|
|
|
|
|
? emojiUrl
|
|
|
|
|
: this.config.proxyRemoteFiles
|
2023-02-04 13:38:51 +09:00
|
|
|
|
? `${this.config.mediaProxy}/emoji.webp?${query({ url: emojiUrl })}`
|
2023-01-26 18:44:43 +09:00
|
|
|
|
: emojiUrl;
|
2023-01-26 15:48:12 +09:00
|
|
|
|
|
|
|
|
|
return url;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 複数の添付用(リモート)カスタム絵文字URLを解決する (キャシュ付き, 存在しないものは結果から除外される)
|
|
|
|
|
*/
|
|
|
|
|
@bindThis
|
|
|
|
|
public async populateEmojis(emojiNames: string[], noteUserHost: string | null): Promise<Record<string, string>> {
|
|
|
|
|
const emojis = await Promise.all(emojiNames.map(x => this.populateEmoji(x, noteUserHost)));
|
|
|
|
|
const res = {} as any;
|
|
|
|
|
for (let i = 0; i < emojiNames.length; i++) {
|
|
|
|
|
if (emojis[i] != null) {
|
|
|
|
|
res[emojiNames[i]] = emojis[i];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@bindThis
|
|
|
|
|
public aggregateNoteEmojis(notes: Note[]) {
|
|
|
|
|
let emojis: { name: string | null; host: string | null; }[] = [];
|
|
|
|
|
for (const note of notes) {
|
|
|
|
|
emojis = emojis.concat(note.emojis
|
|
|
|
|
.map(e => this.parseEmojiStr(e, note.userHost)));
|
|
|
|
|
if (note.renote) {
|
|
|
|
|
emojis = emojis.concat(note.renote.emojis
|
|
|
|
|
.map(e => this.parseEmojiStr(e, note.renote!.userHost)));
|
2023-02-08 20:07:19 +09:00
|
|
|
|
if (note.renote.user) {
|
|
|
|
|
emojis = emojis.concat(note.renote.user.emojis
|
|
|
|
|
.map(e => this.parseEmojiStr(e, note.renote!.userHost)));
|
|
|
|
|
}
|
2023-01-26 15:48:12 +09:00
|
|
|
|
}
|
|
|
|
|
const customReactions = Object.keys(note.reactions).map(x => this.reactionService.decodeReaction(x)).filter(x => x.name != null) as typeof emojis;
|
|
|
|
|
emojis = emojis.concat(customReactions);
|
2023-02-08 20:07:19 +09:00
|
|
|
|
if (note.user) {
|
|
|
|
|
emojis = emojis.concat(note.user.emojis
|
|
|
|
|
.map(e => this.parseEmojiStr(e, note.userHost)));
|
|
|
|
|
}
|
2023-01-26 15:48:12 +09:00
|
|
|
|
}
|
|
|
|
|
return emojis.filter(x => x.name != null && x.host != null) as { name: string; host: string; }[];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 与えられた絵文字のリストをデータベースから取得し、キャッシュに追加します
|
|
|
|
|
*/
|
|
|
|
|
@bindThis
|
|
|
|
|
public async prefetchEmojis(emojis: { name: string; host: string | null; }[]): Promise<void> {
|
|
|
|
|
const notCachedEmojis = emojis.filter(emoji => this.cache.get(`${emoji.name} ${emoji.host}`) == null);
|
|
|
|
|
const emojisQuery: any[] = [];
|
|
|
|
|
const hosts = new Set(notCachedEmojis.map(e => e.host));
|
|
|
|
|
for (const host of hosts) {
|
|
|
|
|
if (host == null) continue;
|
|
|
|
|
emojisQuery.push({
|
|
|
|
|
name: In(notCachedEmojis.filter(e => e.host === host).map(e => e.name)),
|
|
|
|
|
host: host,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
const _emojis = emojisQuery.length > 0 ? await this.emojisRepository.find({
|
|
|
|
|
where: emojisQuery,
|
|
|
|
|
select: ['name', 'host', 'originalUrl', 'publicUrl'],
|
|
|
|
|
}) : [];
|
|
|
|
|
for (const emoji of _emojis) {
|
|
|
|
|
this.cache.set(`${emoji.name} ${emoji.host}`, emoji);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-09-18 03:27:08 +09:00
|
|
|
|
}
|