2013-05-11 17:44:25 +01:00
|
|
|
/**
|
|
|
|
* Main controller for Ghost frontend
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*global require, module */
|
|
|
|
|
2015-05-19 20:00:27 -06:00
|
|
|
var _ = require('lodash'),
|
2015-10-10 18:51:38 +01:00
|
|
|
api = require('../../api'),
|
2015-05-19 20:00:27 -06:00
|
|
|
path = require('path'),
|
2015-10-10 18:51:38 +01:00
|
|
|
config = require('../../config'),
|
|
|
|
errors = require('../../errors'),
|
|
|
|
filters = require('../../filters'),
|
2015-05-19 20:00:27 -06:00
|
|
|
Promise = require('bluebird'),
|
2016-02-09 14:14:24 +00:00
|
|
|
templates = require('./templates'),
|
2015-01-22 19:21:47 +00:00
|
|
|
routeMatch = require('path-match')(),
|
2015-10-21 12:51:01 +01:00
|
|
|
handleError = require('./error'),
|
|
|
|
formatResponse = require('./format-response'),
|
2015-10-10 18:51:38 +01:00
|
|
|
setResponseContext = require('./context'),
|
2016-02-09 14:14:24 +00:00
|
|
|
setRequestIsSecure = require('./secure'),
|
2013-05-11 17:44:25 +01:00
|
|
|
|
2014-02-02 00:29:07 -05:00
|
|
|
frontendControllers,
|
2015-05-19 20:00:27 -06:00
|
|
|
staticPostPermalink = routeMatch('/:slug/:edit?');
|
2014-02-02 00:29:07 -05:00
|
|
|
|
2015-04-16 13:40:32 -06: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 21:18:26 +01:00
|
|
|
return function renderPost(post) {
|
2015-11-24 15:12:50 +00:00
|
|
|
var view = templates.single(req.app.get('activeTheme'), post),
|
2015-10-30 19:02:06 +00:00
|
|
|
response = formatResponse.single(post);
|
2015-04-16 13:40:32 -06:00
|
|
|
|
2015-10-30 19:02:06 +00:00
|
|
|
setResponseContext(req, res, response);
|
|
|
|
res.render(view, response);
|
2015-04-16 13:40:32 -06:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-05-05 00:21:29 -05:00
|
|
|
frontendControllers = {
|
2015-05-30 21:18:26 +01:00
|
|
|
preview: function preview(req, res, next) {
|
2015-04-16 13:40:32 -06:00
|
|
|
var params = {
|
|
|
|
uuid: req.params.uuid,
|
|
|
|
status: 'all',
|
2016-02-11 15:03:33 +00:00
|
|
|
include: 'author,tags'
|
2015-04-16 13:40:32 -06:00
|
|
|
};
|
|
|
|
|
2015-05-30 21:18:26 +01:00
|
|
|
api.posts.read(params).then(function then(result) {
|
2015-04-16 13:40:32 -06: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 12:51:01 +01:00
|
|
|
setRequestIsSecure(req, post);
|
2015-04-16 13:40:32 -06:00
|
|
|
|
|
|
|
filters.doFilter('prePostsRender', post, res.locals)
|
|
|
|
.then(renderPost(req, res));
|
2015-05-30 21:18:26 +01:00
|
|
|
}).catch(handleError(next));
|
2015-04-16 13:40:32 -06:00
|
|
|
},
|
2015-05-30 21:18:26 +01:00
|
|
|
single: function single(req, res, next) {
|
2015-05-19 20:00:27 -06:00
|
|
|
var postPath = req.path,
|
2014-02-02 00:29:07 -05:00
|
|
|
params,
|
2015-12-02 18:06:44 +08: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 19:21:47 +00:00
|
|
|
if (match === false) {
|
2015-12-02 18:06:44 +08:00
|
|
|
return next();
|
2014-02-02 00:29:07 -05:00
|
|
|
}
|
|
|
|
|
2015-12-02 18:06:44 +08:00
|
|
|
usingStaticPermalink = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
params = match;
|
2014-02-02 00:29:07 -05:00
|
|
|
|
2015-12-02 18:06:44 +08:00
|
|
|
// Sanitize params we're going to use to lookup the post.
|
|
|
|
postLookup = _.pick(params, 'slug', 'id');
|
2016-02-11 15:03:33 +00:00
|
|
|
// Add author & tag
|
|
|
|
postLookup.include = 'author,tags';
|
2014-02-02 00:29:07 -05:00
|
|
|
|
2015-12-02 18:06:44 +08:00
|
|
|
// Query database to find post
|
|
|
|
return api.posts.read(postLookup).then(function then(result) {
|
2014-04-16 12:09:03 +02:00
|
|
|
var post = result.posts[0],
|
2015-05-19 20:00:27 -06:00
|
|
|
postUrl = (params.edit) ? postPath.replace(params.edit + '/', '') : postPath;
|
2014-02-02 00:29:07 -05:00
|
|
|
|
|
|
|
if (!post) {
|
|
|
|
return next();
|
|
|
|
}
|
2013-12-30 02:03:29 -05:00
|
|
|
|
|
|
|
function render() {
|
2014-01-19 21:08:39 +00: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-06 19:02:20 -07:00
|
|
|
if (params.edit) {
|
|
|
|
params.edit = params.edit.toLowerCase();
|
|
|
|
}
|
2014-04-19 21:48:14 -07:00
|
|
|
if (params.edit === 'edit') {
|
2014-07-17 10:33:21 -04:00
|
|
|
return res.redirect(config.paths.subdir + '/ghost/editor/' + post.id + '/');
|
2014-04-19 21:48:14 -07:00
|
|
|
} else if (params.edit !== undefined) {
|
2014-05-05 15:51:21 +02:00
|
|
|
// reject with type: 'NotFound'
|
2014-08-17 06:17:23 +00:00
|
|
|
return Promise.reject(new errors.NotFoundError());
|
2014-01-02 23:32:31 -05:00
|
|
|
}
|
2014-02-21 20:25:31 -05:00
|
|
|
|
2015-10-21 12:51:01 +01:00
|
|
|
setRequestIsSecure(req, post);
|
2014-02-21 20:25:31 -05:00
|
|
|
|
2015-04-16 13:40:32 -06:00
|
|
|
filters.doFilter('prePostsRender', post, res.locals)
|
|
|
|
.then(renderPost(req, res));
|
2013-09-17 01:54:36 +01:00
|
|
|
}
|
|
|
|
|
2014-02-02 00:29:07 -05: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 11:44:10 +02:00
|
|
|
if (post.page) {
|
2014-02-02 00:29:07 -05:00
|
|
|
return render();
|
|
|
|
}
|
2014-01-01 15:27:39 +00:00
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
2015-05-19 20:00:27 -06: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 00:29:07 -05:00
|
|
|
return next();
|
|
|
|
}
|
2015-05-30 21:18:26 +01:00
|
|
|
}).catch(handleError(next));
|
2013-08-27 17:31:43 -05:00
|
|
|
},
|
2015-05-30 21:18:26 +01:00
|
|
|
private: function private(req, res) {
|
2015-10-30 19:02:06 +00:00
|
|
|
var defaultPage = path.resolve(config.paths.adminViews, 'private.hbs'),
|
2015-11-24 15:12:50 +00:00
|
|
|
paths = templates.getActiveThemePaths(req.app.get('activeTheme')),
|
2015-10-30 19:02:06 +00:00
|
|
|
data = {};
|
2015-05-13 10:26:49 +01:00
|
|
|
|
2015-10-30 19:02:06 +00:00
|
|
|
if (res.error) {
|
|
|
|
data.error = res.error;
|
|
|
|
}
|
|
|
|
|
|
|
|
setResponseContext(req, res);
|
|
|
|
if (paths.hasOwnProperty('private.hbs')) {
|
|
|
|
return res.render('private', data);
|
|
|
|
} else {
|
|
|
|
return res.render(defaultPage, data);
|
|
|
|
}
|
2015-03-26 02:01:39 -05:00
|
|
|
}
|
2013-06-25 12:43:15 +01:00
|
|
|
};
|
2013-05-11 17:44:25 +01:00
|
|
|
|
2013-10-18 17:18:49 +00:00
|
|
|
module.exports = frontendControllers;
|