Adds support to store push results

This commit is contained in:
Florent Vilmart
2016-03-13 23:34:44 -04:00
parent 05baf36a46
commit 7c387e1ee9
5 changed files with 183 additions and 75 deletions

View File

@@ -10,6 +10,9 @@ import PushAdapter from './PushAdapter';
import { classifyInstallations } from './PushAdapterUtils';
export class ParsePushAdapter extends PushAdapter {
supportsPushTracking = true;
constructor(pushConfig = {}) {
super(pushConfig);
this.validPushTypes = ['ios', 'android'];
@@ -56,7 +59,7 @@ export class ParsePushAdapter extends PushAdapter {
}))
} else {
let devices = deviceMap[pushType];
sendPromises.push(sender.send(data, devices));
sendPromises.push(sender.send(data, devices));
}
}
return Parse.Promise.when(sendPromises);

View File

@@ -4,10 +4,9 @@ import rest from '../rest';
import AdaptableController from './AdaptableController';
import { PushAdapter } from '../Adapters/Push/PushAdapter';
import deepcopy from 'deepcopy';
import { md5Hash } from '../cryptoUtils';
import features from '../features';
import RestQuery from '../RestQuery';
import RestWrite from '../RestWrite';
import pushStatusHandler from '../pushStatusHandler';
const FEATURE_NAME = 'push';
const UNSUPPORTED_BADGE_KEY = "unsupported";
@@ -40,7 +39,7 @@ export class PushController extends AdaptableController {
}
}
sendPush(body = {}, where = {}, config, auth) {
sendPush(body = {}, where = {}, config, auth, wait) {
var pushAdapter = this.adapter;
if (!pushAdapter) {
throw new Parse.Error(Parse.Error.PUSH_MISCONFIGURED,
@@ -83,67 +82,56 @@ export class PushController extends AdaptableController {
})
}
}
let pushStatus;
let pushStatus = pushStatusHandler(config);
return Promise.resolve().then(() => {
return this.saveInitialPushStatus(body, where, config);
}).then((res) => {
pushStatus = res.response;
return pushStatus.setInitial(body, where);
}).then(() => {
return badgeUpdate();
}).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) => {
let badge = installation.badge;
if (installation.deviceType != "ios") {
badge = UNSUPPORTED_BADGE_KEY;
}
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.data.badge;
} else {
payload.data.badge = parseInt(badge);
}
return pushAdapter.send(payload, badgeInstallationsMap[badge], pushStatus);
});
return Promise.all(promises);
}
return pushAdapter.send(body, response.results, pushStatus);
pushStatus.setRunning();
return this.sendToAdapter(body, response.results, pushStatus, config);
}).then((results) => {
// TODO: handle push results
return Promise.resolve(results);
return pushStatus.complete(results);
});
}
saveInitialPushStatus(body, where, config, options = {source: 'rest'}) {
let pushStatus = {
pushTime: (new Date()).toISOString(),
query: JSON.stringify(where),
payload: body.data,
source: options.source,
title: options.title,
expiry: body.expiration_time,
status: "pending",
numSent: 0,
pushHash: md5Hash(JSON.stringify(body.data)),
ACL: new Parse.ACL() // lockdown!
}
let restWrite = new RestWrite(config, {isMaster: true},'_PushStatus',null, pushStatus);
return restWrite.execute();
}
sendToAdapter(body, installations, pushStatus, config) {
if (body.data && body.data.badge && body.data.badge == "Increment") {
// Collect the badges to reduce the # of calls
let badgeInstallationsMap = installations.reduce((map, installation) => {
let badge = installation.badge;
if (installation.deviceType != "ios") {
badge = UNSUPPORTED_BADGE_KEY;
}
map[badge+''] = map[badge+''] || [];
map[badge+''].push(installation);
return map;
}, {});
updatePushStatus(update, where, config) {
let restWrite = new RestWrite(config, {isMaster: true}, '_PushStatus', where, update);
return restWrite.execute();
// 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.data.badge;
} else {
payload.data.badge = parseInt(badge);
}
return this.adapter.send(payload, badgeInstallationsMap[badge]);
});
// Flatten the promises results
return Promise.all(promises).then((results) => {
if (Array.isArray(results)) {
return Promise.resolve(results.reduce((memo, result) => {
return memo.concat(result);
},[]));
} else {
return Promise.resolve(results);
}
})
}
return this.adapter.send(body, installations);
}
/**

View File

@@ -78,9 +78,11 @@ var defaultColumns = {
"expiry": {type:'Number'},
"status": {type:'String'},
"numSent": {type:'Number'},
"numFailed": {type:'Number'},
"pushHash": {type:'String'},
"errorMessage": {type:'Object'},
"sentPerType": {type:'Object'}
"sentPerType": {type:'Object'},
"failedPerType":{type:'Object'},
}
};

76
src/pushStatusHandler.js Normal file
View File

@@ -0,0 +1,76 @@
import RestWrite from './RestWrite';
import { md5Hash } from './cryptoUtils';
export default function pushStatusHandler(config) {
let initialPromise;
let pushStatus;
let setInitial = function(body, where, options = {source: 'rest'}) {
let object = {
pushTime: (new Date()).toISOString(),
query: JSON.stringify(where),
payload: body.data,
source: options.source,
title: options.title,
expiry: body.expiration_time,
status: "pending",
numSent: 0,
pushHash: md5Hash(JSON.stringify(body.data)),
ACL: new Parse.ACL() // lockdown!
}
let restWrite = new RestWrite(config, {isMaster: true},'_PushStatus',null, object);
initialPromise = restWrite.execute().then((res) => {
pushStatus = res.response;
return Promise.resolve(pushStatus);
});
return initialPromise;
}
let setRunning = function() {
return initialPromise.then(() => {
let restWrite = new RestWrite(config, {isMaster: true}, '_PushStatus', {status:"pending", objectId: pushStatus.objectId}, {status: "running"});
return restWrite.execute();
})
}
let complete = function(results) {
let update = {
status: 'succeeded',
numSent: 0,
numFailed: 0,
};
if (Array.isArray(results)) {
results.reduce((memo, result) => {
// Cannot handle that
if (!result.device || !result.device.deviceType) {
return memo;
}
let deviceType = result.device.deviceType;
if (result.transmitted)
{
memo.numSent++;
memo.sentPerType = memo.sentPerType || {};
memo.sentPerType[deviceType] = memo.sentPerType[deviceType] || 0;
memo.sentPerType[deviceType]++;
} else {
memo.numFailed++;
memo.failedPerType = memo.failedPerType || {};
memo.failedPerType[deviceType] = memo.failedPerType[deviceType] || 0;
memo.failedPerType[deviceType]++;
}
return memo;
}, update);
}
return initialPromise.then(() => {
let restWrite = new RestWrite(config, {isMaster: true}, '_PushStatus', {status:"running", objectId: pushStatus.objectId}, update);
return restWrite.execute();
})
}
return Object.freeze({
setInitial,
setRunning,
complete
})
}