2020-04-29 18:44:27 +03:00
|
|
|
const _ = require('lodash');
|
|
|
|
const debug = require('ghost-ignition').debug('services:routing:controllers:channel');
|
2020-05-22 21:22:20 +03:00
|
|
|
const {i18n} = require('../../../../server/lib/common');
|
|
|
|
const errors = require('@tryghost/errors');
|
2020-08-11 16:01:16 +03:00
|
|
|
const security = require('@tryghost/security');
|
2020-04-29 18:44:27 +03:00
|
|
|
const themes = require('../../themes');
|
|
|
|
const helpers = require('../helpers');
|
2018-06-24 01:32:08 +03:00
|
|
|
|
2019-04-22 00:55:22 +03:00
|
|
|
/**
|
|
|
|
* @description Channel controller.
|
|
|
|
*
|
|
|
|
* @TODO: The collection+rss controller do almost the same. Merge!
|
|
|
|
*
|
|
|
|
* @param {Object} req
|
|
|
|
* @param {Object} res
|
|
|
|
* @param {Function} next
|
|
|
|
* @returns {Promise}
|
|
|
|
*/
|
2018-06-24 01:32:08 +03:00
|
|
|
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-10-17 10:23:59 +03:00
|
|
|
return helpers.fetchData(pathOptions, res.routerOptions, res.locals)
|
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) {
|
2020-05-22 21:22:20 +03:00
|
|
|
return next(new errors.NotFoundError({
|
|
|
|
message: i18n.t('errors.errors.pageNotFound')
|
2018-06-24 01:32:08 +03:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Format data 1
|
2019-04-22 00:55:22 +03:00
|
|
|
// @TODO: See helpers/secure for explanation.
|
2018-06-24 01:32:08 +03:00
|
|
|
helpers.secure(req, result.posts);
|
|
|
|
|
2019-04-22 00:55:22 +03:00
|
|
|
// @TODO: See helpers/secure for explanation.
|
2018-06-24 01:32:08 +03:00
|
|
|
_.each(result.data, function (data) {
|
|
|
|
helpers.secure(req, data);
|
|
|
|
});
|
|
|
|
|
2019-04-15 17:15:32 +03:00
|
|
|
const renderer = helpers.renderEntries(req, res);
|
|
|
|
return renderer(result);
|
2018-06-24 01:32:08 +03:00
|
|
|
})
|
|
|
|
.catch(helpers.handleError(next));
|
|
|
|
};
|