2013-05-16 15:21:13 +04:00
|
|
|
// # Ghost Data API
|
|
|
|
// Provides access to the data model
|
|
|
|
|
2014-02-05 12:40:30 +04:00
|
|
|
var _ = require('lodash'),
|
2013-12-06 12:51:35 +04:00
|
|
|
when = require('when'),
|
2013-12-30 11:03:29 +04:00
|
|
|
config = require('../config'),
|
2013-12-06 12:51:35 +04:00
|
|
|
db = require('./db'),
|
|
|
|
settings = require('./settings'),
|
|
|
|
notifications = require('./notifications'),
|
|
|
|
posts = require('./posts'),
|
|
|
|
users = require('./users'),
|
|
|
|
tags = require('./tags'),
|
2014-04-04 05:59:09 +04:00
|
|
|
mail = require('./mail'),
|
2013-06-25 15:43:15 +04:00
|
|
|
requestHandler,
|
2013-12-06 12:51:35 +04:00
|
|
|
init;
|
2013-08-30 15:20:30 +04:00
|
|
|
|
2013-08-06 23:27:56 +04:00
|
|
|
// ## Request Handlers
|
2013-06-25 15:43:15 +04:00
|
|
|
|
2014-01-03 04:37:21 +04:00
|
|
|
function cacheInvalidationHeader(req, result) {
|
2013-09-24 19:21:43 +04:00
|
|
|
var parsedUrl = req._parsedUrl.pathname.replace(/\/$/, '').split('/'),
|
|
|
|
method = req.method,
|
2013-11-03 21:13:19 +04:00
|
|
|
endpoint = parsedUrl[4],
|
|
|
|
id = parsedUrl[5],
|
|
|
|
cacheInvalidate,
|
2014-04-22 05:04:30 +04:00
|
|
|
jsonResult = result.toJSON ? result.toJSON() : result,
|
|
|
|
post,
|
|
|
|
wasPublished,
|
|
|
|
wasDeleted;
|
2013-11-03 21:13:19 +04:00
|
|
|
|
2013-09-24 19:21:43 +04:00
|
|
|
if (method === 'POST' || method === 'PUT' || method === 'DELETE') {
|
2013-12-25 04:05:20 +04:00
|
|
|
if (endpoint === 'settings' || endpoint === 'users' || endpoint === 'db') {
|
2014-03-26 16:45:54 +04:00
|
|
|
cacheInvalidate = '/*';
|
2013-09-24 19:21:43 +04:00
|
|
|
} else if (endpoint === 'posts') {
|
2014-04-22 05:04:30 +04:00
|
|
|
post = jsonResult.posts[0];
|
|
|
|
wasPublished = post.statusChanged && post.status === 'published';
|
|
|
|
wasDeleted = method === 'DELETE';
|
|
|
|
|
|
|
|
// Remove the statusChanged value from the response
|
|
|
|
if (post.statusChanged) {
|
|
|
|
delete post.statusChanged;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't set x-cache-invalidate header for drafts
|
|
|
|
if (wasPublished || wasDeleted) {
|
|
|
|
cacheInvalidate = '/, /page/*, /rss/, /rss/*, /tag/*';
|
|
|
|
if (id && post.slug) {
|
|
|
|
return config.urlForPost(settings, post).then(function (postUrl) {
|
|
|
|
return cacheInvalidate + ', ' + postUrl;
|
|
|
|
});
|
|
|
|
}
|
2013-09-24 19:21:43 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-01-03 04:37:21 +04:00
|
|
|
|
|
|
|
return when(cacheInvalidate);
|
2013-09-24 19:21:43 +04:00
|
|
|
}
|
|
|
|
|
2014-05-02 03:42:23 +04:00
|
|
|
// if api request results in the creation of a new object, construct
|
|
|
|
// a Location: header that points to the new resource.
|
|
|
|
//
|
|
|
|
// arguments: request object, result object from the api call
|
|
|
|
// returns: a promise that will be fulfilled with the location of the
|
|
|
|
// resource
|
|
|
|
function locationHeader(req, result) {
|
|
|
|
var apiRoot = config.urlFor('api'),
|
|
|
|
location,
|
|
|
|
post,
|
|
|
|
notification,
|
|
|
|
parsedUrl = req._parsedUrl.pathname.replace(/\/$/, '').split('/'),
|
|
|
|
endpoint = parsedUrl[4];
|
|
|
|
|
|
|
|
if (req.method === 'POST') {
|
|
|
|
if (result.hasOwnProperty('posts')) {
|
|
|
|
post = result.posts[0];
|
|
|
|
location = apiRoot + '/posts/' + post.id + '/?status=' + post.status;
|
|
|
|
} else if (endpoint === 'notifications') {
|
|
|
|
notification = result;
|
|
|
|
location = apiRoot + '/notifications/' + notification.id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return when(location);
|
|
|
|
}
|
|
|
|
|
2013-08-06 23:27:56 +04:00
|
|
|
// ### requestHandler
|
2013-06-25 15:43:15 +04:00
|
|
|
// decorator for api functions which are called via an HTTP request
|
|
|
|
// takes the API method and wraps it so that it gets data from the request and returns a sensible JSON response
|
|
|
|
requestHandler = function (apiMethod) {
|
|
|
|
return function (req, res) {
|
2014-01-06 18:05:31 +04:00
|
|
|
var options = _.extend(req.body, req.files, req.query, req.params),
|
2013-08-09 05:22:49 +04:00
|
|
|
apiContext = {
|
2014-04-03 17:03:09 +04:00
|
|
|
user: (req.session && req.session.user) ? req.session.user : null
|
2013-12-30 11:03:29 +04:00
|
|
|
};
|
2013-11-28 22:25:45 +04:00
|
|
|
|
2013-12-30 11:03:29 +04:00
|
|
|
return apiMethod.call(apiContext, options).then(function (result) {
|
2014-01-03 04:37:21 +04:00
|
|
|
return cacheInvalidationHeader(req, result).then(function (header) {
|
|
|
|
if (header) {
|
|
|
|
res.set({
|
|
|
|
"X-Cache-Invalidate": header
|
|
|
|
});
|
|
|
|
}
|
2014-05-02 03:42:23 +04:00
|
|
|
})
|
|
|
|
.then(function () {
|
|
|
|
return locationHeader(req, result).then(function (header) {
|
|
|
|
if (header) {
|
|
|
|
res.set({
|
|
|
|
'Location': header
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
res.json(result || {});
|
|
|
|
});
|
2014-01-03 04:37:21 +04:00
|
|
|
});
|
2013-12-30 11:03:29 +04:00
|
|
|
}, function (error) {
|
2014-02-26 21:51:01 +04:00
|
|
|
var errorCode = error.code || 500,
|
2013-12-30 11:03:29 +04:00
|
|
|
errorMsg = {error: _.isString(error) ? error : (_.isObject(error) ? error.message : 'Unknown API Error')};
|
|
|
|
res.json(errorCode, errorMsg);
|
2013-06-08 09:05:40 +04:00
|
|
|
});
|
|
|
|
};
|
2013-06-25 15:43:15 +04:00
|
|
|
};
|
|
|
|
|
2013-12-06 12:51:35 +04:00
|
|
|
init = function () {
|
|
|
|
return settings.updateSettingsCache();
|
|
|
|
};
|
|
|
|
|
2013-08-06 23:27:56 +04:00
|
|
|
// Public API
|
2013-12-06 12:51:35 +04:00
|
|
|
module.exports = {
|
|
|
|
posts: posts,
|
|
|
|
users: users,
|
|
|
|
tags: tags,
|
|
|
|
notifications: notifications,
|
|
|
|
settings: settings,
|
|
|
|
db: db,
|
2014-04-04 05:59:09 +04:00
|
|
|
mail: mail,
|
2013-12-06 12:51:35 +04:00
|
|
|
requestHandler: requestHandler,
|
|
|
|
init: init
|
|
|
|
};
|