Skip to content

Add support for expiration interval in Push #4202

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions spec/PushController.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1254,4 +1254,84 @@ describe('PushController', () => {
.then(done, done.fail);
});
});

describe('With expiration defined', () => {
const auth = {isMaster: true};
const pushController = new PushController();

let config = Config.get(Parse.applicationId);

const pushes = [];
const pushAdapter = {
send(body, installations) {
pushes.push(body);
return successfulTransmissions(body, installations);
},
getValidPushTypes() {
return ["ios"];
}
};

beforeEach((done) => {
reconfigureServer({
push: {adapter: pushAdapter},
})
.then(() => {
config = Config.get(Parse.applicationId);
})
.then(done, done.fail);
});

it('should throw if both expiration_time and expiration_interval are set', () => {
expect(() => pushController.sendPush({
expiration_time: '2017-09-25T13:21:20.841Z',
expiration_interval: 1000,
}, {}, config, auth)).toThrow()
});

it('should throw on invalid expiration_interval', () => {
expect(() => pushController.sendPush({
expiration_interval: -1
}, {}, config, auth)).toThrow();
expect(() => pushController.sendPush({
expiration_interval: '',
}, {}, config, auth)).toThrow();
expect(() => pushController.sendPush({
expiration_time: {},
}, {}, config, auth)).toThrow();
});

describe('For immediate pushes',() => {
it('should transform the expiration_interval into an absolute time', (done) => {
const now = new Date('2017-09-25T13:30:10.452Z');

reconfigureServer({
push: {adapter: pushAdapter},
})
.then(() =>
new Promise((resolve) => {
pushController.sendPush({
data: {
alert: 'immediate push',
},
expiration_interval: 20 * 60, // twenty minutes
}, {}, Config.get(Parse.applicationId), auth, resolve, now)
}))
.then((pushStatusId) => {
const p = new Parse.Object('_PushStatus');
p.id = pushStatusId;
return p.fetch({useMasterKey: true});
})
.then((pushStatus) => {
expect(pushStatus.get('expiry')).toBeDefined('expiry must be set');
expect(pushStatus.get('expiry'))
.toEqual(new Date('2017-09-25T13:50:10.452Z').valueOf());

expect(pushStatus.get('expiration_interval')).toBeDefined('expiration_interval must be defined');
expect(pushStatus.get('expiration_interval')).toBe(20 * 60);
})
.then(done, done.fail);
});
});
});
});
30 changes: 29 additions & 1 deletion src/Controllers/PushController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
33 changes: 17 additions & 16 deletions src/Controllers/SchemaController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'},
Expand Down
1 change: 1 addition & 0 deletions src/StatusHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ export function pushStatusHandler(config, existingObjectId) {
source: options.source,
title: options.title,
expiry: body.expiration_time,
expiration_interval: body.expiration_interval,
status: status,
numSent: 0,
pushHash,
Expand Down