2013-05-11 20:44:25 +04:00
|
|
|
/**
|
|
|
|
* Main controller for Ghost frontend
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*global require, module */
|
|
|
|
|
2015-05-20 05:00:27 +03:00
|
|
|
var _ = require('lodash'),
|
2015-10-10 20:51:38 +03:00
|
|
|
api = require('../../api'),
|
|
|
|
config = require('../../config'),
|
|
|
|
errors = require('../../errors'),
|
|
|
|
filters = require('../../filters'),
|
2015-05-20 05:00:27 +03:00
|
|
|
Promise = require('bluebird'),
|
2016-02-09 17:14:24 +03:00
|
|
|
templates = require('./templates'),
|
2015-01-22 22:21:47 +03:00
|
|
|
routeMatch = require('path-match')(),
|
2015-10-21 14:51:01 +03:00
|
|
|
handleError = require('./error'),
|
|
|
|
formatResponse = require('./format-response'),
|
2015-10-10 20:51:38 +03:00
|
|
|
setResponseContext = require('./context'),
|
2016-02-09 17:14:24 +03:00
|
|
|
setRequestIsSecure = require('./secure'),
|
2013-05-11 20:44:25 +04:00
|
|
|
|
2014-02-02 09:29:07 +04:00
|
|
|
frontendControllers,
|
2015-05-20 05:00:27 +03:00
|
|
|
staticPostPermalink = routeMatch('/:slug/:edit?');
|
2014-02-02 09:29:07 +04:00
|
|
|
|
2015-04-16 22:40:32 +03:00
|
|
|
/*
|
|
|
|
* Sets the response context around a post and renders it
|
|
|
|
* with the current theme's post view. Used by post preview
|
|
|
|
* and single post methods.
|
|
|
|
* Returns a function that takes the post to be rendered.
|
|
|
|
*/
|
|
|
|
function renderPost(req, res) {
|
2015-05-30 23:18:26 +03:00
|
|
|
return function renderPost(post) {
|
2015-11-24 18:12:50 +03:00
|
|
|
var view = templates.single(req.app.get('activeTheme'), post),
|
2015-10-30 22:02:06 +03:00
|
|
|
response = formatResponse.single(post);
|
2015-04-16 22:40:32 +03:00
|
|
|
|
2015-10-30 22:02:06 +03:00
|
|
|
setResponseContext(req, res, response);
|
|
|
|
res.render(view, response);
|
2015-04-16 22:40:32 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-05-05 08:21:29 +03:00
|
|
|
frontendControllers = {
|
2015-05-30 23:18:26 +03:00
|
|
|
preview: function preview(req, res, next) {
|
2015-04-16 22:40:32 +03:00
|
|
|
var params = {
|
|
|
|
uuid: req.params.uuid,
|
|
|
|
status: 'all',
|
2016-02-11 18:03:33 +03:00
|
|
|
include: 'author,tags'
|
2015-04-16 22:40:32 +03:00
|
|
|
};
|
|
|
|
|
2015-05-30 23:18:26 +03:00
|
|
|
api.posts.read(params).then(function then(result) {
|
2015-04-16 22:40:32 +03:00
|
|
|
var post = result.posts[0];
|
|
|
|
|
|
|
|
if (!post) {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (post.status === 'published') {
|
|
|
|
return res.redirect(301, config.urlFor('post', {post: post}));
|
|
|
|
}
|
|
|
|
|
2015-10-21 14:51:01 +03:00
|
|
|
setRequestIsSecure(req, post);
|
2015-04-16 22:40:32 +03:00
|
|
|
|
|
|
|
filters.doFilter('prePostsRender', post, res.locals)
|
|
|
|
.then(renderPost(req, res));
|
2015-05-30 23:18:26 +03:00
|
|
|
}).catch(handleError(next));
|
2015-04-16 22:40:32 +03:00
|
|
|
},
|
2015-05-30 23:18:26 +03:00
|
|
|
single: function single(req, res, next) {
|
2015-05-20 05:00:27 +03:00
|
|
|
var postPath = req.path,
|
2014-02-02 09:29:07 +04:00
|
|
|
params,
|
2015-12-02 13:06:44 +03:00
|
|
|
usingStaticPermalink = false,
|
|
|
|
permalink = config.theme.permalinks,
|
|
|
|
editFormat = permalink.substr(permalink.length - 1) === '/' ? ':edit?' : '/:edit?',
|
|
|
|
postLookup,
|
|
|
|
match;
|
|
|
|
|
|
|
|
// Convert saved permalink into a path-match function
|
|
|
|
permalink = routeMatch(permalink + editFormat);
|
|
|
|
match = permalink(postPath);
|
|
|
|
|
|
|
|
// Check if the path matches the permalink structure.
|
|
|
|
//
|
|
|
|
// If there are no matches found we then
|
|
|
|
// need to verify it's not a static post,
|
|
|
|
// and test against that permalink structure.
|
|
|
|
if (match === false) {
|
|
|
|
match = staticPostPermalink(postPath);
|
|
|
|
// If there are still no matches then call next.
|
2015-01-22 22:21:47 +03:00
|
|
|
if (match === false) {
|
2015-12-02 13:06:44 +03:00
|
|
|
return next();
|
2014-02-02 09:29:07 +04:00
|
|
|
}
|
|
|
|
|
2015-12-02 13:06:44 +03:00
|
|
|
usingStaticPermalink = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
params = match;
|
2014-02-02 09:29:07 +04:00
|
|
|
|
2015-12-02 13:06:44 +03:00
|
|
|
// Sanitize params we're going to use to lookup the post.
|
|
|
|
postLookup = _.pick(params, 'slug', 'id');
|
2016-02-11 18:03:33 +03:00
|
|
|
// Add author & tag
|
|
|
|
postLookup.include = 'author,tags';
|
2014-02-02 09:29:07 +04:00
|
|
|
|
2015-12-02 13:06:44 +03:00
|
|
|
// Query database to find post
|
|
|
|
return api.posts.read(postLookup).then(function then(result) {
|
2014-04-16 14:09:03 +04:00
|
|
|
var post = result.posts[0],
|
2015-05-20 05:00:27 +03:00
|
|
|
postUrl = (params.edit) ? postPath.replace(params.edit + '/', '') : postPath;
|
2014-02-02 09:29:07 +04:00
|
|
|
|
|
|
|
if (!post) {
|
|
|
|
return next();
|
|
|
|
}
|
2013-12-30 11:03:29 +04:00
|
|
|
|
|
|
|
function render() {
|
2014-01-20 01:08:39 +04:00
|
|
|
// If we're ready to render the page but the last param is 'edit' then we'll send you to the edit page.
|
2014-08-07 06:02:20 +04:00
|
|
|
if (params.edit) {
|
|
|
|
params.edit = params.edit.toLowerCase();
|
|
|
|
}
|
2014-04-20 08:48:14 +04:00
|
|
|
if (params.edit === 'edit') {
|
2014-07-17 18:33:21 +04:00
|
|
|
return res.redirect(config.paths.subdir + '/ghost/editor/' + post.id + '/');
|
2014-04-20 08:48:14 +04:00
|
|
|
} else if (params.edit !== undefined) {
|
2014-05-05 17:51:21 +04:00
|
|
|
// reject with type: 'NotFound'
|
2014-08-17 10:17:23 +04:00
|
|
|
return Promise.reject(new errors.NotFoundError());
|
2014-01-03 08:32:31 +04:00
|
|
|
}
|
2014-02-22 05:25:31 +04:00
|
|
|
|
2015-10-21 14:51:01 +03:00
|
|
|
setRequestIsSecure(req, post);
|
2014-02-22 05:25:31 +04:00
|
|
|
|
2015-04-16 22:40:32 +03:00
|
|
|
filters.doFilter('prePostsRender', post, res.locals)
|
|
|
|
.then(renderPost(req, res));
|
2013-09-17 04:54:36 +04:00
|
|
|
}
|
|
|
|
|
2014-02-02 09:29:07 +04:00
|
|
|
// If we've checked the path with the static permalink structure
|
|
|
|
// then the post must be a static post.
|
|
|
|
// If it is not then we must return.
|
|
|
|
if (usingStaticPermalink) {
|
2014-06-12 13:44:10 +04:00
|
|
|
if (post.page) {
|
2014-02-02 09:29:07 +04:00
|
|
|
return render();
|
|
|
|
}
|
2014-01-01 19:27:39 +04:00
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
2015-05-20 05:00:27 +03:00
|
|
|
// Check if the url provided with the post object matches req.path
|
|
|
|
// If it does, render the post
|
|
|
|
// If not, return 404
|
|
|
|
if (post.url && post.url === postUrl) {
|
|
|
|
return render();
|
|
|
|
} else {
|
2014-02-02 09:29:07 +04:00
|
|
|
return next();
|
|
|
|
}
|
2015-05-30 23:18:26 +03:00
|
|
|
}).catch(handleError(next));
|
2015-03-26 10:01:39 +03:00
|
|
|
}
|
2013-06-25 15:43:15 +04:00
|
|
|
};
|
2013-05-11 20:44:25 +04:00
|
|
|
|
2013-10-18 21:18:49 +04:00
|
|
|
module.exports = frontendControllers;
|