Skip to content

Commit

Permalink
Adds X-Parse-Push-Status-Id header (#1412)
Browse files Browse the repository at this point in the history
* Adds X-Parse-Push-Status-Id header

* Waits for _PushStatus to be stored
  • Loading branch information
flovilmart committed Apr 7, 2016
1 parent cedac3f commit bc96f0b
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 10 deletions.
7 changes: 5 additions & 2 deletions spec/PushRouter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ describe('PushRouter', () => {
}).toThrow();
done();
});

it('sends a push through REST', (done) => {
request.post({
url: Parse.serverURL+"/push",
Expand All @@ -82,8 +82,11 @@ describe('PushRouter', () => {
'X-Parse-Master-Key': Parse.masterKey
}
}, function(err, res, body){
expect(res.headers['x-parse-push-status-id']).not.toBe(undefined);
expect(res.headers['x-parse-push-status-id'].length).toBe(10);
expect(res.headers[''])
expect(body.result).toBe(true);
done();
});
});
});
});
3 changes: 2 additions & 1 deletion src/Controllers/PushController.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class PushController extends AdaptableController {
return !!this.adapter;
}

sendPush(body = {}, where = {}, config, auth, wait) {
sendPush(body = {}, where = {}, config, auth, onPushStatusSaved = () => {}) {
var pushAdapter = this.adapter;
if (!this.pushIsAvailable) {
throw new Parse.Error(Parse.Error.PUSH_MISCONFIGURED,
Expand Down Expand Up @@ -84,6 +84,7 @@ export class PushController extends AdaptableController {
return Promise.resolve().then(() => {
return pushStatus.setInitial(body, where);
}).then(() => {
onPushStatusSaved(pushStatus.objectId);
return badgeUpdate();
}).then(() => {
return rest.find(config, auth, '_Installation', where);
Expand Down
5 changes: 5 additions & 0 deletions src/PromiseRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,11 @@ function makeExpressHandler(promiseHandler) {
return res.send('Found. Redirecting to '+result.location);
}
}
if (result.headers) {
Object.keys(result.headers).forEach((header) => {
res.set(header, result.headers[header]);
})
}
res.json(result.response);
}, (e) => {
log.verbose('error:', e);
Expand Down
19 changes: 14 additions & 5 deletions src/Routers/PushRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,21 @@ export class PushRouter extends PromiseRouter {
}

let where = PushRouter.getQueryCondition(req);
pushController.sendPush(req.body, where, req.config, req.auth);
return Promise.resolve({
response: {
'result': true
}
let resolve;
let promise = new Promise((_resolve) => {
resolve = _resolve;
});
pushController.sendPush(req.body, where, req.config, req.auth, (pushStatusId) => {
resolve({
headers: {
'X-Parse-Push-Status-Id': pushStatusId
},
response: {
result: true
}
});
});
return promise;
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/pushStatusHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default function pushStatusHandler(config) {

let initialPromise;
let pushStatus;

let objectId = newObjectId();
let collection = function() {
return config.database.adaptiveCollection('_PushStatus');
}
Expand All @@ -25,7 +25,7 @@ export default function pushStatusHandler(config) {
let now = new Date();
let data = body.data || {};
let object = {
objectId: newObjectId(),
objectId,
pushTime: now.toISOString(),
_created_at: now,
query: JSON.stringify(where),
Expand Down Expand Up @@ -111,6 +111,7 @@ export default function pushStatusHandler(config) {
}

return Object.freeze({
objectId,
setInitial,
setRunning,
complete,
Expand Down

0 comments on commit bc96f0b

Please sign in to comment.