BREAKING CHANGE: This upgrades the internally used Express framework from version 4 to 5, which may be a breaking change. If Parse Server is set up to be mounted on an Express application, we recommend to also use version 5 of the Express framework to avoid any compatibility issues. Note that even if there are no issues after upgrading, future releases of Parse Server may introduce issues if Parse Server internally relies on Express 5-specific features which are unsupported by the Express version on which it is mounted. See the Express [migration guide](https://expressjs.com/en/guide/migrating-5.html) and [release announcement](https://expressjs.com/2024/10/15/v5-release.html#breaking-changes) for more info.
84 lines
2.3 KiB
JavaScript
84 lines
2.3 KiB
JavaScript
import PromiseRouter from '../PromiseRouter';
|
|
import * as middleware from '../middlewares';
|
|
import { Parse } from 'parse/node';
|
|
|
|
export class PushRouter extends PromiseRouter {
|
|
mountRoutes() {
|
|
this.route('POST', '/push', middleware.promiseEnforceMasterKeyAccess, PushRouter.handlePOST);
|
|
}
|
|
|
|
static handlePOST(req) {
|
|
if (req.auth.isReadOnly) {
|
|
throw new Parse.Error(
|
|
Parse.Error.OPERATION_FORBIDDEN,
|
|
"read-only masterKey isn't allowed to send push notifications."
|
|
);
|
|
}
|
|
const pushController = req.config.pushController;
|
|
if (!pushController) {
|
|
throw new Parse.Error(Parse.Error.PUSH_MISCONFIGURED, 'Push controller is not set');
|
|
}
|
|
|
|
const where = PushRouter.getQueryCondition(req);
|
|
let resolve;
|
|
const promise = new Promise(_resolve => {
|
|
resolve = _resolve;
|
|
});
|
|
let pushStatusId;
|
|
pushController
|
|
.sendPush(req.body || {}, where, req.config, req.auth, objectId => {
|
|
pushStatusId = objectId;
|
|
resolve({
|
|
headers: {
|
|
'X-Parse-Push-Status-Id': pushStatusId,
|
|
},
|
|
response: {
|
|
result: true,
|
|
},
|
|
});
|
|
})
|
|
.catch(err => {
|
|
req.config.loggerController.error(
|
|
`_PushStatus ${pushStatusId}: error while sending push`,
|
|
err
|
|
);
|
|
});
|
|
return promise;
|
|
}
|
|
|
|
/**
|
|
* Get query condition from the request body.
|
|
* @param {Object} req A request object
|
|
* @returns {Object} The query condition, the where field in a query api call
|
|
*/
|
|
static getQueryCondition(req) {
|
|
const body = req.body || {};
|
|
const hasWhere = typeof body.where !== 'undefined';
|
|
const hasChannels = typeof body.channels !== 'undefined';
|
|
|
|
let where;
|
|
if (hasWhere && hasChannels) {
|
|
throw new Parse.Error(
|
|
Parse.Error.PUSH_MISCONFIGURED,
|
|
'Channels and query can not be set at the same time.'
|
|
);
|
|
} else if (hasWhere) {
|
|
where = body.where;
|
|
} else if (hasChannels) {
|
|
where = {
|
|
channels: {
|
|
$in: body.channels,
|
|
},
|
|
};
|
|
} else {
|
|
throw new Parse.Error(
|
|
Parse.Error.PUSH_MISCONFIGURED,
|
|
'Sending a push requires either "channels" or a "where" query.'
|
|
);
|
|
}
|
|
return where;
|
|
}
|
|
}
|
|
|
|
export default PushRouter;
|