feat: async/await admin/controllers

v1.18.x
Barış Soner Uşaklı 6 years ago
parent 216eba6847
commit 72590b3462

@ -1,52 +1,31 @@
'use strict'; 'use strict';
var async = require('async'); const groups = require('../../groups');
const categories = require('../../categories');
const privileges = require('../../privileges');
var groups = require('../../groups'); const AdminsMods = module.exports;
var categories = require('../../categories');
var privileges = require('../../privileges');
var AdminsMods = module.exports; AdminsMods.get = async function (req, res) {
const [admins, globalMods, categories] = await Promise.all([
groups.get('administrators', { uid: req.uid }),
groups.get('Global Moderators', { uid: req.uid }),
getModeratorsOfCategories(req.uid),
]);
AdminsMods.get = function (req, res, next) { res.render('admin/manage/admins-mods', {
async.waterfall([ admins: admins,
function (next) { globalMods: globalMods,
async.parallel({ categories: categories,
admins: function (next) { allPrivileges: privileges.userPrivilegeList,
groups.get('administrators', { uid: req.uid }, next); });
},
globalMods: function (next) {
groups.get('Global Moderators', { uid: req.uid }, next);
},
categories: function (next) {
getModeratorsOfCategories(req.uid, next);
},
}, next);
},
function (results) {
results.allPrivileges = privileges.userPrivilegeList;
res.render('admin/manage/admins-mods', results);
},
], next);
}; };
function getModeratorsOfCategories(uid, callback) { async function getModeratorsOfCategories(uid) {
async.waterfall([ const categoryData = await categories.buildForSelect(uid, 'find');
function (next) { const moderators = await Promise.all(categoryData.map(c => categories.getModerators(c.cid)));
categories.buildForSelect(uid, 'find', next); categoryData.forEach((c, index) => {
}, c.moderators = moderators[index];
function (categoryData, next) { });
async.map(categoryData, function (category, next) { return categoryData;
async.waterfall([
function (next) {
categories.getModerators(category.cid, next);
},
function (moderators, next) {
category.moderators = moderators;
next(null, category);
},
], next);
}, next);
},
], callback);
} }

@ -1,9 +1,9 @@
'use strict'; 'use strict';
var appearanceController = module.exports; const appearanceController = module.exports;
appearanceController.get = function (req, res) { appearanceController.get = function (req, res) {
var term = req.params.term ? req.params.term : 'themes'; const term = req.params.term ? req.params.term : 'themes';
res.render('admin/appearance/' + term, {}); res.render('admin/appearance/' + term, {});
}; };

@ -1,22 +1,18 @@
'use strict'; 'use strict';
var async = require('async'); const meta = require('../../meta');
var meta = require('../../meta'); const analytics = require('../../analytics');
var analytics = require('../../analytics');
var blacklistController = module.exports; const blacklistController = module.exports;
blacklistController.get = function (req, res, next) { blacklistController.get = async function (req, res) {
async.parallel({ const [rules, analyticsData] = await Promise.all([
rules: async.apply(meta.blacklist.get), meta.blacklist.get(),
analytics: async.apply(analytics.getBlacklistAnalytics), analytics.getBlacklistAnalytics(),
}, function (err, data) { ]);
if (err) { res.render('admin/manage/ip-blacklist', {
return next(err); title: '[[pages:ip-blacklist]]',
} rules: rules,
analytics: analyticsData,
res.render('admin/manage/ip-blacklist', Object.assign(data, {
title: '[[pages:ip-blacklist]]',
}));
}); });
}; };

