summaryrefslogtreecommitdiff
path: root/packages/backend/src/services/note/read.ts
blob: 915a9e9eeff17e5c18a5d3b86adc3a67f3d27913 (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import { publishMainStream } from '@/services/stream.js';
import { Note } from '@/models/entities/note.js';
import { User } from '@/models/entities/user.js';
import { NoteUnreads, AntennaNotes, Users, Followings, ChannelFollowings } from '@/models/index.js';
import { Not, IsNull, In } from 'typeorm';
import { Channel } from '@/models/entities/channel.js';
import { checkHitAntenna } from '@/misc/check-hit-antenna.js';
import { getAntennas } from '@/misc/antenna-cache.js';
import { readNotificationByQuery } from '@/server/api/common/read-notification.js';
import { Packed } from '@/misc/schema.js';

/**
 * Mark notes as read
 */
export default async function(
	userId: User['id'],
	notes: (Note | Packed<'Note'>)[],
	info?: {
		following: Set<User['id']>;
		followingChannels: Set<Channel['id']>;
	}
) {
	const following = info?.following ? info.following : new Set<string>((await Followings.find({
		where: {
			followerId: userId,
		},
		select: ['followeeId'],
	})).map(x => x.followeeId));
	const followingChannels = info?.followingChannels ? info.followingChannels : new Set<string>((await ChannelFollowings.find({
		where: {
			followerId: userId,
		},
		select: ['followeeId'],
	})).map(x => x.followeeId));

	const myAntennas = (await getAntennas()).filter(a => a.userId === userId);
	const readMentions: (Note | Packed<'Note'>)[] = [];
	const readSpecifiedNotes: (Note | Packed<'Note'>)[] = [];
	const readChannelNotes: (Note | Packed<'Note'>)[] = [];
	const readAntennaNotes: (Note | Packed<'Note'>)[] = [];

	for (const note of notes) {
		if (note.mentions && note.mentions.includes(userId)) {
			readMentions.push(note);
		} else if (note.visibleUserIds && note.visibleUserIds.includes(userId)) {
			readSpecifiedNotes.push(note);
		}

		if (note.channelId && followingChannels.has(note.channelId)) {
			readChannelNotes.push(note);
		}

		if (note.user != null) { // たぶんnullになることは無いはずだけど一応
			for (const antenna of myAntennas) {
				if (await checkHitAntenna(antenna, note, note.user, undefined, Array.from(following))) {
					readAntennaNotes.push(note);
				}
			}
		}
	}

	if ((readMentions.length > 0) || (readSpecifiedNotes.length > 0) || (readChannelNotes.length > 0)) {
		// Remove the record
		await NoteUnreads.delete({
			userId: userId,
			noteId: In([...readMentions.map(n => n.id), ...readSpecifiedNotes.map(n => n.id), ...readChannelNotes.map(n => n.id)]),
		});

		// TODO: ↓まとめてクエリしたい

		NoteUnreads.countBy({
			userId: userId,
			isMentioned: true,
		}).then(mentionsCount => {
			if (mentionsCount === 0) {
				// 全て既読になったイベントを発行
				publishMainStream(userId, 'readAllUnreadMentions');
			}
		});

		NoteUnreads.countBy({
			userId: userId,
			isSpecified: true,
		}).then(specifiedCount => {
			if (specifiedCount === 0) {
				// 全て既読になったイベントを発行
				publishMainStream(userId, 'readAllUnreadSpecifiedNotes');
			}
		});

		NoteUnreads.countBy({
			userId: userId,
			noteChannelId: Not(IsNull()),
		}).then(channelNoteCount => {
			if (channelNoteCount === 0) {
				// 全て既読になったイベントを発行
				publishMainStream(userId, 'readAllChannels');
			}
		});

		readNotificationByQuery(userId, {
			noteId: In([...readMentions.map(n => n.id), ...readSpecifiedNotes.map(n => n.id)]),
		});
	}

	if (readAntennaNotes.length > 0) {
		await AntennaNotes.update({
			antennaId: In(myAntennas.map(a => a.id)),
			noteId: In(readAntennaNotes.map(n => n.id)),
		}, {
			read: true,
		});

		// TODO: まとめてクエリしたい
		for (const antenna of myAntennas) {
			const count = await AntennaNotes.countBy({
				antennaId: antenna.id,
				read: false,
			});

			if (count === 0) {
				publishMainStream(userId, 'readAntenna', antenna);
			}
		}

		Users.getHasUnreadAntenna(userId).then(unread => {
			if (!unread) {
				publishMainStream(userId, 'readAllAntennas');
			}
		});
	}
}