Ghost/core/server/controllers/channel.js
Hannah Wolfe e41d0c76fb
RSS service + controller improved for consistency (#9233)
refs #9192, refs #5091 

- Moved all url generation into generate-feed.js, so we can see as much data processing as possible in a single place.
- Refactored the way res.locals were used, to be more like how express uses them prior to rendering
- Removed a bunch of code & tests todo with context for RSS - I can't see any way that'd be used, unless we switched the rendering to use a template.
- moved the RSS rendering to be part of the service, not controller
- updated the tests significantly 

Note: RSS generate-feed has a complete duplication of the code used in the excerpt helper in order to create an item description
2017-11-10 07:36:39 +00:00

48 lines
2.1 KiB
JavaScript

var _ = require('lodash'),
errors = require('../errors'),
i18n = require('../i18n'),
filters = require('../filters'),
safeString = require('../utils/index').safeString,
handleError = require('./frontend/error'),
fetchData = require('./frontend/fetch-data'),
setRequestIsSecure = require('./frontend/secure'),
renderChannel = require('./frontend/render-channel');
// This here is a controller.
// The "route" is handled in services/channels/router.js
// There's both a top-level channelS router, and an individual channel one
module.exports = function channelController(req, res, next) {
// Parse the parameters we need from the URL
var pageParam = req.params.page !== undefined ? req.params.page : 1,
slugParam = req.params.slug ? safeString(req.params.slug) : undefined;
// @TODO: fix this, we shouldn't change the channel object!
// Set page on postOptions for the query made later
res.locals.channel.postOptions.page = pageParam;
res.locals.channel.slugParam = slugParam;
// Call fetchData to get everything we need from the API
return fetchData(res.locals.channel).then(function handleResult(result) {
// If page is greater than number of pages we have, go straight to 404
if (pageParam > result.meta.pagination.pages) {
return next(new errors.NotFoundError({message: i18n.t('errors.errors.pageNotFound')}));
}
// Format data 1
// @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 (posts) {
result.posts = posts;
return result;
})
.then(renderChannel(req, res));
}).catch(handleError(next));
};