mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-17 06:37:23 +01:00
Push notifications (#7682)
* Fix Social Push notifications * Fix code formatting issues * Fix commented issues * Fix Syntax errors * update push notify dependency * specify push-notify version * change how apn key is loaded * feat(push-notifications): improve logging * feat(push-notifications): disable v2 push notifications * test(push-notifications): add unit tests and improve integration ones * fix(push-notifications): throw when required params are missing * fix(tests): correct descriptions and remove wrong comment * fix(push-notifications): trim APN key * fix(apn): log feedback only if it has data * fix(apn): load cert and key differently * fix(tests): correctly load apn during tests * download creds from S3 and create AWS lib * convert s3 buffer to a string * fix(apn): remove console.log and do not use cert twice * invert key and cert, disable failing test * invert key and cert
This commit is contained in:
95
website/server/controllers/api-v3/pushNotifications.js
Normal file
95
website/server/controllers/api-v3/pushNotifications.js
Normal file
@@ -0,0 +1,95 @@
|
||||
import { authWithHeaders } from '../../middlewares/api-v3/auth';
|
||||
import {
|
||||
NotAuthorized,
|
||||
NotFound,
|
||||
} from '../../libs/api-v3/errors';
|
||||
|
||||
let api = {};
|
||||
|
||||
/**
|
||||
* @apiIgnore
|
||||
* @api {post} /api/v3/user/push-devices Add a push device to a user
|
||||
* @apiVersion 3.0.0
|
||||
* @apiName UserAddPushDevice
|
||||
* @apiGroup User
|
||||
*
|
||||
* @apiParam {string} regId The id of the push device
|
||||
* @apiParam {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) {
|
||||
let user = res.locals.user;
|
||||
|
||||
req.checkBody('regId', res.t('regIdRequired')).notEmpty();
|
||||
req.checkBody('type', res.t('typeRequired')).notEmpty().isIn(['ios', 'android']);
|
||||
|
||||
let validationErrors = req.validationErrors();
|
||||
if (validationErrors) throw validationErrors;
|
||||
|
||||
let pushDevices = user.pushDevices;
|
||||
|
||||
let item = {
|
||||
regId: req.body.regId,
|
||||
type: req.body.type,
|
||||
};
|
||||
|
||||
if (pushDevices.find(device => device.regId === item.regId)) {
|
||||
throw new NotAuthorized(res.t('pushDeviceAlreadyAdded'));
|
||||
}
|
||||
|
||||
pushDevices.push(item);
|
||||
|
||||
await user.save();
|
||||
|
||||
res.respond(200, user.pushDevices, res.t('pushDeviceAdded'));
|
||||
},
|
||||
};
|
||||
|
||||
/**
|
||||
* @apiIgnore
|
||||
* @api {delete} /api/v3/user/push-devices remove a push device from a user
|
||||
* @apiVersion 3.0.0
|
||||
* @apiName UserRemovePushDevice
|
||||
* @apiGroup User
|
||||
*
|
||||
* @apiParam {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) {
|
||||
let user = res.locals.user;
|
||||
|
||||
req.checkParams('regId', res.t('regIdRequired')).notEmpty();
|
||||
let validationErrors = req.validationErrors();
|
||||
if (validationErrors) throw validationErrors;
|
||||
let regId = req.params.regId;
|
||||
|
||||
let pushDevices = user.pushDevices;
|
||||
|
||||
let indexOfPushDevice = pushDevices.findIndex((element) => {
|
||||
return element.regId === regId;
|
||||
});
|
||||
|
||||
if (indexOfPushDevice === -1) {
|
||||
throw new NotFound(res.t('pushDeviceNotFound'));
|
||||
}
|
||||
|
||||
pushDevices.splice(indexOfPushDevice, 1);
|
||||
await user.save();
|
||||
|
||||
res.respond(200, user.pushDevices, res.t('pushDeviceRemoved'));
|
||||
},
|
||||
};
|
||||
|
||||
module.exports = api;
|
||||
Reference in New Issue
Block a user