Skip to content

Commit

Permalink
Switch RSS to use new filter param
Browse files Browse the repository at this point in the history
refs #5943, #5091

- split out channel config
- use config.theme instead of api calls to grab title & desc
- wrap rss call in a function which sets channel config for RSS feeds
- change rss `getData` function to use the new multiple-query-handling fetchData functionality
- make sure channelConfig is set in all tests
  • Loading branch information
ErisDS committed Oct 27, 2015
1 parent 91aaf81 commit ff7517b
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 128 deletions.
42 changes: 42 additions & 0 deletions core/server/controllers/frontend/channel-config.js
@@ -0,0 +1,42 @@
var config = require('../../config'),
defaults;

defaults = {
index: {
name: 'home',
route: '/',
firstPageTemplate: 'home'
},
tag: {
name: 'tag',
route: '/' + config.routeKeywords.tag + '/:slug/',
postOptions: {
filter: 'tags:%s'
},
data: {
tag: {
type: 'read',
resource: 'tags',
options: {slug: '%s'}
}
},
slugTemplate: true
},
author: {
name: 'author',
route: '/' + config.routeKeywords.author + '/:slug/',
postOptions: {
filter: 'author:%s'
},
data: {
author: {
type: 'read',
resource: 'users',
options: {slug: '%s'}
}
},
slugTemplate: true
}
};

