2013-05-11 20:44:25 +04:00
|
|
|
/**
|
|
|
|
* Main controller for Ghost frontend
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*global require, module */
|
|
|
|
|
2016-10-03 11:33:14 +03:00
|
|
|
var debug = require('debug')('ghost:channels:single'),
|
|
|
|
api = require('../../api'),
|
2016-09-09 13:23:47 +03:00
|
|
|
utils = require('../../utils'),
|
2015-10-10 20:51:38 +03:00
|
|
|
filters = require('../../filters'),
|
2016-02-09 17:14:24 +03:00
|
|
|
templates = require('./templates'),
|
2015-10-21 14:51:01 +03:00
|
|
|
handleError = require('./error'),
|
|
|
|
formatResponse = require('./format-response'),
|
2016-05-02 13:09:54 +03:00
|
|
|
postLookup = require('./post-lookup'),
|
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
|
|
|
|
2016-05-02 13:09:54 +03:00
|
|
|
frontendControllers;
|
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) {
|
2016-10-03 11:33:14 +03:00
|
|
|
debug('renderPost called');
|
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);
|
2016-10-03 11:33:14 +03:00
|
|
|
debug('Rendering view: ' + view);
|
2015-10-30 22:02:06 +03:00
|
|
|
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') {
|
2016-09-09 13:23:47 +03:00
|
|
|
return res.redirect(301, utils.url.urlFor('post', {post: post}));
|
2015-04-16 22:40:32 +03:00
|
|
|
}
|
|
|
|
|
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-12-02 13:06:44 +03:00
|
|
|
// Query database to find post
|
2016-05-02 13:09:54 +03:00
|
|
|
return postLookup(req.path).then(function then(lookup) {
|
|
|
|
var post = lookup ? lookup.post : false;
|
2014-02-02 09:29:07 +04:00
|
|
|
|
|
|
|
if (!post) {
|
|
|
|
return next();
|
|
|
|
}
|
2013-12-30 11:03:29 +04:00
|
|
|
|
2016-05-18 17:27:54 +03:00
|
|
|
// CASE: last param is of url is /edit, redirect to admin
|
2016-05-02 13:09:54 +03:00
|
|
|
if (lookup.isEditURL) {
|
2016-09-12 14:53:04 +03:00
|
|
|
return res.redirect(utils.url.getSubdir()
|
|
|
|
+ '/ghost/editor/' + post.id + '/');
|
2013-09-17 04:54:36 +04:00
|
|
|
}
|
|
|
|
|
2016-05-18 17:27:54 +03:00
|
|
|
// CASE: permalink is not valid anymore, we redirect him permanently to the correct one
|
|
|
|
if (post.url !== req.path) {
|
|
|
|
return res.redirect(301, post.url);
|
|
|
|
}
|
|
|
|
|
2016-05-02 13:09:54 +03:00
|
|
|
setRequestIsSecure(req, post);
|
2014-01-01 19:27:39 +04:00
|
|
|
|
2016-05-02 13:09:54 +03:00
|
|
|
filters.doFilter('prePostsRender', post, res.locals)
|
|
|
|
.then(renderPost(req, res));
|
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;
|