2023-07-27 07:31:52 +02:00
|
|
|
/*
|
2024-02-13 16:59:27 +01:00
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 07:31:52 +02:00
|
|
|
* 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';
|
2024-02-28 13:26:26 +01:00
|
|
|
import { FilterUnionByProperty, groupedNotificationTypes } from '@/types.js';
|
|
|
|
import { CacheService } from '@/core/CacheService.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';
|
|
|
|
|
2024-02-28 13:26:26 +01:00
|
|
|
const NOTE_REQUIRED_NOTIFICATION_TYPES = new Set(['note', 'mention', 'reply', 'renote', 'renote:grouped', 'quote', 'reaction', 'reaction:grouped', 'pollEnded'] as (typeof groupedNotificationTypes[number])[]);
|
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,
|
|
|
|
|
2024-02-28 13:26:26 +01:00
|
|
|
private cacheService: CacheService,
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-02-28 13:26:26 +01:00
|
|
|
/**
|
|
|
|
* 通知をパックする共通処理
|
|
|
|
*/
|
|
|
|
async #packInternal <T extends MiNotification | MiGroupedNotification> (
|
|
|
|
src: T,
|
2023-08-16 10:51:28 +02:00
|
|
|
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: {
|
2024-02-28 13:26:26 +01:00
|
|
|
checkValidNotifier?: boolean;
|
2023-04-04 07:06:57 +02:00
|
|
|
},
|
|
|
|
hint?: {
|
2023-08-16 10:51:28 +02:00
|
|
|
packedNotes: Map<MiNote['id'], Packed<'Note'>>;
|
2024-01-31 07:45:35 +01:00
|
|
|
packedUsers: Map<MiUser['id'], Packed<'UserLite'>>;
|
2022-09-17 20:27:08 +02:00
|
|
|
},
|
2024-02-28 13:26:26 +01:00
|
|
|
): Promise<Packed<'Notification'> | null> {
|
2023-04-04 07:06:57 +02:00
|
|
|
const notification = src;
|
2023-01-26 07:48:12 +01:00
|
|
|
|
2024-02-28 13:26:26 +01:00
|
|
|
if (options.checkValidNotifier !== false && !(await this.#isValidNotifier(notification, meId))) return null;
|
2023-04-06 08:09:21 +02:00
|
|
|
|
2024-02-28 13:26:26 +01:00
|
|
|
const needsNote = NOTE_REQUIRED_NOTIFICATION_TYPES.has(notification.type) && 'noteId' in notification;
|
|
|
|
const noteIfNeed = needsNote ? (
|
2023-11-02 07:57:55 +01:00
|
|
|
hint?.packedNotes != null
|
|
|
|
? hint.packedNotes.get(notification.noteId)
|
|
|
|
: this.noteEntityService.pack(notification.noteId, { id: meId }, {
|
|
|
|
detail: true,
|
|
|
|
})
|
|
|
|
) : undefined;
|
2024-02-28 13:26:26 +01:00
|
|
|
// if the note has been deleted, don't show this notification
|
|
|
|
if (needsNote && !noteIfNeed) return null;
|
|
|
|
|
|
|
|
const needsUser = 'notifierId' in notification;
|
|
|
|
const userIfNeed = needsUser ? (
|
2023-11-02 07:57:55 +01:00
|
|
|
hint?.packedUsers != null
|
|
|
|
? hint.packedUsers.get(notification.notifierId)
|
2024-01-31 07:45:35 +01:00
|
|
|
: this.userEntityService.pack(notification.notifierId, { id: meId })
|
2023-11-02 07:57:55 +01:00
|
|
|
) : undefined;
|
2024-02-28 13:26:26 +01:00
|
|
|
// if the user has been deleted, don't show this notification
|
|
|
|
if (needsUser && !userIfNeed) return null;
|
2023-11-02 07:57:55 +01:00
|
|
|
|
2024-02-28 13:26:26 +01:00
|
|
|
// #region Grouped notifications
|
2023-11-02 07:57:55 +01:00
|
|
|
if (notification.type === 'reaction:grouped') {
|
2024-02-28 13:26:26 +01:00
|
|
|
const reactions = (await Promise.all(notification.reactions.map(async reaction => {
|
2023-11-02 07:57:55 +01:00
|
|
|
const user = hint?.packedUsers != null
|
|
|
|
? hint.packedUsers.get(reaction.userId)!
|
2024-01-31 07:45:35 +01:00
|
|
|
: await this.userEntityService.pack(reaction.userId, { id: meId });
|
2023-11-02 07:57:55 +01:00
|
|
|
return {
|
|
|
|
user,
|
|
|
|
reaction: reaction.reaction,
|
|
|
|
};
|
2024-06-21 06:03:51 +02:00
|
|
|
}))).filter(r => r.user != null);
|
2024-02-28 13:26:26 +01:00
|
|
|
// if all users have been deleted, don't show this notification
|
|
|
|
if (reactions.length === 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
reactions,
|
|
|
|
});
|
|
|
|
} else if (notification.type === 'renote:grouped') {
|
2024-02-28 13:26:26 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-01-31 07:45:35 +01:00
|
|
|
return this.userEntityService.pack(userId, { id: meId });
|
2024-06-21 06:03:51 +02:00
|
|
|
}))).filter(x => x != null);
|
2024-02-28 13:26:26 +01:00
|
|
|
// if all users have been deleted, don't show this notification
|
|
|
|
if (users.length === 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
});
|
|
|
|
}
|
2024-02-28 13:26:26 +01:00
|
|
|
// #endregion
|
2023-11-02 07:57:55 +01:00
|
|
|
|
2024-02-28 13:26:26 +01:00
|
|
|
const needsRole = notification.type === 'roleAssigned';
|
|
|
|
const role = needsRole ? await this.roleEntityService.pack(notification.roleId) : undefined;
|
|
|
|
// if the role has been deleted, don't show this notification
|
|
|
|
if (needsRole && !role) {
|
|
|
|
return null;
|
|
|
|
}
|
2023-12-21 03:23:31 +01:00
|
|
|
|
2023-11-02 07:57:55 +01:00
|
|
|
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,
|
|
|
|
} : {}),
|
2023-12-21 03:23:31 +01:00
|
|
|
...(notification.type === 'roleAssigned' ? {
|
|
|
|
role: role,
|
|
|
|
} : {}),
|
2023-11-02 07:57:55 +01:00
|
|
|
...(notification.type === 'achievementEarned' ? {
|
|
|
|
achievement: notification.achievement,
|
|
|
|
} : {}),
|
|
|
|
...(notification.type === 'app' ? {
|
|
|
|
body: notification.customBody,
|
|
|
|
header: notification.customHeader,
|
|
|
|
icon: notification.customIcon,
|
|
|
|
} : {}),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-02-28 13:26:26 +01:00
|
|
|
async #packManyInternal <T extends MiNotification | MiGroupedNotification> (
|
|
|
|
notifications: T[],
|
2023-11-02 07:57:55 +01:00
|
|
|
meId: MiUser['id'],
|
2024-02-28 13:26:26 +01:00
|
|
|
): Promise<T[]> {
|
2023-11-02 07:57:55 +01:00
|
|
|
if (notifications.length === 0) return [];
|
|
|
|
|
|
|
|
let validNotifications = notifications;
|
|
|
|
|
2024-02-28 13:26:26 +01:00
|
|
|
validNotifications = await this.#filterValidNotifier(validNotifications, meId);
|
|
|
|
|
2024-06-21 06:03:51 +02:00
|
|
|
const noteIds = validNotifications.map(x => 'noteId' in x ? x.noteId : null).filter(x => x != null);
|
2023-11-02 07:57:55 +01:00
|
|
|
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) },
|
|
|
|
}) : [];
|
2024-01-31 07:45:35 +01:00
|
|
|
const packedUsersArray = await this.userEntityService.packMany(users, { id: meId });
|
2023-11-02 07:57:55 +01:00
|
|
|
const packedUsers = new Map(packedUsersArray.map(p => [p.id, p]));
|
|
|
|
|
|
|
|
// 既に解決されたフォローリクエストの通知を除外
|
2024-02-28 13:26:26 +01:00
|
|
|
const followRequestNotifications = validNotifications.filter((x): x is FilterUnionByProperty<T, 'type', 'receiveFollowRequest'> => x.type === 'receiveFollowRequest');
|
2023-11-02 07:57:55 +01:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2024-02-28 13:26:26 +01:00
|
|
|
const packPromises = validNotifications.map(x => {
|
|
|
|
return this.pack(
|
|
|
|
x,
|
|
|
|
meId,
|
|
|
|
{ checkValidNotifier: false },
|
|
|
|
{ packedNotes, packedUsers },
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2024-06-21 06:03:51 +02:00
|
|
|
return (await Promise.all(packPromises)).filter(x => x != null);
|
2024-02-28 13:26:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@bindThis
|
|
|
|
public async pack(
|
|
|
|
src: MiNotification | MiGroupedNotification,
|
|
|
|
meId: MiUser['id'],
|
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
|
|
options: {
|
|
|
|
checkValidNotifier?: boolean;
|
|
|
|
},
|
|
|
|
hint?: {
|
|
|
|
packedNotes: Map<MiNote['id'], Packed<'Note'>>;
|
|
|
|
packedUsers: Map<MiUser['id'], Packed<'UserLite'>>;
|
|
|
|
},
|
|
|
|
): Promise<Packed<'Notification'> | null> {
|
|
|
|
return await this.#packInternal(src, meId, options, hint);
|
|
|
|
}
|
|
|
|
|
|
|
|
@bindThis
|
|
|
|
public async packMany(
|
|
|
|
notifications: MiNotification[],
|
|
|
|
meId: MiUser['id'],
|
|
|
|
): Promise<MiNotification[]> {
|
|
|
|
return await this.#packManyInternal(notifications, meId);
|
|
|
|
}
|
|
|
|
|
|
|
|
@bindThis
|
|
|
|
public async packGroupedMany(
|
|
|
|
notifications: MiGroupedNotification[],
|
|
|
|
meId: MiUser['id'],
|
|
|
|
): Promise<MiGroupedNotification[]> {
|
|
|
|
return await this.#packManyInternal(notifications, meId);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* notifierが存在するか、ミュートされていないか、サスペンドされていないかを確認するvalidator
|
|
|
|
*/
|
|
|
|
#validateNotifier <T extends MiNotification | MiGroupedNotification> (
|
|
|
|
notification: T,
|
|
|
|
userIdsWhoMeMuting: Set<MiUser['id']>,
|
|
|
|
userMutedInstances: Set<string>,
|
|
|
|
notifiers: MiUser[],
|
|
|
|
): boolean {
|
|
|
|
if (!('notifierId' in notification)) return true;
|
|
|
|
if (userIdsWhoMeMuting.has(notification.notifierId)) return false;
|
|
|
|
|
|
|
|
const notifier = notifiers.find(x => x.id === notification.notifierId) ?? null;
|
|
|
|
|
|
|
|
if (notifier == null) return false;
|
|
|
|
if (notifier.host && userMutedInstances.has(notifier.host)) return false;
|
|
|
|
|
|
|
|
if (notifier.isSuspended) return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* notifierが存在するか、ミュートされていないか、サスペンドされていないかを実際に確認する
|
|
|
|
*/
|
|
|
|
async #isValidNotifier(
|
|
|
|
notification: MiNotification | MiGroupedNotification,
|
|
|
|
meId: MiUser['id'],
|
|
|
|
): Promise<boolean> {
|
|
|
|
return (await this.#filterValidNotifier([notification], meId)).length === 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* notifierが存在するか、ミュートされていないか、サスペンドされていないかを実際に複数確認する
|
|
|
|
*/
|
|
|
|
async #filterValidNotifier <T extends MiNotification | MiGroupedNotification> (
|
|
|
|
notifications: T[],
|
|
|
|
meId: MiUser['id'],
|
|
|
|
): Promise<T[]> {
|
|
|
|
const [
|
|
|
|
userIdsWhoMeMuting,
|
|
|
|
userMutedInstances,
|
|
|
|
] = await Promise.all([
|
|
|
|
this.cacheService.userMutingsCache.fetch(meId),
|
|
|
|
this.cacheService.userProfileCache.fetch(meId).then(p => new Set(p.mutedInstances)),
|
|
|
|
]);
|
|
|
|
|
2024-06-21 06:03:51 +02:00
|
|
|
const notifierIds = notifications.map(notification => 'notifierId' in notification ? notification.notifierId : null).filter(x => x != null);
|
2024-02-28 13:26:26 +01:00
|
|
|
const notifiers = notifierIds.length > 0 ? await this.usersRepository.find({
|
|
|
|
where: { id: In(notifierIds) },
|
|
|
|
}) : [];
|
|
|
|
|
|
|
|
const filteredNotifications = ((await Promise.all(notifications.map(async (notification) => {
|
|
|
|
const isValid = this.#validateNotifier(notification, userIdsWhoMeMuting, userMutedInstances, notifiers);
|
|
|
|
return isValid ? notification : null;
|
2024-06-21 06:03:51 +02:00
|
|
|
}))) as [T | null] ).filter(x => x != null);
|
2024-02-28 13:26:26 +01:00
|
|
|
|
|
|
|
return filteredNotifications;
|
2023-11-02 07:57:55 +01:00
|
|
|
}
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|