2023-07-27 07:31:52 +02:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import { ModuleRef } from '@nestjs/core';
|
2023-04-04 07:06:57 +02:00
|
|
|
import { In } from 'typeorm';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { DI } from '@/di-symbols.js';
|
2023-10-31 03:45:03 +01:00
|
|
|
import type { FollowRequestsRepository, NotesRepository, MiUser, UsersRepository } from '@/models/_.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { awaitAll } from '@/misc/prelude/await-all.js';
|
2023-11-02 07:57:55 +01:00
|
|
|
import type { MiGroupedNotification, MiNotification } from '@/models/Notification.js';
|
2023-09-20 04:33:36 +02:00
|
|
|
import type { MiNote } from '@/models/Note.js';
|
2023-03-10 06:22:37 +01:00
|
|
|
import type { Packed } from '@/misc/json-schema.js';
|
2022-12-31 00:43:13 +01:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2023-03-03 12:43:31 +01:00
|
|
|
import { isNotNull } from '@/misc/is-not-null.js';
|
2023-11-02 07:57:55 +01:00
|
|
|
import { FilterUnionByProperty, notificationTypes } from '@/types.js';
|
2023-12-21 02:39:11 +01:00
|
|
|
import { RoleEntityService } from './RoleEntityService.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
import type { OnModuleInit } from '@nestjs/common';
|
|
|
|
import type { UserEntityService } from './UserEntityService.js';
|
|
|
|
import type { NoteEntityService } from './NoteEntityService.js';
|
|
|
|
|
2023-09-21 11:48:15 +02:00
|
|
|
const NOTE_REQUIRED_NOTIFICATION_TYPES = new Set(['note', 'mention', 'reply', 'renote', 'quote', 'reaction', 'pollEnded'] as (typeof notificationTypes[number])[]);
|
2023-11-02 07:57:55 +01:00
|
|
|
const NOTE_REQUIRED_GROUPED_NOTIFICATION_TYPES = new Set(['note', 'mention', 'reply', 'renote', 'renote:grouped', 'quote', 'reaction', 'reaction:grouped', 'pollEnded']);
|
2023-03-03 12:43:31 +01:00
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
@Injectable()
|
|
|
|
export class NotificationEntityService implements OnModuleInit {
|
|
|
|
private userEntityService: UserEntityService;
|
|
|
|
private noteEntityService: NoteEntityService;
|
2023-12-21 02:39:11 +01:00
|
|
|
private roleEntityService: RoleEntityService;
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
constructor(
|
|
|
|
private moduleRef: ModuleRef,
|
|
|
|
|
2023-04-04 07:06:57 +02:00
|
|
|
@Inject(DI.notesRepository)
|
|
|
|
private notesRepository: NotesRepository,
|
|
|
|
|
|
|
|
@Inject(DI.usersRepository)
|
|
|
|
private usersRepository: UsersRepository,
|
2022-09-17 20:27:08 +02:00
|
|
|
|
2023-05-02 05:14:06 +02:00
|
|
|
@Inject(DI.followRequestsRepository)
|
|
|
|
private followRequestsRepository: FollowRequestsRepository,
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
//private userEntityService: UserEntityService,
|
|
|
|
//private noteEntityService: NoteEntityService,
|
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
|
|
|
onModuleInit() {
|
|
|
|
this.userEntityService = this.moduleRef.get('UserEntityService');
|
|
|
|
this.noteEntityService = this.moduleRef.get('NoteEntityService');
|
2023-12-21 02:39:11 +01:00
|
|
|
this.roleEntityService = this.moduleRef.get('RoleEntityService');
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2022-09-17 20:27:08 +02:00
|
|
|
public async pack(
|
2023-08-16 10:51:28 +02:00
|
|
|
src: MiNotification,
|
|
|
|
meId: MiUser['id'],
|
2023-04-04 10:32:09 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
2022-09-17 20:27:08 +02:00
|
|
|
options: {
|
2023-07-08 00:08:16 +02:00
|
|
|
|
2023-04-04 07:06:57 +02:00
|
|
|
},
|
|
|
|
hint?: {
|
2023-08-16 10:51:28 +02:00
|
|
|
packedNotes: Map<MiNote['id'], Packed<'Note'>>;
|
|
|
|
packedUsers: Map<MiUser['id'], Packed<'User'>>;
|
2022-09-17 20:27:08 +02:00
|
|
|
},
|
|
|
|
): Promise<Packed<'Notification'>> {
|
2023-04-04 07:06:57 +02:00
|
|
|
const notification = src;
|
2023-11-02 07:57:55 +01:00
|
|
|
const noteIfNeed = NOTE_REQUIRED_NOTIFICATION_TYPES.has(notification.type) && 'noteId' in notification ? (
|
2023-04-04 07:06:57 +02:00
|
|
|
hint?.packedNotes != null
|
|
|
|
? hint.packedNotes.get(notification.noteId)
|
2023-11-02 07:57:55 +01:00
|
|
|
: this.noteEntityService.pack(notification.noteId, { id: meId }, {
|
2023-03-03 12:43:31 +01:00
|
|
|
detail: true,
|
|
|
|
})
|
|
|
|
) : undefined;
|
2023-11-02 07:57:55 +01:00
|
|
|
const userIfNeed = 'notifierId' in notification ? (
|
2023-04-04 07:06:57 +02:00
|
|
|
hint?.packedUsers != null
|
|
|
|
? hint.packedUsers.get(notification.notifierId)
|
2023-11-02 07:57:55 +01:00
|
|
|
: this.userEntityService.pack(notification.notifierId, { id: meId }, {
|
2023-04-04 07:06:57 +02:00
|
|
|
detail: false,
|
|
|
|
})
|
|
|
|
) : undefined;
|
2023-12-21 02:39:11 +01:00
|
|
|
const role = notification.type === 'roleAssigned' ? await this.roleEntityService.pack(notification.roleId) : undefined;
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
return await awaitAll({
|
|
|
|
id: notification.id,
|
2023-04-04 07:06:57 +02:00
|
|
|
createdAt: new Date(notification.createdAt).toISOString(),
|
2022-09-17 20:27:08 +02:00
|
|
|
type: notification.type,
|
2023-11-02 07:57:55 +01:00
|
|
|
userId: 'notifierId' in notification ? notification.notifierId : undefined,
|
2023-04-04 07:06:57 +02:00
|
|
|
...(userIfNeed != null ? { user: userIfNeed } : {}),
|
2023-03-03 12:43:31 +01:00
|
|
|
...(noteIfNeed != null ? { note: noteIfNeed } : {}),
|
2022-09-17 20:27:08 +02:00
|
|
|
...(notification.type === 'reaction' ? {
|
|
|
|
reaction: notification.reaction,
|
|
|
|
} : {}),
|
2023-12-21 02:39:11 +01:00
|
|
|
...(notification.type === 'roleAssigned' ? {
|
|
|
|
role: role,
|
|
|
|
} : {}),
|
2023-01-21 05:14:55 +01:00
|
|
|
...(notification.type === 'achievementEarned' ? {
|
|
|
|
achievement: notification.achievement,
|
|
|
|
} : {}),
|
2022-09-17 20:27:08 +02:00
|
|
|
...(notification.type === 'app' ? {
|
|
|
|
body: notification.customBody,
|
2023-10-31 03:45:03 +01:00
|
|
|
header: notification.customHeader,
|
|
|
|
icon: notification.customIcon,
|
2022-09-17 20:27:08 +02:00
|
|
|
} : {}),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2022-09-17 20:27:08 +02:00
|
|
|
public async packMany(
|
2023-08-16 10:51:28 +02:00
|
|
|
notifications: MiNotification[],
|
|
|
|
meId: MiUser['id'],
|
2022-09-17 20:27:08 +02:00
|
|
|
) {
|
|
|
|
if (notifications.length === 0) return [];
|
2023-03-03 12:26:44 +01:00
|
|
|
|
2023-04-06 08:09:21 +02:00
|
|
|
let validNotifications = notifications;
|
|
|
|
|
2023-11-02 07:57:55 +01:00
|
|
|
const noteIds = validNotifications.map(x => 'noteId' in x ? x.noteId : null).filter(isNotNull);
|
2023-04-04 07:06:57 +02:00
|
|
|
const notes = noteIds.length > 0 ? await this.notesRepository.find({
|
|
|
|
where: { id: In(noteIds) },
|
2023-04-06 12:48:24 +02:00
|
|
|
relations: ['user', 'reply', 'reply.user', 'renote', 'renote.user'],
|
2023-04-04 07:06:57 +02:00
|
|
|
}) : [];
|
2023-03-03 12:43:31 +01:00
|
|
|
const packedNotesArray = await this.noteEntityService.packMany(notes, { id: meId }, {
|
|
|
|
detail: true,
|
|
|
|
});
|
|
|
|
const packedNotes = new Map(packedNotesArray.map(p => [p.id, p]));
|
2023-01-26 07:48:12 +01:00
|
|
|
|
2023-11-02 07:57:55 +01:00
|
|
|
validNotifications = validNotifications.filter(x => !('noteId' in x) || packedNotes.has(x.noteId));
|
2023-04-06 08:09:21 +02:00
|
|
|
|
2023-11-02 07:57:55 +01:00
|
|
|
const userIds = validNotifications.map(x => 'notifierId' in x ? x.notifierId : null).filter(isNotNull);
|
2023-04-04 07:06:57 +02:00
|
|
|
const users = userIds.length > 0 ? await this.usersRepository.find({
|
|
|
|
where: { id: In(userIds) },
|
|
|
|
}) : [];
|
|
|
|
const packedUsersArray = await this.userEntityService.packMany(users, { id: meId }, {
|
|
|
|
detail: false,
|
|
|
|
});
|
|
|
|
const packedUsers = new Map(packedUsersArray.map(p => [p.id, p]));
|
|
|
|
|
2023-05-02 05:14:06 +02:00
|
|
|
// 既に解決されたフォローリクエストの通知を除外
|
2023-11-02 07:57:55 +01:00
|
|
|
const followRequestNotifications = validNotifications.filter((x): x is FilterUnionByProperty<MiGroupedNotification, 'type', 'receiveFollowRequest'> => x.type === 'receiveFollowRequest');
|
2023-05-02 05:14:06 +02:00
|
|
|
if (followRequestNotifications.length > 0) {
|
|
|
|
const reqs = await this.followRequestsRepository.find({
|
2023-11-02 07:57:55 +01:00
|
|
|
where: { followerId: In(followRequestNotifications.map(x => x.notifierId)) },
|
2023-05-02 05:14:06 +02:00
|
|
|
});
|
|
|
|
validNotifications = validNotifications.filter(x => (x.type !== 'receiveFollowRequest') || reqs.some(r => r.followerId === x.notifierId));
|
|
|
|
}
|
|
|
|
|
2023-04-06 08:09:21 +02:00
|
|
|
return await Promise.all(validNotifications.map(x => this.pack(x, meId, {}, {
|
2023-04-04 07:06:57 +02:00
|
|
|
packedNotes,
|
|
|
|
packedUsers,
|
2022-09-17 20:27:08 +02:00
|
|
|
})));
|
|
|
|
}
|
2023-11-02 07:57:55 +01:00
|
|
|
|
|
|
|
@bindThis
|
|
|
|
public async packGrouped(
|
|
|
|
src: MiGroupedNotification,
|
|
|
|
meId: MiUser['id'],
|
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
|
|
options: {
|
|
|
|
|
|
|
|
},
|
|
|
|
hint?: {
|
|
|
|
packedNotes: Map<MiNote['id'], Packed<'Note'>>;
|
|
|
|
packedUsers: Map<MiUser['id'], Packed<'User'>>;
|
|
|
|
},
|
|
|
|
): Promise<Packed<'Notification'>> {
|
|
|
|
const notification = src;
|
|
|
|
const noteIfNeed = NOTE_REQUIRED_GROUPED_NOTIFICATION_TYPES.has(notification.type) && 'noteId' in notification ? (
|
|
|
|
hint?.packedNotes != null
|
|
|
|
? hint.packedNotes.get(notification.noteId)
|
|
|
|
: this.noteEntityService.pack(notification.noteId, { id: meId }, {
|
|
|
|
detail: true,
|
|
|
|
})
|
|
|
|
) : undefined;
|
|
|
|
const userIfNeed = 'notifierId' in notification ? (
|
|
|
|
hint?.packedUsers != null
|
|
|
|
? hint.packedUsers.get(notification.notifierId)
|
|
|
|
: this.userEntityService.pack(notification.notifierId, { id: meId }, {
|
|
|
|
detail: false,
|
|
|
|
})
|
|
|
|
) : undefined;
|
|
|
|
|
|
|
|
if (notification.type === 'reaction:grouped') {
|
|
|
|
const reactions = await Promise.all(notification.reactions.map(async reaction => {
|
|
|
|
const user = hint?.packedUsers != null
|
|
|
|
? hint.packedUsers.get(reaction.userId)!
|
|
|
|
: await this.userEntityService.pack(reaction.userId, { id: meId }, {
|
|
|
|
detail: false,
|
|
|
|
});
|
|
|
|
return {
|
|
|
|
user,
|
|
|
|
reaction: reaction.reaction,
|
|
|
|
};
|
|
|
|
}));
|
|
|
|
return await awaitAll({
|
|
|
|
id: notification.id,
|
|
|
|
createdAt: new Date(notification.createdAt).toISOString(),
|
|
|
|
type: notification.type,
|
|
|
|
note: noteIfNeed,
|
|
|
|
reactions,
|
|
|
|
});
|
|
|
|
} else if (notification.type === 'renote:grouped') {
|
|
|
|
const users = await Promise.all(notification.userIds.map(userId => {
|
2023-11-21 03:13:56 +01:00
|
|
|
const packedUser = hint?.packedUsers != null ? hint.packedUsers.get(userId) : null;
|
|
|
|
if (packedUser) {
|
|
|
|
return packedUser;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.userEntityService.pack(userId, { id: meId }, {
|
|
|
|
detail: false,
|
|
|
|
});
|
2023-11-02 07:57:55 +01:00
|
|
|
}));
|
|
|
|
return await awaitAll({
|
|
|
|
id: notification.id,
|
|
|
|
createdAt: new Date(notification.createdAt).toISOString(),
|
|
|
|
type: notification.type,
|
|
|
|
note: noteIfNeed,
|
|
|
|
users,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return await awaitAll({
|
|
|
|
id: notification.id,
|
|
|
|
createdAt: new Date(notification.createdAt).toISOString(),
|
|
|
|
type: notification.type,
|
|
|
|
userId: 'notifierId' in notification ? notification.notifierId : undefined,
|
|
|
|
...(userIfNeed != null ? { user: userIfNeed } : {}),
|
|
|
|
...(noteIfNeed != null ? { note: noteIfNeed } : {}),
|
|
|
|
...(notification.type === 'reaction' ? {
|
|
|
|
reaction: notification.reaction,
|
|
|
|
} : {}),
|
|
|
|
...(notification.type === 'achievementEarned' ? {
|
|
|
|
achievement: notification.achievement,
|
|
|
|
} : {}),
|
|
|
|
...(notification.type === 'app' ? {
|
|
|
|
body: notification.customBody,
|
|
|
|
header: notification.customHeader,
|
|
|
|
icon: notification.customIcon,
|
|
|
|
} : {}),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@bindThis
|
|
|
|
public async packGroupedMany(
|
|
|
|
notifications: MiGroupedNotification[],
|
|
|
|
meId: MiUser['id'],
|
|
|
|
) {
|
|
|
|
if (notifications.length === 0) return [];
|
|
|
|
|
|
|
|
let validNotifications = notifications;
|
|
|
|
|
|
|
|
const noteIds = validNotifications.map(x => 'noteId' in x ? x.noteId : null).filter(isNotNull);
|
|
|
|
const notes = noteIds.length > 0 ? await this.notesRepository.find({
|
|
|
|
where: { id: In(noteIds) },
|
|
|
|
relations: ['user', 'reply', 'reply.user', 'renote', 'renote.user'],
|
|
|
|
}) : [];
|
|
|
|
const packedNotesArray = await this.noteEntityService.packMany(notes, { id: meId }, {
|
|
|
|
detail: true,
|
|
|
|
});
|
|
|
|
const packedNotes = new Map(packedNotesArray.map(p => [p.id, p]));
|
|
|
|
|
|
|
|
validNotifications = validNotifications.filter(x => !('noteId' in x) || packedNotes.has(x.noteId));
|
|
|
|
|
|
|
|
const userIds = [];
|
|
|
|
for (const notification of validNotifications) {
|
|
|
|
if ('notifierId' in notification) userIds.push(notification.notifierId);
|
|
|
|
if (notification.type === 'reaction:grouped') userIds.push(...notification.reactions.map(x => x.userId));
|
|
|
|
if (notification.type === 'renote:grouped') userIds.push(...notification.userIds);
|
|
|
|
}
|
|
|
|
const users = userIds.length > 0 ? await this.usersRepository.find({
|
|
|
|
where: { id: In(userIds) },
|
|
|
|
}) : [];
|
|
|
|
const packedUsersArray = await this.userEntityService.packMany(users, { id: meId }, {
|
|
|
|
detail: false,
|
|
|
|
});
|
|
|
|
const packedUsers = new Map(packedUsersArray.map(p => [p.id, p]));
|
|
|
|
|
|
|
|
// 既に解決されたフォローリクエストの通知を除外
|
|
|
|
const followRequestNotifications = validNotifications.filter((x): x is FilterUnionByProperty<MiGroupedNotification, 'type', 'receiveFollowRequest'> => x.type === 'receiveFollowRequest');
|
|
|
|
if (followRequestNotifications.length > 0) {
|
|
|
|
const reqs = await this.followRequestsRepository.find({
|
|
|
|
where: { followerId: In(followRequestNotifications.map(x => x.notifierId)) },
|
|
|
|
});
|
|
|
|
validNotifications = validNotifications.filter(x => (x.type !== 'receiveFollowRequest') || reqs.some(r => r.followerId === x.notifierId));
|
|
|
|
}
|
|
|
|
|
|
|
|
return await Promise.all(validNotifications.map(x => this.packGrouped(x, meId, {}, {
|
|
|
|
packedNotes,
|
|
|
|
packedUsers,
|
|
|
|
})));
|
|
|
|
}
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|