summaryrefslogtreecommitdiff
path: root/packages/backend/src/core
diff options
context:
space:
mode:
authorsyuilo <Syuilotan@yahoo.co.jp>2023-01-27 11:44:14 +0900
committersyuilo <Syuilotan@yahoo.co.jp>2023-01-27 11:44:14 +0900
commitf4bee24ccf7fd1ceaf9184a6d6dd62d4a285f812 (patch)
tree19ae57b7388a7a9d87851ccadd0cd1c29d7c0709 /packages/backend/src/core
parentMerge branch 'develop' (diff)
parent13.2.4 (diff)
downloadmisskey-f4bee24ccf7fd1ceaf9184a6d6dd62d4a285f812.tar.gz
misskey-f4bee24ccf7fd1ceaf9184a6d6dd62d4a285f812.tar.bz2
misskey-f4bee24ccf7fd1ceaf9184a6d6dd62d4a285f812.zip
Merge branch 'develop'
Diffstat (limited to 'packages/backend/src/core')
-rw-r--r--packages/backend/src/core/CustomEmojiService.ts146
-rw-r--r--packages/backend/src/core/ImageProcessingService.ts32
-rw-r--r--packages/backend/src/core/entities/NoteEntityService.ts8
-rw-r--r--packages/backend/src/core/entities/NotificationEntityService.ts2
-rw-r--r--packages/backend/src/core/entities/UserEntityService.ts1
5 files changed, 182 insertions, 7 deletions
diff --git a/packages/backend/src/core/CustomEmojiService.ts b/packages/backend/src/core/CustomEmojiService.ts
index 1f0b214159..39814e1be6 100644
--- a/packages/backend/src/core/CustomEmojiService.ts
+++ b/packages/backend/src/core/CustomEmojiService.ts
@@ -6,22 +6,35 @@ import { EmojiEntityService } from '@/core/entities/EmojiEntityService.js';
import { GlobalEventService } from '@/core/GlobalEventService.js';
import type { DriveFile } from '@/models/entities/DriveFile.js';
import type { Emoji } from '@/models/entities/Emoji.js';
-import type { EmojisRepository } from '@/models/index.js';
+import type { EmojisRepository, Note } from '@/models/index.js';
import { bindThis } from '@/decorators.js';
+import { Cache } from '@/misc/cache.js';
+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';
@Injectable()
export class CustomEmojiService {
+ private cache: Cache<Emoji | null>;
+
constructor(
+ @Inject(DI.config)
+ private config: Config,
+
@Inject(DI.db)
private db: DataSource,
@Inject(DI.emojisRepository)
private emojisRepository: EmojisRepository,
+ private utilityService: UtilityService,
private idService: IdService,
private emojiEntityService: EmojiEntityService,
private globalEventService: GlobalEventService,
+ private reactionService: ReactionService,
) {
+ this.cache = new Cache<Emoji | null>(1000 * 60 * 60 * 12);
}
@bindThis
@@ -44,12 +57,135 @@ export class CustomEmojiService {
type: data.driveFile.webpublicType ?? data.driveFile.type,
}).then(x => this.emojisRepository.findOneByOrFail(x.identifiers[0]));
- await this.db.queryResultCache!.remove(['meta_emojis']);
+ if (data.host == null) {
+ await this.db.queryResultCache!.remove(['meta_emojis']);
- this.globalEventService.publishBroadcastStream('emojiAdded', {
- emoji: await this.emojiEntityService.pack(emoji.id),
- });
+ this.globalEventService.publishBroadcastStream('emojiAdded', {
+ emoji: await this.emojiEntityService.pack(emoji.id),
+ });
+ }
return emoji;
}
+
+ @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なので??はだめ)
+ const url = isLocal
+ ? emojiUrl
+ : this.config.proxyRemoteFiles
+ ? `${this.config.url}/proxy/${encodeURIComponent((new URL(emojiUrl)).pathname)}?${query({ url: emojiUrl })}`
+ : emojiUrl;
+
+ 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)));
+ if (note.renote.user) {
+ emojis = emojis.concat(note.renote.user.emojis
+ .map(e => this.parseEmojiStr(e, note.renote!.userHost)));
+ }
+ }
+ 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
+ .map(e => this.parseEmojiStr(e, note.userHost)));
+ }
+ }
+ 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);
+ }
+ }
}
diff --git a/packages/backend/src/core/ImageProcessingService.ts b/packages/backend/src/core/ImageProcessingService.ts
index 312189eea4..fbc02f504b 100644
--- a/packages/backend/src/core/ImageProcessingService.ts
+++ b/packages/backend/src/core/ImageProcessingService.ts
@@ -9,6 +9,14 @@ export type IImage = {
type: string;
};
+export type IImageStream = {
+ data: Readable;
+ ext: string | null;
+ type: string;
+};
+
+export type IImageStreamable = IImage | IImageStream;
+
export const webpDefault: sharp.WebpOptions = {
quality: 85,
alphaQuality: 95,
@@ -19,6 +27,7 @@ export const webpDefault: sharp.WebpOptions = {
};
import { bindThis } from '@/decorators.js';
+import { Readable } from 'node:stream';
@Injectable()
export class ImageProcessingService {
@@ -64,7 +73,7 @@ export class ImageProcessingService {
*/
@bindThis
public async convertToWebp(path: string, width: number, height: number, options: sharp.WebpOptions = webpDefault): Promise<IImage> {
- return this.convertSharpToWebp(await sharp(path), width, height, options);
+ return this.convertSharpToWebp(sharp(path), width, height, options);
}
@bindThis
@@ -85,6 +94,27 @@ export class ImageProcessingService {
};
}
+ @bindThis
+ public convertToWebpStream(path: string, width: number, height: number, options: sharp.WebpOptions = webpDefault): IImageStream {
+ return this.convertSharpToWebpStream(sharp(path), width, height, options);
+ }
+
+ @bindThis
+ public convertSharpToWebpStream(sharp: sharp.Sharp, width: number, height: number, options: sharp.WebpOptions = webpDefault): IImageStream {
+ const data = sharp
+ .resize(width, height, {
+ fit: 'inside',
+ withoutEnlargement: true,
+ })
+ .rotate()
+ .webp(options)
+
+ return {
+ data,
+ ext: 'webp',
+ type: 'image/webp',
+ };
+ }
/**
* Convert to PNG
* with resize, remove metadata, resolve orientation, stop animation
diff --git a/packages/backend/src/core/entities/NoteEntityService.ts b/packages/backend/src/core/entities/NoteEntityService.ts
index 2b179643f3..bd6971adb3 100644
--- a/packages/backend/src/core/entities/NoteEntityService.ts
+++ b/packages/backend/src/core/entities/NoteEntityService.ts
@@ -282,7 +282,9 @@ export class NoteEntityService implements OnModuleInit {
: await this.channelsRepository.findOneBy({ id: note.channelId })
: null;
- const reactionEmojiNames = Object.keys(note.reactions).filter(x => x.startsWith(':')).map(x => this.reactionService.decodeReaction(x).reaction).map(x => x.replace(/:/g, ''));
+ const reactionEmojiNames = Object.keys(note.reactions)
+ .filter(x => x.startsWith(':') && x.includes('@') && !x.includes('@.')) // リモートカスタム絵文字のみ
+ .map(x => this.reactionService.decodeReaction(x).reaction.replaceAll(':', ''));
const packed: Packed<'Note'> = await awaitAll({
id: note.id,
@@ -299,6 +301,8 @@ export class NoteEntityService implements OnModuleInit {
renoteCount: note.renoteCount,
repliesCount: note.repliesCount,
reactions: this.reactionService.convertLegacyReactions(note.reactions),
+ reactionEmojis: this.customEmojiService.populateEmojis(reactionEmojiNames, host),
+ emojis: host != null ? this.customEmojiService.populateEmojis(note.emojis, host) : undefined,
tags: note.tags.length > 0 ? note.tags : undefined,
fileIds: note.fileIds,
files: this.driveFileEntityService.packMany(note.fileIds),
@@ -384,6 +388,8 @@ export class NoteEntityService implements OnModuleInit {
}
}
+ await this.customEmojiService.prefetchEmojis(this.customEmojiService.aggregateNoteEmojis(notes));
+
return await Promise.all(notes.map(n => this.pack(n, me, {
...options,
_hint_: {
diff --git a/packages/backend/src/core/entities/NotificationEntityService.ts b/packages/backend/src/core/entities/NotificationEntityService.ts
index a8210eea02..ded1b512a1 100644
--- a/packages/backend/src/core/entities/NotificationEntityService.ts
+++ b/packages/backend/src/core/entities/NotificationEntityService.ts
@@ -146,6 +146,8 @@ export class NotificationEntityService implements OnModuleInit {
myReactionsMap.set(target, myReactions.find(reaction => reaction.noteId === target) ?? null);
}
+ await this.customEmojiService.prefetchEmojis(this.customEmojiService.aggregateNoteEmojis(notes));
+
return await Promise.all(notifications.map(x => this.pack(x, {
_hintForEachNotes_: {
myReactions: myReactionsMap,
diff --git a/packages/backend/src/core/entities/UserEntityService.ts b/packages/backend/src/core/entities/UserEntityService.ts
index f532b5bf6e..546e61a26e 100644
--- a/packages/backend/src/core/entities/UserEntityService.ts
+++ b/packages/backend/src/core/entities/UserEntityService.ts
@@ -413,6 +413,7 @@ export class UserEntityService implements OnModuleInit {
faviconUrl: instance.faviconUrl,
themeColor: instance.themeColor,
} : undefined) : undefined,
+ emojis: this.customEmojiService.populateEmojis(user.emojis, user.host),
onlineStatus: this.getOnlineStatus(user),
...(opts.detail ? {