2018-09-01 13:58:06 -04:00
|
|
|
import Parse from 'parse/node';
|
2017-08-29 11:47:01 -04:00
|
|
|
import deepcopy from 'deepcopy';
|
2017-01-13 19:34:04 -05:00
|
|
|
|
|
|
|
|
export function isPushIncrementing(body) {
|
2018-07-12 11:34:08 -07:00
|
|
|
if (!body.data || !body.data.badge) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const badge = body.data.badge;
|
2018-09-01 13:58:06 -04:00
|
|
|
if (typeof badge == 'string' && badge.toLowerCase() == 'increment') {
|
2018-07-12 11:34:08 -07:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-01 13:58:06 -04:00
|
|
|
return (
|
|
|
|
|
typeof badge == 'object' &&
|
|
|
|
|
typeof badge.__op == 'string' &&
|
|
|
|
|
badge.__op.toLowerCase() == 'increment' &&
|
|
|
|
|
Number(badge.amount)
|
|
|
|
|
);
|
2017-01-13 19:34:04 -05:00
|
|
|
}
|
|
|
|
|
|
2017-09-01 15:22:02 -04:00
|
|
|
const localizableKeys = ['alert', 'title'];
|
|
|
|
|
|
|
|
|
|
export function getLocalesFromPush(body) {
|
|
|
|
|
const data = body.data;
|
|
|
|
|
if (!data) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
2018-09-01 13:58:06 -04:00
|
|
|
return [
|
|
|
|
|
...new Set(
|
|
|
|
|
Object.keys(data).reduce((memo, key) => {
|
2020-07-13 17:13:08 -05:00
|
|
|
localizableKeys.forEach(localizableKey => {
|
2018-09-01 13:58:06 -04:00
|
|
|
if (key.indexOf(`${localizableKey}-`) == 0) {
|
|
|
|
|
memo.push(key.slice(localizableKey.length + 1));
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return memo;
|
|
|
|
|
}, [])
|
|
|
|
|
),
|
|
|
|
|
];
|
2017-09-01 15:22:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function transformPushBodyForLocale(body, locale) {
|
|
|
|
|
const data = body.data;
|
|
|
|
|
if (!data) {
|
|
|
|
|
return body;
|
|
|
|
|
}
|
|
|
|
|
body = deepcopy(body);
|
2020-07-13 17:13:08 -05:00
|
|
|
localizableKeys.forEach(key => {
|
2017-09-01 15:22:02 -04:00
|
|
|
const localeValue = body.data[`${key}-${locale}`];
|
|
|
|
|
if (localeValue) {
|
|
|
|
|
body.data[key] = localeValue;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return stripLocalesFromBody(body);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function stripLocalesFromBody(body) {
|
2018-09-01 13:58:06 -04:00
|
|
|
if (!body.data) {
|
|
|
|
|
return body;
|
|
|
|
|
}
|
2020-07-13 17:13:08 -05:00
|
|
|
Object.keys(body.data).forEach(key => {
|
|
|
|
|
localizableKeys.forEach(localizableKey => {
|
2017-09-01 15:22:02 -04:00
|
|
|
if (key.indexOf(`${localizableKey}-`) == 0) {
|
|
|
|
|
delete body.data[key];
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
return body;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function bodiesPerLocales(body, locales = []) {
|
|
|
|
|
// Get all tranformed bodies for each locale
|
|
|
|
|
const result = locales.reduce((memo, locale) => {
|
|
|
|
|
memo[locale] = transformPushBodyForLocale(body, locale);
|
|
|
|
|
return memo;
|
|
|
|
|
}, {});
|
|
|
|
|
// Set the default locale, with the stripped body
|
|
|
|
|
result.default = stripLocalesFromBody(body);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function groupByLocaleIdentifier(installations, locales = []) {
|
2018-09-01 13:58:06 -04:00
|
|
|
return installations.reduce(
|
|
|
|
|
(map, installation) => {
|
|
|
|
|
let added = false;
|
2020-07-13 17:13:08 -05:00
|
|
|
locales.forEach(locale => {
|
2018-09-01 13:58:06 -04:00
|
|
|
if (added) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-10-25 15:06:58 -05:00
|
|
|
if (installation.localeIdentifier && installation.localeIdentifier.indexOf(locale) === 0) {
|
2018-09-01 13:58:06 -04:00
|
|
|
added = true;
|
|
|
|
|
map[locale] = map[locale] || [];
|
|
|
|
|
map[locale].push(installation);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
if (!added) {
|
|
|
|
|
map.default.push(installation);
|
2017-09-01 15:22:02 -04:00
|
|
|
}
|
2018-09-01 13:58:06 -04:00
|
|
|
return map;
|
|
|
|
|
},
|
|
|
|
|
{ default: [] }
|
|
|
|
|
);
|
2017-09-01 15:22:02 -04:00
|
|
|
}
|
|
|
|
|
|
2017-01-13 19:34:04 -05:00
|
|
|
/**
|
|
|
|
|
* Check whether the deviceType parameter in qury condition is valid or not.
|
|
|
|
|
* @param {Object} where A query condition
|
|
|
|
|
* @param {Array} validPushTypes An array of valid push types(string)
|
|
|
|
|
*/
|
|
|
|
|
export 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) {
|
2018-09-01 13:58:06 -04:00
|
|
|
throw new Parse.Error(
|
|
|
|
|
Parse.Error.PUSH_MISCONFIGURED,
|
|
|
|
|
deviceType + ' is not supported push type.'
|
|
|
|
|
);
|
2017-01-13 19:34:04 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-08-29 11:47:01 -04:00
|
|
|
|
|
|
|
|
export function applyDeviceTokenExists(where) {
|
|
|
|
|
where = deepcopy(where);
|
2019-08-14 16:57:00 -05:00
|
|
|
if (!Object.prototype.hasOwnProperty.call(where, 'deviceToken')) {
|
2018-09-01 13:58:06 -04:00
|
|
|
where['deviceToken'] = { $exists: true };
|
2017-08-29 11:47:01 -04:00
|
|
|
}
|
|
|
|
|
return where;
|
|
|
|
|
}
|