2016-08-01 20:36:10 +00:00
|
|
|
import { authWithHeaders } from '../../middlewares/auth';
|
2016-06-22 22:19:37 +00:00
|
|
|
import {
|
|
|
|
|
NotFound,
|
2016-08-01 20:36:10 +00:00
|
|
|
} from '../../libs/errors';
|
2018-08-17 12:01:41 +00:00
|
|
|
import { model as PushDevice } from '../../models/pushDevice';
|
2025-09-25 21:04:45 +00:00
|
|
|
import { sendNotification as sendPushNotification } from '../../libs/pushNotifications';
|
2016-06-22 22:19:37 +00:00
|
|
|
|
2019-10-08 14:57:10 +00:00
|
|
|
const api = {};
|
2016-06-22 22:19:37 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @apiIgnore
|
|
|
|
|
* @api {post} /api/v3/user/push-devices Add a push device to a user
|
|
|
|
|
* @apiName UserAddPushDevice
|
|
|
|
|
* @apiGroup User
|
|
|
|
|
*
|
2017-08-15 23:06:19 +00:00
|
|
|
* @apiParam (Body) {String} regId The id of the push device
|
|
|
|
|
* @apiParam (Body) {String} type The type of push device
|
2016-06-22 22:19:37 +00:00
|
|
|
*
|
|
|
|
|
* @apiSuccess {Object} data List of push devices
|
2016-07-27 23:48:03 +00:00
|
|
|
* @apiSuccess {String} message Success message
|
2016-06-22 22:19:37 +00:00
|
|
|
*/
|
|
|
|
|
api.addPushDevice = {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
url: '/user/push-devices',
|
2018-09-21 13:12:20 +00:00
|
|
|
middlewares: [authWithHeaders()],
|
2016-06-22 22:19:37 +00:00
|
|
|
async handler (req, res) {
|
2019-10-08 14:57:10 +00:00
|
|
|
const { user } = res.locals;
|
2016-06-22 22:19:37 +00:00
|
|
|
|
|
|
|
|
req.checkBody('regId', res.t('regIdRequired')).notEmpty();
|
2025-09-17 23:28:17 +00:00
|
|
|
req.checkBody('type', res.t('typeRequired')).notEmpty().isIn(['ios', 'android', 'unifiedpush']);
|
2016-06-22 22:19:37 +00:00
|
|
|
|
2018-08-17 12:01:41 +00:00
|
|
|
const validationErrors = req.validationErrors();
|
2016-06-22 22:19:37 +00:00
|
|
|
if (validationErrors) throw validationErrors;
|
|
|
|
|
|
2019-10-08 14:57:10 +00:00
|
|
|
const { pushDevices } = user;
|
2016-06-22 22:19:37 +00:00
|
|
|
|
2018-08-17 12:01:41 +00:00
|
|
|
const item = {
|
2016-06-22 22:19:37 +00:00
|
|
|
regId: req.body.regId,
|
|
|
|
|
type: req.body.type,
|
|
|
|
|
};
|
|
|
|
|
|
2018-10-20 12:52:02 +00:00
|
|
|
// When adding a duplicate push device, fail silently instead of throwing an error
|
2016-06-22 22:19:37 +00:00
|
|
|
if (pushDevices.find(device => device.regId === item.regId)) {
|
2018-10-20 12:52:02 +00:00
|
|
|
res.respond(200, user.pushDevices, res.t('pushDeviceAdded'));
|
|
|
|
|
return;
|
2016-06-22 22:19:37 +00:00
|
|
|
}
|
|
|
|
|
|
2018-08-17 12:01:41 +00:00
|
|
|
// Concurrency safe update
|
|
|
|
|
const pushDevice = (new PushDevice(item)).toJSON(); // Create a mongo doc
|
2024-01-16 21:18:47 +00:00
|
|
|
await user.updateOne({
|
2018-08-17 12:01:41 +00:00
|
|
|
$push: { pushDevices: pushDevice },
|
|
|
|
|
}).exec();
|
2016-06-22 22:19:37 +00:00
|
|
|
|
2018-08-17 12:01:41 +00:00
|
|
|
// Update the response
|
|
|
|
|
user.pushDevices.push(pushDevice);
|
2016-06-22 22:19:37 +00:00
|
|
|
|
|
|
|
|
res.respond(200, user.pushDevices, res.t('pushDeviceAdded'));
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
2025-09-25 21:04:45 +00:00
|
|
|
/**
|
|
|
|
|
* @apiIgnore
|
|
|
|
|
* @api {post} /api/v3/user/push-devices/test Send a test push notification
|
|
|
|
|
* @apiName UserSendTestPushNotification
|
|
|
|
|
* @apiGroup User
|
|
|
|
|
*
|
|
|
|
|
* @apiParam (Body) {String} [regId] The id of a specific push device to target
|
|
|
|
|
*
|
|
|
|
|
* @apiSuccess {String} message Success message
|
|
|
|
|
*/
|
|
|
|
|
api.sendUnifiedPushTest = {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
url: '/user/push-devices/test',
|
|
|
|
|
middlewares: [authWithHeaders()],
|
|
|
|
|
async handler (req, res) {
|
|
|
|
|
const { user } = res.locals;
|
|
|
|
|
|
|
|
|
|
const regId = req.body?.regId;
|
|
|
|
|
const pushDevices = user.pushDevices?.toObject ? user.pushDevices.toObject() : user.pushDevices;
|
|
|
|
|
let unifiedPushDevices = (pushDevices || []).filter(device => device?.type === 'unifiedpush');
|
|
|
|
|
|
|
|
|
|
if (regId) {
|
|
|
|
|
unifiedPushDevices = unifiedPushDevices.filter(device => device.regId === regId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (unifiedPushDevices.length === 0) {
|
|
|
|
|
throw new NotFound(res.t('pushDeviceNotFound'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const notificationTitle = req.body?.title
|
|
|
|
|
|| res.t('unifiedPushTestTitle', { defaultValue: 'Habitica UnifiedPush Test' });
|
|
|
|
|
const notificationMessage = req.body?.message
|
|
|
|
|
|| res.t('unifiedPushTestMessage', { defaultValue: 'This is a test UnifiedPush notification from Habitica.' });
|
|
|
|
|
const successMessage = res.t('unifiedPushTestSent', { defaultValue: 'UnifiedPush test notification sent.' });
|
|
|
|
|
|
|
|
|
|
const userForPush = user.toObject ? user.toObject() : { ...user };
|
|
|
|
|
userForPush._id = user._id;
|
|
|
|
|
userForPush.pushDevices = unifiedPushDevices;
|
|
|
|
|
|
|
|
|
|
await sendPushNotification(userForPush, {
|
|
|
|
|
identifier: 'unifiedPushTestNotification',
|
|
|
|
|
title: notificationTitle,
|
|
|
|
|
message: notificationMessage,
|
|
|
|
|
payload: {
|
|
|
|
|
message: notificationMessage,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
res.respond(200, null, successMessage);
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
2016-06-22 22:19:37 +00:00
|
|
|
/**
|
|
|
|
|
* @apiIgnore
|
2017-08-15 23:06:19 +00:00
|
|
|
* @api {delete} /api/v3/user/push-devices/:regId remove a push device from a user
|
2016-06-22 22:19:37 +00:00
|
|
|
* @apiName UserRemovePushDevice
|
|
|
|
|
* @apiGroup User
|
|
|
|
|
*
|
2017-08-15 23:06:19 +00:00
|
|
|
* @apiParam (Path) {String} regId The id of the push device
|
2016-06-22 22:19:37 +00:00
|
|
|
*
|
|
|
|
|
* @apiSuccess {Object} data List of push devices
|
2016-07-27 23:48:03 +00:00
|
|
|
* @apiSuccess {String} message Success message
|
2016-06-22 22:19:37 +00:00
|
|
|
*/
|
|
|
|
|
api.removePushDevice = {
|
|
|
|
|
method: 'DELETE',
|
|
|
|
|
url: '/user/push-devices/:regId',
|
2018-09-21 13:12:20 +00:00
|
|
|
middlewares: [authWithHeaders()],
|
2016-06-22 22:19:37 +00:00
|
|
|
async handler (req, res) {
|
2019-10-08 14:57:10 +00:00
|
|
|
const { user } = res.locals;
|
2016-06-22 22:19:37 +00:00
|
|
|
|
|
|
|
|
req.checkParams('regId', res.t('regIdRequired')).notEmpty();
|
2018-08-17 12:01:41 +00:00
|
|
|
|
|
|
|
|
const validationErrors = req.validationErrors();
|
2016-06-22 22:19:37 +00:00
|
|
|
if (validationErrors) throw validationErrors;
|
|
|
|
|
|
2019-10-08 14:57:10 +00:00
|
|
|
const { regId } = req.params;
|
2018-08-17 12:01:41 +00:00
|
|
|
|
2019-10-08 14:57:10 +00:00
|
|
|
const { pushDevices } = user;
|
2016-06-22 22:19:37 +00:00
|
|
|
|
2019-10-08 14:57:10 +00:00
|
|
|
const indexOfPushDevice = pushDevices.findIndex(element => element.regId === regId);
|
2016-06-22 22:19:37 +00:00
|
|
|
|
|
|
|
|
if (indexOfPushDevice === -1) {
|
|
|
|
|
throw new NotFound(res.t('pushDeviceNotFound'));
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-17 12:01:41 +00:00
|
|
|
// Concurrency safe update
|
2019-10-13 16:31:43 +00:00
|
|
|
const pullQuery = { $pull: { pushDevices: { regId } } };
|
2024-01-16 21:18:47 +00:00
|
|
|
await user.updateOne(pullQuery).exec();
|
2018-08-17 12:01:41 +00:00
|
|
|
|
|
|
|
|
// Update the response
|
2016-06-22 22:19:37 +00:00
|
|
|
pushDevices.splice(indexOfPushDevice, 1);
|
|
|
|
|
|
|
|
|
|
res.respond(200, user.pushDevices, res.t('pushDeviceRemoved'));
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
2019-10-02 17:45:27 +00:00
|
|
|
export default api;
|