forked from parse-community/parse-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPushRouter.js
83 lines (77 loc) · 2.26 KB
/
PushRouter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import PromiseRouter from '../PromiseRouter';
import * as middleware from '../middlewares';
import { Parse } from 'parse/node';
export class PushRouter extends PromiseRouter {
mountRoutes() {
this.route('POST', '/push', middleware.promiseEnforceMasterKeyAccess, PushRouter.handlePOST);
}
static handlePOST(req) {
if (req.auth.isReadOnly) {
throw new Parse.Error(
Parse.Error.OPERATION_FORBIDDEN,
"read-only masterKey isn't allowed to send push notifications."
);
}
const pushController = req.config.pushController;
if (!pushController) {
throw new Parse.Error(Parse.Error.PUSH_MISCONFIGURED, 'Push controller is not set');
}
const where = PushRouter.getQueryCondition(req);
let resolve;
const promise = new Promise(_resolve => {
resolve = _resolve;
});
let pushStatusId;
pushController
.sendPush(req.body || {}, where, req.config, req.auth, objectId => {
pushStatusId = objectId;
resolve({
headers: {
'X-Parse-Push-Status-Id': pushStatusId,
},
response: {
result: true,
},
});
})
.catch(err => {
req.config.loggerController.error(
`_PushStatus ${pushStatusId}: error while sending push`,
err
);
});
return promise;
}
/**
* Get query condition from the request body.
* @param {Object} req A request object
* @returns {Object} The query condition, the where field in a query api call
*/
static getQueryCondition(req) {
const body = req.body || {};
const hasWhere = typeof body.where !== 'undefined';
const hasChannels = typeof body.channels !== 'undefined';
let where;
if (hasWhere && hasChannels) {
throw new Parse.Error(
Parse.Error.PUSH_MISCONFIGURED,
'Channels and query can not be set at the same time.'
);
} else if (hasWhere) {
where = body.where;
} else if (hasChannels) {
where = {
channels: {
$in: body.channels,
},
};
} else {
throw new Parse.Error(
Parse.Error.PUSH_MISCONFIGURED,
'Sending a push requires either "channels" or a "where" query.'
);
}
return where;
}
}
export default PushRouter;