2022-09-18 03:27:08 +09:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import { In } from 'typeorm';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2022-09-21 05:33:11 +09:00
|
|
|
import type { NotificationsRepository } from '@/models/index.js';
|
2022-09-18 03:27:08 +09:00
|
|
|
import type { User } from '@/models/entities/User.js';
|
|
|
|
import type { Notification } from '@/models/entities/Notification.js';
|
2022-12-04 10:16:03 +09:00
|
|
|
import { UserEntityService } from '@/core/entities/UserEntityService.js';
|
2022-09-18 03:27:08 +09:00
|
|
|
import { GlobalEventService } from './GlobalEventService.js';
|
|
|
|
import { PushNotificationService } from './PushNotificationService.js';
|
2022-12-04 15:03:09 +09:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2022-09-18 03:27:08 +09:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class NotificationService {
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.notificationsRepository)
|
|
|
|
private notificationsRepository: NotificationsRepository,
|
|
|
|
|
|
|
|
private userEntityService: UserEntityService,
|
|
|
|
private globalEventService: GlobalEventService,
|
|
|
|
private pushNotificationService: PushNotificationService,
|
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
2022-12-04 15:03:09 +09:00
|
|
|
@bindThis
|
2022-09-18 03:27:08 +09:00
|
|
|
public async readNotification(
|
|
|
|
userId: User['id'],
|
|
|
|
notificationIds: Notification['id'][],
|
|
|
|
) {
|
|
|
|
if (notificationIds.length === 0) return;
|
|
|
|
|
|
|
|
// Update documents
|
|
|
|
const result = await this.notificationsRepository.update({
|
|
|
|
notifieeId: userId,
|
|
|
|
id: In(notificationIds),
|
|
|
|
isRead: false,
|
|
|
|
}, {
|
|
|
|
isRead: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (result.affected === 0) return;
|
|
|
|
|
2022-09-19 03:11:50 +09:00
|
|
|
if (!await this.userEntityService.getHasUnreadNotification(userId)) return this.postReadAllNotifications(userId);
|
|
|
|
else return this.postReadNotifications(userId, notificationIds);
|
2022-09-18 03:27:08 +09:00
|
|
|
}
|
|
|
|
|
2022-12-04 15:03:09 +09:00
|
|
|
@bindThis
|
2022-09-18 03:27:08 +09:00
|
|
|
public async readNotificationByQuery(
|
|
|
|
userId: User['id'],
|
|
|
|
query: Record<string, any>,
|
|
|
|
) {
|
|
|
|
const notificationIds = await this.notificationsRepository.findBy({
|
|
|
|
...query,
|
|
|
|
notifieeId: userId,
|
|
|
|
isRead: false,
|
|
|
|
}).then(notifications => notifications.map(notification => notification.id));
|
|
|
|
|
|
|
|
return this.readNotification(userId, notificationIds);
|
|
|
|
}
|
|
|
|
|
2022-12-04 15:03:09 +09:00
|
|
|
@bindThis
|
2022-09-19 03:11:50 +09:00
|
|
|
private postReadAllNotifications(userId: User['id']) {
|
2022-09-18 03:27:08 +09:00
|
|
|
this.globalEventService.publishMainStream(userId, 'readAllNotifications');
|
|
|
|
return this.pushNotificationService.pushNotification(userId, 'readAllNotifications', undefined);
|
|
|
|
}
|
|
|
|
|
2022-12-04 15:03:09 +09:00
|
|
|
@bindThis
|
2022-09-19 03:11:50 +09:00
|
|
|
private postReadNotifications(userId: User['id'], notificationIds: Notification['id'][]) {
|
2022-09-18 03:27:08 +09:00
|
|
|
this.globalEventService.publishMainStream(userId, 'readNotifications', notificationIds);
|
|
|
|
return this.pushNotificationService.pushNotification(userId, 'readNotifications', { notificationIds });
|
|
|
|
}
|
|
|
|
}
|