feat: async/await controllers/accounts

v1.18.x
Baris Usakli 6 years ago
parent 014e31533d
commit a3541d887b

@ -1,45 +1,29 @@
'use strict'; 'use strict';
var async = require('async'); const user = require('../../user');
const categories = require('../../categories');
const accountHelpers = require('./helpers');
var user = require('../../user'); const categoriesController = module.exports;
var categories = require('../../categories');
var accountHelpers = require('./helpers');
var categoriesController = module.exports; categoriesController.get = async function (req, res, next) {
const userData = await accountHelpers.getUserDataByUserSlug(req.params.userslug, req.uid);
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;
if (!userData) { if (!userData) {
return callback(); return next();
} }
const [states, categoriesData] = await Promise.all([
user.getCategoryWatchState(userData.uid),
categories.buildForSelect(userData.uid, 'find'),
]);
async.parallel({ categoriesData.forEach(function (category) {
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) {
if (category) { if (category) {
category.isIgnored = results.states[category.cid] === categories.watchStates.ignoring; category.isIgnored = states[category.cid] === categories.watchStates.ignoring;
category.isWatched = results.states[category.cid] === categories.watchStates.watching; category.isWatched = states[category.cid] === categories.watchStates.watching;
category.isNotWatched = results.states[category.cid] === categories.watchStates.notwatching; category.isNotWatched = states[category.cid] === categories.watchStates.notwatching;
} }
}); });
userData.categories = results.categories; userData.categories = categoriesData;
userData.title = '[[pages:account/watched_categories, ' + userData.username + ']]'; userData.title = '[[pages:account/watched_categories, ' + userData.username + ']]';
res.render('account/categories', userData); res.render('account/categories', userData);
},
], callback);
}; };

