summaryrefslogtreecommitdiff
path: root/src/server/api/common/read-notification.ts
blob: c8d43ba286266cfe3ee7e2c1d34a60f7989b89f7 (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
import { publishMainStream } from '../../../services/stream';
import { User } from '../../../models/entities/user';
import { Notification } from '../../../models/entities/notification';
import { Mutings, Notifications } from '../../../models';
import { In, Not } from 'typeorm';

/**
 * Mark notifications as read
 */
export async function readNotification(
	userId: User['id'],
	notificationIds: Notification['id'][]
) {
	const mute = await Mutings.find({
		muterId: userId
	});
	const mutedUserIds = mute.map(m => m.muteeId);

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

	// Calc count of my unread notifications
	const count = await Notifications.count({
		notifieeId: userId,
		...(mutedUserIds.length > 0 ? { notifierId: Not(In(mutedUserIds)) } : {}),
		isRead: false
	});

	if (count === 0) {
		// 全ての(いままで未読だった)通知を(これで)読みましたよというイベントを発行
		publishMainStream(userId, 'readAllNotifications');
	}
}