summaryrefslogtreecommitdiff
path: root/packages/backend/src/server/api/common/read-notification.ts
blob: 8c4ba41a36f05e37a94ccf9f759e961ce077fc3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { publishMainStream } from '@/services/stream.js';
import { pushNotification } from '@/services/push-notification.js';
import { User } from '@/models/entities/user.js';
import { Notification } from '@/models/entities/notification.js';
import { Notifications, Users } from '@/models/index.js';
import { In } from 'typeorm';

export async function readNotification(
	userId: User['id'],
	notificationIds: Notification['id'][]
) {
	if (notificationIds.length === 0) return;

	// Update documents
	await Notifications.update({
		id: In(notificationIds),
		isRead: false,
	}, {
		isRead: true,
	});

	if (!await Users.getHasUnreadNotification(userId)) return postReadAllNotifications(userId);
	else return postReadNotifications(userId, notificationIds);
}

export async function readNotificationByQuery(
	userId: User['id'],
	query: Record<string, any>
) {
	const notificationIds = await Notifications.find({
		...query,
		notifieeId: userId,
		isRead: false,
	}).then(notifications => notifications.map(notification => notification.id));

	return readNotification(userId, notificationIds);
}

function postReadAllNotifications(userId: User['id']) {
	publishMainStream(userId, 'readAllNotifications');
	return pushNotification(userId, 'readAllNotifications', undefined);
}

function postReadNotifications(userId: User['id'], notificationIds: Notification['id'][]) {
	publishMainStream(userId, 'readNotifications', notificationIds);
	return pushNotification(userId, 'readNotifications', { notificationIds });
}