2018-06-24 01:32:08 +03:00
|
|
|
const _ = require('lodash'),
|
|
|
|
debug = require('ghost-ignition').debug('services:routing:controllers:channel'),
|
|
|
|
common = require('../../../lib/common'),
|
|
|
|
security = require('../../../lib/security'),
|
|
|
|
themes = require('../../themes'),
|
|
|
|
filters = require('../../../filters'),
|
|
|
|
helpers = require('../helpers');
|
|
|
|
|
|
|
|
// @TODO: the collection+rss controller does almost the same
|
|
|
|
module.exports = function channelController(req, res, next) {
|
2018-06-26 02:12:50 +03:00
|
|
|
debug('channelController', req.params, res.routerOptions);
|
2018-06-24 01:32:08 +03:00
|
|
|
|
|
|
|
const pathOptions = {
|
|
|
|
page: req.params.page !== undefined ? req.params.page : 1,
|
|
|
|
slug: req.params.slug ? security.string.safe(req.params.slug) : undefined
|
|
|
|
};
|
|
|
|
|
|
|
|
if (pathOptions.page) {
|
|
|
|
// CASE 1: routes.yaml `limit` is stronger than theme definition
|
|
|
|
// CASE 2: use `posts_per_page` config from theme as `limit` value
|
2018-06-26 02:12:50 +03:00
|
|
|
if (res.routerOptions.limit) {
|
2018-06-24 01:32:08 +03:00
|
|
|
themes.getActive().updateTemplateOptions({
|
|
|
|
data: {
|
|
|
|
config: {
|
2018-06-26 02:12:50 +03:00
|
|
|
posts_per_page: res.routerOptions.limit
|
2018-06-24 01:32:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-06-26 02:12:50 +03:00
|
|
|
pathOptions.limit = res.routerOptions.limit;
|
2018-06-24 01:32:08 +03:00
|
|
|
} else {
|
|
|
|
const postsPerPage = parseInt(themes.getActive().config('posts_per_page'));
|
|
|
|
|
|
|
|
if (!isNaN(postsPerPage) && postsPerPage > 0) {
|
|
|
|
pathOptions.limit = postsPerPage;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-26 02:12:50 +03:00
|
|
|
return helpers.fetchData(pathOptions, res.routerOptions)
|
2018-06-24 01:32:08 +03:00
|
|
|
.then(function handleResult(result) {
|
|
|
|
// CASE: requested page is greater than number of pages we have
|
|
|
|
if (pathOptions.page > result.meta.pagination.pages) {
|
|
|
|
return next(new common.errors.NotFoundError({
|
|
|
|
message: common.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.
|
|
|
|
helpers.secure(req, result.posts);
|
|
|
|
|
|
|
|
// @TODO: get rid of this O_O
|
|
|
|
_.each(result.data, function (data) {
|
|
|
|
helpers.secure(req, data);
|
|
|
|
});
|
|
|
|
|
|
|
|
// @TODO: properly design these filters
|
|
|
|
filters.doFilter('prePostsRender', result.posts, res.locals)
|
|
|
|
.then(function (posts) {
|
|
|
|
result.posts = posts;
|
|
|
|
return result;
|
|
|
|
})
|
|
|
|
.then(helpers.renderEntries(req, res));
|
|
|
|
})
|
|
|
|
.catch(helpers.handleError(next));
|
|
|
|
};
|