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

51 lines
1.1 KiB
TypeScript
Raw Normal View History

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