Skip to content

Node.js based forum software built for the modern web

feat: async/await controllers/accounts

Baris Usaklia3541d887ba0
diff --git a/src/controllers/accounts/categories.js b/src/controllers/accounts/categories.js
index 1371435f49..f5137246d7 100644
--- a/src/controllers/accounts/categories.js
+++ b/src/controllers/accounts/categories.js
@@ -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 = async function (req, res, next) {
+ const userData = await accountHelpers.getUserDataByUserSlug(req.params.userslug, req.uid);
+ if (!userData) {
+ return next();
+ }
+ const [states, categoriesData] = await Promise.all([
+ user.getCategoryWatchState(userData.uid),
+ categories.buildForSelect(userData.uid, 'find'),
+ ]);
-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) {
- return callback();
- }
-
- 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) {
- 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;
- }
- });
- userData.categories = results.categories;
- userData.title = '[[pages:account/watched_categories, ' + userData.username + ']]';
- res.render('account/categories', userData);
- },
- ], callback);
+ categoriesData.forEach(function (category) {
+ if (category) {
+ 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 = categoriesData;
+ userData.title = '[[pages:account/watched_categories, ' + userData.username + ']]';
+ res.render('account/categories', userData);
};
diff --git a/src/controllers/accounts/chats.js b/src/controllers/accounts/chats.js
index 793b43a958..0bfad61b81 100644
--- a/src/controllers/accounts/chats.js
+++ b/src/controllers/accounts/chats.js
@@ -1,85 +1,62 @@
'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');
-var meta = require('../../meta');
-var user = require('../../user');
-var privileges = require('../../privileges');
-var helpers = require('../helpers');
+const chatsController = module.exports;
-var chatsController = module.exports;
-
-chatsController.get = function (req, res, callback) {
+chatsController.get = async function (req, res, next) {
if (meta.config.disableChat) {
- return callback();
+ return next();
}
- var uid;
- var recentChats;
+ const uid = await user.getUidByUserslug(req.params.userslug);
+ if (!uid) {
+ return next();
+ }
+ const canChat = await privileges.global.can('chat', req.uid);
+ if (!canChat) {
+ return next(new Error('[[error:no-privileges]]'));
+ }
+ const recentChats = await messaging.getRecentChats(req.uid, uid, 0, 19);
+ if (!recentChats) {
+ return next();
+ }
- async.waterfall([
- function (next) {
- user.getUidByUserslug(req.params.userslug, next);
- },
- function (_uid, next) {
- uid = _uid;
- if (!uid) {
- return callback();
- }
- privileges.global.can('chat', req.uid, next);
- },
- function (canChat, next) {
- if (!canChat) {
- return next(new Error('[[error:no-privileges]]'));
- }
- messaging.getRecentChats(req.uid, uid, 0, 19, next);
- },
- function (_recentChats, next) {
- recentChats = _recentChats;
- if (!recentChats) {
- return callback();
- }
- if (!req.params.roomid) {
- return res.render('chats', {
- rooms: recentChats.rooms,
- uid: uid,
- userslug: req.params.userslug,
- nextStart: recentChats.nextStart,
- allowed: true,
- title: '[[pages:chats]]',
- });
- }
- messaging.loadRoom(req.uid, { uid: uid, roomId: req.params.roomid }, next);
- },
- function (room) {
- if (!room) {
- return callback();
- }
- room.rooms = recentChats.rooms;
- room.nextStart = recentChats.nextStart;
- room.title = room.roomName || room.usernames || '[[pages:chats]]';
- room.uid = uid;
- room.userslug = req.params.userslug;
- res.render('chats', room);
- },
- ], callback);
+ if (!req.params.roomid) {
+ return res.render('chats', {
+ rooms: recentChats.rooms,
+ uid: uid,
+ userslug: req.params.userslug,
+ nextStart: recentChats.nextStart,
+ allowed: true,
+ title: '[[pages:chats]]',
+ });
+ }
+ const room = await messaging.loadRoom(req.uid, { uid: uid, roomId: req.params.roomid });
+ if (!room) {
+ return next();
+ }
+
+ room.rooms = recentChats.rooms;
+ room.nextStart = recentChats.nextStart;
+ room.title = room.roomName || room.usernames || '[[pages:chats]]';
+ room.uid = uid;
+ room.userslug = req.params.userslug;
+ res.render('chats', room);
};
-chatsController.redirectToChat = function (req, res, next) {
- var roomid = parseInt(req.params.roomid, 10);
+chatsController.redirectToChat = async function (req, res, next) {
if (!req.loggedIn) {
return next();
}
- async.waterfall([
- function (next) {
- user.getUserField(req.uid, 'userslug', next);
- },
- function (userslug, next) {
- if (!userslug) {
- return next();
- }
- helpers.redirect(res, '/user/' + userslug + '/chats' + (roomid ? '/' + roomid : ''));
- },
- ], next);
+ const userslug = await user.getUserField(req.uid, 'userslug');
+ if (!userslug) {
+ return next();
+ }
+ const roomid = parseInt(req.params.roomid, 10);
+ helpers.redirect(res, '/user/' + userslug + '/chats' + (roomid ? '/' + roomid : ''));
};
diff --git a/src/controllers/accounts/consent.js b/src/controllers/accounts/consent.js
index aca8b26df0..9038b2905a 100644
--- a/src/controllers/accounts/consent.js
+++ b/src/controllers/accounts/consent.js
@@ -1,46 +1,30 @@
'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');
-var meta = require('../../meta');
-var helpers = require('../helpers');
-var accountHelpers = require('./helpers');
+const consentController = module.exports;
-var consentController = module.exports;
-
-consentController.get = function (req, res, next) {
+consentController.get = async function (req, res, next) {
if (!meta.config.gdpr_enabled) {
- // GDPR disabled
return next();
}
- var userData;
-
- async.waterfall([
- function (next) {
- accountHelpers.getUserDataByUserSlug(req.params.userslug, req.uid, next);
- },
- function (_userData, next) {
- userData = _userData;
- if (!userData) {
- return next();
- }
-
- // 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.digest = {
- frequency: meta.config.dailyDigestFreq,
- enabled: meta.config.dailyDigestFreq !== 'off',
- };
+ const userData = await accountHelpers.getUserDataByUserSlug(req.params.userslug, req.uid);
+ if (!userData) {
+ return next();
+ }
+ const consented = await db.getObjectField('user:' + userData.uid, 'gdpr_consent');
+ userData.gdpr_consent = parseInt(consented, 10) === 1;
+ userData.digest = {
+ frequency: meta.config.dailyDigestFreq,
+ enabled: meta.config.dailyDigestFreq !== 'off',
+ };
- userData.title = '[[user:consent.title]]';
- userData.breadcrumbs = helpers.buildBreadcrumbs([{ text: userData.username, url: '/user/' + userData.userslug }, { text: '[[user:consent.title]]' }]);
+ userData.title = '[[user:consent.title]]';
+ userData.breadcrumbs = helpers.buildBreadcrumbs([{ text: userData.username, url: '/user/' + userData.userslug }, { text: '[[user:consent.title]]' }]);
- res.render('account/consent', userData);
- },
- ], next);
+ res.render('account/consent', userData);
};
diff --git a/src/controllers/accounts/follow.js b/src/controllers/accounts/follow.js
index 3c3e6a3338..65dedbe14f 100644
--- a/src/controllers/accounts/follow.js
+++ b/src/controllers/accounts/follow.js
@@ -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;
- if (!userData) {
- return callback();
- }
- var method = name === 'following' ? 'getFollowing' : 'getFollowers';
- user[method](userData.uid, start, stop, next);
- },
- function (users) {
- userData.users = users;
- userData.title = '[[pages:' + tpl + ', ' + userData.username + ']]';
- var count = name === 'following' ? userData.followingCount : userData.followerCount;
- var 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);
+async function getFollow(tpl, name, req, res, next) {
+ const userData = await accountHelpers.getUserDataByUserSlug(req.params.userslug, req.uid);
+ if (!userData) {
+ return next();
+ }
+
+ 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 + ']]';
+
+ 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);
}
diff --git a/src/controllers/accounts/groups.js b/src/controllers/accounts/groups.js
index fde4718eb4..fea0d19408 100644
--- a/src/controllers/accounts/groups.js
+++ b/src/controllers/accounts/groups.js
@@ -1,43 +1,25 @@
'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');
-var helpers = require('../helpers');
-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) {
- return callback();
- }
-
- groups.getUserGroups([userData.uid], next);
- },
- function (_groupsData, next) {
- groupsData = _groupsData[0];
- const groupNames = groupsData.filter(Boolean).map(group => group.name);
-
- groups.getMemberUsers(groupNames, 0, 3, next);
- },
- function (members) {
- groupsData.forEach(function (group, index) {
- group.members = members[index];
- });
- userData.groups = groupsData;
- userData.title = '[[pages:account/groups, ' + userData.username + ']]';
- userData.breadcrumbs = helpers.buildBreadcrumbs([{ text: userData.username, url: '/user/' + userData.userslug }, { text: '[[global:header.groups]]' }]);
- res.render('account/groups', userData);
- },
- ], callback);
+groupsController.get = async function (req, res, next) {
+ const userData = await accountHelpers.getUserDataByUserSlug(req.params.userslug, req.uid);
+ if (!userData) {
+ return next();
+ }
+ let groupsData = await groups.getUserGroups([userData.uid]);
+ groupsData = groupsData[0];
+ const groupNames = groupsData.filter(Boolean).map(group => group.name);
+ const members = await groups.getMemberUsers(groupNames, 0, 3);
+ groupsData.forEach(function (group, index) {
+ group.members = members[index];
+ });
+ userData.groups = groupsData;
+ userData.title = '[[pages:account/groups, ' + userData.username + ']]';
+ userData.breadcrumbs = helpers.buildBreadcrumbs([{ text: userData.username, url: '/user/' + userData.userslug }, { text: '[[global:header.groups]]' }]);
+ res.render('account/groups', userData);
};
diff --git a/src/controllers/accounts/info.js b/src/controllers/accounts/info.js
index cb3b6f1abf..00632ed426 100644
--- a/src/controllers/accounts/info.js
+++ b/src/controllers/accounts/info.js
@@ -1,67 +1,54 @@
'use strict';
-var async = require('async');
-
-var db = require('../../database');
-var user = require('../../user');
-var helpers = require('../helpers');
-var accountHelpers = require('./helpers');
-var pagination = require('../../pagination');
-
-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;
- 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);
- }
- 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;
-
- if (userData.isAdminOrGlobalModeratorOrModerator) {
- userData.moderationNotes = data.notes.notes;
- var pageCount = Math.ceil(data.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);
+const db = require('../../database');
+const user = require('../../user');
+const helpers = require('../helpers');
+const accountHelpers = require('./helpers');
+const pagination = require('../../pagination');
+
+const infoController = module.exports;
+
+infoController.get = async function (req, res, next) {
+ const userData = await accountHelpers.getUserDataByUserSlug(req.params.userslug, req.uid);
+ if (!userData) {
+ return next();
+ }
+ 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 = 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);
};
+
+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 };
+}