forked from parse-community/parse-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpushStatusHandler.js
110 lines (100 loc) · 3.03 KB
/
pushStatusHandler.js
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
import { md5Hash, newObjectId } from './cryptoUtils';
import { logger } from './logger';
const PUSH_STATUS_COLLECTION = '_PushStatus';
export function flatten(array) {
return array.reduce((memo, element) => {
if (Array.isArray(element)) {
memo = memo.concat(flatten(element));
} else {
memo = memo.concat(element);
}
return memo;
}, []);
}
export default function pushStatusHandler(config) {
let initialPromise;
let pushStatus;
let objectId = newObjectId();
let database = config.database.WithoutValidation();
let setInitial = function(body = {}, where, options = {source: 'rest'}) {
let now = new Date();
let data = body.data || {};
let payloadString = JSON.stringify(data);
let object = {
objectId,
createdAt: now,
pushTime: now.toISOString(),
query: JSON.stringify(where),
payload: payloadString,
source: options.source,
title: options.title,
expiry: body.expiration_time,
status: "pending",
numSent: 0,
pushHash: md5Hash(payloadString),
// lockdown!
ACL: {}
}
return database.create(PUSH_STATUS_COLLECTION, object).then(() => {
pushStatus = {
objectId
};
return Promise.resolve(pushStatus);
});
}
let setRunning = function(installations) {
logger.verbose('sending push to %d installations', installations.length);
return database.update(PUSH_STATUS_COLLECTION,
{status:"pending", objectId: objectId},
{status: "running", updatedAt: new Date() });
}
let complete = function(results) {
let update = {
status: 'succeeded',
updatedAt: new Date(),
numSent: 0,
numFailed: 0,
};
if (Array.isArray(results)) {
results = flatten(results);
results.reduce((memo, result) => {
// Cannot handle that
if (!result || !result.device || !result.device.deviceType) {
return memo;
}
let deviceType = result.device.deviceType;
if (result.transmitted)
{
memo.numSent++;
memo.sentPerType = memo.sentPerType || {};
memo.sentPerType[deviceType] = memo.sentPerType[deviceType] || 0;
memo.sentPerType[deviceType]++;
} else {
memo.numFailed++;
memo.failedPerType = memo.failedPerType || {};
memo.failedPerType[deviceType] = memo.failedPerType[deviceType] || 0;
memo.failedPerType[deviceType]++;
}
return memo;
}, update);
}
logger.verbose('sent push! %d success, %d failures', update.numSent, update.numFailed);
return database.update(PUSH_STATUS_COLLECTION, {status:"running", objectId }, update);
}
let fail = function(err) {
let update = {
errorMessage: JSON.stringify(err),
status: 'failed',
updatedAt: new Date()
}
logger.error('error while sending push', err);
return database.update(PUSH_STATUS_COLLECTION, { objectId }, update);
}
return Object.freeze({
objectId,
setInitial,
setRunning,
complete,
fail
})
}