2019-09-23 21:42:53 +05:30
|
|
|
const Promise = require('bluebird');
|
|
|
|
const moment = require('moment');
|
|
|
|
const localUtils = require('../utils');
|
2021-04-27 14:18:04 +01:00
|
|
|
const events = require('../../../lib/common/events');
|
2020-05-22 13:22:20 -05:00
|
|
|
const errors = require('@tryghost/errors');
|
2020-05-28 05:57:02 -05:00
|
|
|
const urlUtils = require('../../../../shared/url-utils');
|
2021-05-24 14:55:38 +04:00
|
|
|
const getSignedAdminToken = require('./scheduling-auth-token');
|
2021-05-24 15:00:59 +04:00
|
|
|
const getSchedulerIntegration = require('./scheduler-intergation');
|
|
|
|
|
2019-09-23 21:42:53 +05:30
|
|
|
const _private = {};
|
|
|
|
const SCHEDULED_RESOURCES = ['post', 'page'];
|
|
|
|
|
2019-05-01 22:05:42 +02:00
|
|
|
/**
|
|
|
|
* @description Normalize model data into scheduler notation.
|
|
|
|
* @param {Object} options
|
|
|
|
* @return {Object}
|
|
|
|
*/
|
2019-11-19 19:59:55 +05:30
|
|
|
_private.normalize = function normalize({model, apiUrl, resourceType, integration}, event = '') {
|
2019-09-23 21:42:53 +05:30
|
|
|
const resource = `${resourceType}s`;
|
2021-05-24 17:33:14 +04:00
|
|
|
const publishedAt = (event === 'unscheduled') ? model.previous('published_at') : model.get('published_at');
|
|
|
|
const signedAdminToken = getSignedAdminToken({
|
|
|
|
publishedAt,
|
|
|
|
apiUrl,
|
|
|
|
key: {
|
|
|
|
id: integration.api_keys[0].id,
|
|
|
|
secret: integration.api_keys[0].secret
|
|
|
|
}
|
|
|
|
});
|
2019-09-23 21:42:53 +05:30
|
|
|
let url = `${urlUtils.urlJoin(apiUrl, 'schedules', resource, model.get('id'))}/?token=${signedAdminToken}`;
|
2021-05-24 17:33:14 +04:00
|
|
|
|
2016-05-19 13:49:22 +02:00
|
|
|
return {
|
2019-07-31 22:34:49 +02:00
|
|
|
// NOTE: The scheduler expects a unix timestamp.
|
2019-11-19 19:59:55 +05:30
|
|
|
time: moment(publishedAt).valueOf(),
|
2019-09-23 21:42:53 +05:30
|
|
|
url: url,
|
2016-05-19 13:49:22 +02:00
|
|
|
extra: {
|
|
|
|
httpMethod: 'PUT',
|
2019-05-01 22:05:42 +02:00
|
|
|
oldTime: model.previous('published_at') ? moment(model.previous('published_at')).valueOf() : null
|
2016-05-19 13:49:22 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2019-05-01 22:05:42 +02:00
|
|
|
/**
|
2019-09-23 21:42:53 +05:30
|
|
|
* @description Load all scheduled posts/pages from database.
|
2019-05-01 22:05:42 +02:00
|
|
|
* @return {Promise}
|
|
|
|
*/
|
2021-05-24 17:00:18 +04:00
|
|
|
_private.loadScheduledResources = async function () {
|
2019-09-23 21:42:53 +05:30
|
|
|
const api = require('../../../api');
|
|
|
|
// Fetches all scheduled resources(posts/pages) with default API
|
2021-05-24 17:00:18 +04:00
|
|
|
const results = await Promise.mapSeries(SCHEDULED_RESOURCES, async (resourceType) => {
|
|
|
|
const result = await api.schedules.getScheduled.query({
|
2019-09-23 21:42:53 +05:30
|
|
|
options: {
|
|
|
|
resource: resourceType
|
|
|
|
}
|
|
|
|
});
|
2021-05-24 17:00:18 +04:00
|
|
|
|
|
|
|
return result[resourceType] || [];
|
2019-09-23 21:42:53 +05:30
|
|
|
});
|
2021-05-24 17:00:18 +04:00
|
|
|
|
|
|
|
return SCHEDULED_RESOURCES.reduce(function (obj, entry, index) {
|
|
|
|
return Object.assign(obj, {
|
|
|
|
[entry]: results[index]
|
|
|
|
});
|
|
|
|
}, {});
|
2016-05-19 13:49:22 +02:00
|
|
|
};
|
|
|
|
|
2019-05-01 22:05:42 +02:00
|
|
|
/**
|
|
|
|
* @description Initialise post scheduling.
|
|
|
|
* @param {Object} options
|
2021-05-24 17:33:14 +04:00
|
|
|
* @param {string} options.apiUrl -
|
2019-05-01 22:05:42 +02:00
|
|
|
* @return {*}
|
|
|
|
*/
|
2021-05-24 17:00:18 +04:00
|
|
|
exports.init = async function init(options = {}) {
|
2018-06-25 17:46:31 -05:00
|
|
|
const {apiUrl} = options;
|
2019-09-23 21:42:53 +05:30
|
|
|
let adapter = null;
|
|
|
|
let integration = null;
|
2016-05-19 13:49:22 +02:00
|
|
|
|
2018-06-25 17:46:31 -05:00
|
|
|
if (!Object.keys(options).length) {
|
2020-05-22 13:22:20 -05:00
|
|
|
return Promise.reject(new errors.IncorrectUsageError({message: 'post-scheduling: no config was provided'}));
|
2016-05-19 13:49:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!apiUrl) {
|
2020-05-22 13:22:20 -05:00
|
|
|
return Promise.reject(new errors.IncorrectUsageError({message: 'post-scheduling: no apiUrl was provided'}));
|
2016-05-19 13:49:22 +02:00
|
|
|
}
|
|
|
|
|
2021-05-24 17:00:18 +04:00
|
|
|
integration = await getSchedulerIntegration();
|
|
|
|
adapter = await localUtils.createAdapter();
|
2019-05-01 22:05:42 +02:00
|
|
|
|
2021-05-24 17:00:18 +04:00
|
|
|
let scheduledResources;
|
2019-05-01 22:05:42 +02:00
|
|
|
|
2021-05-24 17:00:18 +04:00
|
|
|
if (!adapter.rescheduleOnBoot) {
|
|
|
|
scheduledResources = [];
|
|
|
|
} else {
|
|
|
|
scheduledResources = await _private.loadScheduledResources();
|
|
|
|
}
|
2016-05-19 13:49:22 +02:00
|
|
|
|
2021-05-24 17:00:18 +04:00
|
|
|
if (Object.keys(scheduledResources).length) {
|
|
|
|
// Reschedules all scheduled resources on boot
|
|
|
|
// NOTE: We are using reschedule, because custom scheduling adapter could use a database, which needs to be updated
|
|
|
|
// and not an in-process implementation!
|
|
|
|
Object.keys(scheduledResources).forEach((resourceType) => {
|
|
|
|
scheduledResources[resourceType].forEach((model) => {
|
|
|
|
adapter.unschedule(_private.normalize({model, apiUrl, integration, resourceType}, 'unscheduled'), {bootstrap: true});
|
|
|
|
adapter.schedule(_private.normalize({model, apiUrl, integration, resourceType}));
|
2016-05-19 13:49:22 +02:00
|
|
|
});
|
|
|
|
});
|
2021-05-24 17:00:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
adapter.run();
|
|
|
|
|
|
|
|
SCHEDULED_RESOURCES.forEach((resource) => {
|
|
|
|
events.on(`${resource}.scheduled`, (model) => {
|
|
|
|
adapter.schedule(_private.normalize({model, apiUrl, integration, resourceType: resource}));
|
|
|
|
});
|
|
|
|
|
|
|
|
/** We want to do reschedule as (unschedule + schedule) due to how token(+url) is generated
|
|
|
|
* We want to first remove existing schedule by generating a matching token(+url)
|
|
|
|
* followed by generating a new token(+url) for the new schedule
|
|
|
|
*/
|
|
|
|
events.on(`${resource}.rescheduled`, (model) => {
|
|
|
|
adapter.unschedule(_private.normalize({model, apiUrl, integration, resourceType: resource}, 'unscheduled'));
|
|
|
|
adapter.schedule(_private.normalize({model, apiUrl, integration, resourceType: resource}));
|
|
|
|
});
|
|
|
|
|
|
|
|
events.on(`${resource}.unscheduled`, (model) => {
|
|
|
|
adapter.unschedule(_private.normalize({model, apiUrl, integration, resourceType: resource}, 'unscheduled'));
|
|
|
|
});
|
|
|
|
});
|
2016-05-19 13:49:22 +02:00
|
|
|
};
|