From 5370fd981c9025b57d70d57c1155087f438b59ff Mon Sep 17 00:00:00 2001 From: Florent Vilmart Date: Mon, 7 Mar 2016 17:19:55 -0500 Subject: [PATCH] Fixes problems related to increment badge - name conventions are aweful in PushController - properly looks at the badge into body.data instead of body - We may want to refactor that as it's confusing to use a full body --- spec/Parse.Push.spec.js | 51 +++++++++++++++++++++++++++++++ spec/PushController.spec.js | 14 ++++----- src/Controllers/PushController.js | 36 ++++++++++++---------- src/Routers/PushRouter.js | 1 - 4 files changed, 78 insertions(+), 24 deletions(-) create mode 100644 spec/Parse.Push.spec.js diff --git a/spec/Parse.Push.spec.js b/spec/Parse.Push.spec.js new file mode 100644 index 0000000000..415dc487c4 --- /dev/null +++ b/spec/Parse.Push.spec.js @@ -0,0 +1,51 @@ +describe('Parse.Push', () => { + it('should properly send push', (done) => { + var pushAdapter = { + send: function(body, installations) { + return Promise.resolve({ + body: body, + installations: installations + }) + }, + getValidPushTypes: function() { + return ["ios", "android"]; + } + } + setServerConfiguration({ + appId: Parse.applicationId, + masterKey: Parse.masterKey, + serverURL: Parse.serverURL, + push: { + adapter: pushAdapter + } + }); + var installations = []; + while(installations.length != 10) { + var installation = new Parse.Object("_Installation"); + installation.set("installationId", "installation_"+installations.length); + installation.set("deviceToken","device_token_"+installations.length) + installation.set("badge", installations.length); + installation.set("originalBadge", installations.length); + installation.set("deviceType", "ios"); + installations.push(installation); + } + Parse.Object.saveAll(installations).then(() => { + return Parse.Push.send({ + where: { + deviceType: 'ios' + }, + data: { + badge: 'Increment', + alert: 'Hello world!' + } + }, {useMasterKey: true}); + }) + .then(() => { + console.log("OK!"); + done(); + }, (err) => { + console.error(err); + done(); + }); + }); +}); \ No newline at end of file diff --git a/spec/PushController.spec.js b/spec/PushController.spec.js index 1821d1a3cf..aa818765cf 100644 --- a/spec/PushController.spec.js +++ b/spec/PushController.spec.js @@ -132,10 +132,10 @@ describe('PushController', () => { it('properly increment badges', (done) => { - var payload = { + var payload = {data:{ alert: "Hello World!", badge: "Increment", - } + }} var installations = []; while(installations.length != 10) { var installation = new Parse.Object("_Installation"); @@ -157,7 +157,7 @@ describe('PushController', () => { var pushAdapter = { send: function(body, installations) { - var badge = body.badge; + var badge = body.data.badge; installations.forEach((installation) => { if (installation.deviceType == "ios") { expect(installation.badge).toEqual(badge); @@ -196,10 +196,10 @@ describe('PushController', () => { it('properly set badges to 1', (done) => { - var payload = { + var payload = {data: { alert: "Hello World!", badge: 1, - } + }} var installations = []; while(installations.length != 10) { var installation = new Parse.Object("_Installation"); @@ -213,7 +213,7 @@ describe('PushController', () => { var pushAdapter = { send: function(body, installations) { - var badge = body.badge; + var badge = body.data.badge; installations.forEach((installation) => { expect(installation.badge).toEqual(badge); expect(1).toEqual(installation.badge); @@ -244,6 +244,6 @@ describe('PushController', () => { done(); }); - }) + }); }); diff --git a/src/Controllers/PushController.js b/src/Controllers/PushController.js index 55cb609517..46f1e45a6a 100644 --- a/src/Controllers/PushController.js +++ b/src/Controllers/PushController.js @@ -60,14 +60,16 @@ export class PushController extends AdaptableController { body['expiration_time'] = PushController.getExpirationTime(body); // 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. - let badgeUpdate = Promise.resolve(); + let badgeUpdate = () => { + return Promise.resolve(); + } - if (body.badge) { + if (body.data && body.data.badge) { var op = {}; - if (body.badge == "Increment") { + if (body.data.badge == "Increment") { op = {'$inc': {'badge': 1}} - } else if (Number(body.badge)) { - op = {'$set': {'badge': body.badge } } + } else if (Number(body.data.badge)) { + op = {'$set': {'badge': body.data.badge } } } else { throw "Invalid value for badge, expected number or 'Increment'"; } @@ -77,33 +79,35 @@ export class PushController extends AdaptableController { updateWhere.deviceType = 'ios'; // TODO: @nlutsenko replace with better thing - badgeUpdate = config.database.rawCollection("_Installation").then((coll) => { - return coll.update(updateWhere, op, { multi: true }); - }); + badgeUpdate = () => { + return config.database.rawCollection("_Installation").then((coll) => { + return coll.update(updateWhere, op, { multi: true }); + }); + } } - return badgeUpdate.then(() => { - return rest.find(config, auth, '_Installation', where) + return badgeUpdate().then(() => { + return rest.find(config, auth, '_Installation', where); }).then((response) => { - if (body.badge && body.badge == "Increment") { + 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) => { let badge = installation.badge; if (installation.deviceType != "ios") { badge = UNSUPPORTED_BADGE_KEY; } - map[badge] = map[badge] || []; - map[badge].push(installation); + map[badge+''] = map[badge+''] || []; + map[badge+''].push(installation); return map; }, {}); - + // Map the on the badges count and return the send result let promises = Object.keys(badgeInstallationsMap).map((badge) => { let payload = deepcopy(body); if (badge == UNSUPPORTED_BADGE_KEY) { - delete payload.badge; + delete payload.data.badge; } else { - payload.badge = parseInt(badge); + payload.data.badge = parseInt(badge); } return pushAdapter.send(payload, badgeInstallationsMap[badge]); }); diff --git a/src/Routers/PushRouter.js b/src/Routers/PushRouter.js index f75d99985b..36ae5e16af 100644 --- a/src/Routers/PushRouter.js +++ b/src/Routers/PushRouter.js @@ -29,7 +29,6 @@ export class PushRouter extends PromiseRouter { } var where = PushRouter.getQueryCondition(req); - pushController.sendPush(req.body, where, req.config, req.auth); return Promise.resolve({ response: {