2016-02-10 12:03:02 -08:00
|
|
|
import { Parse } from 'parse/node';
|
|
|
|
|
import PromiseRouter from '../PromiseRouter';
|
|
|
|
|
import rest from '../rest';
|
2016-01-28 10:58:12 -08:00
|
|
|
|
2016-02-10 12:03:02 -08:00
|
|
|
export class PushController {
|
2016-01-28 10:58:12 -08:00
|
|
|
|
2016-02-10 12:03:02 -08:00
|
|
|
constructor(pushAdapter) {
|
|
|
|
|
this._pushAdapter = pushAdapter;
|
2016-02-20 10:49:32 -05:00
|
|
|
};
|
2016-02-10 12:03:02 -08:00
|
|
|
|
2016-02-20 10:49:32 -05:00
|
|
|
/**
|
|
|
|
|
* Check whether the deviceType parameter in qury condition is valid or not.
|
|
|
|
|
* @param {Object} where A query condition
|
|
|
|
|
* @param {Array} validPushTypes An array of valid push types(string)
|
|
|
|
|
*/
|
|
|
|
|
static validatePushType(where = {}, validPushTypes = []) {
|
|
|
|
|
var deviceTypeField = where.deviceType || {};
|
|
|
|
|
var deviceTypes = [];
|
|
|
|
|
if (typeof deviceTypeField === 'string') {
|
|
|
|
|
deviceTypes.push(deviceTypeField);
|
|
|
|
|
} else if (typeof deviceTypeField['$in'] === 'array') {
|
|
|
|
|
deviceTypes.concat(deviceTypeField['$in']);
|
|
|
|
|
}
|
|
|
|
|
for (var i = 0; i < deviceTypes.length; i++) {
|
|
|
|
|
var deviceType = deviceTypes[i];
|
|
|
|
|
if (validPushTypes.indexOf(deviceType) < 0) {
|
|
|
|
|
throw new Parse.Error(Parse.Error.PUSH_MISCONFIGURED,
|
|
|
|
|
deviceType + ' is not supported push type.');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check whether the api call has master key or not.
|
|
|
|
|
* @param {Object} request A request object
|
|
|
|
|
*/
|
|
|
|
|
static validateMasterKey(auth = {}) {
|
|
|
|
|
if (!auth.isMaster) {
|
2016-02-10 12:03:02 -08:00
|
|
|
throw new Parse.Error(Parse.Error.PUSH_MISCONFIGURED,
|
2016-02-20 10:49:32 -05:00
|
|
|
'Master key is invalid, you should only use master key to send push');
|
2016-02-10 12:03:02 -08:00
|
|
|
}
|
2016-02-20 10:49:32 -05:00
|
|
|
}
|
2016-02-10 12:03:02 -08:00
|
|
|
|
2016-02-20 10:49:32 -05:00
|
|
|
sendPush(body = {}, where = {}, config, auth) {
|
2016-02-10 12:03:02 -08:00
|
|
|
var pushAdapter = this._pushAdapter;
|
2016-02-20 10:49:32 -05:00
|
|
|
if (!pushAdapter) {
|
|
|
|
|
throw new Parse.Error(Parse.Error.PUSH_MISCONFIGURED,
|
|
|
|
|
'Push adapter is not available');
|
|
|
|
|
}
|
|
|
|
|
PushController.validateMasterKey(auth);
|
|
|
|
|
|
|
|
|
|
PushController.validatePushType(where, pushAdapter.getValidPushTypes());
|
2016-02-10 12:03:02 -08:00
|
|
|
// Replace the expiration_time with a valid Unix epoch milliseconds time
|
2016-02-20 10:49:32 -05:00
|
|
|
body['expiration_time'] = PushController.getExpirationTime(body);
|
2016-02-10 12:03:02 -08:00
|
|
|
// TODO: If the req can pass the checking, we return immediately instead of waiting
|
|
|
|
|
// pushes to be sent. We probably change this behaviour in the future.
|
2016-02-20 10:49:32 -05:00
|
|
|
rest.find(config, auth, '_Installation', where).then(function(response) {
|
|
|
|
|
return pushAdapter.send(body, response.results);
|
2016-02-10 12:03:02 -08:00
|
|
|
});
|
2016-02-20 10:49:32 -05:00
|
|
|
};
|
|
|
|
|
/**
|
|
|
|
|
* Get expiration time from the request body.
|
|
|
|
|
* @param {Object} request A request object
|
|
|
|
|
* @returns {Number|undefined} The expiration time if it exists in the request
|
|
|
|
|
*/
|
|
|
|
|
static getExpirationTime(body = {}) {
|
|
|
|
|
var hasExpirationTime = !!body['expiration_time'];
|
|
|
|
|
if (!hasExpirationTime) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var expirationTimeParam = body['expiration_time'];
|
|
|
|
|
var expirationTime;
|
|
|
|
|
if (typeof expirationTimeParam === 'number') {
|
|
|
|
|
expirationTime = new Date(expirationTimeParam * 1000);
|
|
|
|
|
} else if (typeof expirationTimeParam === 'string') {
|
|
|
|
|
expirationTime = new Date(expirationTimeParam);
|
|
|
|
|
} else {
|
2016-02-02 19:51:40 -08:00
|
|
|
throw new Parse.Error(Parse.Error.PUSH_MISCONFIGURED,
|
2016-02-20 10:49:32 -05:00
|
|
|
body['expiration_time'] + ' is not valid time.');
|
2016-02-02 19:51:40 -08:00
|
|
|
}
|
2016-02-20 10:49:32 -05:00
|
|
|
// Check expirationTime is valid or not, if it is not valid, expirationTime is NaN
|
|
|
|
|
if (!isFinite(expirationTime)) {
|
|
|
|
|
throw new Parse.Error(Parse.Error.PUSH_MISCONFIGURED,
|
|
|
|
|
body['expiration_time'] + ' is not valid time.');
|
2016-02-02 19:51:40 -08:00
|
|
|
}
|
2016-02-20 10:49:32 -05:00
|
|
|
return expirationTime.valueOf();
|
|
|
|
|
};
|
|
|
|
|
};
|
2016-02-10 12:03:02 -08:00
|
|
|
|
|
|
|
|
export default PushController;
|