2017-10-09 14:56:44 +03:00
|
|
|
var api = require('../api'),
|
2017-12-11 21:14:05 +03:00
|
|
|
urlService = require('../services/url'),
|
2017-10-09 14:56:44 +03:00
|
|
|
filters = require('../filters'),
|
|
|
|
handleError = require('./frontend/error'),
|
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.
|
|
|
|
// The "route" is handled in site/routes.js
|
2017-11-06 15:17:24 +03:00
|
|
|
module.exports = function previewController(req, res, next) {
|
2017-10-09 14:56:44 +03:00
|
|
|
var params = {
|
|
|
|
uuid: req.params.uuid,
|
|
|
|
status: 'all',
|
2018-04-15 11:08:06 +03:00
|
|
|
include: 'authors,tags'
|
2017-10-09 14:56:44 +03:00
|
|
|
};
|
|
|
|
|
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
|
|
|
api.posts.read(params).then(function then(result) {
|
2017-11-05 15:45:43 +03:00
|
|
|
// Format data 1
|
2017-10-09 14:56:44 +03:00
|
|
|
var post = result.posts[0];
|
|
|
|
|
|
|
|
if (!post) {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req.params.options && req.params.options.toLowerCase() === 'edit') {
|
|
|
|
// CASE: last param is of url is /edit, redirect to admin
|
2017-12-11 21:14:05 +03:00
|
|
|
return urlService.utils.redirectToAdmin(302, res, '#/editor/' + post.id);
|
2017-10-09 14:56:44 +03:00
|
|
|
} else if (req.params.options) {
|
|
|
|
// CASE: unknown options param detected. Ignore and end in 404.
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (post.status === 'published') {
|
2017-12-11 21:14:05 +03:00
|
|
|
return urlService.utils.redirect301(res, urlService.utils.urlFor('post', {post: post}));
|
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));
|
|
|
|
};
|