@ -1,45 +1,31 @@
'use strict'; 'use strict';
var async = require('async'); const messaging = require('../../messaging');
const meta = require('../../meta');
const user = require('../../user');
const privileges = require('../../privileges');
const helpers = require('../helpers');
var messaging = require('../../messaging'); const chatsController = module.exports;
var meta = require('../../meta');
var user = require('../../user');
var privileges = require('../../privileges');
var helpers = require('../helpers');
var chatsController = module.exports; chatsController.get = async function (req, res, next) {
chatsController.get = function (req, res, callback) {
if (meta.config.disableChat) { if (meta.config.disableChat) {
return callback(); return next();
} }
var uid; const uid = await user.getUidByUserslug(req.params.userslug);
var recentChats;
async.waterfall([
function (next) {
user.getUidByUserslug(req.params.userslug, next);
},
function (_uid, next) {
uid = _uid;
if (!uid) { if (!uid) {
return callback(); return next();
} }
privileges.global.can('chat', req.uid, next); const canChat = await privileges.global.can('chat', req.uid);
},
function (canChat, next) {
if (!canChat) { if (!canChat) {
return next(new Error('[[error:no-privileges]]')); return next(new Error('[[error:no-privileges]]'));
} }
messaging.getRecentChats(req.uid, uid, 0, 19, next); const recentChats = await messaging.getRecentChats(req.uid, uid, 0, 19);
},
function (_recentChats, next) {
recentChats = _recentChats;
if (!recentChats) { if (!recentChats) {
return callback(); return next();
} }
if (!req.params.roomid) { if (!req.params.roomid) {
return res.render('chats', { return res.render('chats', {
rooms: recentChats.rooms, rooms: recentChats.rooms,
@ -50,36 +36,27 @@ chatsController.get = function (req, res, callback) {
title: '[[pages:chats]]', title: '[[pages:chats]]',
}); });
} }
messaging.loadRoom(req.uid, { uid: uid, roomId: req.params.roomid }, next); const room = await messaging.loadRoom(req.uid, { uid: uid, roomId: req.params.roomid });
},
function (room) {
if (!room) { if (!room) {
return callback(); return next();
} }
room.rooms = recentChats.rooms; room.rooms = recentChats.rooms;
room.nextStart = recentChats.nextStart; room.nextStart = recentChats.nextStart;
room.title = room.roomName || room.usernames || '[[pages:chats]]'; room.title = room.roomName || room.usernames || '[[pages:chats]]';
room.uid = uid; room.uid = uid;
room.userslug = req.params.userslug; room.userslug = req.params.userslug;
res.render('chats', room); res.render('chats', room);
},
], callback);
}; };
chatsController.redirectToChat = function (req, res, next) { chatsController.redirectToChat = async function (req, res, next) {
var roomid = parseInt(req.params.roomid, 10);
if (!req.loggedIn) { if (!req.loggedIn) {
return next(); return next();
} }
async.waterfall([ const userslug = await user.getUserField(req.uid, 'userslug');
function (next) {
user.getUserField(req.uid, 'userslug', next);
},
function (userslug, next) {
if (!userslug) { if (!userslug) {
return next(); return next();
} }
const roomid = parseInt(req.params.roomid, 10);
helpers.redirect(res, '/user/' + userslug + '/chats' + (roomid ? '/' + roomid : '')); helpers.redirect(res, '/user/' + userslug + '/chats' + (roomid ? '/' + roomid : ''));
},
], next);
}; };

@ -1,36 +1,22 @@
'use strict'; 'use strict';
var async = require('async'); const db = require('../../database');
const meta = require('../../meta');
const helpers = require('../helpers');
const accountHelpers = require('./helpers');
var db = require('../../database'); const consentController = module.exports;
var meta = require('../../meta');
var helpers = require('../helpers');
var accountHelpers = require('./helpers');
var consentController = module.exports; consentController.get = async function (req, res, next) {
consentController.get = function (req, res, next) {
if (!meta.config.gdpr_enabled) { if (!meta.config.gdpr_enabled) {
// GDPR disabled
return next(); return next();
} }
var userData; const userData = await accountHelpers.getUserDataByUserSlug(req.params.userslug, req.uid);
async.waterfall([
function (next) {
accountHelpers.getUserDataByUserSlug(req.params.userslug, req.uid, next);
},
function (_userData, next) {
userData = _userData;
if (!userData) { if (!userData) {
return next(); return next();
} }
const consented = await db.getObjectField('user:' + userData.uid, 'gdpr_consent');
// Direct database call is used here because `gdpr_consent` is a protected user field and is automatically scrubbed from standard user data retrieval calls
db.getObjectField('user:' + userData.uid, 'gdpr_consent', next);
},
function (consented) {
userData.gdpr_consent = parseInt(consented, 10) === 1; userData.gdpr_consent = parseInt(consented, 10) === 1;
userData.digest = { userData.digest = {
frequency: meta.config.dailyDigestFreq, frequency: meta.config.dailyDigestFreq,
@ -41,6 +27,4 @@ consentController.get = function (req, res, next) {
userData.breadcrumbs = helpers.buildBreadcrumbs([{ text: userData.username, url: '/user/' + userData.userslug }, { text: '[[user:consent.title]]' }]); userData.breadcrumbs = helpers.buildBreadcrumbs([{ text: userData.username, url: '/user/' + userData.userslug }, { text: '[[user:consent.title]]' }]);
res.render('account/consent', userData); res.render('account/consent', userData);
},
], next);
}; };

@ -1,51 +1,41 @@
'use strict'; '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'); const followController = module.exports;
var helpers = require('../helpers');
var accountHelpers = require('./helpers');
var pagination = require('../../pagination');
var followController = module.exports; followController.getFollowing = async function (req, res, next) {
await getFollow('account/following', 'following', req, res, next);
followController.getFollowing = function (req, res, next) {
getFollow('account/following', 'following', req, res, next);
}; };
followController.getFollowers = function (req, res, next) { followController.getFollowers = async function (req, res, next) {
getFollow('account/followers', 'followers', req, res, next); await getFollow('account/followers', 'followers', req, res, next);
}; };
function getFollow(tpl, name, req, res, callback) { async function getFollow(tpl, name, req, res, next) {
var userData; const userData = await accountHelpers.getUserDataByUserSlug(req.params.userslug, req.uid);
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;
if (!userData) { if (!userData) {
return callback(); return next();
} }
var method = name === 'following' ? 'getFollowing' : 'getFollowers';
user[method](userData.uid, start, stop, next); const page = parseInt(req.query.page, 10) || 1;
}, const resultsPerPage = 50;
function (users) { const start = Math.max(0, page - 1) * resultsPerPage;
userData.users = users; const stop = start + resultsPerPage - 1;
userData.title = '[[pages:' + tpl + ', ' + userData.username + ']]'; 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.pagination = pagination.create(page, pageCount);
userData.breadcrumbs = helpers.buildBreadcrumbs([{ text: userData.username, url: '/user/' + userData.userslug }, { text: '[[user:' + name + ']]' }]); userData.breadcrumbs = helpers.buildBreadcrumbs([{ text: userData.username, url: '/user/' + userData.userslug }, { text: '[[user:' + name + ']]' }]);
res.render(tpl, userData); res.render(tpl, userData);
},
], callback);
} }