@ -1,23 +1,23 @@
'use strict'; 'use strict';
var cacheController = module.exports; const cacheController = module.exports;
var utils = require('../../utils'); const utils = require('../../utils');
cacheController.get = function (req, res) { cacheController.get = function (req, res) {
var postCache = require('../../posts/cache'); const postCache = require('../../posts/cache');
var groupCache = require('../../groups').cache; const groupCache = require('../../groups').cache;
var objectCache = require('../../database').objectCache; const objectCache = require('../../database').objectCache;
var localCache = require('../../cache'); const localCache = require('../../cache');
var avgPostSize = 0; let avgPostSize = 0;
var percentFull = 0; let percentFull = 0;
if (postCache.itemCount > 0) { if (postCache.itemCount > 0) {
avgPostSize = parseInt((postCache.length / postCache.itemCount), 10); avgPostSize = parseInt((postCache.length / postCache.itemCount), 10);
percentFull = ((postCache.length / postCache.max) * 100).toFixed(2); percentFull = ((postCache.length / postCache.max) * 100).toFixed(2);
} }
var data = { const data = {
postCache: { postCache: {
length: postCache.length, length: postCache.length,
max: postCache.max, max: postCache.max,

@ -1,84 +1,66 @@
'use strict'; 'use strict';
var async = require('async'); const categories = require('../../categories');
const analytics = require('../../analytics');
const plugins = require('../../plugins');
const translator = require('../../translator');
var categories = require('../../categories'); const categoriesController = module.exports;
var analytics = require('../../analytics');
var plugins = require('../../plugins');
var translator = require('../../translator');
var categoriesController = module.exports; categoriesController.get = async function (req, res, next) {
const [categoryData, parent, allCategories] = await Promise.all([
categories.getCategories([req.params.category_id], req.uid),
categories.getParents([req.params.category_id]),
getAllCategories(req.uid),
]);
categoriesController.get = function (req, res, callback) { const category = categoryData[0];
async.waterfall([ if (!category) {
function (next) { return next();
async.parallel({ }
category: async.apply(categories.getCategories, [req.params.category_id], req.uid),
parent: async.apply(categories.getParents, [req.params.category_id]),
allCategories: function (next) {
async.waterfall([
function (next) {
categories.getAllCidsFromSet('categories:cid', next);
},
function (cids, next) {
categories.getCategories(cids, req.uid, next);
},
function (categoryData, next) {
categoryData = categories.getTree(categoryData);
categories.buildForSelectCategories(categoryData, next);
},
], next);
},
}, next);
},
function (data, next) {
var category = data.category[0];
if (!category) { category.parent = parent[0];
return callback(); allCategories.forEach(function (category) {
} if (category) {
category.parent = data.parent[0]; category.selected = parseInt(category.cid, 10) === parseInt(req.params.category_id, 10);
data.allCategories.forEach(function (category) { }
if (category) { });
category.selected = parseInt(category.cid, 10) === parseInt(req.params.category_id, 10);
}
});
plugins.fireHook('filter:admin.category.get', { const data = await plugins.fireHook('filter:admin.category.get', {
req: req, req: req,
res: res, res: res,
category: category, category: category,
customClasses: [], customClasses: [],
allCategories: data.allCategories, allCategories: allCategories,
}, next); });
}, data.category.name = translator.escape(String(data.category.name));
function (data) {
data.category.name = translator.escape(String(data.category.name));
res.render('admin/manage/category', { res.render('admin/manage/category', {
category: data.category, category: data.category,
allCategories: data.allCategories, allCategories: data.allCategories,
customClasses: data.customClasses, customClasses: data.customClasses,
}); });
},
], callback);
}; };
async function getAllCategories(uid) {
const cids = await categories.getAllCidsFromSet('categories:cid');
const categoryData = await categories.getCategories(cids, uid);
const tree = categories.getTree(categoryData);
return await categories.buildForSelectCategories(tree);
}
categoriesController.getAll = function (req, res) { categoriesController.getAll = function (req, res) {
// Categories list will be rendered on client side with recursion, etc. // Categories list will be rendered on client side with recursion, etc.
res.render('admin/manage/categories', {}); res.render('admin/manage/categories', {});
}; };
categoriesController.getAnalytics = function (req, res, next) { categoriesController.getAnalytics = async function (req, res) {
async.waterfall([ const [name, analyticsData] = await Promise.all([
function (next) { categories.getCategoryField(req.params.category_id, 'name'),
async.parallel({ analytics.getCategoryAnalytics(req.params.category_id),
name: async.apply(categories.getCategoryField, req.params.category_id, 'name'), ]);
analytics: async.apply(analytics.getCategoryAnalytics, req.params.category_id), res.render('admin/manage/category-analytics', {
}, next); name: name,
}, analytics: analyticsData,
function (data) { });
res.render('admin/manage/category-analytics', data);
},
], next);
}; };

Loading…
Cancel
Save