2022-09-17 18:27:08 +00:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
2023-03-03 11:26:44 +00:00
|
|
|
import { In } from 'typeorm';
|
2022-09-17 18:27:08 +00:00
|
|
|
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';
|
2023-03-03 11:26:44 +00:00
|
|
|
import type { NoteReaction } from '@/models/entities/NoteReaction.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
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';
|
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';
|
|
|
|
|
|
|
|
@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:26:44 +00:00
|
|
|
_hintForEachNotes_?: {
|
|
|
|
myReactions: Map<Note['id'], NoteReaction | null>;
|
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;
|
|
|
|
|
|
|
|
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:26:44 +00:00
|
|
|
...(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_,
|
|
|
|
}),
|
|
|
|
} : {}),
|
2022-09-17 18:27:08 +00:00
|
|
|
...(notification.type === 'reaction' ? {
|
2023-03-03 11:26:44 +00:00
|
|
|
note: this.noteEntityService.pack(notification.note ?? notification.noteId!, { id: notification.notifieeId }, {
|
|
|
|
detail: true,
|
|
|
|
_hint_: options._hintForEachNotes_,
|
|
|
|
}),
|
2022-09-17 18:27:08 +00:00
|
|
|
reaction: notification.reaction,
|
|
|
|
} : {}),
|
2023-03-03 11:26:44 +00:00
|
|
|
...(notification.type === 'pollEnded' ? {
|
|
|
|
note: this.noteEntityService.pack(notification.note ?? notification.noteId!, { id: notification.notifieeId }, {
|
|
|
|
detail: true,
|
|
|
|
_hint_: options._hintForEachNotes_,
|
|
|
|
}),
|
|
|
|
} : {}),
|
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,
|
|
|
|
} : {}),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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:26:44 +00:00
|
|
|
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),
|
2023-03-03 01:06:59 +00:00
|
|
|
});
|
2023-03-03 11:26:44 +00:00
|
|
|
|
|
|
|
for (const target of targets) {
|
|
|
|
myReactionsMap.set(target, myReactions.find(reaction => reaction.noteId === target) ?? null);
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.customEmojiService.prefetchEmojis(this.customEmojiService.aggregateNoteEmojis(notes));
|
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:26:44 +00:00
|
|
|
_hintForEachNotes_: {
|
|
|
|
myReactions: myReactionsMap,
|
2022-09-17 18:27:08 +00:00
|
|
|
},
|
|
|
|
})));
|
|
|
|
}
|
|
|
|
}
|