2017-10-09 14:56:44 +03:00
|
|
|
var utils = require('../utils'),
|
|
|
|
filters = require('../filters'),
|
|
|
|
handleError = require('./frontend/error'),
|
|
|
|
postLookup = require('./frontend/post-lookup'),
|
2017-11-08 13:00:25 +03:00
|
|
|
renderEntry = require('./frontend/render-entry'),
|
2017-10-09 14:56:44 +03:00
|
|
|
setRequestIsSecure = require('./frontend/secure');
|
|
|
|
|
2017-11-05 15:45:43 +03:00
|
|
|
// This here is a controller.
|
2017-11-08 13:00:25 +03:00
|
|
|
// It renders entries = individual posts or pages
|
2017-11-05 15:45:43 +03:00
|
|
|
// The "route" is handled in site/routes.js
|
2017-11-08 13:00:25 +03:00
|
|
|
module.exports = function entryController(req, res, next) {
|
2017-11-10 15:44:29 +03:00
|
|
|
// Note: this is super similar to the config middleware used in channels
|
|
|
|
// @TODO refactor into to something explicit
|
|
|
|
res._route = {
|
|
|
|
type: 'entry'
|
|
|
|
};
|
|
|
|
|
2017-10-09 14:56:44 +03:00
|
|
|
// Query database to find post
|
|
|
|
return postLookup(req.path).then(function then(lookup) {
|
2017-11-05 15:45:43 +03:00
|
|
|
// Format data 1
|
2017-10-09 14:56:44 +03:00
|
|
|
var post = lookup ? lookup.post : false;
|
|
|
|
|
|
|
|
if (!post) {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
// CASE: postlookup can detect options for example /edit, unknown options get ignored and end in 404
|
|
|
|
if (lookup.isUnknownOption) {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
// CASE: last param is of url is /edit, redirect to admin
|
|
|
|
if (lookup.isEditURL) {
|
2017-10-12 15:36:50 +03:00
|
|
|
return utils.url.redirectToAdmin(302, res, '#/editor/' + post.id);
|
2017-10-09 14:56:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// CASE: permalink is not valid anymore, we redirect him permanently to the correct one
|
|
|
|
if (post.url !== req.path) {
|
2017-10-12 15:36:50 +03:00
|
|
|
return utils.url.redirect301(res, post.url);
|
2017-10-09 14:56:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
setRequestIsSecure(req, post);
|
|
|
|
|
|
|
|
filters.doFilter('prePostsRender', post, res.locals)
|
2017-11-08 13:00:25 +03:00
|
|
|
.then(renderEntry(req, res));
|
2017-10-09 14:56:44 +03:00
|
|
|
}).catch(handleError(next));
|
|
|
|
};
|