habitica-self-host/website/server/controllers/api-v3/pushNotifications.js

105 lines
2.8 KiB
JavaScript
Raw Normal View History

import { authWithHeaders } from '../../middlewares/auth';
import {
NotFound,
} from '../../libs/errors';
import { model as PushDevice } from '../../models/pushDevice';
2019-10-08 14:57:10 +00:00
const api = {};
/**
* @apiIgnore
* @api {post} /api/v3/user/push-devices Add a push device to a user
* @apiName UserAddPushDevice
* @apiGroup User
*
* @apiParam (Body) {String} regId The id of the push device
* @apiParam (Body) {String} type The type of push device
*
* @apiSuccess {Object} data List of push devices
* @apiSuccess {String} message Success message
*/
api.addPushDevice = {
method: 'POST',
url: '/user/push-devices',
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 13:12:20 +00:00
middlewares: [authWithHeaders()],
async handler (req, res) {
2019-10-08 14:57:10 +00:00
const { user } = res.locals;
req.checkBody('regId', res.t('regIdRequired')).notEmpty();
req.checkBody('type', res.t('typeRequired')).notEmpty().isIn(['ios', 'android']);
const validationErrors = req.validationErrors();
if (validationErrors) throw validationErrors;
2019-10-08 14:57:10 +00:00
const { pushDevices } = user;
const item = {
regId: req.body.regId,
type: req.body.type,
};
// When adding a duplicate push device, fail silently instead of throwing an error
if (pushDevices.find(device => device.regId === item.regId)) {
res.respond(200, user.pushDevices, res.t('pushDeviceAdded'));
return;
}
// Concurrency safe update
const pushDevice = (new PushDevice(item)).toJSON(); // Create a mongo doc
await user.update({
$push: { pushDevices: pushDevice },
}).exec();
// Update the response
user.pushDevices.push(pushDevice);
res.respond(200, user.pushDevices, res.t('pushDeviceAdded'));
},
};
/**
* @apiIgnore
* @api {delete} /api/v3/user/push-devices/:regId remove a push device from a user
* @apiName UserRemovePushDevice
* @apiGroup User
*
* @apiParam (Path) {String} regId The id of the push device
*
* @apiSuccess {Object} data List of push devices
* @apiSuccess {String} message Success message
*/
api.removePushDevice = {
method: 'DELETE',
url: '/user/push-devices/:regId',
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 13:12:20 +00:00
middlewares: [authWithHeaders()],
async handler (req, res) {
2019-10-08 14:57:10 +00:00
const { user } = res.locals;
req.checkParams('regId', res.t('regIdRequired')).notEmpty();
const validationErrors = req.validationErrors();
if (validationErrors) throw validationErrors;
2019-10-08 14:57:10 +00:00
const { regId } = req.params;
2019-10-08 14:57:10 +00:00
const { pushDevices } = user;
2019-10-08 14:57:10 +00:00
const indexOfPushDevice = pushDevices.findIndex(element => element.regId === regId);
if (indexOfPushDevice === -1) {
throw new NotFound(res.t('pushDeviceNotFound'));
}
// Concurrency safe update
const pullQuery = { $pull: { pushDevices: { $elemMatch: { regId } } } };
await user.update(pullQuery).exec();
// Update the response
pushDevices.splice(indexOfPushDevice, 1);
res.respond(200, user.pushDevices, res.t('pushDeviceRemoved'));
},
};
export default api;