2023-07-27 05:31:52 +00:00
|
|
|
/*
|
2024-02-13 15:59:27 +00:00
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 05:31:52 +00:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2023-03-16 05:36:21 +00:00
|
|
|
import { setTimeout } from 'node:timers/promises';
|
2023-04-14 04:50:05 +00:00
|
|
|
import * as Redis from 'ioredis';
|
2023-03-16 05:24:11 +00:00
|
|
|
import { Inject, Injectable, OnApplicationShutdown } from '@nestjs/common';
|
2022-09-17 18:27:08 +00:00
|
|
|
import { In } from 'typeorm';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2023-09-15 05:28:29 +00:00
|
|
|
import type { UsersRepository } from '@/models/_.js';
|
2023-09-20 02:33:36 +00:00
|
|
|
import type { MiUser } from '@/models/User.js';
|
|
|
|
import type { MiNotification } from '@/models/Notification.js';
|
2023-02-17 06:15:36 +00:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2023-03-16 05:24:11 +00:00
|
|
|
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 08:32:09 +00:00
|
|
|
import { CacheService } from '@/core/CacheService.js';
|
2023-09-05 08:02:14 +00:00
|
|
|
import type { Config } from '@/config.js';
|
2023-09-29 02:29:54 +00:00
|
|
|
import { UserListService } from '@/core/UserListService.js';
|
2023-11-02 06:57:55 +00:00
|
|
|
import type { FilterUnionByProperty } from '@/types.js';
|
2024-01-08 03:28:13 +00:00
|
|
|
import { trackPromise } from '@/misc/promise-tracker.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
|
|
|
|
@Injectable()
|
2023-03-16 05:24:11 +00:00
|
|
|
export class NotificationService implements OnApplicationShutdown {
|
|
|
|
#shutdownController = new AbortController();
|
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
constructor(
|
2023-09-05 06:03:50 +00:00
|
|
|
@Inject(DI.config)
|
|
|
|
private config: Config,
|
|
|
|
|
2023-04-04 05:06:57 +00:00
|
|
|
@Inject(DI.redis)
|
|
|
|
private redisClient: Redis.Redis,
|
|
|
|
|
2023-03-16 05:24:11 +00:00
|
|
|
@Inject(DI.usersRepository)
|
|
|
|
private usersRepository: UsersRepository,
|
|
|
|
|
|
|
|
private notificationEntityService: NotificationEntityService,
|
|
|
|
private idService: IdService,
|
2022-09-17 18:27:08 +00:00
|
|
|
private globalEventService: GlobalEventService,
|
|
|
|
private pushNotificationService: PushNotificationService,
|
2023-04-04 08:32:09 +00:00
|
|
|
private cacheService: CacheService,
|
2023-09-29 02:29:54 +00:00
|
|
|
private userListService: UserListService,
|
2022-09-17 18:27:08 +00:00
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2023-04-04 05:06:57 +00:00
|
|
|
public async readAllNotification(
|
2023-08-16 08:51:28 +00:00
|
|
|
userId: MiUser['id'],
|
2023-04-05 21:11:59 +00:00
|
|
|
force = false,
|
2022-09-17 18:27:08 +00:00
|
|
|
) {
|
2023-04-04 05:06:57 +00:00
|
|
|
const latestReadNotificationId = await this.redisClient.get(`latestReadNotification:${userId}`);
|
2023-07-07 22:08:16 +00:00
|
|
|
|
2023-04-04 05:06:57 +00:00
|
|
|
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);
|
|
|
|
|
2023-04-05 21:11:59 +00:00
|
|
|
if (force || latestReadNotificationId == null || (latestReadNotificationId < latestNotificationId)) {
|
2023-04-04 05:06:57 +00:00
|
|
|
return this.postReadAllNotifications(userId);
|
|
|
|
}
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2023-08-16 08:51:28 +00:00
|
|
|
private postReadAllNotifications(userId: MiUser['id']) {
|
2022-09-17 18:27:08 +00:00
|
|
|
this.globalEventService.publishMainStream(userId, 'readAllNotifications');
|
2023-04-11 05:11:39 +00:00
|
|
|
this.pushNotificationService.pushNotification(userId, 'readAllNotifications', undefined);
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
2023-03-16 05:24:11 +00:00
|
|
|
|
|
|
|
@bindThis
|
2024-01-08 03:28:13 +00:00
|
|
|
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']>(
|
2023-08-16 08:51:28 +00:00
|
|
|
notifieeId: MiUser['id'],
|
2023-11-02 06:57:55 +00:00
|
|
|
type: T,
|
|
|
|
data: Omit<FilterUnionByProperty<MiNotification, 'type', T>, 'type' | 'id' | 'createdAt' | 'notifierId'>,
|
2023-09-29 02:29:54 +00:00
|
|
|
notifierId?: MiUser['id'] | null,
|
2023-08-16 08:51:28 +00:00
|
|
|
): Promise<MiNotification | null> {
|
2023-04-05 01:21:10 +00:00
|
|
|
const profile = await this.cacheService.userProfileCache.fetch(notifieeId);
|
2023-09-29 22:33:58 +00:00
|
|
|
|
|
|
|
// 古いMisskeyバージョンのキャッシュが残っている可能性がある
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
|
|
const recieveConfig = (profile.notificationRecieveConfig ?? {})[type];
|
2023-09-29 02:29:54 +00:00
|
|
|
if (recieveConfig?.type === 'never') {
|
|
|
|
return null;
|
|
|
|
}
|
2023-03-16 05:24:11 +00:00
|
|
|
|
2023-09-29 02:29:54 +00:00
|
|
|
if (notifierId) {
|
|
|
|
if (notifieeId === notifierId) {
|
2023-04-04 05:06:57 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-04-05 01:21:10 +00:00
|
|
|
const mutings = await this.cacheService.userMutingsCache.fetch(notifieeId);
|
2023-09-29 02:29:54 +00:00
|
|
|
if (mutings.has(notifierId)) {
|
2023-04-04 05:06:57 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-09-29 02:29:54 +00:00
|
|
|
|
|
|
|
if (recieveConfig?.type === 'following') {
|
2023-10-03 11:26:11 +00:00
|
|
|
const isFollowing = await this.cacheService.userFollowingsCache.fetch(notifieeId).then(followings => Object.hasOwn(followings, notifierId));
|
2023-09-29 02:29:54 +00:00
|
|
|
if (!isFollowing) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
} else if (recieveConfig?.type === 'follower') {
|
2023-10-03 11:26:11 +00:00
|
|
|
const isFollower = await this.cacheService.userFollowingsCache.fetch(notifierId).then(followings => Object.hasOwn(followings, notifieeId));
|
2023-09-29 02:29:54 +00:00
|
|
|
if (!isFollower) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
} else if (recieveConfig?.type === 'mutualFollow') {
|
2024-03-02 16:36:49 +00: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)),
|
|
|
|
]);
|
|
|
|
if (!(isFollowing && isFollower)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
} else if (recieveConfig?.type === 'followingOrFollower') {
|
2023-09-29 02:29:54 +00:00
|
|
|
const [isFollowing, isFollower] = await Promise.all([
|
2023-10-03 11:26:11 +00:00
|
|
|
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 02:29:54 +00: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;
|
|
|
|
}
|
|
|
|
}
|
2023-04-04 05:06:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const notification = {
|
2023-10-16 01:45:22 +00:00
|
|
|
id: this.idService.gen(),
|
2023-03-16 05:24:11 +00:00
|
|
|
createdAt: new Date(),
|
|
|
|
type: type,
|
2023-11-02 06:57:55 +00:00
|
|
|
...(notifierId ? {
|
|
|
|
notifierId,
|
|
|
|
} : {}),
|
2023-03-16 05:24:11 +00:00
|
|
|
...data,
|
2023-11-02 06:57:55 +00:00
|
|
|
} as any as FilterUnionByProperty<MiNotification, 'type', T>;
|
2023-04-04 05:06:57 +00:00
|
|
|
|
2023-04-05 21:11:59 +00:00
|
|
|
const redisIdPromise = this.redisClient.xadd(
|
2023-04-04 05:06:57 +00:00
|
|
|
`notificationTimeline:${notifieeId}`,
|
2023-09-05 06:03:50 +00:00
|
|
|
'MAXLEN', '~', this.config.perUserNotificationsMaxCount.toString(),
|
2023-04-10 01:03:53 +00:00
|
|
|
'*',
|
2023-04-04 05:06:57 +00:00
|
|
|
'data', JSON.stringify(notification));
|
2023-03-16 05:24:11 +00:00
|
|
|
|
2023-04-04 05:06:57 +00:00
|
|
|
const packed = await this.notificationEntityService.pack(notification, notifieeId, {});
|
2023-03-16 05:24:11 +00:00
|
|
|
|
2024-03-02 16:36:49 +00:00
|
|
|
if (packed == null) return null;
|
|
|
|
|
2023-03-16 05:24:11 +00:00
|
|
|
// Publish notification event
|
|
|
|
this.globalEventService.publishMainStream(notifieeId, 'notification', packed);
|
|
|
|
|
|
|
|
// 2秒経っても(今回作成した)通知が既読にならなかったら「未読の通知がありますよ」イベントを発行する
|
2023-11-01 04:34:05 +00:00
|
|
|
// テスト通知の場合は即時発行
|
|
|
|
const interval = notification.type === 'test' ? 0 : 2000;
|
|
|
|
setTimeout(interval, 'unread notification', { signal: this.#shutdownController.signal }).then(async () => {
|
2023-04-04 05:06:57 +00:00
|
|
|
const latestReadNotificationId = await this.redisClient.get(`latestReadNotification:${notifieeId}`);
|
2023-04-14 04:50:05 +00:00
|
|
|
if (latestReadNotificationId && (latestReadNotificationId >= (await redisIdPromise)!)) return;
|
2023-03-16 05:24:11 +00:00
|
|
|
|
|
|
|
this.globalEventService.publishMainStream(notifieeId, 'unreadNotification', packed);
|
|
|
|
this.pushNotificationService.pushNotification(notifieeId, 'notification', packed);
|
|
|
|
|
2023-09-29 02:29:54 +00: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! }));
|
2023-03-16 05:24:11 +00:00
|
|
|
}, () => { /* aborted, ignore it */ });
|
|
|
|
|
|
|
|
return notification;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO
|
|
|
|
//const locales = await import('../../../../locales/index.js');
|
|
|
|
|
|
|
|
// TODO: locale ファイルをクライアント用とサーバー用で分けたい
|
|
|
|
|
|
|
|
@bindThis
|
2023-08-16 08:51:28 +00:00
|
|
|
private async emailNotificationFollow(userId: MiUser['id'], follower: MiUser) {
|
2023-03-16 05:24:11 +00:00
|
|
|
/*
|
|
|
|
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
|
2023-08-16 08:51:28 +00:00
|
|
|
private async emailNotificationReceiveFollowRequest(userId: MiUser['id'], follower: MiUser) {
|
2023-03-16 05:24:11 +00:00
|
|
|
/*
|
|
|
|
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)})`);
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
2024-03-02 16:36:49 +00:00
|
|
|
@bindThis
|
|
|
|
public async flushAllNotifications(userId: MiUser['id']) {
|
|
|
|
await Promise.all([
|
|
|
|
this.redisClient.del(`notificationTimeline:${userId}`),
|
|
|
|
this.redisClient.del(`latestReadNotification:${userId}`),
|
|
|
|
]);
|
|
|
|
this.globalEventService.publishMainStream(userId, 'notificationFlushed');
|
|
|
|
}
|
|
|
|
|
2023-05-29 04:21:26 +00:00
|
|
|
@bindThis
|
|
|
|
public dispose(): void {
|
2023-03-16 05:24:11 +00:00
|
|
|
this.#shutdownController.abort();
|
|
|
|
}
|
2023-05-29 04:21:26 +00:00
|
|
|
|
|
|
|
@bindThis
|
|
|
|
public onApplicationShutdown(signal?: string | undefined): void {
|
|
|
|
this.dispose();
|
|
|
|
}
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|