sharkey/packages/backend/src/core/CustomEmojiService.ts

176 lines
6.4 KiB
TypeScript
Raw Normal View History

2022-09-18 03:27:08 +09:00
import { Inject, Injectable } from '@nestjs/common';
import { DataSource, In, IsNull } from 'typeorm';
import { GlobalEventService } from '@/core/GlobalEventService.js';
import { DI } from '@/di-symbols.js';
2022-09-21 05:33:11 +09:00
import type { Config } from '@/config.js';
2022-09-18 03:27:08 +09:00
import { IdService } from '@/core/IdService.js';
import type { DriveFile } from '@/models/entities/DriveFile.js';
import type { Emoji } from '@/models/entities/Emoji.js';
import { Cache } from '@/misc/cache.js';
import { query } from '@/misc/prelude/url.js';
import type { Note } from '@/models/entities/Note.js';
2022-09-21 05:33:11 +09:00
import type { EmojisRepository } from '@/models/index.js';
2022-09-18 03:27:08 +09:00
import { UtilityService } from './UtilityService.js';
import { ReactionService } from './ReactionService.js';
/**
*
*/
type PopulatedEmoji = {
name: string;
url: string;
};
@Injectable()
export class CustomEmojiService {
2022-09-19 03:11:50 +09:00
private cache: Cache<Emoji | null>;
2022-09-18 03:27:08 +09:00
constructor(
@Inject(DI.config)
private config: Config,
@Inject(DI.db)
private db: DataSource,
@Inject(DI.emojisRepository)
private emojisRepository: EmojisRepository,
private idService: IdService,
private globalEventServie: GlobalEventService,
private utilityService: UtilityService,
private reactionService: ReactionService,
) {
2022-09-19 03:11:50 +09:00
this.cache = new Cache<Emoji | null>(1000 * 60 * 60 * 12);
2022-09-18 03:27:08 +09:00
}
public async add(data: {
driveFile: DriveFile;
name: string;
category: string | null;
aliases: string[];
host: string | null;
}): 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,
}).then(x => this.emojisRepository.findOneByOrFail(x.identifiers[0]));
await this.db.queryResultCache!.remove(['meta_emojis']);
return emoji;
}
2022-09-19 03:11:50 +09:00
private normalizeHost(src: string | undefined, noteUserHost: string | null): string | null {
2022-09-18 03:27:08 +09:00
// クエリに使うホスト
let host = src === '.' ? null // .はローカルホスト (ここがマッチするのはリアクションのみ)
: src === undefined ? noteUserHost // ノートなどでホスト省略表記の場合はローカルホスト (ここがリアクションにマッチすることはない)
: this.utilityService.isSelfHost(src) ? null // 自ホスト指定
: (src || noteUserHost); // 指定されたホスト || ノートなどの所有者のホスト (こっちがリアクションにマッチすることはない)
host = this.utilityService.toPunyNullable(host);
return host;
}
2022-09-19 03:11:50 +09:00
private parseEmojiStr(emojiName: string, noteUserHost: string | null) {
2022-09-18 03:27:08 +09:00
const match = emojiName.match(/^(\w+)(?:@([\w.-]+))?$/);
if (!match) return { name: null, host: null };
const name = match[1];
// ホスト正規化
2022-09-19 03:11:50 +09:00
const host = this.utilityService.toPunyNullable(this.normalizeHost(match[2], noteUserHost));
2022-09-18 03:27:08 +09:00
return { name, host };
}
/**
*
* @param emojiName (:, @. (decodeReactionで可能))
* @param noteUserHost
* @returns , nullは未マッチを意味する
*/
public async populateEmoji(emojiName: string, noteUserHost: string | null): Promise<PopulatedEmoji | null> {
2022-09-19 03:11:50 +09:00
const { name, host } = this.parseEmojiStr(emojiName, noteUserHost);
2022-09-18 03:27:08 +09:00
if (name == null) return null;
const queryOrNull = async () => (await this.emojisRepository.findOneBy({
name,
host: host ?? IsNull(),
})) ?? null;
2022-09-19 03:11:50 +09:00
const emoji = await this.cache.fetch(`${name} ${host}`, queryOrNull);
2022-09-18 03:27:08 +09:00
if (emoji == null) return null;
const isLocal = emoji.host == null;
const emojiUrl = emoji.publicUrl || emoji.originalUrl; // || emoji.originalUrl してるのは後方互換性のため
const url = isLocal ? emojiUrl : `${this.config.url}/proxy/${encodeURIComponent((new URL(emojiUrl)).pathname)}?${query({ url: emojiUrl })}`;
return {
name: emojiName,
url,
};
}
/**
* (, )
*/
public async populateEmojis(emojiNames: string[], noteUserHost: string | null): Promise<PopulatedEmoji[]> {
const emojis = await Promise.all(emojiNames.map(x => this.populateEmoji(x, noteUserHost)));
return emojis.filter((x): x is PopulatedEmoji => x != null);
}
public aggregateNoteEmojis(notes: Note[]) {
let emojis: { name: string | null; host: string | null; }[] = [];
for (const note of notes) {
emojis = emojis.concat(note.emojis
2022-09-19 03:11:50 +09:00
.map(e => this.parseEmojiStr(e, note.userHost)));
2022-09-18 03:27:08 +09:00
if (note.renote) {
emojis = emojis.concat(note.renote.emojis
2022-09-19 03:11:50 +09:00
.map(e => this.parseEmojiStr(e, note.renote!.userHost)));
2022-09-18 03:27:08 +09:00
if (note.renote.user) {
emojis = emojis.concat(note.renote.user.emojis
2022-09-19 03:11:50 +09:00
.map(e => this.parseEmojiStr(e, note.renote!.userHost)));
2022-09-18 03:27:08 +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);
if (note.user) {
emojis = emojis.concat(note.user.emojis
2022-09-19 03:11:50 +09:00
.map(e => this.parseEmojiStr(e, note.userHost)));
2022-09-18 03:27:08 +09:00
}
}
return emojis.filter(x => x.name != null) as { name: string; host: string | null; }[];
}
/**
*
*/
public async prefetchEmojis(emojis: { name: string; host: string | null; }[]): Promise<void> {
2022-09-19 03:11:50 +09:00
const notCachedEmojis = emojis.filter(emoji => this.cache.get(`${emoji.name} ${emoji.host}`) == null);
2022-09-18 03:27:08 +09:00
const emojisQuery: any[] = [];
const hosts = new Set(notCachedEmojis.map(e => e.host));
for (const host of hosts) {
emojisQuery.push({
name: In(notCachedEmojis.filter(e => e.host === host).map(e => e.name)),
host: host ?? IsNull(),
});
}
const _emojis = emojisQuery.length > 0 ? await this.emojisRepository.find({
where: emojisQuery,
select: ['name', 'host', 'originalUrl', 'publicUrl'],
}) : [];
for (const emoji of _emojis) {
2022-09-19 03:11:50 +09:00
this.cache.set(`${emoji.name} ${emoji.host}`, emoji);
2022-09-18 03:27:08 +09:00
}
}
}