feat: async/await controllers/accounts
parent
014e31533d
commit
a3541d887b
@ -1,45 +1,29 @@
|
||||
'use strict';
|
||||
|
||||
var async = require('async');
|
||||
const user = require('../../user');
|
||||
const categories = require('../../categories');
|
||||
const accountHelpers = require('./helpers');
|
||||
|
||||
var user = require('../../user');
|
||||
var categories = require('../../categories');
|
||||
var accountHelpers = require('./helpers');
|
||||
const categoriesController = module.exports;
|
||||
|
||||
var categoriesController = module.exports;
|
||||
|
||||
categoriesController.get = function (req, res, callback) {
|
||||
var userData;
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
accountHelpers.getUserDataByUserSlug(req.params.userslug, req.uid, next);
|
||||
},
|
||||
function (_userData, next) {
|
||||
userData = _userData;
|
||||
categoriesController.get = async function (req, res, next) {
|
||||
const userData = await accountHelpers.getUserDataByUserSlug(req.params.userslug, req.uid);
|
||||
if (!userData) {
|
||||
return callback();
|
||||
return next();
|
||||
}
|
||||
const [states, categoriesData] = await Promise.all([
|
||||
user.getCategoryWatchState(userData.uid),
|
||||
categories.buildForSelect(userData.uid, 'find'),
|
||||
]);
|
||||
|
||||
async.parallel({
|
||||
states: function (next) {
|
||||
user.getCategoryWatchState(userData.uid, next);
|
||||
},
|
||||
categories: function (next) {
|
||||
categories.buildForSelect(userData.uid, 'find', next);
|
||||
},
|
||||
}, next);
|
||||
},
|
||||
function (results) {
|
||||
results.categories.forEach(function (category) {
|
||||
categoriesData.forEach(function (category) {
|
||||
if (category) {
|
||||
category.isIgnored = results.states[category.cid] === categories.watchStates.ignoring;
|
||||
category.isWatched = results.states[category.cid] === categories.watchStates.watching;
|
||||
category.isNotWatched = results.states[category.cid] === categories.watchStates.notwatching;
|
||||
category.isIgnored = states[category.cid] === categories.watchStates.ignoring;
|
||||
category.isWatched = states[category.cid] === categories.watchStates.watching;
|
||||
category.isNotWatched = states[category.cid] === categories.watchStates.notwatching;
|
||||
}
|
||||
});
|
||||
userData.categories = results.categories;
|
||||
userData.categories = categoriesData;
|
||||
userData.title = '[[pages:account/watched_categories, ' + userData.username + ']]';
|
||||
res.render('account/categories', userData);
|
||||
},
|
||||
], callback);
|
||||
};
|
||||
|
@ -1,51 +1,41 @@
|
||||
'use strict';
|
||||
|
||||
var async = require('async');
|
||||
const user = require('../../user');
|
||||
const helpers = require('../helpers');
|
||||
const accountHelpers = require('./helpers');
|
||||
const pagination = require('../../pagination');
|
||||
|
||||
var user = require('../../user');
|
||||
var helpers = require('../helpers');
|
||||
var accountHelpers = require('./helpers');
|
||||
var pagination = require('../../pagination');
|
||||
const followController = module.exports;
|
||||
|
||||
var followController = module.exports;
|
||||
|
||||
followController.getFollowing = function (req, res, next) {
|
||||
getFollow('account/following', 'following', req, res, next);
|
||||
followController.getFollowing = async function (req, res, next) {
|
||||
await getFollow('account/following', 'following', req, res, next);
|
||||
};
|
||||
|
||||
followController.getFollowers = function (req, res, next) {
|
||||
getFollow('account/followers', 'followers', req, res, next);
|
||||
followController.getFollowers = async function (req, res, next) {
|
||||
await getFollow('account/followers', 'followers', req, res, next);
|
||||
};
|
||||
|
||||
function getFollow(tpl, name, req, res, callback) {
|
||||
var userData;
|
||||
|
||||
var page = parseInt(req.query.page, 10) || 1;
|
||||
var resultsPerPage = 50;
|
||||
var start = Math.max(0, page - 1) * resultsPerPage;
|
||||
var stop = start + resultsPerPage - 1;
|
||||
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
accountHelpers.getUserDataByUserSlug(req.params.userslug, req.uid, next);
|
||||
},
|
||||
function (data, next) {
|
||||
userData = data;
|
||||
async function getFollow(tpl, name, req, res, next) {
|
||||
const userData = await accountHelpers.getUserDataByUserSlug(req.params.userslug, req.uid);
|
||||
if (!userData) {
|
||||
return callback();
|
||||
return next();
|
||||
}
|
||||
var method = name === 'following' ? 'getFollowing' : 'getFollowers';
|
||||
user[method](userData.uid, start, stop, next);
|
||||
},
|
||||
function (users) {
|
||||
userData.users = users;
|
||||
|
||||
const page = parseInt(req.query.page, 10) || 1;
|
||||
const resultsPerPage = 50;
|
||||
const start = Math.max(0, page - 1) * resultsPerPage;
|
||||
const stop = start + resultsPerPage - 1;
|
||||
|
||||
userData.title = '[[pages:' + tpl + ', ' + userData.username + ']]';
|
||||
var count = name === 'following' ? userData.followingCount : userData.followerCount;
|
||||
var pageCount = Math.ceil(count / resultsPerPage);
|
||||
|
||||
const method = name === 'following' ? 'getFollowing' : 'getFollowers';
|
||||
userData.users = await user[method](userData.uid, start, stop);
|
||||
|
||||
const count = name === 'following' ? userData.followingCount : userData.followerCount;
|
||||
const pageCount = Math.ceil(count / resultsPerPage);
|
||||
userData.pagination = pagination.create(page, pageCount);
|
||||
|
||||
userData.breadcrumbs = helpers.buildBreadcrumbs([{ text: userData.username, url: '/user/' + userData.userslug }, { text: '[[user:' + name + ']]' }]);
|
||||
|
||||
res.render(tpl, userData);
|
||||
},
|
||||
], callback);
|
||||
}
|
||||
|
@ -1,67 +1,54 @@
|
||||
'use strict';
|
||||
|
||||
var async = require('async');
|
||||
const db = require('../../database');
|
||||
const user = require('../../user');
|
||||
const helpers = require('../helpers');
|
||||
const accountHelpers = require('./helpers');
|
||||
const pagination = require('../../pagination');
|
||||
|
||||
var db = require('../../database');
|
||||
var user = require('../../user');
|
||||
var helpers = require('../helpers');
|
||||
var accountHelpers = require('./helpers');
|
||||
var pagination = require('../../pagination');
|
||||
const infoController = module.exports;
|
||||
|
||||
var infoController = module.exports;
|
||||
|
||||
infoController.get = function (req, res, callback) {
|
||||
var userData;
|
||||
var page = Math.max(1, req.query.page || 1);
|
||||
var itemsPerPage = 10;
|
||||
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
accountHelpers.getUserDataByUserSlug(req.params.userslug, req.uid, next);
|
||||
},
|
||||
function (_userData, next) {
|
||||
userData = _userData;
|
||||
infoController.get = async function (req, res, next) {
|
||||
const userData = await accountHelpers.getUserDataByUserSlug(req.params.userslug, req.uid);
|
||||
if (!userData) {
|
||||
return callback();
|
||||
}
|
||||
|
||||
var start = (page - 1) * itemsPerPage;
|
||||
var stop = start + itemsPerPage - 1;
|
||||
async.parallel({
|
||||
history: async.apply(user.getModerationHistory, userData.uid),
|
||||
sessions: async.apply(user.auth.getSessions, userData.uid, req.sessionID),
|
||||
usernames: async.apply(user.getHistory, 'user:' + userData.uid + ':usernames'),
|
||||
emails: async.apply(user.getHistory, 'user:' + userData.uid + ':emails'),
|
||||
notes: function (next) {
|
||||
if (!userData.isAdminOrGlobalModeratorOrModerator) {
|
||||
return setImmediate(next);
|
||||
return next();
|
||||
}
|
||||
async.parallel({
|
||||
notes: function (next) {
|
||||
user.getModerationNotes(userData.uid, start, stop, next);
|
||||
},
|
||||
count: function (next) {
|
||||
db.sortedSetCard('uid:' + userData.uid + ':moderation:notes', next);
|
||||
},
|
||||
}, next);
|
||||
},
|
||||
}, next);
|
||||
},
|
||||
function (data) {
|
||||
userData.history = data.history;
|
||||
userData.sessions = data.sessions;
|
||||
userData.usernames = data.usernames;
|
||||
userData.emails = data.emails;
|
||||
const page = Math.max(1, req.query.page || 1);
|
||||
const itemsPerPage = 10;
|
||||
const start = (page - 1) * itemsPerPage;
|
||||
const stop = start + itemsPerPage - 1;
|
||||
|
||||
const [history, sessions, usernames, emails, notes] = await Promise.all([
|
||||
user.getModerationHistory(userData.uid),
|
||||
user.auth.getSessions(userData.uid, req.sessionID),
|
||||
user.getHistory('user:' + userData.uid + ':usernames'),
|
||||
user.getHistory('user:' + userData.uid + ':emails'),
|
||||
getNotes(userData, start, stop),
|
||||
]);
|
||||
|
||||
userData.history = history;
|
||||
userData.sessions = sessions;
|
||||
userData.usernames = usernames;
|
||||
userData.emails = emails;
|
||||
|
||||
if (userData.isAdminOrGlobalModeratorOrModerator) {
|
||||
userData.moderationNotes = data.notes.notes;
|
||||
var pageCount = Math.ceil(data.notes.count / itemsPerPage);
|
||||
userData.moderationNotes = notes.notes;
|
||||
const pageCount = Math.ceil(notes.count / itemsPerPage);
|
||||
userData.pagination = pagination.create(page, pageCount, req.query);
|
||||
}
|
||||
userData.title = '[[pages:account/info]]';
|
||||
userData.breadcrumbs = helpers.buildBreadcrumbs([{ text: userData.username, url: '/user/' + userData.userslug }, { text: '[[user:account_info]]' }]);
|
||||
|
||||
res.render('account/info', userData);
|
||||
},
|
||||
], callback);
|
||||
};
|
||||
|
||||
async function getNotes(userData, start, stop) {
|
||||
if (!userData.isAdminOrGlobalModeratorOrModerator) {
|
||||
return;
|
||||
}
|
||||
const [notes, count] = await Promise.all([
|
||||
user.getModerationNotes(userData.uid, start, stop),
|
||||
db.sortedSetCard('uid:' + userData.uid + ':moderati;on:notes'),
|
||||
]);
|
||||
return { notes: notes, count: count };
|
||||
}
|
||||
|
Loading…
Reference in New Issue