2020-04-29 18:44:27 +03:00
|
|
|
const _ = require('lodash');
|
2021-06-15 19:01:22 +03:00
|
|
|
const debug = require('@tryghost/debug')('services:routing:controllers:channel');
|
2021-09-26 22:21:49 +03:00
|
|
|
const tpl = require('@tryghost/tpl');
|
2020-05-22 21:22:20 +03:00
|
|
|
const errors = require('@tryghost/errors');
|
2020-08-11 16:01:16 +03:00
|
|
|
const security = require('@tryghost/security');
|
2021-04-23 15:22:45 +03:00
|
|
|
const themeEngine = require('../../theme-engine');
|
2020-04-29 18:44:27 +03:00
|
|
|
const helpers = require('../helpers');
|
2018-06-24 01:32:08 +03:00
|
|
|
|
2021-09-26 22:21:49 +03:00
|
|
|
const messages = {
|
|
|
|
pageNotFound: 'Page not found.'
|
|
|
|
};
|
|
|
|
|
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) {
|
2021-04-23 15:22:45 +03:00
|
|
|
themeEngine.getActive().updateTemplateOptions({
|
2018-06-24 01:32:08 +03:00
|
|
|
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 {
|
2021-04-23 15:22:45 +03:00
|
|
|
const postsPerPage = parseInt(themeEngine.getActive().config('posts_per_page'));
|
2018-06-24 01:32:08 +03:00
|
|
|
|
|
|
|
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({
|
2021-09-26 22:21:49 +03:00
|
|
|
message: tpl(messages.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));
|
|
|
|
};
|