Ghost/core/server/adapters/scheduling/post-scheduling/index.js
kirrg001 4265afe580 Moved utils/url.js to UrlService
refs #9178

- we have to take care that we don't end up in circular dependencies
  - e.g. API requires UrlService and UrlService needs to require the API (for requesting data)
- update the references
- we would like to get rid of the utils folder, this is/was the most complicated change
2017-12-11 20:05:33 +01:00

98 lines
3.2 KiB
JavaScript

var Promise = require('bluebird'),
moment = require('moment'),
localUtils = require('../utils'),
events = require('../../../events'),
errors = require('../../../errors'),
models = require('../../../models'),
schedules = require('../../../api/schedules'),
urlService = require('../../../services/url'),
_private = {};
_private.normalize = function normalize(options) {
var object = options.object,
apiUrl = options.apiUrl,
client = options.client;
return {
time: moment(object.get('published_at')).valueOf(),
url: urlService.utils.urlJoin(apiUrl, 'schedules', 'posts', object.get('id')) + '?client_id=' + client.get('slug') + '&client_secret=' + client.get('secret'),
extra: {
httpMethod: 'PUT',
oldTime: object.updated('published_at') ? moment(object.updated('published_at')).valueOf() : null
}
};
};
_private.loadClient = function loadClient() {
return models.Client.findOne({slug: 'ghost-scheduler'}, {columns: ['slug', 'secret']});
};
_private.loadScheduledPosts = function () {
return schedules.getScheduledPosts()
.then(function (result) {
return result.posts || [];
});
};
exports.init = function init(options) {
var config = options || {},
apiUrl = config.apiUrl,
adapter = null,
client = null;
if (!config) {
return Promise.reject(new errors.IncorrectUsageError({message: 'post-scheduling: no config was provided'}));
}
if (!apiUrl) {
return Promise.reject(new errors.IncorrectUsageError({message: 'post-scheduling: no apiUrl was provided'}));
}
return _private.loadClient()
.then(function (_client) {
client = _client;
return localUtils.createAdapter(config);
})
.then(function (_adapter) {
adapter = _adapter;
if (!adapter.rescheduleOnBoot) {
return [];
}
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 () {
events.onMany([
'post.scheduled',
'page.scheduled'
], function (object) {
adapter.schedule(_private.normalize({object: object, apiUrl: apiUrl, client: client}));
});
events.onMany([
'post.rescheduled',
'page.rescheduled'
], function (object) {
adapter.reschedule(_private.normalize({object: object, apiUrl: apiUrl, client: client}));
});
events.onMany([
'post.unscheduled',
'page.unscheduled'
], function (object) {
adapter.unschedule(_private.normalize({object: object, apiUrl: apiUrl, client: client}));
});
});
};