2017-08-29 11:47:01 -04:00
|
|
|
import { ParseMessageQueue } from '../ParseMessageQueue';
|
|
|
|
|
import rest from '../rest';
|
|
|
|
|
import { applyDeviceTokenExists } from './utils';
|
2017-09-18 15:25:03 -04:00
|
|
|
import Parse from 'parse/node';
|
2017-01-13 19:34:04 -05:00
|
|
|
|
|
|
|
|
const PUSH_CHANNEL = 'parse-server-push';
|
|
|
|
|
const DEFAULT_BATCH_SIZE = 100;
|
|
|
|
|
|
|
|
|
|
export class PushQueue {
|
|
|
|
|
parsePublisher: Object;
|
|
|
|
|
channel: String;
|
|
|
|
|
batchSize: Number;
|
|
|
|
|
|
|
|
|
|
// config object of the publisher, right now it only contains the redisURL,
|
|
|
|
|
// but we may extend it later.
|
|
|
|
|
constructor(config: any = {}) {
|
2017-09-18 15:25:03 -04:00
|
|
|
this.channel = config.channel || PushQueue.defaultPushChannel();
|
2017-01-13 19:34:04 -05:00
|
|
|
this.batchSize = config.batchSize || DEFAULT_BATCH_SIZE;
|
|
|
|
|
this.parsePublisher = ParseMessageQueue.createPublisher(config);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static defaultPushChannel() {
|
2017-09-18 15:25:03 -04:00
|
|
|
return `${Parse.applicationId}-${PUSH_CHANNEL}`;
|
2017-01-13 19:34:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
enqueue(body, where, config, auth, pushStatus) {
|
|
|
|
|
const limit = this.batchSize;
|
2017-08-29 11:47:01 -04:00
|
|
|
|
|
|
|
|
where = applyDeviceTokenExists(where);
|
|
|
|
|
|
|
|
|
|
// Order by objectId so no impact on the DB
|
|
|
|
|
const order = 'objectId';
|
2017-01-13 19:34:04 -05:00
|
|
|
return Promise.resolve().then(() => {
|
|
|
|
|
return rest.find(config,
|
2017-06-20 09:15:26 -07:00
|
|
|
auth,
|
|
|
|
|
'_Installation',
|
|
|
|
|
where,
|
|
|
|
|
{limit: 0, count: true});
|
2017-01-13 19:34:04 -05:00
|
|
|
}).then(({results, count}) => {
|
2017-08-29 11:47:01 -04:00
|
|
|
if (!results || count == 0) {
|
2018-01-13 15:12:19 -05:00
|
|
|
return pushStatus.complete();
|
2017-01-13 19:34:04 -05:00
|
|
|
}
|
2017-11-05 10:04:46 -08:00
|
|
|
pushStatus.setRunning(Math.ceil(count / limit));
|
2017-01-13 19:34:04 -05:00
|
|
|
let skip = 0;
|
|
|
|
|
while (skip < count) {
|
|
|
|
|
const query = { where,
|
|
|
|
|
limit,
|
|
|
|
|
skip,
|
|
|
|
|
order };
|
|
|
|
|
|
|
|
|
|
const pushWorkItem = {
|
|
|
|
|
body,
|
|
|
|
|
query,
|
|
|
|
|
pushStatus: { objectId: pushStatus.objectId },
|
|
|
|
|
applicationId: config.applicationId
|
|
|
|
|
}
|
|
|
|
|
this.parsePublisher.publish(this.channel, JSON.stringify(pushWorkItem));
|
|
|
|
|
skip += limit;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|