2018-09-19 14:18:34 +09:00
|
|
|
import NoteUnread from '../../models/note-unread';
|
|
|
|
import User, { IUser } from '../../models/user';
|
|
|
|
import { INote } from '../../models/note';
|
|
|
|
import Mute from '../../models/mute';
|
2018-10-07 11:06:17 +09:00
|
|
|
import { publishMainStream } from '../../stream';
|
2018-09-19 14:18:34 +09:00
|
|
|
|
|
|
|
export default async function(user: IUser, note: INote, isSpecified = false) {
|
2018-09-21 08:37:26 +09:00
|
|
|
//#region ミュートしているなら無視
|
|
|
|
const mute = await Mute.find({
|
|
|
|
muterId: user._id
|
|
|
|
});
|
|
|
|
const mutedUserIds = mute.map(m => m.muteeId.toString());
|
|
|
|
if (mutedUserIds.includes(note.userId.toString())) return;
|
|
|
|
//#endregion
|
|
|
|
|
2018-09-19 14:18:34 +09:00
|
|
|
const unread = await NoteUnread.insert({
|
|
|
|
noteId: note._id,
|
|
|
|
userId: user._id,
|
|
|
|
isSpecified,
|
|
|
|
_note: {
|
|
|
|
userId: note.userId
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-10-08 02:10:46 +09:00
|
|
|
// 2秒経っても既読にならなかったら「未読の投稿がありますよ」イベントを発行する
|
2018-09-19 14:18:34 +09:00
|
|
|
setTimeout(async () => {
|
|
|
|
const exist = await NoteUnread.findOne({ _id: unread._id });
|
|
|
|
if (exist == null) return;
|
|
|
|
|
|
|
|
User.update({
|
|
|
|
_id: user._id
|
|
|
|
}, {
|
|
|
|
$set: isSpecified ? {
|
|
|
|
hasUnreadSpecifiedNotes: true,
|
|
|
|
hasUnreadMentions: true
|
|
|
|
} : {
|
|
|
|
hasUnreadMentions: true
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-10-07 11:06:17 +09:00
|
|
|
publishMainStream(user._id, 'unreadMention', note._id);
|
2018-09-19 14:18:34 +09:00
|
|
|
|
|
|
|
if (isSpecified) {
|
2018-10-07 11:06:17 +09:00
|
|
|
publishMainStream(user._id, 'unreadSpecifiedNote', note._id);
|
2018-09-19 14:18:34 +09:00
|
|
|
}
|
2018-10-08 02:10:46 +09:00
|
|
|
}, 2000);
|
2018-09-19 14:18:34 +09:00
|
|
|
}
|