Files
kami-parse-server/src/Routers/PushRouter.js
Florent Vilmart 6df944704c Adds support for localized push notification in push payload (#4129)
* Adds support for localized push data keys

- passign alert-[lang|locale] or title-[lang|locale] will inject the
  proper locale on the push body based on the installation

* Better handling of the default cases

* Updates changelog

* nits

* nits
2017-09-01 15:22:02 -04:00

65 lines
1.8 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) {
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;
});
pushController.sendPush(req.body, where, req.config, req.auth, (pushStatusId) => {
resolve({
headers: {
'X-Parse-Push-Status-Id': pushStatusId
},
response: {
result: true
}
});
}).catch(req.config.loggerController.error);
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;