2022-02-27 11:07:39 +09:00
|
|
|
import { publishMainStream } from '@/services/stream.js';
|
2022-04-30 21:52:07 +09:00
|
|
|
import { pushNotification } from '@/services/push-notification.js';
|
2022-02-27 11:07:39 +09:00
|
|
|
import { User } from '@/models/entities/user.js';
|
|
|
|
import { Notification } from '@/models/entities/notification.js';
|
|
|
|
import { Notifications, Users } from '@/models/index.js';
|
2020-01-30 04:37:25 +09:00
|
|
|
import { In } from 'typeorm';
|
2017-10-30 22:12:10 +09:00
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
export async function readNotification(
|
|
|
|
userId: User['id'],
|
|
|
|
notificationIds: Notification['id'][]
|
|
|
|
) {
|
2022-06-12 19:28:13 +09:00
|
|
|
if (notificationIds.length === 0) return;
|
|
|
|
|
2017-10-30 22:12:10 +09:00
|
|
|
// Update documents
|
2019-04-07 21:50:36 +09:00
|
|
|
await Notifications.update({
|
|
|
|
id: In(notificationIds),
|
2021-12-09 23:58:30 +09:00
|
|
|
isRead: false,
|
2017-10-30 22:12:10 +09:00
|
|
|
}, {
|
2021-12-09 23:58:30 +09:00
|
|
|
isRead: true,
|
2019-04-07 21:50:36 +09:00
|
|
|
});
|
2017-10-30 22:12:10 +09:00
|
|
|
|
2022-04-30 21:52:07 +09:00
|
|
|
if (!await Users.getHasUnreadNotification(userId)) return postReadAllNotifications(userId);
|
|
|
|
else return postReadNotifications(userId, notificationIds);
|
2021-07-09 01:07:55 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function readNotificationByQuery(
|
|
|
|
userId: User['id'],
|
|
|
|
query: Record<string, any>
|
|
|
|
) {
|
2022-04-30 21:52:07 +09:00
|
|
|
const notificationIds = await Notifications.find({
|
2021-07-09 01:07:55 +09:00
|
|
|
...query,
|
|
|
|
notifieeId: userId,
|
2021-12-09 23:58:30 +09:00
|
|
|
isRead: false,
|
2022-04-30 21:52:07 +09:00
|
|
|
}).then(notifications => notifications.map(notification => notification.id));
|
|
|
|
|
|
|
|
return readNotification(userId, notificationIds);
|
|
|
|
}
|
2021-07-09 01:07:55 +09:00
|
|
|
|
2022-04-30 21:52:07 +09:00
|
|
|
function postReadAllNotifications(userId: User['id']) {
|
|
|
|
publishMainStream(userId, 'readAllNotifications');
|
|
|
|
return pushNotification(userId, 'readAllNotifications', undefined);
|
2021-07-09 01:07:55 +09:00
|
|
|
}
|
|
|
|
|
2022-04-30 21:52:07 +09:00
|
|
|
function postReadNotifications(userId: User['id'], notificationIds: Notification['id'][]) {
|
|
|
|
publishMainStream(userId, 'readNotifications', notificationIds);
|
|
|
|
return pushNotification(userId, 'readNotifications', { notificationIds });
|
2019-04-07 21:50:36 +09:00
|
|
|
}
|