Skip to content

Commit

Permalink
Removes _PushStatus from system classes, uses direct DB access to write
Browse files Browse the repository at this point in the history
  • Loading branch information
flovilmart committed Mar 14, 2016
1 parent 120f23c commit 18781f1
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 15 deletions.
12 changes: 12 additions & 0 deletions spec/PushController.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,11 +283,18 @@ describe('PushController', () => {
Parse.Object.saveAll(installations).then(() => {
return pushController.sendPush(payload, {}, config, auth);
}).then((result) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve();
}, 1000);
});
}).then(() => {
let query = new Parse.Query('_PushStatus');
return query.find({useMasterKey: true});
}).then((results) => {
expect(results.length).toBe(1);
let result = results[0];
expect(result.createdAt instanceof Date).toBe(true);
expect(result.get('source')).toEqual('rest');
expect(result.get('query')).toEqual(JSON.stringify({}));
expect(result.get('payload')).toEqual(payload.data);
Expand All @@ -300,6 +307,11 @@ describe('PushController', () => {
expect(result.get('failedPerType')).toEqual({
'android': 5 // android
});
// Try to get it without masterKey
let query = new Parse.Query('_PushStatus');
return query.find();
}).then((results) => {
expect(results.length).toBe(0);
done();
});

Expand Down
2 changes: 1 addition & 1 deletion src/Schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ var requiredColumns = {
_Role: ["name", "ACL"]
}

const systemClasses = ['_User', '_Installation', '_Role', '_Session', '_Product', '_PushStatus'];
const systemClasses = ['_User', '_Installation', '_Role', '_Session', '_Product'];

// 10 alpha numberic chars + uppercase
const userIdRegex = /^[a-zA-Z0-9]{10}$/;
Expand Down
42 changes: 28 additions & 14 deletions src/pushStatusHandler.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import RestWrite from './RestWrite';
import { md5Hash } from './cryptoUtils';
import { md5Hash, newObjectId } from './cryptoUtils';

export default function pushStatusHandler(config) {

let initialPromise;
let pushStatus;

let collection = function() {
return config.database.adaptiveCollection('_PushStatus');
}

let setInitial = function(body, where, options = {source: 'rest'}) {
let now = new Date();
let object = {
pushTime: (new Date()).toISOString(),
objectId: newObjectId(),
pushTime: now.toISOString(),
_created_at: now,
query: JSON.stringify(where),
payload: body.data,
source: options.source,
Expand All @@ -16,21 +23,27 @@ export default function pushStatusHandler(config) {
status: "pending",
numSent: 0,
pushHash: md5Hash(JSON.stringify(body.data)),
ACL: new Parse.ACL() // lockdown!
// lockdown!
_wperm: [],
_rperm: []
}
let restWrite = new RestWrite(config, {isMaster: true},'_PushStatus',null, object);
initialPromise = restWrite.execute().then((res) => {
pushStatus = res.response;
initialPromise = collection().then((collection) => {
return collection.insertOne(object);
}).then((res) => {
pushStatus = {
objectId: object.objectId
};
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();
})
return collection();
}).then((collection) => {
return collection.updateOne({status:"pending", objectId: pushStatus.objectId}, {$set: {status: "running"}});
});
}

let complete = function(results) {
Expand Down Expand Up @@ -63,9 +76,10 @@ export default function pushStatusHandler(config) {
}

return initialPromise.then(() => {
let restWrite = new RestWrite(config, {isMaster: true}, '_PushStatus', {status:"running", objectId: pushStatus.objectId}, update);
return restWrite.execute();
})
return collection();
}).then((collection) => {
return collection.updateOne({status:"running", objectId: pushStatus.objectId}, {$set: update});
});
}

return Object.freeze({
Expand Down

0 comments on commit 18781f1

Please sign in to comment.