forked from parse-community/parse-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
121 lines (110 loc) · 3.32 KB
/
utils.js
File metadata and controls
121 lines (110 loc) · 3.32 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import Parse from 'parse/node';
import deepcopy from 'deepcopy';
export 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'];
export 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;
}, []))];
}
export function transformPushBodyForLocale(body, locale) {
const data = body.data;
if (!data) {
return body;
}
body = deepcopy(body);
localizableKeys.forEach((key) => {
const localeValue = body.data[`${key}-${locale}`];
if (localeValue) {
body.data[key] = localeValue;
}
});
return stripLocalesFromBody(body);
}
export 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;
}
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 = []) {
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: []});
}
/**
* 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) {
throw new Parse.Error(Parse.Error.PUSH_MISCONFIGURED,
deviceType + ' is not supported push type.');
}
}
}
export function applyDeviceTokenExists(where) {
where = deepcopy(where);
if (!where.hasOwnProperty('deviceToken')) {
where['deviceToken'] = {'$exists': true};
}
return where;
}