2023-03-16 07:36:21 +02:00
|
|
|
import { setTimeout } from 'node:timers/promises';
|
2023-04-04 08:06:57 +03:00
|
|
|
import Redis from 'ioredis';
|
2023-03-16 07:24:11 +02:00
|
|
|
import { Inject, Injectable, OnApplicationShutdown } from '@nestjs/common';
|
2022-09-17 21:27:08 +03:00
|
|
|
import { In } from 'typeorm';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2023-04-04 11:32:09 +03:00
|
|
|
import type { MutingsRepository, UserProfile, UserProfilesRepository, UsersRepository } from '@/models/index.js';
|
2022-09-17 21:27:08 +03:00
|
|
|
import type { User } from '@/models/entities/User.js';
|
|
|
|
import type { Notification } from '@/models/entities/Notification.js';
|
2022-12-04 03:16:03 +02:00
|
|
|
import { UserEntityService } from '@/core/entities/UserEntityService.js';
|
2023-02-17 08:15:36 +02:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2023-03-16 07:24:11 +02: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 11:32:09 +03:00
|
|
|
import { CacheService } from '@/core/CacheService.js';
|
2022-09-17 21:27:08 +03:00
|
|
|
|
|
|
|
@Injectable()
|
2023-03-16 07:24:11 +02:00
|
|
|
export class NotificationService implements OnApplicationShutdown {
|
|
|
|
#shutdownController = new AbortController();
|
|
|
|
|
2022-09-17 21:27:08 +03:00
|
|
|
constructor(
|
2023-04-04 08:06:57 +03:00
|
|
|
@Inject(DI.redis)
|
|
|
|
private redisClient: Redis.Redis,
|
|
|
|
|
2023-03-16 07:24:11 +02:00
|
|
|
@Inject(DI.usersRepository)
|
|
|
|
private usersRepository: UsersRepository,
|
|
|
|
|
|
|
|
@Inject(DI.userProfilesRepository)
|
|
|
|
private userProfilesRepository: UserProfilesRepository,
|
|
|
|
|
|
|
|
@Inject(DI.mutingsRepository)
|
|
|
|
private mutingsRepository: MutingsRepository,
|
|
|
|
|
|
|
|
private notificationEntityService: NotificationEntityService,
|
2022-09-17 21:27:08 +03:00
|
|
|
private userEntityService: UserEntityService,
|
2023-03-16 07:24:11 +02:00
|
|
|
private idService: IdService,
|
2022-09-17 21:27:08 +03:00
|
|
|
private globalEventService: GlobalEventService,
|
|
|
|
private pushNotificationService: PushNotificationService,
|
2023-04-04 11:32:09 +03:00
|
|
|
private cacheService: CacheService,
|
2022-09-17 21:27:08 +03:00
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
2022-12-04 08:03:09 +02:00
|
|
|
@bindThis
|
2023-04-04 08:06:57 +03:00
|
|
|
public async readAllNotification(
|
2022-09-17 21:27:08 +03:00
|
|
|
userId: User['id'],
|
2023-04-06 00:11:59 +03:00
|
|
|
force = false,
|
2022-09-17 21:27:08 +03:00
|
|
|
) {
|
2023-04-04 08:06:57 +03: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);
|
|
|
|
|
2023-04-06 00:11:59 +03:00
|
|
|
if (force || latestReadNotificationId == null || (latestReadNotificationId < latestNotificationId)) {
|
2023-04-04 08:06:57 +03:00
|
|
|
return this.postReadAllNotifications(userId);
|
|
|
|
}
|
2022-09-17 21:27:08 +03:00
|
|
|
}
|
|
|
|
|
2022-12-04 08:03:09 +02:00
|
|
|
@bindThis
|
2022-09-18 21:11:50 +03:00
|
|
|
private postReadAllNotifications(userId: User['id']) {
|
2022-09-17 21:27:08 +03:00
|
|
|
this.globalEventService.publishMainStream(userId, 'readAllNotifications');
|
|
|
|
}
|
2023-03-16 07:24:11 +02:00
|
|
|
|
|
|
|
@bindThis
|
|
|
|
public async createNotification(
|
|
|
|
notifieeId: User['id'],
|
|
|
|
type: Notification['type'],
|
|
|
|
data: Partial<Notification>,
|
|
|
|
): Promise<Notification | null> {
|
2023-04-05 04:21:10 +03:00
|
|
|
const profile = await this.cacheService.userProfileCache.fetch(notifieeId);
|
2023-04-04 11:32:09 +03:00
|
|
|
const isMuted = profile.mutingNotificationTypes.includes(type);
|
2023-04-04 08:06:57 +03:00
|
|
|
if (isMuted) return null;
|
2023-03-16 07:24:11 +02:00
|
|
|
|
2023-04-04 08:06:57 +03:00
|
|
|
if (data.notifierId) {
|
|
|
|
if (notifieeId === data.notifierId) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-04-05 04:21:10 +03:00
|
|
|
const mutings = await this.cacheService.userMutingsCache.fetch(notifieeId);
|
|
|
|
if (mutings.has(data.notifierId)) {
|
2023-04-04 08:06:57 +03:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const notification = {
|
2023-03-16 07:24:11 +02:00
|
|
|
id: this.idService.genId(),
|
|
|
|
createdAt: new Date(),
|
|
|
|
type: type,
|
|
|
|
...data,
|
2023-04-04 08:06:57 +03:00
|
|
|
} as Notification;
|
|
|
|
|
2023-04-06 00:11:59 +03:00
|
|
|
const redisIdPromise = this.redisClient.xadd(
|
2023-04-04 08:06:57 +03:00
|
|
|
`notificationTimeline:${notifieeId}`,
|
|
|
|
'MAXLEN', '~', '300',
|
2023-04-10 04:03:53 +03:00
|
|
|
'*',
|
2023-04-04 08:06:57 +03:00
|
|
|
'data', JSON.stringify(notification));
|
2023-03-16 07:24:11 +02:00
|
|
|
|
2023-04-04 08:06:57 +03:00
|
|
|
const packed = await this.notificationEntityService.pack(notification, notifieeId, {});
|
2023-03-16 07:24:11 +02:00
|
|
|
|
|
|
|
// Publish notification event
|
|
|
|
this.globalEventService.publishMainStream(notifieeId, 'notification', packed);
|
|
|
|
|
|
|
|
// 2秒経っても(今回作成した)通知が既読にならなかったら「未読の通知がありますよ」イベントを発行する
|
2023-04-04 08:06:57 +03:00
|
|
|
setTimeout(2000, 'unread notification', { signal: this.#shutdownController.signal }).then(async () => {
|
|
|
|
const latestReadNotificationId = await this.redisClient.get(`latestReadNotification:${notifieeId}`);
|
2023-04-06 00:11:59 +03:00
|
|
|
if (latestReadNotificationId && (latestReadNotificationId >= await redisIdPromise)) return;
|
2023-03-16 07:24:11 +02:00
|
|
|
|
|
|
|
this.globalEventService.publishMainStream(notifieeId, 'unreadNotification', packed);
|
|
|
|
this.pushNotificationService.pushNotification(notifieeId, 'notification', packed);
|
|
|
|
|
|
|
|
if (type === 'follow') this.emailNotificationFollow(notifieeId, await this.usersRepository.findOneByOrFail({ id: data.notifierId! }));
|
|
|
|
if (type === 'receiveFollowRequest') this.emailNotificationReceiveFollowRequest(notifieeId, await this.usersRepository.findOneByOrFail({ id: data.notifierId! }));
|
|
|
|
}, () => { /* aborted, ignore it */ });
|
|
|
|
|
|
|
|
return notification;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO
|
|
|
|
//const locales = await import('../../../../locales/index.js');
|
|
|
|
|
|
|
|
// TODO: locale ファイルをクライアント用とサーバー用で分けたい
|
|
|
|
|
|
|
|
@bindThis
|
|
|
|
private async emailNotificationFollow(userId: User['id'], follower: User) {
|
|
|
|
/*
|
|
|
|
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: User['id'], follower: User) {
|
|
|
|
/*
|
|
|
|
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)})`);
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
onApplicationShutdown(signal?: string | undefined): void {
|
|
|
|
this.#shutdownController.abort();
|
|
|
|
}
|
2022-09-17 21:27:08 +03:00
|
|
|
}
|