2019-02-05 06:14:23 +01:00
|
|
|
import { publishMainStream } from '../stream';
|
2019-04-07 14:50:36 +02:00
|
|
|
import { Note } from '../../models/entities/note';
|
|
|
|
import { User } from '../../models/entities/user';
|
2020-01-29 20:37:25 +01:00
|
|
|
import { NoteUnreads, Antennas, AntennaNotes, Users } from '../../models';
|
2020-08-18 15:44:21 +02:00
|
|
|
import { Not, IsNull } from 'typeorm';
|
2020-02-09 23:23:43 +01:00
|
|
|
|
2018-09-19 07:18:34 +02:00
|
|
|
/**
|
|
|
|
* Mark a note as read
|
|
|
|
*/
|
2020-08-18 15:44:21 +02:00
|
|
|
export default async function(
|
2019-04-07 14:50:36 +02:00
|
|
|
userId: User['id'],
|
|
|
|
noteId: Note['id']
|
2020-08-18 15:44:21 +02:00
|
|
|
) {
|
|
|
|
async function careNoteUnreads() {
|
|
|
|
const exist = await NoteUnreads.findOne({
|
|
|
|
userId: userId,
|
|
|
|
noteId: noteId,
|
|
|
|
});
|
2018-09-19 07:18:34 +02:00
|
|
|
|
2020-08-18 15:44:21 +02:00
|
|
|
if (!exist) return;
|
2018-09-23 09:05:26 +02:00
|
|
|
|
2020-08-18 15:44:21 +02:00
|
|
|
// Remove the record
|
|
|
|
await NoteUnreads.delete({
|
2020-01-29 20:37:25 +01:00
|
|
|
userId: userId,
|
2020-08-18 15:44:21 +02:00
|
|
|
noteId: noteId,
|
|
|
|
});
|
2018-09-19 07:18:34 +02:00
|
|
|
|
2020-08-18 15:44:21 +02:00
|
|
|
if (exist.isMentioned) {
|
|
|
|
NoteUnreads.count({
|
|
|
|
userId: userId,
|
|
|
|
isMentioned: true
|
|
|
|
}).then(mentionsCount => {
|
|
|
|
if (mentionsCount === 0) {
|
|
|
|
// 全て既読になったイベントを発行
|
|
|
|
publishMainStream(userId, 'readAllUnreadMentions');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2018-09-19 07:18:34 +02:00
|
|
|
|
2020-08-18 15:44:21 +02:00
|
|
|
if (exist.isSpecified) {
|
|
|
|
NoteUnreads.count({
|
|
|
|
userId: userId,
|
|
|
|
isSpecified: true
|
|
|
|
}).then(specifiedCount => {
|
|
|
|
if (specifiedCount === 0) {
|
|
|
|
// 全て既読になったイベントを発行
|
|
|
|
publishMainStream(userId, 'readAllUnreadSpecifiedNotes');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (exist.noteChannelId) {
|
|
|
|
NoteUnreads.count({
|
|
|
|
userId: userId,
|
|
|
|
noteChannelId: Not(IsNull())
|
|
|
|
}).then(channelNoteCount => {
|
|
|
|
if (channelNoteCount === 0) {
|
|
|
|
// 全て既読になったイベントを発行
|
|
|
|
publishMainStream(userId, 'readAllChannels');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2018-09-19 07:18:34 +02:00
|
|
|
}
|
2020-01-29 20:37:25 +01:00
|
|
|
|
2020-08-18 15:44:21 +02:00
|
|
|
async function careAntenna() {
|
|
|
|
const beforeUnread = await Users.getHasUnreadAntenna(userId);
|
|
|
|
if (!beforeUnread) return;
|
2020-01-29 20:37:25 +01:00
|
|
|
|
2020-08-18 15:44:21 +02:00
|
|
|
const antennas = await Antennas.find({ userId });
|
2020-01-29 20:37:25 +01:00
|
|
|
|
2020-08-18 15:44:21 +02:00
|
|
|
await Promise.all(antennas.map(async antenna => {
|
|
|
|
const countBefore = await AntennaNotes.count({
|
|
|
|
antennaId: antenna.id,
|
|
|
|
read: false
|
|
|
|
});
|
2020-01-29 20:37:25 +01:00
|
|
|
|
2020-08-18 15:44:21 +02:00
|
|
|
if (countBefore === 0) return;
|
2020-01-29 20:37:25 +01:00
|
|
|
|
2020-08-18 15:44:21 +02:00
|
|
|
await AntennaNotes.update({
|
|
|
|
antennaId: antenna.id,
|
|
|
|
noteId: noteId
|
|
|
|
}, {
|
|
|
|
read: true
|
|
|
|
});
|
|
|
|
|
|
|
|
const countAfter = await AntennaNotes.count({
|
|
|
|
antennaId: antenna.id,
|
|
|
|
read: false
|
|
|
|
});
|
|
|
|
|
|
|
|
if (countAfter === 0) {
|
|
|
|
publishMainStream(userId, 'readAntenna', antenna);
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
|
|
|
Users.getHasUnreadAntenna(userId).then(unread => {
|
|
|
|
if (!unread) {
|
|
|
|
publishMainStream(userId, 'readAllAntennas');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
careNoteUnreads();
|
|
|
|
careAntenna();
|
|
|
|
}
|