refactor: async/await users

v1.18.x
Baris Usakli 6 years ago
parent 19b63bfaa7
commit f9a804e014

@ -1,19 +1,18 @@
'use strict'; 'use strict';
var async = require('async'); const user = require('../user');
var user = require('../user'); const meta = require('../meta');
var meta = require('../meta');
var db = require('../database'); const db = require('../database');
var pagination = require('../pagination'); const pagination = require('../pagination');
var privileges = require('../privileges'); const privileges = require('../privileges');
var helpers = require('./helpers'); const helpers = require('./helpers');
var usersController = module.exports; const usersController = module.exports;
usersController.index = function (req, res, next) { usersController.index = async function (req, res, next) {
var section = req.query.section || 'joindate'; const section = req.query.section || 'joindate';
var sectionToController = { const sectionToController = {
joindate: usersController.getUsersSortedByJoinDate, joindate: usersController.getUsersSortedByJoinDate,
online: usersController.getOnlineUsers, online: usersController.getOnlineUsers,
'sort-posts': usersController.getUsersSortedByPosts, 'sort-posts': usersController.getUsersSortedByPosts,
@ -23,133 +22,99 @@ usersController.index = function (req, res, next) {
}; };
if (req.query.term) { if (req.query.term) {
usersController.search(req, res, next); await usersController.search(req, res, next);
} else if (sectionToController[section]) { } else if (sectionToController[section]) {
sectionToController[section](req, res, next); await sectionToController[section](req, res, next);
} else { } else {
usersController.getUsersSortedByJoinDate(req, res, next); await usersController.getUsersSortedByJoinDate(req, res, next);
} }
}; };
usersController.search = function (req, res, next) { usersController.search = async function (req, res, next) {
async.waterfall([ const allowed = await privileges.global.can('search:users', req.uid);
function (next) { if (!allowed) {
privileges.global.can('search:users', req.uid, next); return next(new Error('[[error:no-privileges]]'));
}, }
function (allowed, next) { const [searchData, isAdminOrGlobalMod] = await Promise.all([
if (!allowed) { user.search({
return next(new Error('[[error:no-privileges]]')); query: req.query.term,
} searchBy: req.query.searchBy || 'username',
async.parallel({ page: req.query.page || 1,
search: function (next) { sortBy: req.query.sortBy || 'joindate',
user.search({ onlineOnly: req.query.onlineOnly === 'true',
query: req.query.term, bannedOnly: req.query.bannedOnly === 'true',
searchBy: req.query.searchBy || 'username', flaggedOnly: req.query.flaggedOnly === 'true',
page: req.query.page || 1, }),
sortBy: req.query.sortBy || 'joindate', user.isAdminOrGlobalMod(req.uid),
onlineOnly: req.query.onlineOnly === 'true', ]);
bannedOnly: req.query.bannedOnly === 'true', const section = req.query.section || 'joindate';
flaggedOnly: req.query.flaggedOnly === 'true',
}, next); searchData.isAdminOrGlobalMod = isAdminOrGlobalMod;
}, searchData.pagination = pagination.create(req.query.page, searchData.pageCount, req.query);
isAdminOrGlobalMod: function (next) { searchData['section_' + section] = true;
user.isAdminOrGlobalMod(req.uid, next); searchData.displayUserSearch = true;
}, await render(req, res, searchData);
}, next);
},
function (results, next) {
var section = req.query.section || 'joindate';
results.search.isAdminOrGlobalMod = results.isAdminOrGlobalMod;
results.search.pagination = pagination.create(req.query.page, results.search.pageCount, req.query);
results.search['section_' + section] = true;
results.displayUserSearch = true;
render(req, res, results.search, next);
},
], next);
}; };
usersController.getOnlineUsers = function (req, res, next) { usersController.getOnlineUsers = async function (req, res) {
async.waterfall([ const [userData, guests] = await Promise.all([
function (next) { usersController.getUsers('users:online', req.uid, req.query),
async.parallel({ require('../socket.io/admin/rooms').getTotalGuestCount(),
users: function (next) { ]);
usersController.getUsers('users:online', req.uid, req.query, next);
}, let hiddenCount = 0;
guests: function (next) { if (!userData.isAdminOrGlobalMod) {
require('../socket.io/admin/rooms').getTotalGuestCount(next); userData.users = userData.users.filter(function (user) {
}, if (user && user.status === 'offline') {
}, next); hiddenCount += 1;
},
function (results, next) {
var userData = results.users;
var hiddenCount = 0;
if (!userData.isAdminOrGlobalMod) {
userData.users = userData.users.filter(function (user) {
if (user && user.status === 'offline') {
hiddenCount += 1;
}
return user && user.status !== 'offline';
});
} }
return user && user.status !== 'offline';
});
}
userData.anonymousUserCount = results.guests + hiddenCount; userData.anonymousUserCount = guests + hiddenCount;
render(req, res, userData, next); await render(req, res, userData);
},
], next);
}; };
usersController.getUsersSortedByPosts = function (req, res, next) { usersController.getUsersSortedByPosts = async function (req, res) {
usersController.renderUsersPage('users:postcount', req, res, next); await usersController.renderUsersPage('users:postcount', req, res);
}; };
usersController.getUsersSortedByReputation = function (req, res, next) { usersController.getUsersSortedByReputation = async function (req, res, next) {
if (meta.config['reputation:disabled']) { if (meta.config['reputation:disabled']) {
return next(); return next();
} }
usersController.renderUsersPage('users:reputation', req, res, next); await usersController.renderUsersPage('users:reputation', req, res);
}; };
usersController.getUsersSortedByJoinDate = function (req, res, next) { usersController.getUsersSortedByJoinDate = async function (req, res) {
usersController.renderUsersPage('users:joindate', req, res, next); await usersController.renderUsersPage('users:joindate', req, res);
}; };
usersController.getBannedUsers = function (req, res, next) { usersController.getBannedUsers = async function (req, res) {
renderIfAdminOrGlobalMod('users:banned', req, res, next); await renderIfAdminOrGlobalMod('users:banned', req, res);
}; };
usersController.getFlaggedUsers = function (req, res, next) { usersController.getFlaggedUsers = async function (req, res) {
renderIfAdminOrGlobalMod('users:flags', req, res, next); await renderIfAdminOrGlobalMod('users:flags', req, res);
}; };
function renderIfAdminOrGlobalMod(set, req, res, next) { async function renderIfAdminOrGlobalMod(set, req, res) {
async.waterfall([ const isAdminOrGlobalMod = await user.isAdminOrGlobalMod(req.uid);
function (next) { if (!isAdminOrGlobalMod) {
user.isAdminOrGlobalMod(req.uid, next); return helpers.notAllowed(req, res);
}, }
function (isAdminOrGlobalMod, next) { await usersController.renderUsersPage(set, req, res);
if (!isAdminOrGlobalMod) {
return helpers.notAllowed(req, res);
}
usersController.renderUsersPage(set, req, res, next);
},
], next);
} }
usersController.renderUsersPage = function (set, req, res, next) { usersController.renderUsersPage = async function (set, req, res) {
async.waterfall([ const userData = await usersController.getUsers(set, req.uid, req.query);
function (next) { render(req, res, userData);
usersController.getUsers(set, req.uid, req.query, next);
},
function (userData, next) {
render(req, res, userData, next);
},
], next);
}; };
usersController.getUsers = function (set, uid, query, callback) { usersController.getUsers = async function (set, uid, query) {
var setToData = { const setToData = {
'users:postcount': { title: '[[pages:users/sort-posts]]', crumb: '[[users:top_posters]]' }, 'users:postcount': { title: '[[pages:users/sort-posts]]', crumb: '[[users:top_posters]]' },
'users:reputation': { title: '[[pages:users/sort-reputation]]', crumb: '[[users:most_reputation]]' }, 'users:reputation': { title: '[[pages:users/sort-reputation]]', crumb: '[[users:most_reputation]]' },
'users:joindate': { title: '[[pages:users/latest]]', crumb: '[[global:users]]' }, 'users:joindate': { title: '[[pages:users/latest]]', crumb: '[[global:users]]' },
@ -162,96 +127,63 @@ usersController.getUsers = function (set, uid, query, callback) {
setToData[set] = { title: '', crumb: '' }; setToData[set] = { title: '', crumb: '' };
} }
var breadcrumbs = [{ text: setToData[set].crumb }]; const breadcrumbs = [{ text: setToData[set].crumb }];
if (set !== 'users:joindate') { if (set !== 'users:joindate') {
breadcrumbs.unshift({ text: '[[global:users]]', url: '/users' }); breadcrumbs.unshift({ text: '[[global:users]]', url: '/users' });
} }
var page = parseInt(query.page, 10) || 1; const page = parseInt(query.page, 10) || 1;
var resultsPerPage = meta.config.userSearchResultsPerPage; const resultsPerPage = meta.config.userSearchResultsPerPage;
var start = Math.max(0, page - 1) * resultsPerPage; const start = Math.max(0, page - 1) * resultsPerPage;
var stop = start + resultsPerPage - 1; const stop = start + resultsPerPage - 1;
async.waterfall([ const [isAdminOrGlobalMod, canSearch, usersData] = await Promise.all([
function (next) { user.isAdminOrGlobalMod(uid),
async.parallel({ privileges.global.can('search:users', uid),
isAdminOrGlobalMod: function (next) { usersController.getUsersAndCount(set, uid, start, stop),
user.isAdminOrGlobalMod(uid, next); ]);
}, const pageCount = Math.ceil(usersData.count / resultsPerPage);
canSearch: function (next) { return {
privileges.global.can('search:users', uid, next); users: usersData.users,
}, pagination: pagination.create(page, pageCount, query),
usersData: function (next) { userCount: usersData.count,
usersController.getUsersAndCount(set, uid, start, stop, next); title: setToData[set].title || '[[pages:users/latest]]',
}, breadcrumbs: helpers.buildBreadcrumbs(breadcrumbs),
}, next); isAdminOrGlobalMod: isAdminOrGlobalMod,
}, displayUserSearch: canSearch,
function (results, next) { ['section_' + (query.section || 'joindate')]: true,
var pageCount = Math.ceil(results.usersData.count / resultsPerPage); };
var userData = {
users: results.usersData.users,
pagination: pagination.create(page, pageCount, query),
userCount: results.usersData.count,
title: setToData[set].title || '[[pages:users/latest]]',
breadcrumbs: helpers.buildBreadcrumbs(breadcrumbs),
isAdminOrGlobalMod: results.isAdminOrGlobalMod,
displayUserSearch: results.canSearch,
};
userData['section_' + (query.section || 'joindate')] = true;
next(null, userData);
},
], callback);
}; };
usersController.getUsersAndCount = function (set, uid, start, stop, callback) { usersController.getUsersAndCount = async function (set, uid, start, stop) {
async.waterfall([ async function getCount() {
function (next) { if (set === 'users:online') {
async.parallel({ return await db.sortedSetCount('users:online', Date.now() - 300000, '+inf');
users: function (next) { } else if (set === 'users:banned' || set === 'users:flags') {
user.getUsersFromSet(set, uid, start, stop, next); return await db.sortedSetCard(set);
}, }
count: function (next) { return await db.getObjectField('global', 'userCount');
if (set === 'users:online') { }
var now = Date.now(); const [usersData, count] = await Promise.all([
db.sortedSetCount('users:online', now - 300000, '+inf', next); user.getUsersFromSet(set, uid, start, stop),
} else if (set === 'users:banned') { getCount(),
db.sortedSetCard('users:banned', next); ]);
} else if (set === 'users:flags') { return {
db.sortedSetCard('users:flags', next); users: usersData.filter(user => user && parseInt(user.uid, 10)),
} else { count: count,
db.getObjectField('global', 'userCount', next); };
}
},
}, next);
},
function (results, next) {
results.users = results.users.filter(function (user) {
return user && parseInt(user.uid, 10);
});
next(null, results);
},
], callback);
}; };
function render(req, res, data, next) { async function render(req, res, data) {
var registrationType = meta.config.registrationType; const registrationType = meta.config.registrationType;
data.maximumInvites = meta.config.maximumInvites; data.maximumInvites = meta.config.maximumInvites;
data.inviteOnly = registrationType === 'invite-only' || registrationType === 'admin-invite-only'; data.inviteOnly = registrationType === 'invite-only' || registrationType === 'admin-invite-only';
data.adminInviteOnly = registrationType === 'admin-invite-only'; data.adminInviteOnly = registrationType === 'admin-invite-only';
data['reputation:disabled'] = meta.config['reputation:disabled']; data['reputation:disabled'] = meta.config['reputation:disabled'];
data.invites = await user.getInvitesNumber(req.uid);
async.waterfall([ res.append('X-Total-Count', data.userCount);
function (next) { res.render('users', data);
user.getInvitesNumber(req.uid, next);
},
function (numInvites) {
res.append('X-Total-Count', data.userCount);
data.invites = numInvites;
res.render('users', data);
},
], next);
} }

Loading…
Cancel
Save