2019-02-05 07:14:23 +02:00
|
|
|
import { publishMainStream } from '../stream';
|
2019-04-07 15:50:36 +03:00
|
|
|
import { Note } from '../../models/entities/note';
|
|
|
|
import { User } from '../../models/entities/user';
|
2020-01-29 21:37:25 +02:00
|
|
|
import { NoteUnreads, Antennas, AntennaNotes, Users } from '../../models';
|
2018-09-19 08:18:34 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Mark a note as read
|
|
|
|
*/
|
|
|
|
export default (
|
2019-04-07 15:50:36 +03:00
|
|
|
userId: User['id'],
|
|
|
|
noteId: Note['id']
|
2018-09-19 08:18:34 +03:00
|
|
|
) => new Promise<any>(async (resolve, reject) => {
|
|
|
|
// Remove document
|
2019-04-15 06:11:32 +03:00
|
|
|
/*const res = */await NoteUnreads.delete({
|
2018-09-19 08:18:34 +03:00
|
|
|
userId: userId,
|
|
|
|
noteId: noteId
|
|
|
|
});
|
|
|
|
|
2019-04-07 15:50:36 +03:00
|
|
|
// v11 TODO: https://github.com/typeorm/typeorm/issues/2415
|
2020-01-29 21:37:25 +02:00
|
|
|
//if (res.affected === 0) {
|
2019-04-15 06:11:32 +03:00
|
|
|
// return;
|
|
|
|
//}
|
2018-09-23 10:05:26 +03:00
|
|
|
|
2020-01-29 21:37:25 +02:00
|
|
|
const [count1, count2] = await Promise.all([
|
|
|
|
NoteUnreads.count({
|
|
|
|
userId: userId,
|
|
|
|
isSpecified: false
|
|
|
|
}),
|
|
|
|
NoteUnreads.count({
|
|
|
|
userId: userId,
|
|
|
|
isSpecified: true
|
|
|
|
})
|
|
|
|
]);
|
2018-09-19 08:18:34 +03:00
|
|
|
|
2020-01-29 21:37:25 +02:00
|
|
|
if (count1 === 0) {
|
2018-09-19 08:18:34 +03:00
|
|
|
// 全て既読になったイベントを発行
|
2018-10-07 05:06:17 +03:00
|
|
|
publishMainStream(userId, 'readAllUnreadMentions');
|
2018-09-19 08:18:34 +03:00
|
|
|
}
|
|
|
|
|
2020-01-29 21:37:25 +02:00
|
|
|
if (count2 === 0) {
|
2018-09-19 08:18:34 +03:00
|
|
|
// 全て既読になったイベントを発行
|
2018-10-07 05:06:17 +03:00
|
|
|
publishMainStream(userId, 'readAllUnreadSpecifiedNotes');
|
2018-09-19 08:18:34 +03:00
|
|
|
}
|
2020-01-29 21:37:25 +02:00
|
|
|
|
|
|
|
const antennas = await Antennas.find({ userId });
|
|
|
|
|
|
|
|
await Promise.all(antennas.map(async antenna => {
|
|
|
|
await AntennaNotes.update({
|
|
|
|
antennaId: antenna.id,
|
|
|
|
noteId: noteId
|
|
|
|
}, {
|
|
|
|
read: true
|
|
|
|
});
|
|
|
|
|
|
|
|
const count = await AntennaNotes.count({
|
|
|
|
antennaId: antenna.id,
|
|
|
|
read: false
|
|
|
|
});
|
|
|
|
|
|
|
|
if (count === 0) {
|
|
|
|
publishMainStream(userId, 'readAntenna', antenna);
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
|
|
|
Users.getHasUnreadAntenna(userId).then(unread => {
|
|
|
|
if (!unread) {
|
|
|
|
publishMainStream(userId, 'readAllAntennas');
|
|
|
|
}
|
|
|
|
})
|
2018-09-19 08:18:34 +03:00
|
|
|
});
|