mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-17 22:57:21 +01:00
* 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
106 lines
2.8 KiB
JavaScript
106 lines
2.8 KiB
JavaScript
import { authWithHeaders } from '../../middlewares/auth';
|
|
import {
|
|
NotAuthorized,
|
|
NotFound,
|
|
} from '../../libs/errors';
|
|
import { model as PushDevice } from '../../models/pushDevice';
|
|
|
|
let 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',
|
|
middlewares: [authWithHeaders()],
|
|
async handler (req, res) {
|
|
const user = res.locals.user;
|
|
|
|
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;
|
|
|
|
const pushDevices = user.pushDevices;
|
|
|
|
const item = {
|
|
regId: req.body.regId,
|
|
type: req.body.type,
|
|
};
|
|
|
|
if (pushDevices.find(device => device.regId === item.regId)) {
|
|
throw new NotAuthorized(res.t('pushDeviceAlreadyAdded'));
|
|
}
|
|
|
|
// 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',
|
|
middlewares: [authWithHeaders()],
|
|
async handler (req, res) {
|
|
const user = res.locals.user;
|
|
|
|
req.checkParams('regId', res.t('regIdRequired')).notEmpty();
|
|
|
|
const validationErrors = req.validationErrors();
|
|
if (validationErrors) throw validationErrors;
|
|
|
|
const regId = req.params.regId;
|
|
|
|
const pushDevices = user.pushDevices;
|
|
|
|
const indexOfPushDevice = pushDevices.findIndex((element) => {
|
|
return 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'));
|
|
},
|
|
};
|
|
|
|
module.exports = api;
|