mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-14 18:52:05 +03:00
ae2f34c910
no issue - move the post lookup functionality into its own file - handle the pre and post lookup checks to verify that we found the correct post - checking that the URL matches handles checking that a post looked up with pagePermalink is a page - all cases where there is no match throws a 404 directly, this could just call next() ?
83 lines
2.4 KiB
JavaScript
83 lines
2.4 KiB
JavaScript
/**
|
|
* Main controller for Ghost frontend
|
|
*/
|
|
|
|
/*global require, module */
|
|
|
|
var api = require('../../api'),
|
|
config = require('../../config'),
|
|
filters = require('../../filters'),
|
|
templates = require('./templates'),
|
|
handleError = require('./error'),
|
|
formatResponse = require('./format-response'),
|
|
postLookup = require('./post-lookup'),
|
|
setResponseContext = require('./context'),
|
|
setRequestIsSecure = require('./secure'),
|
|
|
|
frontendControllers;
|
|
|
|
/*
|
|
* 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) {
|
|
return function renderPost(post) {
|
|
var view = templates.single(req.app.get('activeTheme'), post),
|
|
response = formatResponse.single(post);
|
|
|
|
setResponseContext(req, res, response);
|
|
res.render(view, response);
|
|
};
|
|
}
|
|
|
|
frontendControllers = {
|
|
preview: function preview(req, res, next) {
|
|
var params = {
|
|
uuid: req.params.uuid,
|
|
status: 'all',
|
|
include: 'author,tags'
|
|
};
|
|
|
|
api.posts.read(params).then(function then(result) {
|
|
var post = result.posts[0];
|
|
|
|
if (!post) {
|
|
return next();
|
|
}
|
|
|
|
if (post.status === 'published') {
|
|
return res.redirect(301, config.urlFor('post', {post: post}));
|
|
}
|
|
|
|
setRequestIsSecure(req, post);
|
|
|
|
filters.doFilter('prePostsRender', post, res.locals)
|
|
.then(renderPost(req, res));
|
|
}).catch(handleError(next));
|
|
},
|
|
single: function single(req, res, next) {
|
|
// Query database to find post
|
|
return postLookup(req.path).then(function then(lookup) {
|
|
var post = lookup ? lookup.post : false;
|
|
|
|
if (!post) {
|
|
return next();
|
|
}
|
|
|
|
// If we're ready to render the page but the last param is 'edit' then we'll send you to the edit page.
|
|
if (lookup.isEditURL) {
|
|
return res.redirect(config.paths.subdir + '/ghost/editor/' + post.id + '/');
|
|
}
|
|
|
|
setRequestIsSecure(req, post);
|
|
|
|
filters.doFilter('prePostsRender', post, res.locals)
|
|
.then(renderPost(req, res));
|
|
}).catch(handleError(next));
|
|
}
|
|
};
|
|
|
|
module.exports = frontendControllers;
|