Skip to content

Cleanup PushController/PushRouter, remove raw mongo collection access. #903

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions spec/ParseGlobalConfig.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ var Parse = require('parse/node').Parse;
let Config = require('../src/Config');

describe('a GlobalConfig', () => {
beforeEach(function (done) {
beforeEach(done => {
let config = new Config('test');
config.database.adaptiveCollection('_GlobalConfig')
.then(coll => coll.upsertOne({ '_id': 1 }, { $set: { params: { companies: ['US', 'DK'] } } }))
.then(done());
.then(() => { done(); });
});

it('can be retrieved', (done) => {
Expand Down
25 changes: 0 additions & 25 deletions spec/PushController.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,6 @@ var PushController = require('../src/Controllers/PushController').PushController
var Config = require('../src/Config');

describe('PushController', () => {
it('can check valid master key of request', (done) => {
// Make mock request
var auth = {
isMaster: true
}

expect(() => {
PushController.validateMasterKey(auth);
}).not.toThrow();
done();
});

it('can check invalid master key of request', (done) => {
// Make mock request
var auth = {
isMaster: false
}

expect(() => {
PushController.validateMasterKey(auth);
}).toThrow();
done();
});


it('can validate device type when no device type is set', (done) => {
// Make query condition
var where = {
Expand Down
34 changes: 0 additions & 34 deletions spec/PushRouter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,6 @@ var PushRouter = require('../src/Routers/PushRouter').PushRouter;
var request = require('request');

describe('PushRouter', () => {
it('can check valid master key of request', (done) => {
// Make mock request
var request = {
info: {
masterKey: 'masterKey'
},
config: {
masterKey: 'masterKey'
}
}

expect(() => {
PushRouter.validateMasterKey(request);
}).not.toThrow();
done();
});

it('can check invalid master key of request', (done) => {
// Make mock request
var request = {
info: {
masterKey: 'masterKey'
},
config: {
masterKey: 'masterKeyAgain'
}
}

expect(() => {
PushRouter.validateMasterKey(request);
}).toThrow();
done();
});

it('can get query condition when channels is set', (done) => {
// Make mock request
var request = {
Expand Down
4 changes: 4 additions & 0 deletions src/Adapters/Storage/Mongo/MongoCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ export default class MongoCollection {
return this._mongoCollection.update(query, update, { upsert: true });
}

updateMany(query, update) {
return this._mongoCollection.updateMany(query, update);
}

// Atomically find and delete an object based on query.
// The result is the promise with an object that was in the database before deleting.
// Postgres Note: Translates directly to `DELETE * FROM ... RETURNING *`, which will return data after delete is done.
Expand Down
6 changes: 1 addition & 5 deletions src/Controllers/DatabaseController.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ DatabaseController.prototype.collection = function(className) {
throw new Parse.Error(Parse.Error.INVALID_CLASS_NAME,
'invalid className: ' + className);
}
return this.rawCollection(className);
return this.adapter.collection(this.collectionPrefix + className);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

};

DatabaseController.prototype.adaptiveCollection = function(className) {
Expand All @@ -46,10 +46,6 @@ DatabaseController.prototype.collectionExists = function(className) {
return this.adapter.collectionExists(this.collectionPrefix + className);
};

DatabaseController.prototype.rawCollection = function(className) {
return this.adapter.collection(this.collectionPrefix + className);
};

DatabaseController.prototype.dropCollection = function(className) {
return this.adapter.dropCollection(this.collectionPrefix + className);
};
Expand Down
30 changes: 7 additions & 23 deletions src/Controllers/PushController.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,13 @@ export class PushController extends AdaptableController {
}
}
}

/**
* Check whether the api call has master key or not.
* @param {Object} request A request object
*/
static validateMasterKey(auth = {}) {
if (!auth.isMaster) {
throw new Parse.Error(Parse.Error.PUSH_MISCONFIGURED,
'Master key is invalid, you should only use master key to send push');
}
}

sendPush(body = {}, where = {}, config, auth) {
var pushAdapter = this.adapter;
if (!pushAdapter) {
throw new Parse.Error(Parse.Error.PUSH_MISCONFIGURED,
'Push adapter is not available');
}
PushController.validateMasterKey(auth);
PushController.validatePushType(where, pushAdapter.getValidPushTypes());
// Replace the expiration_time with a valid Unix epoch milliseconds time
body['expiration_time'] = PushController.getExpirationTime(body);
Expand All @@ -63,23 +51,19 @@ export class PushController extends AdaptableController {
let badgeUpdate = Promise.resolve();

if (body.badge) {
var op = {};
let op = {};
if (body.badge == "Increment") {
op = {'$inc': {'badge': 1}}
op = { $inc: { badge: 1 } }
} else if (Number(body.badge)) {
op = {'$set': {'badge': body.badge } }
op = { $set: { badge: body.badge } }
} else {
throw "Invalid value for badge, expected number or 'Increment'";
}
let updateWhere = deepcopy(where);
updateWhere.deviceType = 'ios'; // Only on iOS!

// Only on iOS!
updateWhere.deviceType = 'ios';

// TODO: @nlutsenko replace with better thing
badgeUpdate = config.database.rawCollection("_Installation").then((coll) => {
return coll.update(updateWhere, op, { multi: true });
});
badgeUpdate = config.database.adaptiveCollection("_Installation")
.then(coll => coll.updateMany(updateWhere, op));
}

return badgeUpdate.then(() => {
Expand Down Expand Up @@ -144,6 +128,6 @@ export class PushController extends AdaptableController {
expectedAdapterType() {
return PushAdapter;
}
};
}

export default PushController;
52 changes: 18 additions & 34 deletions src/Routers/PushRouter.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,42 @@
import PushController from '../Controllers/PushController'
import PromiseRouter from '../PromiseRouter';
import * as middleware from "../middlewares";
import { Parse } from "parse/node";

export class PushRouter extends PromiseRouter {

mountRoutes() {
this.route("POST", "/push", req => { return this.handlePOST(req); });
}

/**
* Check whether the api call has master key or not.
* @param {Object} request A request object
*/
static validateMasterKey(req) {
if (req.info.masterKey !== req.config.masterKey) {
throw new Parse.Error(Parse.Error.PUSH_MISCONFIGURED,
'Master key is invalid, you should only use master key to send push');
}
this.route("POST", "/push", middleware.promiseEnforceMasterKeyAccess, PushRouter.handlePOST);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}

handlePOST(req) {
// TODO: move to middlewares when support for Promise middlewares
PushRouter.validateMasterKey(req);

static handlePOST(req) {
const pushController = req.config.pushController;
if (!pushController) {
throw new Parse.Error(Parse.Error.PUSH_MISCONFIGURED,
'Push controller is not set');
throw new Parse.Error(Parse.Error.PUSH_MISCONFIGURED, 'Push controller is not set');
}

var where = PushRouter.getQueryCondition(req);

let where = PushRouter.getQueryCondition(req);
pushController.sendPush(req.body, where, req.config, req.auth);
return Promise.resolve({
response: {
'result': true
}
response: {
'result': true
}
});
}
/**

/**
* Get query condition from the request body.
* @param {Object} request A request object
* @param {Object} req A request object
* @returns {Object} The query condition, the where field in a query api call
*/
static getQueryCondition(req) {
var body = req.body || {};
var hasWhere = typeof body.where !== 'undefined';
var hasChannels = typeof body.channels !== 'undefined';
let body = req.body || {};
let hasWhere = typeof body.where !== 'undefined';
let hasChannels = typeof body.channels !== 'undefined';

var where;
let where;
if (hasWhere && hasChannels) {
throw new Parse.Error(Parse.Error.PUSH_MISCONFIGURED,
'Channels and query can not be set at the same time.');
'Channels and query can not be set at the same time.');
} else if (hasWhere) {
where = body.where;
} else if (hasChannels) {
Expand All @@ -62,11 +47,10 @@ export class PushRouter extends PromiseRouter {
}
} else {
throw new Parse.Error(Parse.Error.PUSH_MISCONFIGURED,
'Channels and query should be set at least one.');
'Channels and query should be set at least one.');
}
return where;
}

}

export default PushRouter;