summaryrefslogtreecommitdiff
path: root/src/api/endpoints/notifications
diff options
context:
space:
mode:
authorsyuilo <syuilotan@yahoo.co.jp>2016-12-29 07:49:51 +0900
committersyuilo <syuilotan@yahoo.co.jp>2016-12-29 07:49:51 +0900
commitb3f42e62af698a67c2250533c437569559f1fdf9 (patch)
treecdf6937576e99cccf85e6fa3aa8860a1173c7cfb /src/api/endpoints/notifications
downloadsharkey-b3f42e62af698a67c2250533c437569559f1fdf9.tar.gz
sharkey-b3f42e62af698a67c2250533c437569559f1fdf9.tar.bz2
sharkey-b3f42e62af698a67c2250533c437569559f1fdf9.zip
Initial commit :four_leaf_clover:
Diffstat (limited to 'src/api/endpoints/notifications')
-rw-r--r--src/api/endpoints/notifications/mark_as_read.js54
1 files changed, 54 insertions, 0 deletions
diff --git a/src/api/endpoints/notifications/mark_as_read.js b/src/api/endpoints/notifications/mark_as_read.js
new file mode 100644
index 0000000000..16eb2009ac
--- /dev/null
+++ b/src/api/endpoints/notifications/mark_as_read.js
@@ -0,0 +1,54 @@
+'use strict';
+
+/**
+ * Module dependencies
+ */
+import * as mongo from 'mongodb';
+import Notification from '../../../models/notification';
+import serialize from '../../../serializers/notification';
+import event from '../../../event';
+
+/**
+ * Mark as read a notification
+ *
+ * @param {Object} params
+ * @param {Object} user
+ * @return {Promise<object>}
+ */
+module.exports = (params, user) =>
+ new Promise(async (res, rej) =>
+{
+ const notificationId = params.notification;
+
+ if (notificationId === undefined || notificationId === null) {
+ return rej('notification is required');
+ }
+
+ // Get notifcation
+ const notification = await Notification
+ .findOne({
+ _id: new mongo.ObjectID(notificationId),
+ i: user._id
+ });
+
+ if (notification === null) {
+ return rej('notification-not-found');
+ }
+
+ // Update
+ notification.is_read = true;
+ Notification.updateOne({ _id: notification._id }, {
+ $set: {
+ is_read: true
+ }
+ });
+
+ // Response
+ res();
+
+ // Serialize
+ const notificationObj = await serialize(notification);
+
+ // Publish read_notification event
+ event(user._id, 'read_notification', notificationObj);
+});