misskey/src/api/endpoints/notifications/mark_as_read.ts

48 lines
1.0 KiB
TypeScript
Raw Normal View History

2016-12-29 07:49:51 +09:00
/**
* Module dependencies
*/
2017-03-09 03:50:09 +09:00
import $ from 'cafy';
2017-03-03 08:24:48 +09:00
import Notification from '../../models/notification';
import serialize from '../../serializers/notification';
import event from '../../event';
2016-12-29 07:49:51 +09:00
/**
* Mark as read a notification
*
2017-03-01 17:37:01 +09:00
* @param {any} params
* @param {any} user
* @return {Promise<any>}
2016-12-29 07:49:51 +09:00
*/
2017-03-04 04:28:38 +09:00
module.exports = (params, user) => new Promise(async (res, rej) => {
2017-03-09 03:50:09 +09:00
const [notificationId, notificationIdErr] = $(params.notification_id).id().$;
2017-03-04 04:28:38 +09:00
if (notificationIdErr) return rej('invalid notification_id param');
// Get notification
const notification = await Notification
.findOne({
_id: notificationId,
i: user._id
});
2016-12-29 07:49:51 +09:00
2017-03-04 04:28:38 +09:00
if (notification === null) {
return rej('notification-not-found');
}
2016-12-29 07:49:51 +09:00
2017-03-04 04:28:38 +09:00
// Update
notification.is_read = true;
Notification.update({ _id: notification._id }, {
$set: {
is_read: true
2016-12-29 07:49:51 +09:00
}
2017-03-04 04:28:38 +09:00
});
2016-12-29 07:49:51 +09:00
2017-03-04 04:28:38 +09:00
// Response
res();
2016-12-29 07:49:51 +09:00
2017-03-04 04:28:38 +09:00
// Serialize
const notificationObj = await serialize(notification);
2016-12-29 07:49:51 +09:00
2017-03-04 04:28:38 +09:00
// Publish read_notification event
event(user._id, 'read_notification', notificationObj);
});