From a392c088d868186724f5904650033fba44605356 Mon Sep 17 00:00:00 2001 From: Florent Vilmart Date: Sat, 12 Mar 2016 15:20:52 -0500 Subject: [PATCH] Uses the resolved promise from the adapter --- spec/PushController.spec.js | 19 ++++++++------ src/APNS.js | 36 ++++++++++++++++++++++----- src/Adapters/Push/ParsePushAdapter.js | 6 ++--- src/Controllers/PushController.js | 10 +++++--- src/GCM.js | 9 +++++-- 5 files changed, 57 insertions(+), 23 deletions(-) diff --git a/spec/PushController.spec.js b/spec/PushController.spec.js index 68e52794..97dc18c5 100644 --- a/spec/PushController.spec.js +++ b/spec/PushController.spec.js @@ -143,8 +143,8 @@ describe('PushController', () => { } }) return Promise.resolve({ - body: body, - installations: installations + error: null + payload: body, }) }, getValidPushTypes: function() { @@ -195,8 +195,8 @@ describe('PushController', () => { expect(1).toEqual(installation.badge); }) return Promise.resolve({ - body: body, - installations: installations + payload: body, + error: null }) }, getValidPushTypes: function() { @@ -233,9 +233,10 @@ describe('PushController', () => { send: function(body, installations) { var badge = body.data.badge; return Promise.resolve({ - body: body, - installations: installations - }) + error: null, + response: "OK!", + payload: body + }); }, getValidPushTypes: function() { return ["ios"]; @@ -271,7 +272,9 @@ describe('PushController', () => { var pushAdapter = { send: function(body, installations) { - return Promise.resolve(); + return Promise.resolve({ + error:null + }); }, getValidPushTypes: function() { return ["ios"]; diff --git a/src/APNS.js b/src/APNS.js index 500be9e2..345e3dbe 100644 --- a/src/APNS.js +++ b/src/APNS.js @@ -66,6 +66,12 @@ function APNS(args) { }); conn.on('transmitted', function(notification, device) { + if (device.callback) { + device.callback(null, { + notification: notification, + device: device + }); + } console.log('APNS Connection %d Notification transmitted to %s', conn.index, device.token.toString('hex')); }); @@ -91,11 +97,14 @@ APNS.prototype.send = function(data, devices) { let coreData = data.data; let expirationTime = data['expiration_time']; let notification = generateNotification(coreData, expirationTime); - for (let device of devices) { + + let promises = devices.map((device) => { let qualifiedConnIndexs = chooseConns(this.conns, device); // We can not find a valid conn, just ignore this device if (qualifiedConnIndexs.length == 0) { - continue; + return Promise.resolve({ + err: 'No connection available' + }); } let conn = this.conns[qualifiedConnIndexs[0]]; let apnDevice = new apn.Device(device.deviceToken); @@ -104,9 +113,19 @@ APNS.prototype.send = function(data, devices) { if (device.appIdentifier) { apnDevice.appIdentifier = device.appIdentifier; } - conn.pushNotification(notification, apnDevice); - } - return Parse.Promise.as(); + return new Promise((resolve, reject) => { + apnDevice.callback = (err, res) => { + resolve({ + error: err, + response: res, + payload: notification, + deviceType: 'ios' + }); + } + conn.pushNotification(notification, apnDevice); + }); + }); + return Parse.Promise.when(promises); } function handleTransmissionError(conns, errCode, notification, apnDevice) { @@ -133,7 +152,12 @@ function handleTransmissionError(conns, errCode, notification, apnDevice) { } // There is no more available conns, we give up in this case if (newConnIndex < 0 || newConnIndex >= conns.length) { - console.log('APNS can not find vaild connection for %j', apnDevice.token); + if (apnDevice.callback) { + apnDevice.callback({ + error: `APNS can not find vaild connection for ${apnDevice.token}`, + code: errCode + }); + } return; } diff --git a/src/Adapters/Push/ParsePushAdapter.js b/src/Adapters/Push/ParsePushAdapter.js index c953d157..d49c1b1c 100644 --- a/src/Adapters/Push/ParsePushAdapter.js +++ b/src/Adapters/Push/ParsePushAdapter.js @@ -19,7 +19,7 @@ export class ParsePushAdapter extends PushAdapter { immediatePush: true }; let pushTypes = Object.keys(pushConfig); - + for (let pushType of pushTypes) { if (this.validPushTypes.indexOf(pushType) < 0) { throw new Parse.Error(Parse.Error.PUSH_MISCONFIGURED, @@ -35,7 +35,7 @@ export class ParsePushAdapter extends PushAdapter { } } } - + getValidPushTypes() { return this.validPushTypes; } @@ -43,7 +43,7 @@ export class ParsePushAdapter extends PushAdapter { static classifyInstallations(installations, validTypes) { return classifyInstallations(installations, validTypes) } - + send(data, installations) { let deviceMap = classifyInstallations(installations, this.validPushTypes); let sendPromises = []; diff --git a/src/Controllers/PushController.js b/src/Controllers/PushController.js index d8b3b792..ae2b5318 100644 --- a/src/Controllers/PushController.js +++ b/src/Controllers/PushController.js @@ -92,6 +92,7 @@ export class PushController extends AdaptableController { }).then(() => { return rest.find(config, auth, '_Installation', where); }).then((response) => { + this.updatePushStatus({status: "running"}, {status:"pending", objectId: pushStatus.objectId}, config); if (body.data && body.data.badge && body.data.badge == "Increment") { // Collect the badges to reduce the # of calls let badgeInstallationsMap = response.results.reduce((map, installation) => { @@ -117,8 +118,9 @@ export class PushController extends AdaptableController { return Promise.all(promises); } return pushAdapter.send(body, response.results, pushStatus); - }).then(() => { - return this.updatePushStatus({status: "running"}, pushStatus, config); + }).then((results) => { + console.log(results); + return Promise.resolve(results); }); } @@ -139,8 +141,8 @@ export class PushController extends AdaptableController { return restWrite.execute(); } - updatePushStatus(update, pushStatus, config) { - let restWrite = new RestWrite(config, {isMaster: true}, '_PushStatus', {objectId: pushStatus.objectId, "status": "pending"}, update); + updatePushStatus(update, where, config) { + let restWrite = new RestWrite(config, {isMaster: true}, '_PushStatus', where, update); return restWrite.execute(); } diff --git a/src/GCM.js b/src/GCM.js index a13a6751..473b5ed9 100644 --- a/src/GCM.js +++ b/src/GCM.js @@ -21,7 +21,7 @@ function GCM(args) { * @param {Array} devices A array of devices * @returns {Object} A promise which is resolved after we get results from gcm */ -GCM.prototype.send = function(data, devices) { +GCM.prototype.send = function(data, devices, callback) { let pushId = cryptoUtils.newObjectId(); let timeStamp = Date.now(); let expirationTime; @@ -52,7 +52,12 @@ GCM.prototype.send = function(data, devices) { request: message, response: response }); - sendPromise.resolve(); + sendPromise.resolve({ + error: error, + response: response, + payload: message, + deviceType: 'android' + }); }); sendPromises.push(sendPromise); }