2018-06-25 17:46:31 -05:00
|
|
|
const Promise = require('bluebird'),
|
2016-05-19 13:49:22 +02:00
|
|
|
moment = require('moment'),
|
2017-12-11 19:14:05 +01:00
|
|
|
localUtils = require('../utils'),
|
2017-12-11 22:47:46 +01:00
|
|
|
common = require('../../../lib/common'),
|
2017-12-11 19:14:05 +01:00
|
|
|
models = require('../../../models'),
|
2019-06-18 15:13:55 +02:00
|
|
|
urlUtils = require('../../../lib/url-utils'),
|
2016-05-19 13:49:22 +02:00
|
|
|
_private = {};
|
|
|
|
|
2019-05-01 22:05:42 +02:00
|
|
|
/**
|
|
|
|
* @description Normalize model data into scheduler notation.
|
|
|
|
* @param {Object} options
|
|
|
|
* @return {Object}
|
|
|
|
*/
|
2016-05-19 13:49:22 +02:00
|
|
|
_private.normalize = function normalize(options) {
|
2019-05-01 22:05:42 +02:00
|
|
|
const {model, apiUrl, client} = options;
|
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-05-01 22:05:42 +02:00
|
|
|
time: moment(model.get('published_at')).valueOf(),
|
|
|
|
// @TODO: We are still using API v0.1
|
2019-06-18 15:13:55 +02:00
|
|
|
url: `${urlUtils.urlJoin(apiUrl, 'schedules', 'posts', model.get('id'))}?client_id=${client.get('slug')}&client_secret=${client.get('secret')}`,
|
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
|
|
|
/**
|
|
|
|
* @description Load the client credentials for v0.1 API.
|
|
|
|
*
|
|
|
|
* @TODO: Remove when we drop v0.1. API v2 uses integrations.
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
2016-05-19 13:49:22 +02:00
|
|
|
_private.loadClient = function loadClient() {
|
|
|
|
return models.Client.findOne({slug: 'ghost-scheduler'}, {columns: ['slug', 'secret']});
|
|
|
|
};
|
|
|
|
|
2019-05-01 22:05:42 +02:00
|
|
|
/**
|
|
|
|
* @description Load all scheduled posts from database.
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
2016-05-19 13:49:22 +02:00
|
|
|
_private.loadScheduledPosts = function () {
|
2019-07-31 22:34:49 +02:00
|
|
|
// TODO: make this version aware?
|
2019-09-11 18:27:57 +02:00
|
|
|
// const api = require('../../../api');
|
|
|
|
// return api.schedules.getScheduled()
|
|
|
|
// .then((result) => {
|
|
|
|
// return result.posts || [];
|
|
|
|
// });
|
|
|
|
return Promise.resolve([]);
|
2016-05-19 13:49:22 +02:00
|
|
|
};
|
|
|
|
|
2019-05-01 22:05:42 +02:00
|
|
|
/**
|
|
|
|
* @description Initialise post scheduling.
|
|
|
|
* @param {Object} options
|
|
|
|
* @return {*}
|
|
|
|
*/
|
2018-06-25 17:46:31 -05:00
|
|
|
exports.init = function init(options = {}) {
|
2019-09-11 18:27:57 +02:00
|
|
|
return Promise.resolve();
|
|
|
|
// TODO: fix once working on scheduler migration to v2
|
2019-09-11 18:37:30 +02:00
|
|
|
/*eslint-disable */
|
2018-06-25 17:46:31 -05:00
|
|
|
const {apiUrl} = options;
|
|
|
|
let adapter = null,
|
2016-05-19 13:49:22 +02:00
|
|
|
client = null;
|
|
|
|
|
2018-06-25 17:46:31 -05:00
|
|
|
if (!Object.keys(options).length) {
|
2017-12-11 22:47:46 +01:00
|
|
|
return Promise.reject(new common.errors.IncorrectUsageError({message: 'post-scheduling: no config was provided'}));
|
2016-05-19 13:49:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!apiUrl) {
|
2017-12-11 22:47:46 +01:00
|
|
|
return Promise.reject(new common.errors.IncorrectUsageError({message: 'post-scheduling: no apiUrl was provided'}));
|
2016-05-19 13:49:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return _private.loadClient()
|
2018-06-25 17:46:31 -05:00
|
|
|
.then((_client) => {
|
2016-05-19 13:49:22 +02:00
|
|
|
client = _client;
|
2018-06-25 17:46:31 -05:00
|
|
|
return localUtils.createAdapter(options);
|
2016-05-19 13:49:22 +02:00
|
|
|
})
|
2018-06-25 17:46:31 -05:00
|
|
|
.then((_adapter) => {
|
2016-05-19 13:49:22 +02:00
|
|
|
adapter = _adapter;
|
2019-05-01 22:05:42 +02:00
|
|
|
|
2017-11-07 23:24:34 +00:00
|
|
|
if (!adapter.rescheduleOnBoot) {
|
|
|
|
return [];
|
|
|
|
}
|
2019-05-01 22:05:42 +02:00
|
|
|
|
2016-05-19 13:49:22 +02:00
|
|
|
return _private.loadScheduledPosts();
|
|
|
|
})
|
2018-06-25 17:46:31 -05:00
|
|
|
.then((scheduledPosts) => {
|
2016-05-19 13:49:22 +02:00
|
|
|
if (!scheduledPosts.length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-05-01 22:05:42 +02:00
|
|
|
scheduledPosts.forEach((model) => {
|
2019-05-06 11:11:43 +02:00
|
|
|
// NOTE: We are using reschedule, because custom scheduling adapter could use a database, which needs to be updated
|
|
|
|
// and not an in-process implementation!
|
|
|
|
adapter.reschedule(_private.normalize({model, apiUrl, client}), {bootstrap: true});
|
2016-05-19 13:49:22 +02:00
|
|
|
});
|
|
|
|
})
|
2018-06-25 17:46:31 -05:00
|
|
|
.then(() => {
|
2016-05-19 13:49:22 +02:00
|
|
|
adapter.run();
|
|
|
|
})
|
2018-06-25 17:46:31 -05:00
|
|
|
.then(() => {
|
2017-12-11 22:47:46 +01:00
|
|
|
common.events.onMany([
|
2016-05-19 13:49:22 +02:00
|
|
|
'post.scheduled',
|
|
|
|
'page.scheduled'
|
2019-05-01 22:05:42 +02:00
|
|
|
], (model) => {
|
|
|
|
adapter.schedule(_private.normalize({model, apiUrl, client}));
|
2016-05-19 13:49:22 +02:00
|
|
|
});
|
|
|
|
|
2017-12-11 22:47:46 +01:00
|
|
|
common.events.onMany([
|
2016-05-19 13:49:22 +02:00
|
|
|
'post.rescheduled',
|
|
|
|
'page.rescheduled'
|
2019-05-01 22:05:42 +02:00
|
|
|
], (model) => {
|
|
|
|
adapter.reschedule(_private.normalize({model, apiUrl, client}));
|
2016-05-19 13:49:22 +02:00
|
|
|
});
|
|
|
|
|
2017-12-11 22:47:46 +01:00
|
|
|
common.events.onMany([
|
2016-05-19 13:49:22 +02:00
|
|
|
'post.unscheduled',
|
|
|
|
'page.unscheduled'
|
2019-05-01 22:05:42 +02:00
|
|
|
], (model) => {
|
|
|
|
adapter.unschedule(_private.normalize({model, apiUrl, client}));
|
2016-05-19 13:49:22 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|