2016-08-30 07:19:21 -04:00
|
|
|
import { Parse } from 'parse/node';
|
|
|
|
|
import deepcopy from 'deepcopy';
|
|
|
|
|
import RestQuery from '../RestQuery';
|
|
|
|
|
import RestWrite from '../RestWrite';
|
|
|
|
|
import { master } from '../Auth';
|
|
|
|
|
import { pushStatusHandler } from '../StatusHandler';
|
2016-03-01 07:35:28 -08:00
|
|
|
|
2017-01-13 19:34:04 -05:00
|
|
|
export class PushController {
|
2016-03-23 12:33:26 -07:00
|
|
|
|
2016-04-07 18:08:09 -04:00
|
|
|
sendPush(body = {}, where = {}, config, auth, onPushStatusSaved = () => {}) {
|
2017-01-13 19:34:04 -05:00
|
|
|
if (!config.hasPushSupport) {
|
2016-06-14 00:26:02 +08:00
|
|
|
throw new Parse.Error(Parse.Error.PUSH_MISCONFIGURED,
|
|
|
|
|
'Missing push configuration');
|
|
|
|
|
}
|
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-03-07 17:19:55 -05:00
|
|
|
let badgeUpdate = () => {
|
|
|
|
|
return Promise.resolve();
|
|
|
|
|
}
|
|
|
|
|
if (body.data && body.data.badge) {
|
2016-12-07 15:17:05 -08:00
|
|
|
const badge = body.data.badge;
|
2016-04-14 19:24:56 -04:00
|
|
|
let restUpdate = {};
|
2016-03-29 09:44:04 -04:00
|
|
|
if (typeof badge == 'string' && badge.toLowerCase() === 'increment') {
|
2016-04-14 19:24:56 -04:00
|
|
|
restUpdate = { badge: { __op: 'Increment', amount: 1 } }
|
2016-03-07 17:19:55 -05:00
|
|
|
} else if (Number(badge)) {
|
2016-04-14 19:24:56 -04:00
|
|
|
restUpdate = { badge: badge }
|
2016-03-01 15:10:06 -05:00
|
|
|
} else {
|
|
|
|
|
throw "Invalid value for badge, expected number or 'Increment'";
|
|
|
|
|
}
|
2016-12-07 15:17:05 -08:00
|
|
|
const updateWhere = deepcopy(where);
|
2016-03-01 15:10:06 -05:00
|
|
|
|
2016-03-12 14:32:39 -05:00
|
|
|
badgeUpdate = () => {
|
2016-04-14 19:24:56 -04:00
|
|
|
updateWhere.deviceType = 'ios';
|
|
|
|
|
// Build a real RestQuery so we can use it in RestWrite
|
2016-12-07 15:17:05 -08:00
|
|
|
const restQuery = new RestQuery(config, master(config), '_Installation', updateWhere);
|
2016-04-14 19:24:56 -04:00
|
|
|
return restQuery.buildRestWhere().then(() => {
|
2016-12-07 15:17:05 -08:00
|
|
|
const write = new RestWrite(config, master(config), '_Installation', restQuery.restWhere, restUpdate);
|
2016-04-14 19:24:56 -04:00
|
|
|
write.runOptions.many = true;
|
|
|
|
|
return write.execute();
|
|
|
|
|
});
|
2016-03-07 17:19:55 -05:00
|
|
|
}
|
2016-03-01 15:10:06 -05:00
|
|
|
}
|
2016-12-07 15:17:05 -08:00
|
|
|
const pushStatus = pushStatusHandler(config);
|
2016-03-12 14:32:39 -05:00
|
|
|
return Promise.resolve().then(() => {
|
2016-03-13 23:34:44 -04:00
|
|
|
return pushStatus.setInitial(body, where);
|
|
|
|
|
}).then(() => {
|
2016-04-07 18:08:09 -04:00
|
|
|
onPushStatusSaved(pushStatus.objectId);
|
2016-03-12 14:32:39 -05:00
|
|
|
return badgeUpdate();
|
|
|
|
|
}).then(() => {
|
2017-01-13 19:34:04 -05:00
|
|
|
return config.pushControllerQueue.enqueue(body, where, config, auth, pushStatus);
|
2016-03-29 22:42:37 -04:00
|
|
|
}).catch((err) => {
|
2016-11-24 15:47:41 -05:00
|
|
|
return pushStatus.fail(err).then(() => {
|
2016-08-30 07:19:21 -04:00
|
|
|
throw err;
|
|
|
|
|
});
|
2016-02-10 12:03:02 -08:00
|
|
|
});
|
2016-02-21 23:47:07 -05:00
|
|
|
}
|
2016-03-07 17:19:55 -05: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-21 23:47:07 -05:00
|
|
|
}
|
2016-03-07 14:25:04 -08:00
|
|
|
}
|
2016-02-10 12:03:02 -08:00
|
|
|
|
|
|
|
|
export default PushController;
|