2016-05-19 14:49:22 +03:00
|
|
|
var Promise = require('bluebird'),
|
|
|
|
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'),
|
|
|
|
schedules = require('../../../api/schedules'),
|
|
|
|
urlService = require('../../../services/url'),
|
2016-05-19 14:49:22 +03:00
|
|
|
_private = {};
|
|
|
|
|
|
|
|
_private.normalize = function normalize(options) {
|
|
|
|
var object = options.object,
|
|
|
|
apiUrl = options.apiUrl,
|
|
|
|
client = options.client;
|
|
|
|
|
|
|
|
return {
|
2016-06-14 15:15:38 +03:00
|
|
|
time: moment(object.get('published_at')).valueOf(),
|
2017-12-11 21:14:05 +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',
|
2016-06-14 15:15:38 +03:00
|
|
|
oldTime: object.updated('published_at') ? moment(object.updated('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 () {
|
2016-10-14 15:39:10 +03:00
|
|
|
return schedules.getScheduledPosts()
|
|
|
|
.then(function (result) {
|
|
|
|
return result.posts || [];
|
|
|
|
});
|
2016-05-19 14:49:22 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.init = function init(options) {
|
2016-06-14 14:49:40 +03:00
|
|
|
var config = options || {},
|
|
|
|
apiUrl = config.apiUrl,
|
2016-05-19 14:49:22 +03:00
|
|
|
adapter = null,
|
|
|
|
client = null;
|
|
|
|
|
|
|
|
if (!config) {
|
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()
|
|
|
|
.then(function (_client) {
|
|
|
|
client = _client;
|
2016-09-09 13:23:47 +03:00
|
|
|
return localUtils.createAdapter(config);
|
2016-05-19 14:49:22 +03:00
|
|
|
})
|
|
|
|
.then(function (_adapter) {
|
|
|
|
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();
|
|
|
|
})
|
|
|
|
.then(function (scheduledPosts) {
|
|
|
|
if (!scheduledPosts.length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
scheduledPosts.forEach(function (object) {
|
|
|
|
adapter.reschedule(_private.normalize({object: object, apiUrl: apiUrl, client: client}));
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.then(function () {
|
|
|
|
adapter.run();
|
|
|
|
})
|
|
|
|
.then(function () {
|
2017-12-12 00:47:46 +03:00
|
|
|
common.events.onMany([
|
2016-05-19 14:49:22 +03:00
|
|
|
'post.scheduled',
|
|
|
|
'page.scheduled'
|
|
|
|
], function (object) {
|
|
|
|
adapter.schedule(_private.normalize({object: object, apiUrl: apiUrl, client: client}));
|
|
|
|
});
|
|
|
|
|
2017-12-12 00:47:46 +03:00
|
|
|
common.events.onMany([
|
2016-05-19 14:49:22 +03:00
|
|
|
'post.rescheduled',
|
|
|
|
'page.rescheduled'
|
|
|
|
], function (object) {
|
|
|
|
adapter.reschedule(_private.normalize({object: object, apiUrl: apiUrl, client: client}));
|
|
|
|
});
|
|
|
|
|
2017-12-12 00:47:46 +03:00
|
|
|
common.events.onMany([
|
2016-05-19 14:49:22 +03:00
|
|
|
'post.unscheduled',
|
|
|
|
'page.unscheduled'
|
|
|
|
], function (object) {
|
|
|
|
adapter.unschedule(_private.normalize({object: object, apiUrl: apiUrl, client: client}));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|