0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-27 22:49:56 -05:00
ghost/core/server/scheduling/post-scheduling/index.js
Katharina Irrgang d81bc91bd2 Error creation (#7477)
refs #7116, refs #2001

- Changes the way Ghost errors are implemented to benefit from proper inheritance
- Moves all error definitions into a single file
- Changes the error constructor to take an options object, rather than needing the arguments to be passed in the correct order.
- Provides a wrapper so that any errors that haven't already been converted to GhostErrors get converted before they are displayed.

Summary of changes:

* 🐛  set NODE_ENV in config handler
*   add GhostError implementation (core/server/errors.js)
  - register all errors in one file
  - inheritance from GhostError
  - option pattern
* 🔥  remove all error files
*   wrap all errors into GhostError in case of HTTP
* 🎨  adaptions
  - option pattern for errors
  - use GhostError when needed
* 🎨  revert debug deletion and add TODO for error id's
2016-10-06 13:27:35 +01:00

97 lines
3.2 KiB
JavaScript

var Promise = require('bluebird'),
moment = require('moment'),
localUtils = require(__dirname + '/../utils'),
events = require(__dirname + '/../../events'),
errors = require(__dirname + '/../../errors'),
models = require(__dirname + '/../../models'),
schedules = require(__dirname + '/../../api/schedules'),
_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: 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({
from: moment().subtract(7, 'days').startOf('day').toDate(),
to: moment().endOf('day').toDate()
}).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;
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}));
});
});
};