module.exports = defaults;
12 changes: 11 additions & 1 deletion core/server/controllers/frontend/fetch-data.js
Expand Up @@ -86,7 +86,17 @@ function processQuery(query, slugParam) {
* @returns {Promise} response
*/
function fetchData(channelOptions, slugParam) {
return fetchPostsPerPage(channelOptions.postOptions).then(function fetchData(pageOptions) {
// Temporary workaround to make RSS work, moving towards dynamic channels will provide opportunities to
// improve this, I hope :)
function handlePostsPerPage(channelOptions) {
if (channelOptions.isRSS) {
return Promise.resolve({options: channelOptions.postOptions});
} else {
return fetchPostsPerPage(channelOptions.postOptions);
}
}

return handlePostsPerPage(channelOptions).then(function fetchData(pageOptions) {
var postQuery,
props = {};

Expand Down
64 changes: 24 additions & 40 deletions core/server/controllers/frontend/index.js
Expand Up @@ -18,6 +18,7 @@ var _ = require('lodash'),
handleError = require('./error'),
fetchData = require('./fetch-data'),
formatResponse = require('./format-response'),
channelConfig = require('./channel-config'),
setResponseContext = require('./context'),
setRequestIsSecure = require('./secure'),
getActiveThemePaths = require('./theme-paths'),
Expand All @@ -44,14 +45,13 @@ function renderPost(req, res) {
}

function renderChannel(channelOpts) {
// Ensure we at least have an empty object for postOptions
channelOpts.postOptions = channelOpts.postOptions || {};

return function renderChannel(req, res, next) {
// Parse the parameters we need from the URL
var pageParam = req.params.page !== undefined ? parseInt(req.params.page, 10) : 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;

Expand Down Expand Up @@ -114,41 +114,27 @@ function renderChannel(channelOpts) {
}

frontendControllers = {
homepage: renderChannel({
name: 'home',
route: '/',
firstPageTemplate: 'home'
}),
tag: renderChannel({
name: 'tag',
route: '/' + config.routeKeywords.tag + '/:slug/',
postOptions: {
filter: 'tags:%s'
},
data: {
tag: {
type: 'read',
resource: 'tags',
options: {slug: '%s'}
}
},
slugTemplate: true
}),
author: renderChannel({
name: 'author',
route: '/' + config.routeKeywords.author + '/:slug/',
postOptions: {
filter: 'author:%s'
},
data: {
author: {
type: 'read',
resource: 'users',
options: {slug: '%s'}
}
},
slugTemplate: true
}),
index: renderChannel(_.cloneDeep(channelConfig.index)),
tag: renderChannel(_.cloneDeep(channelConfig.tag)),
author: renderChannel(_.cloneDeep(channelConfig.author)),
rss: function (req, res, next) {
// Temporary hack, channels will allow us to resolve this better eventually
var tagPattern = new RegExp('^\\/' + config.routeKeywords.tag + '\\/.+'),
authorPattern = new RegExp('^\\/' + config.routeKeywords.author + '\\/.+');

if (tagPattern.test(res.locals.relativeUrl)) {
req.channelConfig = _.cloneDeep(channelConfig.tag);
} else if (authorPattern.test(res.locals.relativeUrl)) {
req.channelConfig = _.cloneDeep(channelConfig.author);
} else {
req.channelConfig = _.cloneDeep(channelConfig.index);
}

req.channelConfig.isRSS = true;

return rss(req, res, next);
},

preview: function preview(req, res, next) {
var params = {
uuid: req.params.uuid,
Expand All @@ -173,7 +159,6 @@ frontendControllers = {
.then(renderPost(req, res));
}).catch(handleError(next));
},

single: function single(req, res, next) {
var postPath = req.path,
params,
Expand Down Expand Up @@ -262,7 +247,6 @@ frontendControllers = {
}
}).catch(handleError(next));
},
rss: rss,
private: function private(req, res) {
var defaultPage = path.resolve(config.paths.adminViews, 'private.hbs');
return getActiveThemePaths().then(function then(paths) {
Expand Down
62 changes: 30 additions & 32 deletions core/server/data/xml/rss/index.js
@@ -1,14 +1,15 @@
var _ = require('lodash'),
Promise = require('bluebird'),
cheerio = require('cheerio'),
crypto = require('crypto'),
downsize = require('downsize'),
RSS = require('rss'),
url = require('url'),
config = require('../../../config'),
api = require('../../../api'),
filters = require('../../../filters'),

// Really ugly temporary hack for location of things
fetchData = require('../../../controllers/frontend/fetch-data'),

generate,
generateFeed,
getFeedXml,
Expand All @@ -28,37 +29,30 @@ function handleError(next) {
};
}

function getOptions(req, pageParam, slugParam) {
var options = {};

if (pageParam) { options.page = pageParam; }
if (isTag(req)) { options.tag = slugParam; }
if (isAuthor(req)) { options.author = slugParam; }

options.include = 'author,tags,fields';

return options;
}

function getData(options) {
var ops = {
title: api.settings.read('title'),
description: api.settings.read('description'),
permalinks: api.settings.read('permalinks'),
results: api.posts.browse(options)
function getData(channelOpts, slugParam) {
channelOpts.data = channelOpts.data || {};
channelOpts.data.permalinks = {
type: 'read',
resource: 'settings',
options: 'permalinks'
};

return Promise.props(ops).then(function (result) {
var titleStart = '';
if (options.tag) { titleStart = result.results.meta.filters.tags[0].name + ' - ' || ''; }
if (options.author) { titleStart = result.results.meta.filters.author.name + ' - ' || ''; }
return fetchData(channelOpts, slugParam).then(function (result) {
var response = {},
titleStart = '';

if (result.data.tag) { titleStart = result.data.tag[0].name + ' - ' || ''; }
if (result.data.author) { titleStart = result.data.author[0].name + ' - ' || ''; }

return {
title: titleStart + result.title.settings[0].value,
description: result.description.settings[0].value,
permalinks: result.permalinks.settings[0],
results: result.results
response.title = titleStart + config.theme.title;
response.description = config.theme.description;
response.permalinks = result.data.permalinks[0];
response.results = {
posts: result.posts,
meta: result.meta
};

return response;
});
}

Expand Down Expand Up @@ -198,15 +192,19 @@ generate = function generate(req, res, next) {
// Initialize RSS
var pageParam = req.params.page !== undefined ? parseInt(req.params.page, 10) : 1,
slugParam = req.params.slug,
baseUrl = getBaseUrl(req, slugParam),
options = getOptions(req, pageParam, slugParam);
baseUrl = getBaseUrl(req, slugParam);

// Ensure we at least have an empty object for postOptions
req.channelConfig.postOptions = req.channelConfig.postOptions || {};
// Set page on postOptions for the query made later
req.channelConfig.postOptions.page = pageParam;

// No negative pages, or page 1
if (isNaN(pageParam) || pageParam < 1 || (req.params.page !== undefined && pageParam === 1)) {
return res.redirect(baseUrl);
}

return getData(options).then(function then(data) {
return getData(req.channelConfig, slugParam).then(function then(data) {
var maxPage = data.results.meta.pagination.pages;

// If page is greater than number of pages we have, redirect to last page
Expand Down
4 changes: 2 additions & 2 deletions core/server/routes/frontend.js
Expand Up @@ -55,8 +55,8 @@ frontendRoutes = function frontendRoutes(middleware) {
});

// Index
indexRouter.route('/').get(frontend.homepage);
indexRouter.route('/' + routeKeywords.page + '/:page/').get(frontend.homepage);
indexRouter.route('/').get(frontend.index);
indexRouter.route('/' + routeKeywords.page + '/:page/').get(frontend.index);
indexRouter.use(rssRouter);

// Tags
Expand Down

0 comments on commit ff7517b

Please sign in to comment.