From 061e389340a7e5683be2ba0da4e1a1048597ccb6 Mon Sep 17 00:00:00 2001 From: syuilo Date: Sat, 14 Oct 2023 10:04:13 +0900 Subject: [PATCH 1/2] =?UTF-8?q?perf(backend):=20nyaize=E3=82=92=E6=8A=95?= =?UTF-8?q?=E7=A8=BF=E6=99=82=E3=81=AB=E3=82=84=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Resolve #12030 --- .../backend/src/core/NoteCreateService.ts | 25 ++++++++++++++++++- .../src/core/entities/NoteEntityService.ts | 21 ---------------- 2 files changed, 24 insertions(+), 22 deletions(-) diff --git a/packages/backend/src/core/NoteCreateService.ts b/packages/backend/src/core/NoteCreateService.ts index 64d2880ba1..4496be3e7d 100644 --- a/packages/backend/src/core/NoteCreateService.ts +++ b/packages/backend/src/core/NoteCreateService.ts @@ -55,6 +55,7 @@ import { MetaService } from '@/core/MetaService.js'; import { SearchService } from '@/core/SearchService.js'; import { FeaturedService } from '@/core/FeaturedService.js'; import { RedisTimelineService } from '@/core/RedisTimelineService.js'; +import { nyaize } from '@/misc/nyaize.js'; type NotificationType = 'reply' | 'renote' | 'quote' | 'mention'; @@ -223,7 +224,10 @@ export class NoteCreateService implements OnApplicationShutdown { host: MiUser['host']; createdAt: MiUser['createdAt']; isBot: MiUser['isBot']; + isCat: MiUser['isCat']; }, data: Option, silent = false): Promise { + let patsedText: mfm.MfmNode[] | null = null; + // チャンネル外にリプライしたら対象のスコープに合わせる // (クライアントサイドでやっても良い処理だと思うけどとりあえずサーバーサイドで) if (data.reply && data.channel && data.reply.channelId !== data.channel.id) { @@ -302,6 +306,25 @@ export class NoteCreateService implements OnApplicationShutdown { data.text = data.text.slice(0, DB_MAX_NOTE_TEXT_LENGTH); } data.text = data.text.trim(); + + if (user.isCat) { + patsedText = patsedText ?? mfm.parse(data.text); + function nyaizeNode(node: mfm.MfmNode) { + if (node.type === 'quote') return; + if (node.type === 'text') { + node.props.text = nyaize(node.props.text); + } + if (node.children) { + for (const child of node.children) { + nyaizeNode(child); + } + } + } + for (const node of patsedText) { + nyaizeNode(node); + } + data.text = mfm.toString(patsedText); + } } else { data.text = null; } @@ -312,7 +335,7 @@ export class NoteCreateService implements OnApplicationShutdown { // Parse MFM if needed if (!tags || !emojis || !mentionedUsers) { - const tokens = data.text ? mfm.parse(data.text)! : []; + const tokens = patsedText ?? (data.text ? mfm.parse(data.text)! : []); const cwTokens = data.cw ? mfm.parse(data.cw)! : []; const choiceTokens = data.poll && data.poll.choices ? concat(data.poll.choices.map(choice => mfm.parse(choice)!)) diff --git a/packages/backend/src/core/entities/NoteEntityService.ts b/packages/backend/src/core/entities/NoteEntityService.ts index abe4aafd6e..25132977f2 100644 --- a/packages/backend/src/core/entities/NoteEntityService.ts +++ b/packages/backend/src/core/entities/NoteEntityService.ts @@ -5,11 +5,9 @@ import { Inject, Injectable } from '@nestjs/common'; import { In } from 'typeorm'; -import * as mfm from 'mfm-js'; import { ModuleRef } from '@nestjs/core'; import { DI } from '@/di-symbols.js'; import type { Packed } from '@/misc/json-schema.js'; -import { nyaize } from '@/misc/nyaize.js'; import { awaitAll } from '@/misc/prelude/await-all.js'; import type { MiUser } from '@/models/User.js'; import type { MiNote } from '@/models/Note.js'; @@ -362,25 +360,6 @@ export class NoteEntityService implements OnModuleInit { } : {}), }); - if (packed.user.isCat && packed.text) { - const tokens = packed.text ? mfm.parse(packed.text) : []; - function nyaizeNode(node: mfm.MfmNode) { - if (node.type === 'quote') return; - if (node.type === 'text') { - node.props.text = nyaize(node.props.text); - } - if (node.children) { - for (const child of node.children) { - nyaizeNode(child); - } - } - } - for (const node of tokens) { - nyaizeNode(node); - } - packed.text = mfm.toString(tokens); - } - if (!opts.skipHide) { await this.hideNote(packed, meId); } From 329830e2c3d1d46de712d1cbe4cf3bac51144dcb Mon Sep 17 00:00:00 2001 From: syuilo Date: Sat, 14 Oct 2023 10:05:44 +0900 Subject: [PATCH 2/2] perf(backend): tweak populateMyReaction --- packages/backend/src/core/entities/NoteEntityService.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/backend/src/core/entities/NoteEntityService.ts b/packages/backend/src/core/entities/NoteEntityService.ts index 25132977f2..316367f23a 100644 --- a/packages/backend/src/core/entities/NoteEntityService.ts +++ b/packages/backend/src/core/entities/NoteEntityService.ts @@ -180,8 +180,8 @@ export class NoteEntityService implements OnModuleInit { // 実装上抜けがあるだけかもしれないので、「ヒントに含まれてなかったら(=undefinedなら)return」のようにはしない } - // パフォーマンスのためノートが作成されてから1秒以上経っていない場合はリアクションを取得しない - if (note.createdAt.getTime() + 1000 > Date.now()) { + // パフォーマンスのためノートが作成されてから2秒以上経っていない場合はリアクションを取得しない + if (note.createdAt.getTime() + 2000 > Date.now()) { return undefined; } @@ -382,8 +382,8 @@ export class NoteEntityService implements OnModuleInit { const myReactionsMap = new Map(); if (meId) { const renoteIds = notes.filter(n => n.renoteId != null).map(n => n.renoteId!); - // パフォーマンスのためノートが作成されてから1秒以上経っていない場合はリアクションを取得しない - const targets = [...notes.filter(n => n.createdAt.getTime() + 1000 < Date.now()).map(n => n.id), ...renoteIds]; + // パフォーマンスのためノートが作成されてから2秒以上経っていない場合はリアクションを取得しない + const targets = [...notes.filter(n => n.createdAt.getTime() + 2000 < Date.now()).map(n => n.id), ...renoteIds]; const myReactions = await this.noteReactionsRepository.findBy({ userId: meId, noteId: In(targets),