123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- "use strict";
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.default = exports.PushController = void 0;
- var _node = require("parse/node");
- var _RestQuery = _interopRequireDefault(require("../RestQuery"));
- var _RestWrite = _interopRequireDefault(require("../RestWrite"));
- var _Auth = require("../Auth");
- var _StatusHandler = require("../StatusHandler");
- var _utils = require("../Push/utils");
- function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
- class PushController {
- sendPush(body = {}, where = {}, config, auth, onPushStatusSaved = () => {}, now = new Date()) {
- if (!config.hasPushSupport) {
- throw new _node.Parse.Error(_node.Parse.Error.PUSH_MISCONFIGURED, 'Missing push configuration');
- }
-
- body.expiration_time = PushController.getExpirationTime(body);
- body.expiration_interval = PushController.getExpirationInterval(body);
- if (body.expiration_time && body.expiration_interval) {
- throw new _node.Parse.Error(_node.Parse.Error.PUSH_MISCONFIGURED, 'Both expiration_time and expiration_interval cannot be set');
- }
-
- if (body.expiration_interval && !Object.prototype.hasOwnProperty.call(body, 'push_time')) {
- const ttlMs = body.expiration_interval * 1000;
- body.expiration_time = new Date(now.valueOf() + ttlMs).valueOf();
- }
- const pushTime = PushController.getPushTime(body);
- if (pushTime && pushTime.date !== 'undefined') {
- body['push_time'] = PushController.formatPushTime(pushTime);
- }
-
-
- let badgeUpdate = () => {
- return Promise.resolve();
- };
- if (body.data && body.data.badge) {
- const badge = body.data.badge;
- let restUpdate = {};
- if (typeof badge == 'string' && badge.toLowerCase() === 'increment') {
- restUpdate = {
- badge: {
- __op: 'Increment',
- amount: 1
- }
- };
- } else if (typeof badge == 'object' && typeof badge.__op == 'string' && badge.__op.toLowerCase() == 'increment' && Number(badge.amount)) {
- restUpdate = {
- badge: {
- __op: 'Increment',
- amount: badge.amount
- }
- };
- } else if (Number(badge)) {
- restUpdate = {
- badge: badge
- };
- } else {
- throw "Invalid value for badge, expected number or 'Increment' or {increment: number}";
- }
-
- const updateWhere = (0, _utils.applyDeviceTokenExists)(where);
- badgeUpdate = async () => {
-
- const restQuery = await (0, _RestQuery.default)({
- method: _RestQuery.default.Method.find,
- config,
- runBeforeFind: false,
- auth: (0, _Auth.master)(config),
- className: '_Installation',
- restWhere: updateWhere
- });
- return restQuery.buildRestWhere().then(() => {
- const write = new _RestWrite.default(config, (0, _Auth.master)(config), '_Installation', restQuery.restWhere, restUpdate);
- write.runOptions.many = true;
- return write.execute();
- });
- };
- }
- const pushStatus = (0, _StatusHandler.pushStatusHandler)(config);
- return Promise.resolve().then(() => {
- return pushStatus.setInitial(body, where);
- }).then(() => {
- onPushStatusSaved(pushStatus.objectId);
- return badgeUpdate();
- }).then(() => {
-
- if (body.audience_id) {
- const audienceId = body.audience_id;
- var updateAudience = {
- lastUsed: {
- __type: 'Date',
- iso: new Date().toISOString()
- },
- timesUsed: {
- __op: 'Increment',
- amount: 1
- }
- };
- const write = new _RestWrite.default(config, (0, _Auth.master)(config), '_Audience', {
- objectId: audienceId
- }, updateAudience);
- write.execute();
- }
-
- return Promise.resolve();
- }).then(() => {
- if (Object.prototype.hasOwnProperty.call(body, 'push_time') && config.hasPushScheduledSupport) {
- return Promise.resolve();
- }
- return config.pushControllerQueue.enqueue(body, where, config, auth, pushStatus);
- }).catch(err => {
- return pushStatus.fail(err).then(() => {
- throw err;
- });
- });
- }
-
- static getExpirationTime(body = {}) {
- var hasExpirationTime = Object.prototype.hasOwnProperty.call(body, 'expiration_time');
- if (!hasExpirationTime) {
- return;
- }
- var expirationTimeParam = body['expiration_time'];
- var expirationTime;
- if (typeof expirationTimeParam === 'number') {
- expirationTime = new Date(expirationTimeParam * 1000);
- } else if (typeof expirationTimeParam === 'string') {
- expirationTime = new Date(expirationTimeParam);
- } else {
- throw new _node.Parse.Error(_node.Parse.Error.PUSH_MISCONFIGURED, body['expiration_time'] + ' is not valid time.');
- }
-
- if (!isFinite(expirationTime)) {
- throw new _node.Parse.Error(_node.Parse.Error.PUSH_MISCONFIGURED, body['expiration_time'] + ' is not valid time.');
- }
- return expirationTime.valueOf();
- }
- static getExpirationInterval(body = {}) {
- const hasExpirationInterval = Object.prototype.hasOwnProperty.call(body, 'expiration_interval');
- if (!hasExpirationInterval) {
- return;
- }
- var expirationIntervalParam = body['expiration_interval'];
- if (typeof expirationIntervalParam !== 'number' || expirationIntervalParam <= 0) {
- throw new _node.Parse.Error(_node.Parse.Error.PUSH_MISCONFIGURED, `expiration_interval must be a number greater than 0`);
- }
- return expirationIntervalParam;
- }
-
- static getPushTime(body = {}) {
- var hasPushTime = Object.prototype.hasOwnProperty.call(body, 'push_time');
- if (!hasPushTime) {
- return;
- }
- var pushTimeParam = body['push_time'];
- var date;
- var isLocalTime = true;
- if (typeof pushTimeParam === 'number') {
- date = new Date(pushTimeParam * 1000);
- } else if (typeof pushTimeParam === 'string') {
- isLocalTime = !PushController.pushTimeHasTimezoneComponent(pushTimeParam);
- date = new Date(pushTimeParam);
- } else {
- throw new _node.Parse.Error(_node.Parse.Error.PUSH_MISCONFIGURED, body['push_time'] + ' is not valid time.');
- }
-
- if (!isFinite(date)) {
- throw new _node.Parse.Error(_node.Parse.Error.PUSH_MISCONFIGURED, body['push_time'] + ' is not valid time.');
- }
- return {
- date,
- isLocalTime
- };
- }
-
- static pushTimeHasTimezoneComponent(pushTimeParam) {
- const offsetPattern = /(.+)([+-])\d\d:\d\d$/;
- return pushTimeParam.indexOf('Z') === pushTimeParam.length - 1 || offsetPattern.test(pushTimeParam)
- ;
- }
-
- static formatPushTime({
- date,
- isLocalTime
- }) {
- if (isLocalTime) {
-
- const isoString = date.toISOString();
- return isoString.substring(0, isoString.indexOf('Z'));
- }
- return date.toISOString();
- }
- }
- exports.PushController = PushController;
- var _default = exports.default = PushController;
|