| 7 | |
| 8 | export class PushController { |
| 9 | sendPush(body = {}, where = {}, config, auth, onPushStatusSaved = () => {}, now = new Date()) { |
| 10 | if (!config.hasPushSupport) { |
| 11 | throw new Parse.Error(Parse.Error.PUSH_MISCONFIGURED, 'Missing push configuration'); |
| 12 | } |
| 13 | |
| 14 | // Replace the expiration_time and push_time with a valid Unix epoch milliseconds time |
| 15 | body.expiration_time = PushController.getExpirationTime(body); |
| 16 | body.expiration_interval = PushController.getExpirationInterval(body); |
| 17 | if (body.expiration_time && body.expiration_interval) { |
| 18 | throw new Parse.Error( |
| 19 | Parse.Error.PUSH_MISCONFIGURED, |
| 20 | 'Both expiration_time and expiration_interval cannot be set' |
| 21 | ); |
| 22 | } |
| 23 | |
| 24 | // Immediate push |
| 25 | if (body.expiration_interval && !Object.prototype.hasOwnProperty.call(body, 'push_time')) { |
| 26 | const ttlMs = body.expiration_interval * 1000; |
| 27 | body.expiration_time = new Date(now.valueOf() + ttlMs).valueOf(); |
| 28 | } |
| 29 | |
| 30 | const pushTime = PushController.getPushTime(body); |
| 31 | if (pushTime && pushTime.date !== 'undefined') { |
| 32 | body['push_time'] = PushController.formatPushTime(pushTime); |
| 33 | } |
| 34 | |
| 35 | // TODO: If the req can pass the checking, we return immediately instead of waiting |
| 36 | // pushes to be sent. We probably change this behaviour in the future. |
| 37 | let badgeUpdate = () => { |
| 38 | return Promise.resolve(); |
| 39 | }; |
| 40 | |
| 41 | if (body.data && body.data.badge) { |
| 42 | const badge = body.data.badge; |
| 43 | let restUpdate = {}; |
| 44 | if (typeof badge == 'string' && badge.toLowerCase() === 'increment') { |
| 45 | restUpdate = { badge: { __op: 'Increment', amount: 1 } }; |
| 46 | } else if ( |
| 47 | typeof badge == 'object' && |
| 48 | typeof badge.__op == 'string' && |
| 49 | badge.__op.toLowerCase() == 'increment' && |
| 50 | Number(badge.amount) |
| 51 | ) { |
| 52 | restUpdate = { badge: { __op: 'Increment', amount: badge.amount } }; |
| 53 | } else if (Number(badge)) { |
| 54 | restUpdate = { badge: badge }; |
| 55 | } else { |
| 56 | throw "Invalid value for badge, expected number or 'Increment' or {increment: number}"; |
| 57 | } |
| 58 | |
| 59 | // Force filtering on only valid device tokens |
| 60 | const updateWhere = applyDeviceTokenExists(where); |
| 61 | badgeUpdate = async () => { |
| 62 | // Build a real RestQuery so we can use it in RestWrite |
| 63 | const restQuery = await RestQuery({ |
| 64 | method: RestQuery.Method.find, |
| 65 | config, |
| 66 | runBeforeFind: false, |