Sharkey/packages/backend/src/core/NotificationService.ts

227 lines
8.6 KiB
TypeScript
Raw Normal View History

/*
* SPDX-FileCopyrightText: syuilo and misskey-project
* SPDX-License-Identifier: AGPL-3.0-only
*/
2023-03-16 06:36:21 +01:00
import { setTimeout } from 'node:timers/promises';
2023-04-14 06:50:05 +02:00
import * as Redis from 'ioredis';
import { Inject, Injectable, OnApplicationShutdown } from '@nestjs/common';
2022-09-17 20:27:08 +02:00
import { In } from 'typeorm';
import { DI } from '@/di-symbols.js';
import type { UsersRepository } from '@/models/_.js';
import type { MiUser } from '@/models/User.js';
import type { MiNotification } from '@/models/Notification.js';
2023-02-17 07:15:36 +01:00
import { bindThis } from '@/decorators.js';
import { GlobalEventService } from '@/core/GlobalEventService.js';
import { PushNotificationService } from '@/core/PushNotificationService.js';
import { NotificationEntityService } from '@/core/entities/NotificationEntityService.js';
import { IdService } from '@/core/IdService.js';
2023-04-04 10:32:09 +02:00
import { CacheService } from '@/core/CacheService.js';
2023-09-05 10:02:14 +02:00
import type { Config } from '@/config.js';
2023-09-29 04:29:54 +02:00
import { UserListService } from '@/core/UserListService.js';
import type { FilterUnionByProperty } from '@/types.js';
import { trackPromise } from '@/misc/promise-tracker.js';
2022-09-17 20:27:08 +02:00
@Injectable()
export class NotificationService implements OnApplicationShutdown {
#shutdownController = new AbortController();
2022-09-17 20:27:08 +02:00
constructor(
@Inject(DI.config)
private config: Config,
@Inject(DI.redis)
private redisClient: Redis.Redis,
@Inject(DI.usersRepository)
private usersRepository: UsersRepository,
private notificationEntityService: NotificationEntityService,
private idService: IdService,
2022-09-17 20:27:08 +02:00
private globalEventService: GlobalEventService,
private pushNotificationService: PushNotificationService,
2023-04-04 10:32:09 +02:00
private cacheService: CacheService,
2023-09-29 04:29:54 +02:00
private userListService: UserListService,
2022-09-17 20:27:08 +02:00
) {
}
@bindThis
public async readAllNotification(
userId: MiUser['id'],
force = false,
2022-09-17 20:27:08 +02:00
) {
const latestReadNotificationId = await this.redisClient.get(`latestReadNotification:${userId}`);
const latestNotificationIdsRes = await this.redisClient.xrevrange(
`notificationTimeline:${userId}`,
'+',
'-',
'COUNT', 1);
const latestNotificationId = latestNotificationIdsRes[0]?.[0];
if (latestNotificationId == null) return;
this.redisClient.set(`latestReadNotification:${userId}`, latestNotificationId);
if (force || latestReadNotificationId == null || (latestReadNotificationId < latestNotificationId)) {
return this.postReadAllNotifications(userId);
}
2022-09-17 20:27:08 +02:00
}
@bindThis
private postReadAllNotifications(userId: MiUser['id']) {
2022-09-17 20:27:08 +02:00
this.globalEventService.publishMainStream(userId, 'readAllNotifications');
this.pushNotificationService.pushNotification(userId, 'readAllNotifications', undefined);
2022-09-17 20:27:08 +02:00
}
@bindThis
public createNotification<T extends MiNotification['type']>(
notifieeId: MiUser['id'],
type: T,
data: Omit<FilterUnionByProperty<MiNotification, 'type', T>, 'type' | 'id' | 'createdAt' | 'notifierId'>,
notifierId?: MiUser['id'] | null,
) {
trackPromise(
this.#createNotificationInternal(notifieeId, type, data, notifierId),
);
}
async #createNotificationInternal<T extends MiNotification['type']>(
notifieeId: MiUser['id'],
type: T,
data: Omit<FilterUnionByProperty<MiNotification, 'type', T>, 'type' | 'id' | 'createdAt' | 'notifierId'>,
2023-09-29 04:29:54 +02:00
notifierId?: MiUser['id'] | null,
): Promise<MiNotification | null> {
2023-04-05 03:21:10 +02:00
const profile = await this.cacheService.userProfileCache.fetch(notifieeId);
// 古いMisskeyバージョンのキャッシュが残っている可能性がある
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
const recieveConfig = (profile.notificationRecieveConfig ?? {})[type];
2023-09-29 04:29:54 +02:00
if (recieveConfig?.type === 'never') {
return null;
}
2023-09-29 04:29:54 +02:00
if (notifierId) {
if (notifieeId === notifierId) {
return null;
}
2023-04-05 03:21:10 +02:00
const mutings = await this.cacheService.userMutingsCache.fetch(notifieeId);
2023-09-29 04:29:54 +02:00
if (mutings.has(notifierId)) {
return null;
}
2023-09-29 04:29:54 +02:00
if (recieveConfig?.type === 'following') {
const isFollowing = await this.cacheService.userFollowingsCache.fetch(notifieeId).then(followings => Object.hasOwn(followings, notifierId));
2023-09-29 04:29:54 +02:00
if (!isFollowing) {
return null;
}
} else if (recieveConfig?.type === 'follower') {
const isFollower = await this.cacheService.userFollowingsCache.fetch(notifierId).then(followings => Object.hasOwn(followings, notifieeId));
2023-09-29 04:29:54 +02:00
if (!isFollower) {
return null;
}
} else if (recieveConfig?.type === 'mutualFollow') {
const [isFollowing, isFollower] = await Promise.all([
this.cacheService.userFollowingsCache.fetch(notifieeId).then(followings => Object.hasOwn(followings, notifierId)),
this.cacheService.userFollowingsCache.fetch(notifierId).then(followings => Object.hasOwn(followings, notifieeId)),
]);
if (!(isFollowing && isFollower)) {
return null;
}
} else if (recieveConfig?.type === 'followingOrFollower') {
2023-09-29 04:29:54 +02:00
const [isFollowing, isFollower] = await Promise.all([
this.cacheService.userFollowingsCache.fetch(notifieeId).then(followings => Object.hasOwn(followings, notifierId)),
this.cacheService.userFollowingsCache.fetch(notifierId).then(followings => Object.hasOwn(followings, notifieeId)),
2023-09-29 04:29:54 +02:00
]);
if (!isFollowing && !isFollower) {
return null;
}
} else if (recieveConfig?.type === 'list') {
const isMember = await this.userListService.membersCache.fetch(recieveConfig.userListId).then(members => members.has(notifierId));
if (!isMember) {
return null;
}
}
}
const notification = {
id: this.idService.gen(),
createdAt: new Date(),
type: type,
...(notifierId ? {
notifierId,
} : {}),
...data,
} as any as FilterUnionByProperty<MiNotification, 'type', T>;
const redisIdPromise = this.redisClient.xadd(
`notificationTimeline:${notifieeId}`,
'MAXLEN', '~', this.config.perUserNotificationsMaxCount.toString(),
'*',
'data', JSON.stringify(notification));
const packed = await this.notificationEntityService.pack(notification, notifieeId, {});
enhance(backend): 通知がミュート・凍結を考慮するようにする (#13412) * Never return broken notifications #409 Since notifications are stored in Redis, we can't expect relational integrity: deleting a user will *not* delete notifications that mention it. But if we return notifications with missing bits (a `follow` without a `user`, for example), the frontend will get very confused and throw an exception while trying to render them. This change makes sure we never expose those broken notifications. For uniformity, I've applied the same logic to notes and roles mentioned in notifications, even if nobody reported breakage in those cases. Tested by creating a few types of notifications with a `notifierId`, then deleting their user. (cherry picked from commit 421f8d49e5d7a8dc3a798cc54716c767df8be3cb) * Update Changelog * Update CHANGELOG.md * enhance: 通知がミュートを考慮するようにする * enhance: 通知が凍結も考慮するようにする * fix: notifierIdがない通知が消えてしまう問題 * Add tests (通知がミュートを考慮しているかどうか) * fix: notifierIdがない通知が消えてしまう問題 (grouped) * Remove unused import * Fix: typo * Revert "enhance: 通知が凍結も考慮するようにする" This reverts commit b1e57e571dfd9a7d8b2430294473c2053cc3ea33. * Revert API handling * Remove unused imports * enhance: Check if notifierId is valid in NotificationEntityService * 通知作成時にpackしてnullになったらあとの処理をやめる * Remove duplication of valid notifier check * add filter notification is not null * Revert "Remove duplication of valid notifier check" This reverts commit 239a6952f717add53d52c3e701e7362eb1987645. * Improve performance * Fix packGrouped * Refactor: 判定部分を共通化 * Fix condition * use isNotNull * Update CHANGELOG.md * filterの改善 * Refactor: DONT REPEAT YOURSELF Note: GroupedNotificationはNotificationの拡張なのでその例外だけ書けば基本的に共通の処理になり複雑な個別の処理は増えにくいと思われる * Add groupedNotificationTypes * Update misskey-js typedef * Refactor: less sql calls * refactor * clean up * filter notes to mark as read * packed noteがmapなのでそちらを使う * if (notesToRead.size > 0) * if (notes.length === 0) return; * fix * Revert "if (notes.length === 0) return;" This reverts commit 22e2324f9633bddba50769ef838bc5ddb4564c88. * :art: * console.error * err * remove try-catch * 不要なジェネリクスを除去 * Revert (既読処理をpack内で行うものを元に戻す) * Clean * Update packages/backend/src/core/entities/NotificationEntityService.ts * Update packages/backend/src/core/entities/NotificationEntityService.ts * Update packages/backend/src/core/entities/NotificationEntityService.ts * Update packages/backend/src/core/entities/NotificationEntityService.ts * Update packages/backend/src/core/NotificationService.ts * Clean --------- Co-authored-by: dakkar <dakkar@thenautilus.net> Co-authored-by: kakkokari-gtyih <daisho7308+f@gmail.com> Co-authored-by: かっこかり <67428053+kakkokari-gtyih@users.noreply.github.com> Co-authored-by: tamaina <tamaina@hotmail.co.jp> Co-authored-by: syuilo <4439005+syuilo@users.noreply.github.com>
2024-02-28 13:26:26 +01:00
if (packed == null) return null;
// Publish notification event
this.globalEventService.publishMainStream(notifieeId, 'notification', packed);
// 2秒経っても(今回作成した)通知が既読にならなかったら「未読の通知がありますよ」イベントを発行する
// テスト通知の場合は即時発行
const interval = notification.type === 'test' ? 0 : 2000;
setTimeout(interval, 'unread notification', { signal: this.#shutdownController.signal }).then(async () => {
const latestReadNotificationId = await this.redisClient.get(`latestReadNotification:${notifieeId}`);
2023-04-14 06:50:05 +02:00
if (latestReadNotificationId && (latestReadNotificationId >= (await redisIdPromise)!)) return;
this.globalEventService.publishMainStream(notifieeId, 'unreadNotification', packed);
this.pushNotificationService.pushNotification(notifieeId, 'notification', packed);
2023-09-29 04:29:54 +02:00
if (type === 'follow') this.emailNotificationFollow(notifieeId, await this.usersRepository.findOneByOrFail({ id: notifierId! }));
if (type === 'receiveFollowRequest') this.emailNotificationReceiveFollowRequest(notifieeId, await this.usersRepository.findOneByOrFail({ id: notifierId! }));
}, () => { /* aborted, ignore it */ });
return notification;
}
// TODO
//const locales = await import('../../../../locales/index.js');
// TODO: locale ファイルをクライアント用とサーバー用で分けたい
@bindThis
private async emailNotificationFollow(userId: MiUser['id'], follower: MiUser) {
/*
const userProfile = await UserProfiles.findOneByOrFail({ userId: userId });
if (!userProfile.email || !userProfile.emailNotificationTypes.includes('follow')) return;
const locale = locales[userProfile.lang ?? 'ja-JP'];
const i18n = new I18n(locale);
// TODO: render user information html
sendEmail(userProfile.email, i18n.t('_email._follow.title'), `${follower.name} (@${Acct.toString(follower)})`, `${follower.name} (@${Acct.toString(follower)})`);
*/
}
@bindThis
private async emailNotificationReceiveFollowRequest(userId: MiUser['id'], follower: MiUser) {
/*
const userProfile = await UserProfiles.findOneByOrFail({ userId: userId });
if (!userProfile.email || !userProfile.emailNotificationTypes.includes('receiveFollowRequest')) return;
const locale = locales[userProfile.lang ?? 'ja-JP'];
const i18n = new I18n(locale);
// TODO: render user information html
sendEmail(userProfile.email, i18n.t('_email._receiveFollowRequest.title'), `${follower.name} (@${Acct.toString(follower)})`, `${follower.name} (@${Acct.toString(follower)})`);
*/
}
2023-05-29 06:21:26 +02:00
@bindThis
public dispose(): void {
this.#shutdownController.abort();
}
2023-05-29 06:21:26 +02:00
@bindThis
public onApplicationShutdown(signal?: string | undefined): void {
this.dispose();
}
2022-09-17 20:27:08 +02:00
}