2018-09-01 13:58:06 -04:00
|
|
|
import PromiseRouter from '../PromiseRouter';
|
|
|
|
|
import * as middleware from '../middlewares';
|
|
|
|
|
import { Parse } from 'parse/node';
|
2016-02-20 10:49:32 -05:00
|
|
|
|
|
|
|
|
export class PushRouter extends PromiseRouter {
|
|
|
|
|
mountRoutes() {
|
2020-10-25 15:06:58 -05:00
|
|
|
this.route('POST', '/push', middleware.promiseEnforceMasterKeyAccess, PushRouter.handlePOST);
|
2016-02-20 10:49:32 -05:00
|
|
|
}
|
|
|
|
|
|
2016-03-07 14:23:05 -08:00
|
|
|
static handlePOST(req) {
|
2017-10-26 15:35:07 -04:00
|
|
|
if (req.auth.isReadOnly) {
|
2018-09-01 13:58:06 -04:00
|
|
|
throw new Parse.Error(
|
|
|
|
|
Parse.Error.OPERATION_FORBIDDEN,
|
|
|
|
|
"read-only masterKey isn't allowed to send push notifications."
|
|
|
|
|
);
|
2017-10-26 15:35:07 -04:00
|
|
|
}
|
2016-02-20 10:49:32 -05:00
|
|
|
const pushController = req.config.pushController;
|
|
|
|
|
if (!pushController) {
|
2020-10-25 15:06:58 -05:00
|
|
|
throw new Parse.Error(Parse.Error.PUSH_MISCONFIGURED, 'Push controller is not set');
|
2016-02-20 10:49:32 -05:00
|
|
|
}
|
|
|
|
|
|
2016-12-07 15:17:05 -08:00
|
|
|
const where = PushRouter.getQueryCondition(req);
|
2016-04-07 18:08:09 -04:00
|
|
|
let resolve;
|
2020-07-13 17:13:08 -05:00
|
|
|
const promise = new Promise(_resolve => {
|
2016-04-07 18:08:09 -04:00
|
|
|
resolve = _resolve;
|
|
|
|
|
});
|
2017-09-17 09:57:07 -04:00
|
|
|
let pushStatusId;
|
2018-09-01 13:58:06 -04:00
|
|
|
pushController
|
2020-07-13 17:13:08 -05:00
|
|
|
.sendPush(req.body, where, req.config, req.auth, objectId => {
|
2018-09-01 13:58:06 -04:00
|
|
|
pushStatusId = objectId;
|
|
|
|
|
resolve({
|
|
|
|
|
headers: {
|
|
|
|
|
'X-Parse-Push-Status-Id': pushStatusId,
|
|
|
|
|
},
|
|
|
|
|
response: {
|
|
|
|
|
result: true,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
})
|
2020-07-13 17:13:08 -05:00
|
|
|
.catch(err => {
|
2018-09-01 13:58:06 -04:00
|
|
|
req.config.loggerController.error(
|
|
|
|
|
`_PushStatus ${pushStatusId}: error while sending push`,
|
|
|
|
|
err
|
|
|
|
|
);
|
2016-04-07 18:08:09 -04:00
|
|
|
});
|
|
|
|
|
return promise;
|
2016-02-20 10:49:32 -05:00
|
|
|
}
|
2016-03-07 14:23:05 -08:00
|
|
|
|
|
|
|
|
/**
|
2016-02-20 10:49:32 -05:00
|
|
|
* Get query condition from the request body.
|
2016-03-07 14:23:05 -08:00
|
|
|
* @param {Object} req A request object
|
2016-02-20 10:49:32 -05:00
|
|
|
* @returns {Object} The query condition, the where field in a query api call
|
|
|
|
|
*/
|
|
|
|
|
static getQueryCondition(req) {
|
2016-12-07 15:17:05 -08:00
|
|
|
const body = req.body || {};
|
|
|
|
|
const hasWhere = typeof body.where !== 'undefined';
|
|
|
|
|
const hasChannels = typeof body.channels !== 'undefined';
|
2016-02-20 10:49:32 -05:00
|
|
|
|
2016-03-07 14:23:05 -08:00
|
|
|
let where;
|
2016-02-20 10:49:32 -05:00
|
|
|
if (hasWhere && hasChannels) {
|
2018-09-01 13:58:06 -04:00
|
|
|
throw new Parse.Error(
|
|
|
|
|
Parse.Error.PUSH_MISCONFIGURED,
|
|
|
|
|
'Channels and query can not be set at the same time.'
|
|
|
|
|
);
|
2016-02-20 10:49:32 -05:00
|
|
|
} else if (hasWhere) {
|
|
|
|
|
where = body.where;
|
|
|
|
|
} else if (hasChannels) {
|
|
|
|
|
where = {
|
2018-09-01 13:58:06 -04:00
|
|
|
channels: {
|
|
|
|
|
$in: body.channels,
|
|
|
|
|
},
|
|
|
|
|
};
|
2016-02-20 10:49:32 -05:00
|
|
|
} else {
|
2018-09-01 13:58:06 -04:00
|
|
|
throw new Parse.Error(
|
|
|
|
|
Parse.Error.PUSH_MISCONFIGURED,
|
|
|
|
|
'Sending a push requires either "channels" or a "where" query.'
|
|
|
|
|
);
|
2016-02-20 10:49:32 -05:00
|
|
|
}
|
|
|
|
|
return where;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default PushRouter;
|