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