Skip to content

Commit 7c387e1

Browse files
committed
Adds support to store push results
1 parent 05baf36 commit 7c387e1

File tree

5 files changed

+183
-75
lines changed

5 files changed

+183
-75
lines changed

spec/PushController.spec.js

Lines changed: 58 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,30 @@ var PushController = require('../src/Controllers/PushController').PushController
33

44
var Config = require('../src/Config');
55

6+
const successfulTransmissions = function(body, installations) {
7+
8+
let promises = installations.map((device) => {
9+
return Promise.resolve({
10+
transmitted: true,
11+
device: device,
12+
})
13+
});
14+
15+
return Promise.all(promises);
16+
}
17+
18+
const successfulIOS = function(body, installations) {
19+
20+
let promises = installations.map((device) => {
21+
return Promise.resolve({
22+
transmitted: device.deviceType == "ios",
23+
device: device,
24+
})
25+
});
26+
27+
return Promise.all(promises);
28+
}
29+
630
describe('PushController', () => {
731
it('can validate device type when no device type is set', (done) => {
832
// Make query condition
@@ -142,10 +166,7 @@ describe('PushController', () => {
142166
expect(installation.badge).toBeUndefined();
143167
}
144168
})
145-
return Promise.resolve({
146-
error: null,
147-
payload: body,
148-
})
169+
return successfulTransmissions(body, installations);
149170
},
150171
getValidPushTypes: function() {
151172
return ["ios", "android"];
@@ -194,10 +215,7 @@ describe('PushController', () => {
194215
expect(installation.badge).toEqual(badge);
195216
expect(1).toEqual(installation.badge);
196217
})
197-
return Promise.resolve({
198-
payload: body,
199-
error: null
200-
})
218+
return successfulTransmissions(body, installations);
201219
},
202220
getValidPushTypes: function() {
203221
return ["ios"];
@@ -224,19 +242,32 @@ describe('PushController', () => {
224242

225243
it('properly creates _PushStatus', (done) => {
226244

245+
var installations = [];
246+
while(installations.length != 10) {
247+
var installation = new Parse.Object("_Installation");
248+
installation.set("installationId", "installation_"+installations.length);
249+
installation.set("deviceToken","device_token_"+installations.length)
250+
installation.set("badge", installations.length);
251+
installation.set("originalBadge", installations.length);
252+
installation.set("deviceType", "ios");
253+
installations.push(installation);
254+
}
255+
256+
while(installations.length != 15) {
257+
var installation = new Parse.Object("_Installation");
258+
installation.set("installationId", "installation_"+installations.length);
259+
installation.set("deviceToken","device_token_"+installations.length)
260+
installation.set("deviceType", "android");
261+
installations.push(installation);
262+
}
227263
var payload = {data: {
228264
alert: "Hello World!",
229265
badge: 1,
230266
}}
231267

232268
var pushAdapter = {
233269
send: function(body, installations) {
234-
var badge = body.data.badge;
235-
return Promise.resolve({
236-
error: null,
237-
response: "OK!",
238-
payload: body
239-
});
270+
return successfulIOS(body, installations);
240271
},
241272
getValidPushTypes: function() {
242273
return ["ios"];
@@ -249,7 +280,9 @@ describe('PushController', () => {
249280
}
250281

251282
var pushController = new PushController(pushAdapter, Parse.applicationId);
252-
pushController.sendPush(payload, {}, config, auth).then((result) => {
283+
Parse.Object.saveAll(installations).then(() => {
284+
return pushController.sendPush(payload, {}, config, auth);
285+
}).then((result) => {
253286
let query = new Parse.Query('_PushStatus');
254287
return query.find({useMasterKey: true});
255288
}).then((results) => {
@@ -258,7 +291,15 @@ describe('PushController', () => {
258291
expect(result.get('source')).toEqual('rest');
259292
expect(result.get('query')).toEqual(JSON.stringify({}));
260293
expect(result.get('payload')).toEqual(payload.data);
261-
expect(result.get('status')).toEqual("running");
294+
expect(result.get('status')).toEqual('succeeded');
295+
expect(result.get('numSent')).toEqual(10);
296+
expect(result.get('sentPerType')).toEqual({
297+
'ios': 10 // 10 ios
298+
});
299+
expect(result.get('numFailed')).toEqual(5);
300+
expect(result.get('failedPerType')).toEqual({
301+
'android': 5 // android
302+
});
262303
done();
263304
});
264305

@@ -272,9 +313,7 @@ describe('PushController', () => {
272313

273314
var pushAdapter = {
274315
send: function(body, installations) {
275-
return Promise.resolve({
276-
error:null
277-
});
316+
return successfulTransmissions(body, installations);
278317
},
279318
getValidPushTypes: function() {
280319
return ["ios"];

src/Adapters/Push/ParsePushAdapter.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import PushAdapter from './PushAdapter';
1010
import { classifyInstallations } from './PushAdapterUtils';
1111

1212
export class ParsePushAdapter extends PushAdapter {
13+
14+
supportsPushTracking = true;
15+
1316
constructor(pushConfig = {}) {
1417
super(pushConfig);
1518
this.validPushTypes = ['ios', 'android'];
@@ -56,7 +59,7 @@ export class ParsePushAdapter extends PushAdapter {
5659
}))
5760
} else {
5861
let devices = deviceMap[pushType];
59-
sendPromises.push(sender.send(data, devices));
62+
sendPromises.push(sender.send(data, devices));
6063
}
6164
}
6265
return Parse.Promise.when(sendPromises);

src/Controllers/PushController.js

Lines changed: 42 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ import rest from '../rest';
44
import AdaptableController from './AdaptableController';
55
import { PushAdapter } from '../Adapters/Push/PushAdapter';
66
import deepcopy from 'deepcopy';
7-
import { md5Hash } from '../cryptoUtils';
87
import features from '../features';
98
import RestQuery from '../RestQuery';
10-
import RestWrite from '../RestWrite';
9+
import pushStatusHandler from '../pushStatusHandler';
1110

1211
const FEATURE_NAME = 'push';
1312
const UNSUPPORTED_BADGE_KEY = "unsupported";
@@ -40,7 +39,7 @@ export class PushController extends AdaptableController {
4039
}
4140
}
4241

43-
sendPush(body = {}, where = {}, config, auth) {
42+
sendPush(body = {}, where = {}, config, auth, wait) {
4443
var pushAdapter = this.adapter;
4544
if (!pushAdapter) {
4645
throw new Parse.Error(Parse.Error.PUSH_MISCONFIGURED,
@@ -83,67 +82,56 @@ export class PushController extends AdaptableController {
8382
})
8483
}
8584
}
86-
let pushStatus;
85+
let pushStatus = pushStatusHandler(config);
8786
return Promise.resolve().then(() => {
88-
return this.saveInitialPushStatus(body, where, config);
89-
}).then((res) => {
90-
pushStatus = res.response;
87+
return pushStatus.setInitial(body, where);
88+
}).then(() => {
9189
return badgeUpdate();
9290
}).then(() => {
9391
return rest.find(config, auth, '_Installation', where);
9492
}).then((response) => {
95-
this.updatePushStatus({status: "running"}, {status:"pending", objectId: pushStatus.objectId}, config);
96-
if (body.data && body.data.badge && body.data.badge == "Increment") {
97-
// Collect the badges to reduce the # of calls
98-
let badgeInstallationsMap = response.results.reduce((map, installation) => {
99-
let badge = installation.badge;
100-
if (installation.deviceType != "ios") {
101-
badge = UNSUPPORTED_BADGE_KEY;
102-
}
103-
map[badge+''] = map[badge+''] || [];
104-
map[badge+''].push(installation);
105-
return map;
106-
}, {});
107-
108-
// Map the on the badges count and return the send result
109-
let promises = Object.keys(badgeInstallationsMap).map((badge) => {
110-
let payload = deepcopy(body);
111-
if (badge == UNSUPPORTED_BADGE_KEY) {
112-
delete payload.data.badge;
113-
} else {
114-
payload.data.badge = parseInt(badge);
115-
}
116-
return pushAdapter.send(payload, badgeInstallationsMap[badge], pushStatus);
117-
});
118-
return Promise.all(promises);
119-
}
120-
return pushAdapter.send(body, response.results, pushStatus);
93+
pushStatus.setRunning();
94+
return this.sendToAdapter(body, response.results, pushStatus, config);
12195
}).then((results) => {
122-
// TODO: handle push results
123-
return Promise.resolve(results);
96+
return pushStatus.complete(results);
12497
});
12598
}
12699

127-
saveInitialPushStatus(body, where, config, options = {source: 'rest'}) {
128-
let pushStatus = {
129-
pushTime: (new Date()).toISOString(),
130-
query: JSON.stringify(where),
131-
payload: body.data,
132-
source: options.source,
133-
title: options.title,
134-
expiry: body.expiration_time,
135-
status: "pending",
136-
numSent: 0,
137-
pushHash: md5Hash(JSON.stringify(body.data)),
138-
ACL: new Parse.ACL() // lockdown!
139-
}
140-
let restWrite = new RestWrite(config, {isMaster: true},'_PushStatus',null, pushStatus);
141-
return restWrite.execute();
142-
}
100+
sendToAdapter(body, installations, pushStatus, config) {
101+
if (body.data && body.data.badge && body.data.badge == "Increment") {
102+
// Collect the badges to reduce the # of calls
103+
let badgeInstallationsMap = installations.reduce((map, installation) => {
104+
let badge = installation.badge;
105+
if (installation.deviceType != "ios") {
106+
badge = UNSUPPORTED_BADGE_KEY;
107+
}
108+
map[badge+''] = map[badge+''] || [];
109+
map[badge+''].push(installation);
110+
return map;
111+
}, {});
143112

144-
updatePushStatus(update, where, config) {
145-
let restWrite = new RestWrite(config, {isMaster: true}, '_PushStatus', where, update);
146-
return restWrite.execute();
113+
// Map the on the badges count and return the send result
114+
let promises = Object.keys(badgeInstallationsMap).map((badge) => {
115+
let payload = deepcopy(body);
116+
if (badge == UNSUPPORTED_BADGE_KEY) {
117+
delete payload.data.badge;
118+
} else {
119+
payload.data.badge = parseInt(badge);
120+
}
121+
return this.adapter.send(payload, badgeInstallationsMap[badge]);
122+
});
123+
// Flatten the promises results
124+
return Promise.all(promises).then((results) => {
125+
if (Array.isArray(results)) {
126+
return Promise.resolve(results.reduce((memo, result) => {
127+
return memo.concat(result);
128+
},[]));
129+
} else {
130+
return Promise.resolve(results);
131+
}
132+
})
133+
}
134+
return this.adapter.send(body, installations);
147135
}
148136

149137
/**

src/Schema.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,11 @@ var defaultColumns = {
7878
"expiry": {type:'Number'},
7979
"status": {type:'String'},
8080
"numSent": {type:'Number'},
81+
"numFailed": {type:'Number'},
8182
"pushHash": {type:'String'},
8283
"errorMessage": {type:'Object'},
83-
"sentPerType": {type:'Object'}
84+
"sentPerType": {type:'Object'},
85+
"failedPerType":{type:'Object'},
8486
}
8587
};
8688

src/pushStatusHandler.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import RestWrite from './RestWrite';
2+
import { md5Hash } from './cryptoUtils';
3+
4+
export default function pushStatusHandler(config) {
5+
6+
let initialPromise;
7+
let pushStatus;
8+
let setInitial = function(body, where, options = {source: 'rest'}) {
9+
let object = {
10+
pushTime: (new Date()).toISOString(),
11+
query: JSON.stringify(where),
12+
payload: body.data,
13+
source: options.source,
14+
title: options.title,
15+
expiry: body.expiration_time,
16+
status: "pending",
17+
numSent: 0,
18+
pushHash: md5Hash(JSON.stringify(body.data)),
19+
ACL: new Parse.ACL() // lockdown!
20+
}
21+
let restWrite = new RestWrite(config, {isMaster: true},'_PushStatus',null, object);
22+
initialPromise = restWrite.execute().then((res) => {
23+
pushStatus = res.response;
24+
return Promise.resolve(pushStatus);
25+
});
26+
return initialPromise;
27+
}
28+
29+
let setRunning = function() {
30+
return initialPromise.then(() => {
31+
let restWrite = new RestWrite(config, {isMaster: true}, '_PushStatus', {status:"pending", objectId: pushStatus.objectId}, {status: "running"});
32+
return restWrite.execute();
33+
})
34+
}
35+
36+
let complete = function(results) {
37+
let update = {
38+
status: 'succeeded',
39+
numSent: 0,
40+
numFailed: 0,
41+
};
42+
if (Array.isArray(results)) {
43+
results.reduce((memo, result) => {
44+
// Cannot handle that
45+
if (!result.device || !result.device.deviceType) {
46+
return memo;
47+
}
48+
let deviceType = result.device.deviceType;
49+
if (result.transmitted)
50+
{
51+
memo.numSent++;
52+
memo.sentPerType = memo.sentPerType || {};
53+
memo.sentPerType[deviceType] = memo.sentPerType[deviceType] || 0;
54+
memo.sentPerType[deviceType]++;
55+
} else {
56+
memo.numFailed++;
57+
memo.failedPerType = memo.failedPerType || {};
58+
memo.failedPerType[deviceType] = memo.failedPerType[deviceType] || 0;
59+
memo.failedPerType[deviceType]++;
60+
}
61+
return memo;
62+
}, update);
63+
}
64+
65+
return initialPromise.then(() => {
66+
let restWrite = new RestWrite(config, {isMaster: true}, '_PushStatus', {status:"running", objectId: pushStatus.objectId}, update);
67+
return restWrite.execute();
68+
})
69+
}
70+
71+
return Object.freeze({
72+
setInitial,
73+
setRunning,
74+
complete
75+
})
76+
}

0 commit comments

Comments
 (0)