* Start DB runner from tests * Connect GridstoreAdapter only when needed * removes unused package * better test errors reporting * Adds support for __op.Delete * Better test error reporting * Makes sure all tests can run without crashing * Use xdescribe to skip test suite * Removes unused dependencies * Let volatiles classes be created with PG on start * Do not fail if class dont exist * adds index.spec.js to the pg suite * Use a new config each test to prevent side effects * Enable EmailVerificationToken specs with pg * Makes sure failure output is not cut * Reduces number of ignored tests in ParseObject.spec * Inspect reconfiguration errors * Mark GlobalConfig is incompatible with PG - Problem is with nested updates (param.prop = value) * PG: Nested JSON queries and updates - Adds support for nested json and . operator queries - Adds debug support for PG adapter - Adds loglevel support in helper * Enable working specs in ParseUser * Sets default logLevel in tests to undefined * Adds File type support, retores purchaseValidation specs * Adds support for updating jsonb objects - Restores PushController tests * Proper implementation of deleteByQuery and ORs - Adds ParseInstallation spec to the test suite * xit only failing tests * Nit on ParseAPI spec * add sorting operator * properly bound order keys * reverts describe_only_db behavior * Enables passing tests * Adds basic support for relations, upsertOneObject aliased to createObject * progress on queries options * Fix ACL update related problems * Creates relation tables on class creation * Adds Relation tests * remove flaky tests * use promises instead of CB * disable flaky test * nits * Fixes on schema spec - Next thing is to implemenet geopoint and files correctly * fix failues * Basic GeoPoint support * Adds support for $nearSphere/$maxDistance geopoint queries * enable passing tests * drop tables afterEach for PG, clean up relation tables too * Better initialization/dropTables
130 lines
3.6 KiB
JavaScript
130 lines
3.6 KiB
JavaScript
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;
|
||
let lastPromise;
|
||
let setInitial = function(body = {}, where, options = {source: 'rest'}) {
|
||
let now = new Date();
|
||
let data = body.data || {};
|
||
let payloadString = JSON.stringify(data);
|
||
let pushHash;
|
||
if (typeof data.alert === 'string') {
|
||
pushHash = md5Hash(data.alert);
|
||
} else if (typeof data.alert === 'object') {
|
||
pushHash = md5Hash(JSON.stringify(data.alert));
|
||
} else {
|
||
pushHash = 'd41d8cd98f00b204e9800998ecf8427e';
|
||
}
|
||
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,
|
||
// lockdown!
|
||
ACL: {}
|
||
}
|
||
lastPromise = Promise.resolve().then(() => {
|
||
return database.create(PUSH_STATUS_COLLECTION, object).then(() => {
|
||
pushStatus = {
|
||
objectId
|
||
};
|
||
return Promise.resolve(pushStatus);
|
||
});
|
||
});
|
||
return lastPromise;
|
||
}
|
||
|
||
let setRunning = function(installations) {
|
||
logger.verbose('sending push to %d installations', installations.length);
|
||
lastPromise = lastPromise.then(() => {
|
||
return database.update(PUSH_STATUS_COLLECTION,
|
||
{status:"pending", objectId: objectId},
|
||
{status: "running", updatedAt: new Date() });
|
||
});
|
||
return lastPromise;
|
||
}
|
||
|
||
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);
|
||
lastPromise = lastPromise.then(() => {
|
||
return database.update(PUSH_STATUS_COLLECTION, {status:"running", objectId }, update);
|
||
});
|
||
return lastPromise;
|
||
}
|
||
|
||
let fail = function(err) {
|
||
let update = {
|
||
errorMessage: JSON.stringify(err),
|
||
status: 'failed',
|
||
updatedAt: new Date()
|
||
}
|
||
logger.info('warning: error while sending push', err);
|
||
lastPromise = lastPromise.then(() => {
|
||
return database.update(PUSH_STATUS_COLLECTION, { objectId }, update);
|
||
});
|
||
return lastPromise;
|
||
}
|
||
|
||
return Object.freeze({
|
||
objectId,
|
||
setInitial,
|
||
setRunning,
|
||
complete,
|
||
fail
|
||
})
|
||
}
|