@ -1,36 +1,20 @@
'use strict'; 'use strict';
const groups = require('../../groups');
const helpers = require('../helpers');
const accountHelpers = require('./helpers');
var async = require('async'); const groupsController = module.exports;
var groups = require('../../groups'); groupsController.get = async function (req, res, next) {
var helpers = require('../helpers'); const userData = await accountHelpers.getUserDataByUserSlug(req.params.userslug, req.uid);
var accountHelpers = require('./helpers');
var groupsController = module.exports;
groupsController.get = function (req, res, callback) {
var userData;
var groupsData;
async.waterfall([
function (next) {
accountHelpers.getUserDataByUserSlug(req.params.userslug, req.uid, next);
},
function (_userData, next) {
userData = _userData;
if (!userData) { if (!userData) {
return callback(); return next();
} }
let groupsData = await groups.getUserGroups([userData.uid]);
groups.getUserGroups([userData.uid], next); groupsData = groupsData[0];
},
function (_groupsData, next) {
groupsData = _groupsData[0];
const groupNames = groupsData.filter(Boolean).map(group => group.name); const groupNames = groupsData.filter(Boolean).map(group => group.name);
const members = await groups.getMemberUsers(groupNames, 0, 3);
groups.getMemberUsers(groupNames, 0, 3, next);
},
function (members) {
groupsData.forEach(function (group, index) { groupsData.forEach(function (group, index) {
group.members = members[index]; group.members = members[index];
}); });
@ -38,6 +22,4 @@ groupsController.get = function (req, res, callback) {
userData.title = '[[pages:account/groups, ' + userData.username + ']]'; userData.title = '[[pages:account/groups, ' + userData.username + ']]';
userData.breadcrumbs = helpers.buildBreadcrumbs([{ text: userData.username, url: '/user/' + userData.userslug }, { text: '[[global:header.groups]]' }]); userData.breadcrumbs = helpers.buildBreadcrumbs([{ text: userData.username, url: '/user/' + userData.userslug }, { text: '[[global:header.groups]]' }]);
res.render('account/groups', userData); res.render('account/groups', userData);
},
], callback);
}; };

@ -1,67 +1,54 @@
'use strict'; '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'); const infoController = module.exports;
var user = require('../../user');
var helpers = require('../helpers');
var accountHelpers = require('./helpers');
var pagination = require('../../pagination');
var infoController = module.exports; infoController.get = async function (req, res, next) {
const userData = await accountHelpers.getUserDataByUserSlug(req.params.userslug, req.uid);
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;
if (!userData) { if (!userData) {
return callback(); return next();
}
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);
} }
async.parallel({ const page = Math.max(1, req.query.page || 1);
notes: function (next) { const itemsPerPage = 10;
user.getModerationNotes(userData.uid, start, stop, next); const start = (page - 1) * itemsPerPage;
}, const stop = start + itemsPerPage - 1;
count: function (next) {
db.sortedSetCard('uid:' + userData.uid + ':moderation:notes', next); const [history, sessions, usernames, emails, notes] = await Promise.all([
}, user.getModerationHistory(userData.uid),
}, next); user.auth.getSessions(userData.uid, req.sessionID),
}, user.getHistory('user:' + userData.uid + ':usernames'),
}, next); user.getHistory('user:' + userData.uid + ':emails'),
}, getNotes(userData, start, stop),
function (data) { ]);
userData.history = data.history;
userData.sessions = data.sessions; userData.history = history;
userData.usernames = data.usernames; userData.sessions = sessions;
userData.emails = data.emails; userData.usernames = usernames;
userData.emails = emails;
if (userData.isAdminOrGlobalModeratorOrModerator) { if (userData.isAdminOrGlobalModeratorOrModerator) {
userData.moderationNotes = data.notes.notes; userData.moderationNotes = notes.notes;
var pageCount = Math.ceil(data.notes.count / itemsPerPage); const pageCount = Math.ceil(notes.count / itemsPerPage);
userData.pagination = pagination.create(page, pageCount, req.query); userData.pagination = pagination.create(page, pageCount, req.query);
} }
userData.title = '[[pages:account/info]]'; userData.title = '[[pages:account/info]]';
userData.breadcrumbs = helpers.buildBreadcrumbs([{ text: userData.username, url: '/user/' + userData.userslug }, { text: '[[user:account_info]]' }]); userData.breadcrumbs = helpers.buildBreadcrumbs([{ text: userData.username, url: '/user/' + userData.userslug }, { text: '[[user:account_info]]' }]);
res.render('account/info', userData); 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…
Cancel
Save