2021-05-26 09:44:02 +03:00
|
|
|
const Promise = require('bluebird');
|
2021-04-27 16:18:04 +03:00
|
|
|
const events = require('../../../lib/common/events');
|
2021-05-25 21:32:31 +03:00
|
|
|
const localUtils = require('../utils');
|
|
|
|
const PostScheduler = require('./post-scheduler');
|
2021-05-24 14:00:59 +03:00
|
|
|
const getSchedulerIntegration = require('./scheduler-intergation');
|
|
|
|
|
2019-05-01 23:05:42 +03:00
|
|
|
/**
|
2019-09-23 19:12:53 +03:00
|
|
|
* @description Load all scheduled posts/pages from database.
|
2019-05-01 23:05:42 +03:00
|
|
|
* @return {Promise}
|
|
|
|
*/
|
2021-05-25 21:32:31 +03:00
|
|
|
const loadScheduledResources = async function () {
|
2019-09-23 19:12:53 +03:00
|
|
|
const api = require('../../../api');
|
2021-05-25 21:32:31 +03:00
|
|
|
const SCHEDULED_RESOURCES = ['post', 'page'];
|
|
|
|
|
2019-09-23 19:12:53 +03:00
|
|
|
// Fetches all scheduled resources(posts/pages) with default API
|
2021-05-24 16:00:18 +03:00
|
|
|
const results = await Promise.mapSeries(SCHEDULED_RESOURCES, async (resourceType) => {
|
|
|
|
const result = await api.schedules.getScheduled.query({
|
2019-09-23 19:12:53 +03:00
|
|
|
options: {
|
|
|
|
resource: resourceType
|
|
|
|
}
|
|
|
|
});
|
2021-05-24 16:00:18 +03:00
|
|
|
|
|
|
|
return result[resourceType] || [];
|
2019-09-23 19:12:53 +03:00
|
|
|
});
|
2021-05-24 16:00:18 +03:00
|
|
|
|
|
|
|
return SCHEDULED_RESOURCES.reduce(function (obj, entry, index) {
|
|
|
|
return Object.assign(obj, {
|
|
|
|
[entry]: results[index]
|
|
|
|
});
|
|
|
|
}, {});
|
2016-05-19 14:49:22 +03:00
|
|
|
};
|
|
|
|
|
2021-05-25 21:32:31 +03:00
|
|
|
const init = async (options) => {
|
|
|
|
const integration = await getSchedulerIntegration();
|
|
|
|
const adapter = await localUtils.createAdapter();
|
2019-05-01 23:05:42 +03:00
|
|
|
|
2021-05-24 16:00:18 +03:00
|
|
|
let scheduledResources;
|
2019-05-01 23:05:42 +03:00
|
|
|
|
2021-05-24 16:00:18 +03:00
|
|
|
if (!adapter.rescheduleOnBoot) {
|
|
|
|
scheduledResources = [];
|
|
|
|
} else {
|
2021-05-25 21:32:31 +03:00
|
|
|
scheduledResources = await loadScheduledResources();
|
2021-05-24 16:00:18 +03:00
|
|
|
}
|
2016-05-19 14:49:22 +03:00
|
|
|
|
2021-05-25 21:32:31 +03:00
|
|
|
return new PostScheduler({
|
|
|
|
apiUrl: options.apiUrl,
|
|
|
|
integration,
|
|
|
|
adapter,
|
|
|
|
scheduledResources,
|
|
|
|
events
|
2021-05-24 16:00:18 +03:00
|
|
|
});
|
2016-05-19 14:49:22 +03:00
|
|
|
};
|
2021-05-25 21:32:31 +03:00
|
|
|
|
|
|
|
module.exports = init;
|