Skip to content

Commit 4259c5d

Browse files
committed
Merge pull request #903 from ParsePlatform/nlutsenko.push
Cleanup PushController/PushRouter, remove raw mongo collection access.
2 parents 7909f0e + 654a540 commit 4259c5d

File tree

7 files changed

+32
-123
lines changed

7 files changed

+32
-123
lines changed

spec/ParseGlobalConfig.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ var Parse = require('parse/node').Parse;
55
let Config = require('../src/Config');
66

77
describe('a GlobalConfig', () => {
8-
beforeEach(function (done) {
8+
beforeEach(done => {
99
let config = new Config('test');
1010
config.database.adaptiveCollection('_GlobalConfig')
1111
.then(coll => coll.upsertOne({ '_id': 1 }, { $set: { params: { companies: ['US', 'DK'] } } }))
12-
.then(done());
12+
.then(() => { done(); });
1313
});
1414

1515
it('can be retrieved', (done) => {

spec/PushController.spec.js

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,6 @@ var PushController = require('../src/Controllers/PushController').PushController
33
var Config = require('../src/Config');
44

55
describe('PushController', () => {
6-
it('can check valid master key of request', (done) => {
7-
// Make mock request
8-
var auth = {
9-
isMaster: true
10-
}
11-
12-
expect(() => {
13-
PushController.validateMasterKey(auth);
14-
}).not.toThrow();
15-
done();
16-
});
17-
18-
it('can check invalid master key of request', (done) => {
19-
// Make mock request
20-
var auth = {
21-
isMaster: false
22-
}
23-
24-
expect(() => {
25-
PushController.validateMasterKey(auth);
26-
}).toThrow();
27-
done();
28-
});
29-
30-
316
it('can validate device type when no device type is set', (done) => {
327
// Make query condition
338
var where = {

spec/PushRouter.spec.js

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,6 @@ var PushRouter = require('../src/Routers/PushRouter').PushRouter;
22
var request = require('request');
33

44
describe('PushRouter', () => {
5-
it('can check valid master key of request', (done) => {
6-
// Make mock request
7-
var request = {
8-
info: {
9-
masterKey: 'masterKey'
10-
},
11-
config: {
12-
masterKey: 'masterKey'
13-
}
14-
}
15-
16-
expect(() => {
17-
PushRouter.validateMasterKey(request);
18-
}).not.toThrow();
19-
done();
20-
});
21-
22-
it('can check invalid master key of request', (done) => {
23-
// Make mock request
24-
var request = {
25-
info: {
26-
masterKey: 'masterKey'
27-
},
28-
config: {
29-
masterKey: 'masterKeyAgain'
30-
}
31-
}
32-
33-
expect(() => {
34-
PushRouter.validateMasterKey(request);
35-
}).toThrow();
36-
done();
37-
});
38-
395
it('can get query condition when channels is set', (done) => {
406
// Make mock request
417
var request = {

src/Adapters/Storage/Mongo/MongoCollection.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ export default class MongoCollection {
6464
return this._mongoCollection.update(query, update, { upsert: true });
6565
}
6666

67+
updateMany(query, update) {
68+
return this._mongoCollection.updateMany(query, update);
69+
}
70+
6771
// Atomically find and delete an object based on query.
6872
// The result is the promise with an object that was in the database before deleting.
6973
// Postgres Note: Translates directly to `DELETE * FROM ... RETURNING *`, which will return data after delete is done.

src/Controllers/DatabaseController.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ DatabaseController.prototype.collection = function(className) {
3535
throw new Parse.Error(Parse.Error.INVALID_CLASS_NAME,
3636
'invalid className: ' + className);
3737
}
38-
return this.rawCollection(className);
38+
return this.adapter.collection(this.collectionPrefix + className);
3939
};
4040

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

49-
DatabaseController.prototype.rawCollection = function(className) {
50-
return this.adapter.collection(this.collectionPrefix + className);
51-
};
52-
5349
DatabaseController.prototype.dropCollection = function(className) {
5450
return this.adapter.dropCollection(this.collectionPrefix + className);
5551
};

src/Controllers/PushController.js

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -36,25 +36,13 @@ export class PushController extends AdaptableController {
3636
}
3737
}
3838
}
39-
40-
/**
41-
* Check whether the api call has master key or not.
42-
* @param {Object} request A request object
43-
*/
44-
static validateMasterKey(auth = {}) {
45-
if (!auth.isMaster) {
46-
throw new Parse.Error(Parse.Error.PUSH_MISCONFIGURED,
47-
'Master key is invalid, you should only use master key to send push');
48-
}
49-
}
5039

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

6553
if (body.badge) {
66-
var op = {};
54+
let op = {};
6755
if (body.badge == "Increment") {
68-
op = {'$inc': {'badge': 1}}
56+
op = { $inc: { badge: 1 } }
6957
} else if (Number(body.badge)) {
70-
op = {'$set': {'badge': body.badge } }
58+
op = { $set: { badge: body.badge } }
7159
} else {
7260
throw "Invalid value for badge, expected number or 'Increment'";
7361
}
7462
let updateWhere = deepcopy(where);
63+
updateWhere.deviceType = 'ios'; // Only on iOS!
7564

76-
// Only on iOS!
77-
updateWhere.deviceType = 'ios';
78-
79-
// TODO: @nlutsenko replace with better thing
80-
badgeUpdate = config.database.rawCollection("_Installation").then((coll) => {
81-
return coll.update(updateWhere, op, { multi: true });
82-
});
65+
badgeUpdate = config.database.adaptiveCollection("_Installation")
66+
.then(coll => coll.updateMany(updateWhere, op));
8367
}
8468

8569
return badgeUpdate.then(() => {
@@ -144,6 +128,6 @@ export class PushController extends AdaptableController {
144128
expectedAdapterType() {
145129
return PushAdapter;
146130
}
147-
};
131+
}
148132

149133
export default PushController;

src/Routers/PushRouter.js

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,42 @@
1-
import PushController from '../Controllers/PushController'
21
import PromiseRouter from '../PromiseRouter';
2+
import * as middleware from "../middlewares";
3+
import { Parse } from "parse/node";
34

45
export class PushRouter extends PromiseRouter {
56

67
mountRoutes() {
7-
this.route("POST", "/push", req => { return this.handlePOST(req); });
8-
}
9-
10-
/**
11-
* Check whether the api call has master key or not.
12-
* @param {Object} request A request object
13-
*/
14-
static validateMasterKey(req) {
15-
if (req.info.masterKey !== req.config.masterKey) {
16-
throw new Parse.Error(Parse.Error.PUSH_MISCONFIGURED,
17-
'Master key is invalid, you should only use master key to send push');
18-
}
8+
this.route("POST", "/push", middleware.promiseEnforceMasterKeyAccess, PushRouter.handlePOST);
199
}
2010

21-
handlePOST(req) {
22-
// TODO: move to middlewares when support for Promise middlewares
23-
PushRouter.validateMasterKey(req);
24-
11+
static handlePOST(req) {
2512
const pushController = req.config.pushController;
2613
if (!pushController) {
27-
throw new Parse.Error(Parse.Error.PUSH_MISCONFIGURED,
28-
'Push controller is not set');
14+
throw new Parse.Error(Parse.Error.PUSH_MISCONFIGURED, 'Push controller is not set');
2915
}
3016

31-
var where = PushRouter.getQueryCondition(req);
32-
17+
let where = PushRouter.getQueryCondition(req);
3318
pushController.sendPush(req.body, where, req.config, req.auth);
3419
return Promise.resolve({
35-
response: {
36-
'result': true
37-
}
20+
response: {
21+
'result': true
22+
}
3823
});
3924
}
40-
41-
/**
25+
26+
/**
4227
* Get query condition from the request body.
43-
* @param {Object} request A request object
28+
* @param {Object} req A request object
4429
* @returns {Object} The query condition, the where field in a query api call
4530
*/
4631
static getQueryCondition(req) {
47-
var body = req.body || {};
48-
var hasWhere = typeof body.where !== 'undefined';
49-
var hasChannels = typeof body.channels !== 'undefined';
32+
let body = req.body || {};
33+
let hasWhere = typeof body.where !== 'undefined';
34+
let hasChannels = typeof body.channels !== 'undefined';
5035

51-
var where;
36+
let where;
5237
if (hasWhere && hasChannels) {
5338
throw new Parse.Error(Parse.Error.PUSH_MISCONFIGURED,
54-
'Channels and query can not be set at the same time.');
39+
'Channels and query can not be set at the same time.');
5540
} else if (hasWhere) {
5641
where = body.where;
5742
} else if (hasChannels) {
@@ -62,11 +47,10 @@ export class PushRouter extends PromiseRouter {
6247
}
6348
} else {
6449
throw new Parse.Error(Parse.Error.PUSH_MISCONFIGURED,
65-
'Channels and query should be set at least one.');
50+
'Channels and query should be set at least one.');
6651
}
6752
return where;
6853
}
69-
7054
}
7155

7256
export default PushRouter;

0 commit comments

Comments
 (0)