Add support for expiration interval in Push (#4202)
* Add support for expiration_interval in Push * Support expiration_interval for immediate pushes * Test * Add 'expiration_interval' to _PushStatus class * Fix coverage
This commit is contained in:
@@ -7,13 +7,27 @@ import { applyDeviceTokenExists } from '../Push/utils';
|
||||
|
||||
export class PushController {
|
||||
|
||||
sendPush(body = {}, where = {}, config, auth, onPushStatusSaved = () => {}) {
|
||||
sendPush(body = {}, where = {}, config, auth, onPushStatusSaved = () => {}, now = new Date()) {
|
||||
if (!config.hasPushSupport) {
|
||||
throw new Parse.Error(Parse.Error.PUSH_MISCONFIGURED,
|
||||
'Missing push configuration');
|
||||
}
|
||||
|
||||
// Replace the expiration_time and push_time with a valid Unix epoch milliseconds time
|
||||
body.expiration_time = PushController.getExpirationTime(body);
|
||||
body.expiration_interval = PushController.getExpirationInterval(body);
|
||||
if (body.expiration_time && body.expiration_interval) {
|
||||
throw new Parse.Error(
|
||||
Parse.Error.PUSH_MISCONFIGURED,
|
||||
'Both expiration_time and expiration_interval cannot be set');
|
||||
}
|
||||
|
||||
// Immediate push
|
||||
if (body.expiration_interval && !body.hasOwnProperty('push_time')) {
|
||||
const ttlMs = body.expiration_interval * 1000;
|
||||
body.expiration_time = (new Date(now.valueOf() + ttlMs)).valueOf();
|
||||
}
|
||||
|
||||
const pushTime = PushController.getPushTime(body);
|
||||
if (pushTime && pushTime.date !== 'undefined') {
|
||||
body['push_time'] = PushController.formatPushTime(pushTime);
|
||||
@@ -108,6 +122,20 @@ export class PushController {
|
||||
return expirationTime.valueOf();
|
||||
}
|
||||
|
||||
static getExpirationInterval(body = {}) {
|
||||
const hasExpirationInterval = body.hasOwnProperty('expiration_interval');
|
||||
if (!hasExpirationInterval) {
|
||||
return;
|
||||
}
|
||||
|
||||
var expirationIntervalParam = body['expiration_interval'];
|
||||
if (typeof expirationIntervalParam !== 'number' || expirationIntervalParam <= 0) {
|
||||
throw new Parse.Error(Parse.Error.PUSH_MISCONFIGURED,
|
||||
`expiration_interval must be a number greater than 0`);
|
||||
}
|
||||
return expirationIntervalParam;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get push time from the request body.
|
||||
* @param {Object} request A request object
|
||||
|
||||
@@ -72,22 +72,23 @@ const defaultColumns = Object.freeze({
|
||||
"subtitle": {type:'String'},
|
||||
},
|
||||
_PushStatus: {
|
||||
"pushTime": {type:'String'},
|
||||
"source": {type:'String'}, // rest or webui
|
||||
"query": {type:'String'}, // the stringified JSON query
|
||||
"payload": {type:'String'}, // the stringified JSON payload,
|
||||
"title": {type:'String'},
|
||||
"expiry": {type:'Number'},
|
||||
"status": {type:'String'},
|
||||
"numSent": {type:'Number'},
|
||||
"numFailed": {type:'Number'},
|
||||
"pushHash": {type:'String'},
|
||||
"errorMessage": {type:'Object'},
|
||||
"sentPerType": {type:'Object'},
|
||||
"failedPerType": {type:'Object'},
|
||||
"sentPerUTCOffset": {type:'Object'},
|
||||
"failedPerUTCOffset": {type:'Object'},
|
||||
"count": {type:'Number'}
|
||||
"pushTime": {type:'String'},
|
||||
"source": {type:'String'}, // rest or webui
|
||||
"query": {type:'String'}, // the stringified JSON query
|
||||
"payload": {type:'String'}, // the stringified JSON payload,
|
||||
"title": {type:'String'},
|
||||
"expiry": {type:'Number'},
|
||||
"expiration_interval": {type:'Number'},
|
||||
"status": {type:'String'},
|
||||
"numSent": {type:'Number'},
|
||||
"numFailed": {type:'Number'},
|
||||
"pushHash": {type:'String'},
|
||||
"errorMessage": {type:'Object'},
|
||||
"sentPerType": {type:'Object'},
|
||||
"failedPerType": {type:'Object'},
|
||||
"sentPerUTCOffset": {type:'Object'},
|
||||
"failedPerUTCOffset": {type:'Object'},
|
||||
"count": {type:'Number'}
|
||||
},
|
||||
_JobStatus: {
|
||||
"jobName": {type: 'String'},
|
||||
|
||||
Reference in New Issue
Block a user