summaryrefslogtreecommitdiff
path: root/src/services/note/read.ts
blob: 44d75bd850f8ef12299bd16f9feb43f66d865a67 (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
import { publishMainStream } from '../stream';
import { Note } from '../../models/entities/note';
import { User } from '../../models/entities/user';
import { NoteUnreads } from '../../models';

/**
 * Mark a note as read
 */
export default (
	userId: User['id'],
	noteId: Note['id']
) => new Promise<any>(async (resolve, reject) => {
	// Remove document
	const res = await NoteUnreads.delete({
		userId: userId,
		noteId: noteId
	});

	// v11 TODO: https://github.com/typeorm/typeorm/issues/2415
	if (res.affected == 0) {
		return;
	}

	const count1 = await NoteUnreads.count({
		userId: userId,
		isSpecified: false
	});

	const count2 = await NoteUnreads.count({
		userId: userId,
		isSpecified: true
	});

	if (count1 == 0) {
		// 全て既読になったイベントを発行
		publishMainStream(userId, 'readAllUnreadMentions');
	}

	if (count2 == 0) {
		// 全て既読になったイベントを発行
		publishMainStream(userId, 'readAllUnreadSpecifiedNotes');
	}
});