2022-09-17 18:27:08 +00:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import { ModuleRef } from '@nestjs/core';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2023-02-01 11:15:11 +00:00
|
|
|
import type { AccessTokensRepository, NoteReactionsRepository, NotificationsRepository, User } from '@/models/index.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
import { awaitAll } from '@/misc/prelude/await-all.js';
|
|
|
|
import type { Notification } from '@/models/entities/Notification.js';
|
|
|
|
import type { Note } from '@/models/entities/Note.js';
|
|
|
|
import type { Packed } from '@/misc/schema.js';
|
2022-12-30 23:43:13 +00:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2023-03-03 11:43:31 +00:00
|
|
|
import { isNotNull } from '@/misc/is-not-null.js';
|
|
|
|
import { notificationTypes } from '@/types.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
import type { OnModuleInit } from '@nestjs/common';
|
|
|
|
import type { CustomEmojiService } from '../CustomEmojiService.js';
|
|
|
|
import type { UserEntityService } from './UserEntityService.js';
|
|
|
|
import type { NoteEntityService } from './NoteEntityService.js';
|
|
|
|
|
2023-03-03 11:43:31 +00:00
|
|
|
const NOTE_REQUIRED_NOTIFICATION_TYPES = new Set(['mention', 'reply', 'renote', 'quote', 'reaction', 'pollEnded'] as (typeof notificationTypes[number])[]);
|
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
@Injectable()
|
|
|
|
export class NotificationEntityService implements OnModuleInit {
|
|
|
|
private userEntityService: UserEntityService;
|
|
|
|
private noteEntityService: NoteEntityService;
|
|
|
|
private customEmojiService: CustomEmojiService;
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
private moduleRef: ModuleRef,
|
|
|
|
|
|
|
|
@Inject(DI.notificationsRepository)
|
|
|
|
private notificationsRepository: NotificationsRepository,
|
|
|
|
|
|
|
|
@Inject(DI.noteReactionsRepository)
|
|
|
|
private noteReactionsRepository: NoteReactionsRepository,
|
|
|
|
|
|
|
|
@Inject(DI.accessTokensRepository)
|
|
|
|
private accessTokensRepository: AccessTokensRepository,
|
|
|
|
|
|
|
|
//private userEntityService: UserEntityService,
|
|
|
|
//private noteEntityService: NoteEntityService,
|
|
|
|
//private customEmojiService: CustomEmojiService,
|
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
|
|
|
onModuleInit() {
|
|
|
|
this.userEntityService = this.moduleRef.get('UserEntityService');
|
|
|
|
this.noteEntityService = this.moduleRef.get('NoteEntityService');
|
|
|
|
this.customEmojiService = this.moduleRef.get('CustomEmojiService');
|
|
|
|
}
|
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2022-09-17 18:27:08 +00:00
|
|
|
public async pack(
|
|
|
|
src: Notification['id'] | Notification,
|
|
|
|
options: {
|
2023-03-03 11:43:31 +00:00
|
|
|
_hint_?: {
|
|
|
|
packedNotes: Map<Note['id'], Packed<'Note'>>;
|
2022-09-17 18:27:08 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
): 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;
|
2023-03-03 11:43:31 +00:00
|
|
|
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;
|
2022-09-17 18:27:08 +00:00
|
|
|
|
|
|
|
return await awaitAll({
|
|
|
|
id: notification.id,
|
|
|
|
createdAt: notification.createdAt.toISOString(),
|
|
|
|
type: notification.type,
|
|
|
|
isRead: notification.isRead,
|
|
|
|
userId: notification.notifierId,
|
|
|
|
user: notification.notifierId ? this.userEntityService.pack(notification.notifier ?? notification.notifierId) : null,
|
2023-03-03 11:43:31 +00:00
|
|
|
...(noteIfNeed != null ? { note: noteIfNeed } : {}),
|
2022-09-17 18:27:08 +00:00
|
|
|
...(notification.type === 'reaction' ? {
|
|
|
|
reaction: notification.reaction,
|
|
|
|
} : {}),
|
2023-01-21 04:14:55 +00:00
|
|
|
...(notification.type === 'achievementEarned' ? {
|
|
|
|
achievement: notification.achievement,
|
|
|
|
} : {}),
|
2022-09-17 18:27:08 +00:00
|
|
|
...(notification.type === 'app' ? {
|
|
|
|
body: notification.customBody,
|
|
|
|
header: notification.customHeader ?? token?.name,
|
|
|
|
icon: notification.customIcon ?? token?.iconUrl,
|
|
|
|
} : {}),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-03-03 11:43:31 +00:00
|
|
|
/**
|
|
|
|
* @param notifications you should join "note" property when fetch from DB, and all notifieeId should be same as meId
|
|
|
|
*/
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2022-09-17 18:27:08 +00:00
|
|
|
public async packMany(
|
|
|
|
notifications: Notification[],
|
|
|
|
meId: User['id'],
|
|
|
|
) {
|
|
|
|
if (notifications.length === 0) return [];
|
2023-03-03 11:43:31 +00:00
|
|
|
|
|
|
|
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');
|
|
|
|
}
|
2023-03-03 11:26:44 +00:00
|
|
|
}
|
|
|
|
|
2023-03-03 11:43:31 +00:00
|
|
|
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]));
|
2023-01-26 06:48:12 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
return await Promise.all(notifications.map(x => this.pack(x, {
|
2023-03-03 11:43:31 +00:00
|
|
|
_hint_: {
|
|
|
|
packedNotes,
|
2022-09-17 18:27:08 +00:00
|
|
|
},
|
|
|
|
})));
|
|
|
|
}
|
|
|
|
}
|