123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- "use strict";
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.applyDeviceTokenExists = applyDeviceTokenExists;
- exports.bodiesPerLocales = bodiesPerLocales;
- exports.getLocalesFromPush = getLocalesFromPush;
- exports.groupByLocaleIdentifier = groupByLocaleIdentifier;
- exports.isPushIncrementing = isPushIncrementing;
- exports.stripLocalesFromBody = stripLocalesFromBody;
- exports.transformPushBodyForLocale = transformPushBodyForLocale;
- exports.validatePushType = validatePushType;
- var _node = _interopRequireDefault(require("parse/node"));
- var _deepcopy = _interopRequireDefault(require("deepcopy"));
- function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
- function isPushIncrementing(body) {
- if (!body.data || !body.data.badge) {
- return false;
- }
- const badge = body.data.badge;
- if (typeof badge == 'string' && badge.toLowerCase() == 'increment') {
- return true;
- }
- return typeof badge == 'object' && typeof badge.__op == 'string' && badge.__op.toLowerCase() == 'increment' && Number(badge.amount);
- }
- const localizableKeys = ['alert', 'title'];
- function getLocalesFromPush(body) {
- const data = body.data;
- if (!data) {
- return [];
- }
- return [...new Set(Object.keys(data).reduce((memo, key) => {
- localizableKeys.forEach(localizableKey => {
- if (key.indexOf(`${localizableKey}-`) == 0) {
- memo.push(key.slice(localizableKey.length + 1));
- }
- });
- return memo;
- }, []))];
- }
- function transformPushBodyForLocale(body, locale) {
- const data = body.data;
- if (!data) {
- return body;
- }
- body = (0, _deepcopy.default)(body);
- localizableKeys.forEach(key => {
- const localeValue = body.data[`${key}-${locale}`];
- if (localeValue) {
- body.data[key] = localeValue;
- }
- });
- return stripLocalesFromBody(body);
- }
- function stripLocalesFromBody(body) {
- if (!body.data) {
- return body;
- }
- Object.keys(body.data).forEach(key => {
- localizableKeys.forEach(localizableKey => {
- if (key.indexOf(`${localizableKey}-`) == 0) {
- delete body.data[key];
- }
- });
- });
- return body;
- }
- function bodiesPerLocales(body, locales = []) {
-
- const result = locales.reduce((memo, locale) => {
- memo[locale] = transformPushBodyForLocale(body, locale);
- return memo;
- }, {});
-
- result.default = stripLocalesFromBody(body);
- return result;
- }
- function groupByLocaleIdentifier(installations, locales = []) {
- return installations.reduce((map, installation) => {
- let added = false;
- locales.forEach(locale => {
- if (added) {
- return;
- }
- if (installation.localeIdentifier && installation.localeIdentifier.indexOf(locale) === 0) {
- added = true;
- map[locale] = map[locale] || [];
- map[locale].push(installation);
- }
- });
- if (!added) {
- map.default.push(installation);
- }
- return map;
- }, {
- default: []
- });
- }
- function validatePushType(where = {}, validPushTypes = []) {
- var deviceTypeField = where.deviceType || {};
- var deviceTypes = [];
- if (typeof deviceTypeField === 'string') {
- deviceTypes.push(deviceTypeField);
- } else if (Array.isArray(deviceTypeField['$in'])) {
- deviceTypes.concat(deviceTypeField['$in']);
- }
- for (var i = 0; i < deviceTypes.length; i++) {
- var deviceType = deviceTypes[i];
- if (validPushTypes.indexOf(deviceType) < 0) {
- throw new _node.default.Error(_node.default.Error.PUSH_MISCONFIGURED, deviceType + ' is not supported push type.');
- }
- }
- }
- function applyDeviceTokenExists(where) {
- where = (0, _deepcopy.default)(where);
- if (!Object.prototype.hasOwnProperty.call(where, 'deviceToken')) {
- where['deviceToken'] = {
- $exists: true
- };
- }
- return where;
- }
|