summaryrefslogtreecommitdiff
path: root/src/server/api/endpoints/i/read-all-messaging-messages.ts
blob: e47ef16bd90afe600e4476e8598d58e8f064d7aa (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
import { publishMainStream } from '../../../../services/stream';
import define from '../../define';
import { MessagingMessages, UserGroupJoinings } from '../../../../models';

export const meta = {
	desc: {
		'ja-JP': 'トークメッセージをすべて既読にします。',
		'en-US': 'Mark all talk messages as read.'
	},

	tags: ['account', 'messaging'],

	requireCredential: true as const,

	kind: 'write:account',

	params: {
	}
};

export default define(meta, async (ps, user) => {
	// Update documents
	await MessagingMessages.update({
		recipientId: user.id,
		isRead: false
	}, {
		isRead: true
	});

	const joinings = await UserGroupJoinings.find({ userId: user.id });

	await Promise.all(joinings.map(j => MessagingMessages.createQueryBuilder().update()
		.set({
			reads: (() => `array_append("reads", '${user.id}')`) as any
		})
		.where(`groupId = :groupId`, { groupId: j.userGroupId })
		.andWhere('userId != :userId', { userId: user.id })
		.andWhere('NOT (:userId = ANY(reads))', { userId: user.id })
		.execute()));

	publishMainStream(user.id, 'readAllMessagingMessages');
});