2018-06-26 01:46:31 +03:00
|
|
|
const Promise = require('bluebird'),
|
2016-05-19 14:49:22 +03:00
|
|
|
moment = require('moment'),
|
2017-12-11 21:14:05 +03:00
|
|
|
localUtils = require('../utils'),
|
2017-12-12 00:47:46 +03:00
|
|
|
common = require('../../../lib/common'),
|
2017-12-11 21:14:05 +03:00
|
|
|
models = require('../../../models'),
|
2019-06-18 16:13:55 +03:00
|
|
|
urlUtils = require('../../../lib/url-utils'),
|
2016-05-19 14:49:22 +03:00
|
|
|
_private = {};
|
|
|
|
|
2019-05-01 23:05:42 +03:00
|
|
|
/**
|
|
|
|
* @description Normalize model data into scheduler notation.
|
|
|
|
* @param {Object} options
|
|
|
|
* @return {Object}
|
|
|
|
*/
|
2016-05-19 14:49:22 +03:00
|
|
|
_private.normalize = function normalize(options) {
|
2019-05-01 23:05:42 +03:00
|
|
|
const {model, apiUrl, client} = options;
|
2016-05-19 14:49:22 +03:00
|
|
|
|
|
|
|
return {
|
2019-05-01 23:05:42 +03:00
|
|
|
// NOTE: The scheduler expects a unix timestmap.
|
|
|
|
time: moment(model.get('published_at')).valueOf(),
|
|
|
|
// @TODO: We are still using API v0.1
|
2019-06-18 16:13:55 +03:00
|
|
|
url: `${urlUtils.urlJoin(apiUrl, 'schedules', 'posts', model.get('id'))}?client_id=${client.get('slug')}&client_secret=${client.get('secret')}`,
|
2016-05-19 14:49:22 +03:00
|
|
|
extra: {
|
|
|
|
httpMethod: 'PUT',
|
2019-05-01 23:05:42 +03:00
|
|
|
oldTime: model.previous('published_at') ? moment(model.previous('published_at')).valueOf() : null
|
2016-05-19 14:49:22 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2019-05-01 23:05:42 +03: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 14:49:22 +03:00
|
|
|
_private.loadClient = function loadClient() {
|
|
|
|
return models.Client.findOne({slug: 'ghost-scheduler'}, {columns: ['slug', 'secret']});
|
|
|
|
};
|
|
|
|
|
2019-05-01 23:05:42 +03:00
|
|
|
/**
|
|
|
|
* @description Load all scheduled posts from database.
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
2016-05-19 14:49:22 +03:00
|
|
|
_private.loadScheduledPosts = function () {
|
2018-09-20 18:36:47 +03:00
|
|
|
const api = require('../../../api');
|
|
|
|
return api.schedules.getScheduledPosts()
|
2018-06-26 01:46:31 +03:00
|
|
|
.then((result) => {
|
2016-10-14 15:39:10 +03:00
|
|
|
return result.posts || [];
|
|
|
|
});
|
2016-05-19 14:49:22 +03:00
|
|
|
};
|
|
|
|
|
2019-05-01 23:05:42 +03:00
|
|
|
/**
|
|
|
|
* @description Initialise post scheduling.
|
|
|
|
* @param {Object} options
|
|
|
|
* @return {*}
|
|
|
|
*/
|
2018-06-26 01:46:31 +03:00
|
|
|
exports.init = function init(options = {}) {
|
|
|
|
const {apiUrl} = options;
|
|
|
|
let adapter = null,
|
2016-05-19 14:49:22 +03:00
|
|
|
client = null;
|
|
|
|
|
2018-06-26 01:46:31 +03:00
|
|
|
if (!Object.keys(options).length) {
|
2017-12-12 00:47:46 +03:00
|
|
|
return Promise.reject(new common.errors.IncorrectUsageError({message: 'post-scheduling: no config was provided'}));
|
2016-05-19 14:49:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!apiUrl) {
|
2017-12-12 00:47:46 +03:00
|
|
|
return Promise.reject(new common.errors.IncorrectUsageError({message: 'post-scheduling: no apiUrl was provided'}));
|
2016-05-19 14:49:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return _private.loadClient()
|
2018-06-26 01:46:31 +03:00
|
|
|
.then((_client) => {
|
2016-05-19 14:49:22 +03:00
|
|
|
client = _client;
|
2018-06-26 01:46:31 +03:00
|
|
|
return localUtils.createAdapter(options);
|
2016-05-19 14:49:22 +03:00
|
|
|
})
|
2018-06-26 01:46:31 +03:00
|
|
|
.then((_adapter) => {
|
2016-05-19 14:49:22 +03:00
|
|
|
adapter = _adapter;
|
2019-05-01 23:05:42 +03:00
|
|
|
|
2017-11-08 02:24:34 +03:00
|
|
|
if (!adapter.rescheduleOnBoot) {
|
|
|
|
return [];
|
|
|
|
}
|
2019-05-01 23:05:42 +03:00
|
|
|
|
2016-05-19 14:49:22 +03:00
|
|
|
return _private.loadScheduledPosts();
|
|
|
|
})
|
2018-06-26 01:46:31 +03:00
|
|
|
.then((scheduledPosts) => {
|
2016-05-19 14:49:22 +03:00
|
|
|
if (!scheduledPosts.length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-05-01 23:05:42 +03:00
|
|
|
scheduledPosts.forEach((model) => {
|
2019-05-06 12:11:43 +03: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 14:49:22 +03:00
|
|
|
});
|
|
|
|
})
|
2018-06-26 01:46:31 +03:00
|
|
|
.then(() => {
|
2016-05-19 14:49:22 +03:00
|
|
|
adapter.run();
|
|
|
|
})
|
2018-06-26 01:46:31 +03:00
|
|
|
.then(() => {
|
2017-12-12 00:47:46 +03:00
|
|
|
common.events.onMany([
|
2016-05-19 14:49:22 +03:00
|
|
|
'post.scheduled',
|
|
|
|
'page.scheduled'
|
2019-05-01 23:05:42 +03:00
|
|
|
], (model) => {
|
|
|
|
adapter.schedule(_private.normalize({model, apiUrl, client}));
|
2016-05-19 14:49:22 +03:00
|
|
|
});
|
|
|
|
|
2017-12-12 00:47:46 +03:00
|
|
|
common.events.onMany([
|
2016-05-19 14:49:22 +03:00
|
|
|
'post.rescheduled',
|
|
|
|
'page.rescheduled'
|
2019-05-01 23:05:42 +03:00
|
|
|
], (model) => {
|
|
|
|
adapter.reschedule(_private.normalize({model, apiUrl, client}));
|
2016-05-19 14:49:22 +03:00
|
|
|
});
|
|
|
|
|
2017-12-12 00:47:46 +03:00
|
|
|
common.events.onMany([
|
2016-05-19 14:49:22 +03:00
|
|
|
'post.unscheduled',
|
|
|
|
'page.unscheduled'
|
2019-05-01 23:05:42 +03:00
|
|
|
], (model) => {
|
|
|
|
adapter.unschedule(_private.normalize({model, apiUrl, client}));
|
2016-05-19 14:49:22 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|