2016-10-03 11:33:14 +03:00
|
|
|
var debug = require('debug')('ghost:channels:render'),
|
|
|
|
_ = require('lodash'),
|
2016-02-09 17:14:24 +03:00
|
|
|
errors = require('../../errors'),
|
2016-10-04 18:33:43 +03:00
|
|
|
i18n = require('../../i18n'),
|
2016-02-09 17:14:24 +03:00
|
|
|
filters = require('../../filters'),
|
|
|
|
safeString = require('../../utils/index').safeString,
|
|
|
|
handleError = require('./error'),
|
|
|
|
fetchData = require('./fetch-data'),
|
|
|
|
formatResponse = require('./format-response'),
|
|
|
|
setResponseContext = require('./context'),
|
|
|
|
setRequestIsSecure = require('./secure'),
|
|
|
|
templates = require('./templates');
|
|
|
|
|
|
|
|
function renderChannel(req, res, next) {
|
2016-10-03 11:33:14 +03:00
|
|
|
debug('renderChannel called');
|
2016-02-09 17:14:24 +03:00
|
|
|
// Parse the parameters we need from the URL
|
|
|
|
var channelOpts = req.channelConfig,
|
|
|
|
pageParam = req.params.page !== undefined ? req.params.page : 1,
|
|
|
|
slugParam = req.params.slug ? safeString(req.params.slug) : undefined;
|
|
|
|
|
|
|
|
// Ensure we at least have an empty object for postOptions
|
|
|
|
channelOpts.postOptions = channelOpts.postOptions || {};
|
|
|
|
// Set page on postOptions for the query made later
|
|
|
|
channelOpts.postOptions.page = pageParam;
|
|
|
|
channelOpts.slugParam = slugParam;
|
|
|
|
|
|
|
|
// Call fetchData to get everything we need from the API
|
|
|
|
return fetchData(channelOpts).then(function handleResult(result) {
|
|
|
|
// If page is greater than number of pages we have, go straight to 404
|
|
|
|
if (pageParam > result.meta.pagination.pages) {
|
2016-10-06 15:27:35 +03:00
|
|
|
return next(new errors.NotFoundError({message: i18n.t('errors.errors.pageNotFound')}));
|
2016-02-09 17:14:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// @TODO: figure out if this can be removed, it's supposed to ensure that absolutely URLs get generated
|
|
|
|
// correctly for the various objects, but I believe it doesn't work and a different approach is needed.
|
|
|
|
setRequestIsSecure(req, result.posts);
|
|
|
|
_.each(result.data, function (data) {
|
|
|
|
setRequestIsSecure(req, data);
|
|
|
|
});
|
|
|
|
|
|
|
|
// @TODO: properly design these filters
|
|
|
|
filters.doFilter('prePostsRender', result.posts, res.locals).then(function then(posts) {
|
|
|
|
var view = templates.channel(req.app.get('activeTheme'), channelOpts);
|
|
|
|
|
|
|
|
// Do final data formatting and then render
|
|
|
|
result.posts = posts;
|
|
|
|
result = formatResponse.channel(result);
|
|
|
|
setResponseContext(req, res);
|
2016-10-03 11:33:14 +03:00
|
|
|
debug('Rendering view: ' + view);
|
2016-02-09 17:14:24 +03:00
|
|
|
res.render(view, result);
|
|
|
|
});
|
|
|
|
}).catch(handleError(next));
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = renderChannel;
|