mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-22 02:11:44 +03:00
df7e64fafa
refs #10790 - Moved /core/apps into core/frontend - Moved /core/server/helpers to /core/frontend/helpers along with /core/server/services/themes - Changed helper location in overrides - Moved /core/server/services/routing to /core/frontend/services - Moved /core/server/services/url to /core/frontend/services - Moved /core/server/data/meta to /core/frontend/meta - Moved /core/server/services/rss to /core/frontend/services - Moved /core/server/data/xml to /core/frontend/services
71 lines
2.4 KiB
JavaScript
71 lines
2.4 KiB
JavaScript
const _ = require('lodash'),
|
|
debug = require('ghost-ignition').debug('services:routing:controllers:channel'),
|
|
common = require('../../../../server/lib/common'),
|
|
security = require('../../../../server/lib/security'),
|
|
themes = require('../../themes'),
|
|
helpers = require('../helpers');
|
|
|
|
/**
|
|
* @description Channel controller.
|
|
*
|
|
* @TODO: The collection+rss controller do almost the same. Merge!
|
|
*
|
|
* @param {Object} req
|
|
* @param {Object} res
|
|
* @param {Function} next
|
|
* @returns {Promise}
|
|
*/
|
|
module.exports = function channelController(req, res, next) {
|
|
debug('channelController', req.params, res.routerOptions);
|
|
|
|
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
|
|
if (res.routerOptions.limit) {
|
|
themes.getActive().updateTemplateOptions({
|
|
data: {
|
|
config: {
|
|
posts_per_page: res.routerOptions.limit
|
|
}
|
|
}
|
|
});
|
|
|
|
pathOptions.limit = res.routerOptions.limit;
|
|
} else {
|
|
const postsPerPage = parseInt(themes.getActive().config('posts_per_page'));
|
|
|
|
if (!isNaN(postsPerPage) && postsPerPage > 0) {
|
|
pathOptions.limit = postsPerPage;
|
|
}
|
|
}
|
|
}
|
|
|
|
return helpers.fetchData(pathOptions, res.routerOptions, res.locals)
|
|
.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: See helpers/secure for explanation.
|
|
helpers.secure(req, result.posts);
|
|
|
|
// @TODO: See helpers/secure for explanation.
|
|
_.each(result.data, function (data) {
|
|
helpers.secure(req, data);
|
|
});
|
|
|
|
const renderer = helpers.renderEntries(req, res);
|
|
return renderer(result);
|
|
})
|
|
.catch(helpers.handleError(next));
|
|
};
|