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