summaryrefslogtreecommitdiff
path: root/packages/backend/src
diff options
context:
space:
mode:
authorsyuilo <Syuilotan@yahoo.co.jp>2023-03-03 20:43:31 +0900
committersyuilo <Syuilotan@yahoo.co.jp>2023-03-03 20:43:31 +0900
commitad7ecbaf376d87f5f870572129aeee334e65443b (patch)
tree1bd646ff66ff410f2b06a6a04e98ac86c4420bea /packages/backend/src
parentRevert "fix(server): DriveFile related N+1 query when call note packMany (#10... (diff)
downloadmisskey-ad7ecbaf376d87f5f870572129aeee334e65443b.tar.gz
misskey-ad7ecbaf376d87f5f870572129aeee334e65443b.tar.bz2
misskey-ad7ecbaf376d87f5f870572129aeee334e65443b.zip
通知部分は残す
Co-Authored-By: rinsuki <428rinsuki+contact.github@gmail.com>
Diffstat (limited to 'packages/backend/src')
-rw-r--r--packages/backend/src/core/entities/NotificationEntityService.ts84
-rw-r--r--packages/backend/src/misc/is-not-null.ts5
2 files changed, 35 insertions, 54 deletions
diff --git a/packages/backend/src/core/entities/NotificationEntityService.ts b/packages/backend/src/core/entities/NotificationEntityService.ts
index 33c76c6937..be88a213f4 100644
--- a/packages/backend/src/core/entities/NotificationEntityService.ts
+++ b/packages/backend/src/core/entities/NotificationEntityService.ts
@@ -1,19 +1,21 @@
import { Inject, Injectable } from '@nestjs/common';
-import { In } from 'typeorm';
import { ModuleRef } from '@nestjs/core';
import { DI } from '@/di-symbols.js';
import type { AccessTokensRepository, NoteReactionsRepository, NotificationsRepository, User } from '@/models/index.js';
import { awaitAll } from '@/misc/prelude/await-all.js';
import type { Notification } from '@/models/entities/Notification.js';
-import type { NoteReaction } from '@/models/entities/NoteReaction.js';
import type { Note } from '@/models/entities/Note.js';
import type { Packed } from '@/misc/schema.js';
import { bindThis } from '@/decorators.js';
+import { isNotNull } from '@/misc/is-not-null.js';
+import { notificationTypes } from '@/types.js';
import type { OnModuleInit } from '@nestjs/common';
import type { CustomEmojiService } from '../CustomEmojiService.js';
import type { UserEntityService } from './UserEntityService.js';
import type { NoteEntityService } from './NoteEntityService.js';
+const NOTE_REQUIRED_NOTIFICATION_TYPES = new Set(['mention', 'reply', 'renote', 'quote', 'reaction', 'pollEnded'] as (typeof notificationTypes[number])[]);
+
@Injectable()
export class NotificationEntityService implements OnModuleInit {
private userEntityService: UserEntityService;
@@ -48,13 +50,20 @@ export class NotificationEntityService implements OnModuleInit {
public async pack(
src: Notification['id'] | Notification,
options: {
- _hintForEachNotes_?: {
- myReactions: Map<Note['id'], NoteReaction | null>;
+ _hint_?: {
+ packedNotes: Map<Note['id'], Packed<'Note'>>;
};
},
): Promise<Packed<'Notification'>> {
const notification = typeof src === 'object' ? src : await this.notificationsRepository.findOneByOrFail({ id: src });
const token = notification.appAccessTokenId ? await this.accessTokensRepository.findOneByOrFail({ id: notification.appAccessTokenId }) : null;
+ const noteIfNeed = NOTE_REQUIRED_NOTIFICATION_TYPES.has(notification.type) && notification.noteId != null ? (
+ options._hint_?.packedNotes != null
+ ? options._hint_.packedNotes.get(notification.noteId)
+ : this.noteEntityService.pack(notification.note ?? notification.noteId!, { id: notification.notifieeId }, {
+ detail: true,
+ })
+ ) : undefined;
return await awaitAll({
id: notification.id,
@@ -63,43 +72,10 @@ export class NotificationEntityService implements OnModuleInit {
isRead: notification.isRead,
userId: notification.notifierId,
user: notification.notifierId ? this.userEntityService.pack(notification.notifier ?? notification.notifierId) : null,
- ...(notification.type === 'mention' ? {
- note: this.noteEntityService.pack(notification.note ?? notification.noteId!, { id: notification.notifieeId }, {
- detail: true,
- _hint_: options._hintForEachNotes_,
- }),
- } : {}),
- ...(notification.type === 'reply' ? {
- note: this.noteEntityService.pack(notification.note ?? notification.noteId!, { id: notification.notifieeId }, {
- detail: true,
- _hint_: options._hintForEachNotes_,
- }),
- } : {}),
- ...(notification.type === 'renote' ? {
- note: this.noteEntityService.pack(notification.note ?? notification.noteId!, { id: notification.notifieeId }, {
- detail: true,
- _hint_: options._hintForEachNotes_,
- }),
- } : {}),
- ...(notification.type === 'quote' ? {
- note: this.noteEntityService.pack(notification.note ?? notification.noteId!, { id: notification.notifieeId }, {
- detail: true,
- _hint_: options._hintForEachNotes_,
- }),
- } : {}),
+ ...(noteIfNeed != null ? { note: noteIfNeed } : {}),
...(notification.type === 'reaction' ? {
- note: this.noteEntityService.pack(notification.note ?? notification.noteId!, { id: notification.notifieeId }, {
- detail: true,
- _hint_: options._hintForEachNotes_,
- }),
reaction: notification.reaction,
} : {}),
- ...(notification.type === 'pollEnded' ? {
- note: this.noteEntityService.pack(notification.note ?? notification.noteId!, { id: notification.notifieeId }, {
- detail: true,
- _hint_: options._hintForEachNotes_,
- }),
- } : {}),
...(notification.type === 'achievementEarned' ? {
achievement: notification.achievement,
} : {}),
@@ -111,32 +87,32 @@ export class NotificationEntityService implements OnModuleInit {
});
}
+ /**
+ * @param notifications you should join "note" property when fetch from DB, and all notifieeId should be same as meId
+ */
@bindThis
public async packMany(
notifications: Notification[],
meId: User['id'],
) {
if (notifications.length === 0) return [];
-
- const notes = notifications.filter(x => x.note != null).map(x => x.note!);
- const noteIds = notes.map(n => n.id);
- const myReactionsMap = new Map<Note['id'], NoteReaction | null>();
- const renoteIds = notes.filter(n => n.renoteId != null).map(n => n.renoteId!);
- const targets = [...noteIds, ...renoteIds];
- const myReactions = await this.noteReactionsRepository.findBy({
- userId: meId,
- noteId: In(targets),
- });
-
- for (const target of targets) {
- myReactionsMap.set(target, myReactions.find(reaction => reaction.noteId === target) ?? null);
+
+ for (const notification of notifications) {
+ if (meId !== notification.notifieeId) {
+ // because we call note packMany with meId, all notifieeId should be same as meId
+ throw new Error('TRY_TO_PACK_ANOTHER_USER_NOTIFICATION');
+ }
}
- await this.customEmojiService.prefetchEmojis(this.customEmojiService.aggregateNoteEmojis(notes));
+ const notes = notifications.map(x => x.note).filter(isNotNull);
+ const packedNotesArray = await this.noteEntityService.packMany(notes, { id: meId }, {
+ detail: true,
+ });
+ const packedNotes = new Map(packedNotesArray.map(p => [p.id, p]));
return await Promise.all(notifications.map(x => this.pack(x, {
- _hintForEachNotes_: {
- myReactions: myReactionsMap,
+ _hint_: {
+ packedNotes,
},
})));
}
diff --git a/packages/backend/src/misc/is-not-null.ts b/packages/backend/src/misc/is-not-null.ts
new file mode 100644
index 0000000000..d89a1957be
--- /dev/null
+++ b/packages/backend/src/misc/is-not-null.ts
@@ -0,0 +1,5 @@
+// we are using {} as "any non-nullish value" as expected
+// eslint-disable-next-line @typescript-eslint/ban-types
+export function isNotNull<T extends {}>(input: T | undefined | null): input is T {
+ return input != null;
+}