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
This commit is contained in:
59
spec/Parse.Push.spec.js
Normal file
59
spec/Parse.Push.spec.js
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
describe('Parse.Push', () => {
|
||||||
|
it('should properly send push', (done) => {
|
||||||
|
var pushAdapter = {
|
||||||
|
send: function(body, installations) {
|
||||||
|
var badge = body.data.badge;
|
||||||
|
installations.forEach((installation) => {
|
||||||
|
if (installation.deviceType == "ios") {
|
||||||
|
expect(installation.badge).toEqual(badge);
|
||||||
|
expect(installation.originalBadge+1).toEqual(installation.badge);
|
||||||
|
} else {
|
||||||
|
expect(installation.badge).toBeUndefined();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
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(() => {
|
||||||
|
done();
|
||||||
|
}, (err) => {
|
||||||
|
console.error(err);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -107,10 +107,10 @@ describe('PushController', () => {
|
|||||||
|
|
||||||
it('properly increment badges', (done) => {
|
it('properly increment badges', (done) => {
|
||||||
|
|
||||||
var payload = {
|
var payload = {data:{
|
||||||
alert: "Hello World!",
|
alert: "Hello World!",
|
||||||
badge: "Increment",
|
badge: "Increment",
|
||||||
}
|
}}
|
||||||
var installations = [];
|
var installations = [];
|
||||||
while(installations.length != 10) {
|
while(installations.length != 10) {
|
||||||
var installation = new Parse.Object("_Installation");
|
var installation = new Parse.Object("_Installation");
|
||||||
@@ -132,7 +132,7 @@ describe('PushController', () => {
|
|||||||
|
|
||||||
var pushAdapter = {
|
var pushAdapter = {
|
||||||
send: function(body, installations) {
|
send: function(body, installations) {
|
||||||
var badge = body.badge;
|
var badge = body.data.badge;
|
||||||
installations.forEach((installation) => {
|
installations.forEach((installation) => {
|
||||||
if (installation.deviceType == "ios") {
|
if (installation.deviceType == "ios") {
|
||||||
expect(installation.badge).toEqual(badge);
|
expect(installation.badge).toEqual(badge);
|
||||||
@@ -171,10 +171,10 @@ describe('PushController', () => {
|
|||||||
|
|
||||||
it('properly set badges to 1', (done) => {
|
it('properly set badges to 1', (done) => {
|
||||||
|
|
||||||
var payload = {
|
var payload = {data: {
|
||||||
alert: "Hello World!",
|
alert: "Hello World!",
|
||||||
badge: 1,
|
badge: 1,
|
||||||
}
|
}}
|
||||||
var installations = [];
|
var installations = [];
|
||||||
while(installations.length != 10) {
|
while(installations.length != 10) {
|
||||||
var installation = new Parse.Object("_Installation");
|
var installation = new Parse.Object("_Installation");
|
||||||
@@ -188,7 +188,7 @@ describe('PushController', () => {
|
|||||||
|
|
||||||
var pushAdapter = {
|
var pushAdapter = {
|
||||||
send: function(body, installations) {
|
send: function(body, installations) {
|
||||||
var badge = body.badge;
|
var badge = body.data.badge;
|
||||||
installations.forEach((installation) => {
|
installations.forEach((installation) => {
|
||||||
expect(installation.badge).toEqual(badge);
|
expect(installation.badge).toEqual(badge);
|
||||||
expect(1).toEqual(installation.badge);
|
expect(1).toEqual(installation.badge);
|
||||||
@@ -219,6 +219,6 @@ describe('PushController', () => {
|
|||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
|
||||||
})
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -48,55 +48,60 @@ export class PushController extends AdaptableController {
|
|||||||
body['expiration_time'] = PushController.getExpirationTime(body);
|
body['expiration_time'] = PushController.getExpirationTime(body);
|
||||||
// TODO: If the req can pass the checking, we return immediately instead of waiting
|
// 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.
|
// 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) {
|
||||||
|
let badge = body.data.badge;
|
||||||
let op = {};
|
let op = {};
|
||||||
if (body.badge == "Increment") {
|
if (badge == "Increment") {
|
||||||
op = { $inc: { badge: 1 } }
|
op = { $inc: { badge: 1 } }
|
||||||
} else if (Number(body.badge)) {
|
} else if (Number(badge)) {
|
||||||
op = { $set: { badge: body.badge } }
|
op = { $set: { badge: badge } }
|
||||||
} else {
|
} else {
|
||||||
throw "Invalid value for badge, expected number or 'Increment'";
|
throw "Invalid value for badge, expected number or 'Increment'";
|
||||||
}
|
}
|
||||||
let updateWhere = deepcopy(where);
|
let updateWhere = deepcopy(where);
|
||||||
updateWhere.deviceType = 'ios'; // Only on iOS!
|
updateWhere.deviceType = 'ios'; // Only on iOS!
|
||||||
|
|
||||||
badgeUpdate = config.database.adaptiveCollection("_Installation")
|
badgeUpdate = () => {
|
||||||
|
return config.database.adaptiveCollection("_Installation")
|
||||||
.then(coll => coll.updateMany(updateWhere, op));
|
.then(coll => coll.updateMany(updateWhere, op));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return badgeUpdate.then(() => {
|
return badgeUpdate().then(() => {
|
||||||
return rest.find(config, auth, '_Installation', where)
|
return rest.find(config, auth, '_Installation', where);
|
||||||
}).then((response) => {
|
}).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
|
// Collect the badges to reduce the # of calls
|
||||||
let badgeInstallationsMap = response.results.reduce((map, installation) => {
|
let badgeInstallationsMap = response.results.reduce((map, installation) => {
|
||||||
let badge = installation.badge;
|
let badge = installation.badge;
|
||||||
if (installation.deviceType != "ios") {
|
if (installation.deviceType != "ios") {
|
||||||
badge = UNSUPPORTED_BADGE_KEY;
|
badge = UNSUPPORTED_BADGE_KEY;
|
||||||
}
|
}
|
||||||
map[badge] = map[badge] || [];
|
map[badge+''] = map[badge+''] || [];
|
||||||
map[badge].push(installation);
|
map[badge+''].push(installation);
|
||||||
return map;
|
return map;
|
||||||
}, {});
|
}, {});
|
||||||
|
|
||||||
// Map the on the badges count and return the send result
|
// Map the on the badges count and return the send result
|
||||||
let promises = Object.keys(badgeInstallationsMap).map((badge) => {
|
let promises = Object.keys(badgeInstallationsMap).map((badge) => {
|
||||||
let payload = deepcopy(body);
|
let payload = deepcopy(body);
|
||||||
if (badge == UNSUPPORTED_BADGE_KEY) {
|
if (badge == UNSUPPORTED_BADGE_KEY) {
|
||||||
delete payload.badge;
|
delete payload.data.badge;
|
||||||
} else {
|
} else {
|
||||||
payload.badge = parseInt(badge);
|
payload.data.badge = parseInt(badge);
|
||||||
}
|
}
|
||||||
return pushAdapter.send(payload, badgeInstallationsMap[badge]);
|
return pushAdapter.send(payload, badgeInstallationsMap[badge]);
|
||||||
});
|
});
|
||||||
return Promise.all(promises);
|
return Promise.all(promises);
|
||||||
}
|
}
|
||||||
return pushAdapter.send(body, response.results);
|
return pushAdapter.send(body, response.results);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get expiration time from the request body.
|
* Get expiration time from the request body.
|
||||||
* @param {Object} request A request object
|
* @param {Object} request A request object
|
||||||
|
|||||||
Reference in New Issue
Block a user