summaryrefslogtreecommitdiff
path: root/src/services/note/read.ts
diff options
context:
space:
mode:
authorsyuilo <syuilotan@yahoo.co.jp>2018-09-19 14:18:34 +0900
committersyuilo <syuilotan@yahoo.co.jp>2018-09-19 14:18:34 +0900
commitd9f0e158a35eec183da77e84a3b038fab645bf62 (patch)
treea7cbad45883ff56d35771d849f95dbd911e3e45c /src/services/note/read.ts
parent8.55.0 (diff)
downloadmisskey-d9f0e158a35eec183da77e84a3b038fab645bf62.tar.gz
misskey-d9f0e158a35eec183da77e84a3b038fab645bf62.tar.bz2
misskey-d9f0e158a35eec183da77e84a3b038fab645bf62.zip
Implement #2736
Diffstat (limited to 'src/services/note/read.ts')
-rw-r--r--src/services/note/read.ts62
1 files changed, 62 insertions, 0 deletions
diff --git a/src/services/note/read.ts b/src/services/note/read.ts
new file mode 100644
index 0000000000..46918bc38c
--- /dev/null
+++ b/src/services/note/read.ts
@@ -0,0 +1,62 @@
+import * as mongo from 'mongodb';
+import { publishUserStream } from '../../stream';
+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) => {
+
+ const userId: mongo.ObjectID = mongo.ObjectID.prototype.isPrototypeOf(user)
+ ? user as mongo.ObjectID
+ : new mongo.ObjectID(user);
+
+ const noteId: mongo.ObjectID = mongo.ObjectID.prototype.isPrototypeOf(note)
+ ? note as mongo.ObjectID
+ : new mongo.ObjectID(note);
+
+ // Remove document
+ await NoteUnread.remove({
+ userId: userId,
+ noteId: noteId
+ });
+
+ 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) {
+ // 全て既読になったイベントを発行
+ publishUserStream(userId, 'readAllUnreadMentions');
+ }
+
+ if (count2 == 0) {
+ // 全て既読になったイベントを発行
+ publishUserStream(userId, 'readAllUnreadSpecifiedNotes');
+ }
+});