2017-11-07 23:00:03 +03:00
|
|
|
var _ = require('lodash'),
|
|
|
|
url = require('url'),
|
2017-12-12 00:47:46 +03:00
|
|
|
common = require('../lib/common'),
|
2017-12-06 19:37:54 +03:00
|
|
|
safeString = require('../utils').safeString,
|
2017-11-08 10:58:52 +03:00
|
|
|
settingsCache = require('../settings/cache'),
|
2017-11-07 23:00:03 +03:00
|
|
|
|
2017-11-08 10:58:52 +03:00
|
|
|
// Slightly less ugly temporary hack for location of things
|
|
|
|
fetchData = require('./frontend/fetch-data'),
|
|
|
|
handleError = require('./frontend/error'),
|
2017-11-07 23:00:03 +03:00
|
|
|
|
2017-11-10 10:36:39 +03:00
|
|
|
rssService = require('../services/rss'),
|
2017-11-07 23:00:03 +03:00
|
|
|
generate;
|
|
|
|
|
|
|
|
// @TODO: is this the right logic? Where should this live?!
|
|
|
|
function getBaseUrlForRSSReq(originalUrl, pageParam) {
|
|
|
|
return url.parse(originalUrl).pathname.replace(new RegExp('/' + pageParam + '/$'), '/');
|
|
|
|
}
|
|
|
|
|
|
|
|
// @TODO: is this really correct? Should we be using meta data title?
|
|
|
|
function getTitle(relatedData) {
|
|
|
|
relatedData = relatedData || {};
|
2017-12-12 00:47:46 +03:00
|
|
|
var titleStart = _.get(relatedData, 'author[0].name') || _.get(relatedData, 'tag[0].name') || '';
|
2017-11-07 23:00:03 +03:00
|
|
|
|
|
|
|
titleStart += titleStart ? ' - ' : '';
|
2017-12-12 00:47:46 +03:00
|
|
|
return titleStart + settingsCache.get('title');
|
2017-11-07 23:00:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// @TODO: merge this with the rest of the data processing for RSS
|
|
|
|
// @TODO: swap the fetchData call + duplicate code from channels with something DRY
|
|
|
|
function getData(channelOpts) {
|
|
|
|
channelOpts.data = channelOpts.data || {};
|
|
|
|
|
|
|
|
return fetchData(channelOpts).then(function formatResult(result) {
|
2017-11-10 10:36:39 +03:00
|
|
|
var response = _.pick(result, ['posts', 'meta']);
|
2017-11-07 23:00:03 +03:00
|
|
|
|
|
|
|
response.title = getTitle(result.data);
|
|
|
|
response.description = settingsCache.get('description');
|
|
|
|
|
|
|
|
return response;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-11-05 15:45:43 +03:00
|
|
|
// This here is a controller.
|
2017-11-10 10:36:39 +03:00
|
|
|
// The "route" is handled in services/channels/router.js
|
2017-11-05 15:45:43 +03:00
|
|
|
// We can only generate RSS for channels, so that sorta makes sense, but the location is rubbish
|
2017-11-07 23:00:03 +03:00
|
|
|
// @TODO finish refactoring this - it's now a controller
|
|
|
|
generate = function generate(req, res, next) {
|
|
|
|
// Parse the parameters we need from the URL
|
|
|
|
var pageParam = req.params.page !== undefined ? req.params.page : 1,
|
2017-11-10 10:36:39 +03:00
|
|
|
slugParam = req.params.slug ? safeString(req.params.slug) : undefined,
|
|
|
|
// Base URL needs to be the URL for the feed without pagination:
|
|
|
|
baseUrl = getBaseUrlForRSSReq(req.originalUrl, pageParam);
|
2017-11-07 23:00:03 +03:00
|
|
|
|
|
|
|
// @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;
|
|
|
|
|
|
|
|
return getData(res.locals.channel).then(function handleResult(data) {
|
2017-11-10 10:36:39 +03:00
|
|
|
// If page is greater than number of pages we have, go straight to 404
|
|
|
|
if (pageParam > data.meta.pagination.pages) {
|
2017-12-12 00:47:46 +03:00
|
|
|
return next(new common.errors.NotFoundError({message: common.i18n.t('errors.errors.pageNotFound')}));
|
2017-11-07 23:00:03 +03:00
|
|
|
}
|
|
|
|
|
2017-11-10 10:36:39 +03:00
|
|
|
// Render call - to a special RSS renderer
|
|
|
|
return rssService.render(res, baseUrl, data);
|
2017-11-07 23:00:03 +03:00
|
|
|
}).catch(handleError(next));
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = generate;
|