Files
habitica/website/server/controllers/api-v3/notifications.js
Matteo Pagliazzi 26c8323e70 Move inbox to its own model (#10428)
* shared model for chat and inbox

* disable inbox schema

* inbox: use separate model

* remove old code that used group.chat

* add back chat field (not used) and remove old tests

* remove inbox exclusions when loading user

* add GET /api/v3/inbox/messages

* add comment

* implement DELETE /inbox/messages/:messageid in v4

* implement GET /inbox/messages in v4 and update tests

* implement DELETE /api/v4/inbox/clear

* fix url

* fix doc

* update /export/inbox.html

* update other data exports

* add back messages in user schema

* add user.toJSONWithInbox

* add compativility until migration is done

* more compatibility

* fix tojson called twice

* add compatibility methods

* fix common tests

* fix v4 integration tests

* v3 get user -> with inbox

* start to fix tests

* fix v3 integration tests

* wip

* wip, client use new route

* update tests for members/send-private-message

* tests for get user in v4

* add tests for DELETE /inbox/messages/:messageId

* add tests for DELETE /inbox/clear in v4

* update docs

* fix tests

* initial migration

* fix migration

* fix migration

* migration fixes

* migrate api.enterCouponCode

* migrate api.castSpell

* migrate reset, reroll, rebirth

* add routes to v4 version

* fix tests

* fixes

* api.updateUser

* remove .only

* get user -> userLib

* refactor inbox.vue to work with new data model

* fix return message when messaging yourself

* wip fix bug with new conversation

* wip

* fix remaining ui issues

* move api.registerLocal, fixes

* keep only v3 version of GET /inbox/messages
2018-09-21 15:12:20 +02:00

195 lines
5.3 KiB
JavaScript

import { authWithHeaders } from '../../middlewares/auth';
import {
NotFound,
} from '../../libs/errors';
import {
model as User,
} from '../../models/user';
import {
model as UserNotification,
} from '../../models/userNotification';
let api = {};
/**
* @api {post} /api/v3/notifications/:notificationId/read Mark one notification as read
* @apiName ReadNotification
* @apiGroup Notification
*
* @apiParam (Path) {UUID} notificationId
*
* @apiSuccess {Object} data user.notifications
*/
api.readNotification = {
method: 'POST',
url: '/notifications/:notificationId/read',
middlewares: [authWithHeaders()],
async handler (req, res) {
let user = res.locals.user;
req.checkParams('notificationId', res.t('notificationIdRequired')).notEmpty();
let validationErrors = req.validationErrors();
if (validationErrors) throw validationErrors;
const index = user.notifications.findIndex(n => {
return n && n.id === req.params.notificationId;
});
if (index === -1) {
throw new NotFound(res.t('messageNotificationNotFound'));
}
user.notifications.splice(index, 1);
// Update the user version field manually,
// it cannot be updated in the pre update hook
// See https://github.com/HabitRPG/habitica/pull/9321#issuecomment-354187666 for more info
user._v++;
await user.update({
$pull: { notifications: { id: req.params.notificationId } },
}).exec();
res.respond(200, UserNotification.convertNotificationsToSafeJson(user.notifications));
},
};
/**
* @api {post} /api/v3/notifications/read Mark multiple notifications as read
* @apiName ReadNotifications
* @apiGroup Notification
*
* @apiSuccess {Object} data user.notifications
*/
api.readNotifications = {
method: 'POST',
url: '/notifications/read',
middlewares: [authWithHeaders()],
async handler (req, res) {
let user = res.locals.user;
req.checkBody('notificationIds', res.t('notificationsRequired')).notEmpty();
let validationErrors = req.validationErrors();
if (validationErrors) throw validationErrors;
let notificationsIds = req.body.notificationIds;
for (let notificationId of notificationsIds) {
const index = user.notifications.findIndex(n => {
return n && n.id === notificationId;
});
if (index === -1) {
throw new NotFound(res.t('messageNotificationNotFound'));
}
user.notifications.splice(index, 1);
}
await user.update({
$pull: { notifications: { id: { $in: notificationsIds } } },
}).exec();
// Update the user version field manually,
// it cannot be updated in the pre update hook
// See https://github.com/HabitRPG/habitica/pull/9321#issuecomment-354187666 for more info
user._v++;
res.respond(200, UserNotification.convertNotificationsToSafeJson(user.notifications));
},
};
/**
* @api {post} /api/v3/notifications/:notificationId/see Mark one notification as seen
* @apiDescription Mark a notification as seen. Different from marking them as read in that the notification isn't removed but the `seen` field is set to `true`
* @apiName SeeNotification
* @apiGroup Notification
*
* @apiParam (Path) {UUID} notificationId
*
* @apiSuccess {Object} data The modified notification
*/
api.seeNotification = {
method: 'POST',
url: '/notifications/:notificationId/see',
middlewares: [authWithHeaders()],
async handler (req, res) {
let user = res.locals.user;
req.checkParams('notificationId', res.t('notificationIdRequired')).notEmpty();
let validationErrors = req.validationErrors();
if (validationErrors) throw validationErrors;
const notificationId = req.params.notificationId;
const notification = user.notifications.find(n => {
return n && n.id === notificationId;
});
if (!notification) {
throw new NotFound(res.t('messageNotificationNotFound'));
}
notification.seen = true;
await User.update({
_id: user._id,
'notifications.id': notificationId,
}, {
$set: {
'notifications.$.seen': true,
},
}).exec();
// Update the user version field manually,
// it cannot be updated in the pre update hook
// See https://github.com/HabitRPG/habitica/pull/9321#issuecomment-354187666 for more info
user._v++;
res.respond(200, notification);
},
};
/**
* @api {post} /api/v3/notifications/see Mark multiple notifications as seen
* @apiName SeeNotifications
* @apiGroup Notification
*
* @apiSuccess {Object} data user.notifications
*/
api.seeNotifications = {
method: 'POST',
url: '/notifications/see',
middlewares: [authWithHeaders()],
async handler (req, res) {
let user = res.locals.user;
req.checkBody('notificationIds', res.t('notificationsRequired')).notEmpty();
let validationErrors = req.validationErrors();
if (validationErrors) throw validationErrors;
let notificationsIds = req.body.notificationIds;
for (let notificationId of notificationsIds) {
const notification = user.notifications.find(n => {
return n && n.id === notificationId;
});
if (!notification) {
throw new NotFound(res.t('messageNotificationNotFound'));
}
notification.seen = true;
}
await user.save();
res.respond(200, UserNotification.convertNotificationsToSafeJson(user.notifications));
},
};
module.exports = api;