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'),
|
|
|
|
urlService = require('../../../services/url'),
|
2016-05-19 14:49:22 +03:00
|
|
|
_private = {};
|
|
|
|
|
|
|
|
_private.normalize = function normalize(options) {
|
2018-06-26 01:46:31 +03:00
|
|
|
const {object, apiUrl, client} = options;
|
2016-05-19 14:49:22 +03:00
|
|
|
|
|
|
|
return {
|
2016-06-14 15:15:38 +03:00
|
|
|
time: moment(object.get('published_at')).valueOf(),
|
2018-06-26 01:46:31 +03:00
|
|
|
url: `${urlService.utils.urlJoin(apiUrl, 'schedules', 'posts', object.get('id'))}?client_id=${client.get('slug')}&client_secret=${client.get('secret')}`,
|
2016-05-19 14:49:22 +03:00
|
|
|
extra: {
|
|
|
|
httpMethod: 'PUT',
|
2019-01-21 23:53:11 +03:00
|
|
|
oldTime: object.previous('published_at') ? moment(object.previous('published_at')).valueOf() : null
|
2016-05-19 14:49:22 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
_private.loadClient = function loadClient() {
|
|
|
|
return models.Client.findOne({slug: 'ghost-scheduler'}, {columns: ['slug', 'secret']});
|
|
|
|
};
|
|
|
|
|
|
|
|
_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
|
|
|
};
|
|
|
|
|
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;
|
2017-11-08 02:24:34 +03:00
|
|
|
if (!adapter.rescheduleOnBoot) {
|
|
|
|
return [];
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2018-06-26 01:46:31 +03:00
|
|
|
scheduledPosts.forEach((object) => {
|
|
|
|
adapter.reschedule(_private.normalize({object, apiUrl, client}));
|
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'
|
2018-06-26 01:46:31 +03:00
|
|
|
], (object) => {
|
|
|
|
adapter.schedule(_private.normalize({object, 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'
|
2018-06-26 01:46:31 +03:00
|
|
|
], (object) => {
|
|
|
|
adapter.reschedule(_private.normalize({object, 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'
|
2018-06-26 01:46:31 +03:00
|
|
|
], (object) => {
|
|
|
|
adapter.unschedule(_private.normalize({object, apiUrl, client}));
|
2016-05-19 14:49:22 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|