2018-09-19 08:18:34 +03:00
|
|
|
import * as mongo from 'mongodb';
|
2018-10-16 05:38:09 +03:00
|
|
|
import isObjectId from '../../misc/is-objectid';
|
2018-10-07 05:06:17 +03:00
|
|
|
import { publishMainStream } from '../../stream';
|
2018-09-19 08:18:34 +03:00
|
|
|
import User from '../../models/user';
|
|
|
|
import NoteUnread from '../../models/note-unread';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mark a note as read
|
|
|
|
*/
|
|
|
|
export default (
|
|
|
|
user: string | mongo.ObjectID,
|
|
|
|
note: string | mongo.ObjectID
|
|
|
|
) => new Promise<any>(async (resolve, reject) => {
|
|
|
|
|
2018-10-16 05:38:09 +03:00
|
|
|
const userId: mongo.ObjectID = isObjectId(user)
|
2018-09-19 08:18:34 +03:00
|
|
|
? user as mongo.ObjectID
|
|
|
|
: new mongo.ObjectID(user);
|
|
|
|
|
2018-10-16 05:38:09 +03:00
|
|
|
const noteId: mongo.ObjectID = isObjectId(note)
|
2018-09-19 08:18:34 +03:00
|
|
|
? note as mongo.ObjectID
|
|
|
|
: new mongo.ObjectID(note);
|
|
|
|
|
|
|
|
// Remove document
|
2018-09-23 10:05:26 +03:00
|
|
|
const res = await NoteUnread.remove({
|
2018-09-19 08:18:34 +03:00
|
|
|
userId: userId,
|
|
|
|
noteId: noteId
|
|
|
|
});
|
|
|
|
|
2018-09-23 10:05:26 +03:00
|
|
|
if (res.deletedCount == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-09-19 08:18:34 +03:00
|
|
|
const count1 = await NoteUnread
|
|
|
|
.count({
|
|
|
|
userId: userId,
|
|
|
|
isSpecified: false
|
|
|
|
}, {
|
|
|
|
limit: 1
|
|
|
|
});
|
|
|
|
|
|
|
|
const count2 = await NoteUnread
|
|
|
|
.count({
|
|
|
|
userId: userId,
|
|
|
|
isSpecified: true
|
|
|
|
}, {
|
|
|
|
limit: 1
|
|
|
|
});
|
|
|
|
|
|
|
|
if (count1 == 0 || count2 == 0) {
|
|
|
|
User.update({ _id: userId }, {
|
|
|
|
$set: {
|
|
|
|
hasUnreadMentions: count1 != 0 || count2 != 0,
|
|
|
|
hasUnreadSpecifiedNotes: count2 != 0
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count1 == 0) {
|
|
|
|
// 全て既読になったイベントを発行
|
2018-10-07 05:06:17 +03:00
|
|
|
publishMainStream(userId, 'readAllUnreadMentions');
|
2018-09-19 08:18:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (count2 == 0) {
|
|
|
|
// 全て既読になったイベントを発行
|
2018-10-07 05:06:17 +03:00
|
|
|
publishMainStream(userId, 'readAllUnreadSpecifiedNotes');
|
2018-09-19 08:18:34 +03:00
|
|
|
}
|
|
|
|
});
|