feat: convert categories controller to async/await

v1.18.x
Baris Usakli 6 years ago
parent 90bcd65a00
commit e9fd49e234

@ -1,15 +1,14 @@
'use strict'; 'use strict';
var async = require('async'); const nconf = require('nconf');
var nconf = require('nconf');
var categories = require('../categories'); const categories = require('../categories');
var meta = require('../meta'); const meta = require('../meta');
var helpers = require('./helpers'); const helpers = require('./helpers');
var categoriesController = module.exports; const categoriesController = module.exports;
categoriesController.list = function (req, res, next) { categoriesController.list = async function (req, res) {
res.locals.metaTags = [{ res.locals.metaTags = [{
name: 'title', name: 'title',
content: String(meta.config.title || 'NodeBB'), content: String(meta.config.title || 'NodeBB'),
@ -18,45 +17,34 @@ categoriesController.list = function (req, res, next) {
content: 'website', content: 'website',
}]; }];
var categoryData; const categoryData = await categories.getCategoriesByPrivilege('categories:cid', req.uid, 'find');
let tree; const tree = categories.getTree(categoryData, 0);
async.waterfall([ await categories.getRecentTopicReplies(categoryData, req.uid);
function (next) {
categories.getCategoriesByPrivilege('categories:cid', req.uid, 'find', next); const data = {
}, title: meta.config.homePageTitle || '[[pages:home]]',
function (_categoryData, next) { categories: tree,
categoryData = _categoryData; };
tree = categories.getTree(categoryData, 0); if (req.originalUrl.startsWith(nconf.get('relative_path') + '/api/categories') || req.originalUrl.startsWith(nconf.get('relative_path') + '/categories')) {
categories.getRecentTopicReplies(categoryData, req.uid, next); data.title = '[[pages:categories]]';
}, data.breadcrumbs = helpers.buildBreadcrumbs([{ text: data.title }]);
function () { res.locals.metaTags.push({
var data = { property: 'og:title',
title: meta.config.homePageTitle || '[[pages:home]]', content: '[[pages:categories]]',
categories: tree, });
}
data.categories.forEach(function (category) {
if (category && Array.isArray(category.posts) && category.posts.length && category.posts[0]) {
category.teaser = {
url: nconf.get('relative_path') + '/post/' + category.posts[0].pid,
timestampISO: category.posts[0].timestampISO,
pid: category.posts[0].pid,
topic: category.posts[0].topic,
}; };
}
});
if (req.originalUrl.startsWith(nconf.get('relative_path') + '/api/categories') || req.originalUrl.startsWith(nconf.get('relative_path') + '/categories')) { res.render('categories', data);
data.title = '[[pages:categories]]';
data.breadcrumbs = helpers.buildBreadcrumbs([{ text: data.title }]);
res.locals.metaTags.push({
property: 'og:title',
content: '[[pages:categories]]',
});
}
data.categories.forEach(function (category) {
if (category && Array.isArray(category.posts) && category.posts.length && category.posts[0]) {
category.teaser = {
url: nconf.get('relative_path') + '/post/' + category.posts[0].pid,
timestampISO: category.posts[0].timestampISO,
pid: category.posts[0].pid,
topic: category.posts[0].topic,
};
}
});
res.render('categories', data);
},
], next);
}; };

Loading…
Cancel
Save