refactor: controllers/category.js

v1.18.x
Baris Usakli 6 years ago
parent 8c1ecdc13c
commit bd8736db38

@ -1,135 +1,100 @@
'use strict'; 'use strict';
var async = require('async'); const nconf = require('nconf');
var nconf = require('nconf');
const db = require('../database');
var db = require('../database'); const privileges = require('../privileges');
var privileges = require('../privileges'); const user = require('../user');
var user = require('../user'); const categories = require('../categories');
var categories = require('../categories'); const meta = require('../meta');
var meta = require('../meta'); const pagination = require('../pagination');
var pagination = require('../pagination'); const helpers = require('./helpers');
var helpers = require('./helpers'); const utils = require('../utils');
var utils = require('../utils'); const translator = require('../translator');
var translator = require('../translator'); const analytics = require('../analytics');
var analytics = require('../analytics');
const categoryController = module.exports;
var categoryController = module.exports;
categoryController.get = async function (req, res, next) {
categoryController.get = function (req, res, callback) { const cid = req.params.category_id;
var cid = req.params.category_id;
var currentPage = parseInt(req.query.page, 10) || 1;
var pageCount = 1;
var userPrivileges;
var settings;
var rssToken;
if ((req.params.topic_index && !utils.isNumber(req.params.topic_index)) || !utils.isNumber(cid)) { if ((req.params.topic_index && !utils.isNumber(req.params.topic_index)) || !utils.isNumber(cid)) {
return callback(); return next();
} }
var topicIndex = utils.isNumber(req.params.topic_index) ? parseInt(req.params.topic_index, 10) - 1 : 0; const [categoryFields, userPrivileges, userSettings, rssToken] = await Promise.all([
categories.getCategoryFields(cid, ['slug', 'disabled', 'topic_count']),
privileges.categories.get(cid, req.uid),
user.getSettings(req.uid),
user.auth.getFeedToken(req.uid),
]);
async.waterfall([ if (!categoryFields.slug || (categoryFields && categoryFields.disabled)) {
function (next) { return next();
async.parallel({
categoryData: function (next) {
categories.getCategoryFields(cid, ['slug', 'disabled', 'topic_count'], next);
},
privileges: function (next) {
privileges.categories.get(cid, req.uid, next);
},
userSettings: function (next) {
user.getSettings(req.uid, next);
},
rssToken: function (next) {
user.auth.getFeedToken(req.uid, next);
},
}, next);
},
function (results, next) {
userPrivileges = results.privileges;
rssToken = results.rssToken;
if (!results.categoryData.slug || (results.categoryData && results.categoryData.disabled)) {
return callback();
} }
if (!results.privileges.read) { if (!userPrivileges.read) {
return helpers.notAllowed(req, res); return helpers.notAllowed(req, res);
} }
if (!res.locals.isAPI && (!req.params.slug || results.categoryData.slug !== cid + '/' + req.params.slug) && (results.categoryData.slug && results.categoryData.slug !== cid + '/')) { if (!res.locals.isAPI && (!req.params.slug || categoryFields.slug !== cid + '/' + req.params.slug) && (categoryFields.slug && categoryFields.slug !== cid + '/')) {
return helpers.redirect(res, '/category/' + results.categoryData.slug); return helpers.redirect(res, '/category/' + categoryFields.slug);
} }
settings = results.userSettings; const topicCount = categoryFields.topic_count;
let topicIndex = utils.isNumber(req.params.topic_index) ? parseInt(req.params.topic_index, 10) - 1 : 0;
var topicCount = results.categoryData.topic_count;
pageCount = Math.max(1, Math.ceil(topicCount / settings.topicsPerPage));
if (topicIndex < 0 || topicIndex > Math.max(topicCount - 1, 0)) { if (topicIndex < 0 || topicIndex > Math.max(topicCount - 1, 0)) {
return helpers.redirect(res, '/category/' + cid + '/' + req.params.slug + (topicIndex > topicCount ? '/' + topicCount : '')); return helpers.redirect(res, '/category/' + cid + '/' + req.params.slug + (topicIndex > topicCount ? '/' + topicCount : ''));
} }
const pageCount = Math.max(1, Math.ceil(topicCount / userSettings.topicsPerPage));
if (settings.usePagination && (currentPage < 1 || currentPage > pageCount)) { let currentPage = parseInt(req.query.page, 10) || 1;
return callback(); if (userSettings.usePagination && (currentPage < 1 || currentPage > pageCount)) {
return next();
} }
if (!settings.usePagination) { if (!userSettings.usePagination) {
topicIndex = Math.max(0, topicIndex - (Math.ceil(settings.topicsPerPage / 2) - 1)); topicIndex = Math.max(0, topicIndex - (Math.ceil(userSettings.topicsPerPage / 2) - 1));
} else if (!req.query.page) { } else if (!req.query.page) {
var index = Math.max(parseInt((topicIndex || 0), 10), 0); const index = Math.max(parseInt((topicIndex || 0), 10), 0);
currentPage = Math.ceil((index + 1) / settings.topicsPerPage); currentPage = Math.ceil((index + 1) / userSettings.topicsPerPage);
topicIndex = 0; topicIndex = 0;
} }
user.getUidByUserslug(req.query.author, next); const targetUid = await user.getUidByUserslug(req.query.author);
}, const start = ((currentPage - 1) * userSettings.topicsPerPage) + topicIndex;
function (targetUid, next) { const stop = start + userSettings.topicsPerPage - 1;
var start = ((currentPage - 1) * settings.topicsPerPage) + topicIndex;
var stop = start + settings.topicsPerPage - 1; const categoryData = await categories.getCategoryById({
categories.getCategoryById({
uid: req.uid, uid: req.uid,
cid: cid, cid: cid,
start: start, start: start,
stop: stop, stop: stop,
sort: req.query.sort || settings.categoryTopicSort, sort: req.query.sort || userSettings.categoryTopicSort,
settings: settings, settings: userSettings,
query: req.query, query: req.query,
tag: req.query.tag, tag: req.query.tag,
targetUid: targetUid, targetUid: targetUid,
}, next); });
},
function (categoryData, next) {
if (!categoryData) { if (!categoryData) {
return callback(); return next();
} }
categories.modifyTopicsByPrivilege(categoryData.topics, userPrivileges); categories.modifyTopicsByPrivilege(categoryData.topics, userPrivileges);
if (categoryData.link) { if (categoryData.link) {
db.incrObjectField('category:' + categoryData.cid, 'timesClicked'); await db.incrObjectField('category:' + categoryData.cid, 'timesClicked');
return helpers.redirect(res, categoryData.link); return helpers.redirect(res, categoryData.link);
} }
await buildBreadcrumbs(req, categoryData);
buildBreadcrumbs(req, categoryData, next); if (categoryData.children.length) {
}, const allCategories = [];
function (categoryData, next) { categories.flattenCategories(allCategories, categoryData.children);
if (!categoryData.children.length) { await categories.getRecentTopicReplies(allCategories, req.uid);
return next(null, categoryData);
} }
var allCategories = []; categoryData.title = translator.escape(categoryData.name);
categories.flattenCategories(allCategories, categoryData.children);
categories.getRecentTopicReplies(allCategories, req.uid, function (err) {
next(err, categoryData);
});
},
function (categoryData) {
categoryData.description = translator.escape(categoryData.description); categoryData.description = translator.escape(categoryData.description);
categoryData.privileges = userPrivileges; categoryData.privileges = userPrivileges;
categoryData.showSelect = categoryData.privileges.editable; categoryData.showSelect = userPrivileges.editable;
categoryData.rssFeedUrl = nconf.get('url') + '/category/' + categoryData.cid + '.rss'; categoryData.rssFeedUrl = nconf.get('url') + '/category/' + categoryData.cid + '.rss';
if (parseInt(req.uid, 10)) { if (parseInt(req.uid, 10)) {
categories.markAsRead([cid], req.uid); categories.markAsRead([cid], req.uid);
@ -140,8 +105,7 @@ categoryController.get = function (req, res, callback) {
categoryData['feeds:disableRSS'] = meta.config['feeds:disableRSS']; categoryData['feeds:disableRSS'] = meta.config['feeds:disableRSS'];
categoryData['reputation:disabled'] = meta.config['reputation:disabled']; categoryData['reputation:disabled'] = meta.config['reputation:disabled'];
categoryData.title = translator.escape(categoryData.name);
pageCount = Math.max(1, Math.ceil(categoryData.topic_count / settings.topicsPerPage));
categoryData.pagination = pagination.create(currentPage, pageCount, req.query); categoryData.pagination = pagination.create(currentPage, pageCount, req.query);
categoryData.pagination.rel.forEach(function (rel) { categoryData.pagination.rel.forEach(function (rel) {
rel.href = nconf.get('url') + '/category/' + categoryData.slug + rel.href; rel.href = nconf.get('url') + '/category/' + categoryData.slug + rel.href;
@ -151,28 +115,19 @@ categoryController.get = function (req, res, callback) {
analytics.increment(['pageviews:byCid:' + categoryData.cid]); analytics.increment(['pageviews:byCid:' + categoryData.cid]);
res.render('category', categoryData); res.render('category', categoryData);
},
], callback);
}; };
function buildBreadcrumbs(req, categoryData, callback) { async function buildBreadcrumbs(req, categoryData) {
var breadcrumbs = [ const breadcrumbs = [
{ {
text: categoryData.name, text: categoryData.name,
url: nconf.get('relative_path') + '/category/' + categoryData.slug, url: nconf.get('relative_path') + '/category/' + categoryData.slug,
}, },
]; ];
async.waterfall([ const crumbs = await helpers.buildCategoryBreadcrumbs(categoryData.parentCid);
function (next) {
helpers.buildCategoryBreadcrumbs(categoryData.parentCid, next);
},
function (crumbs, next) {
if (req.originalUrl.startsWith(nconf.get('relative_path') + '/api/category') || req.originalUrl.startsWith(nconf.get('relative_path') + '/category')) { if (req.originalUrl.startsWith(nconf.get('relative_path') + '/api/category') || req.originalUrl.startsWith(nconf.get('relative_path') + '/category')) {
categoryData.breadcrumbs = crumbs.concat(breadcrumbs); categoryData.breadcrumbs = crumbs.concat(breadcrumbs);
} }
next(null, categoryData);
},
], callback);
} }
function addTags(categoryData, res) { function addTags(categoryData, res) {

Loading…
Cancel
Save