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);
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) { categoriesData.forEach(function (category) {
var userData; if (category) {
async.waterfall([ category.isIgnored = states[category.cid] === categories.watchStates.ignoring;
function (next) { category.isWatched = states[category.cid] === categories.watchStates.watching;
accountHelpers.getUserDataByUserSlug(req.params.userslug, req.uid, next); category.isNotWatched = states[category.cid] === categories.watchStates.notwatching;
}, }
function (_userData, next) { });
userData = _userData; userData.categories = categoriesData;
if (!userData) { userData.title = '[[pages:account/watched_categories, ' + userData.username + ']]';
return callback(); res.render('account/categories', userData);
}
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);
}; };

@ -1,85 +1,62 @@
'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; 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([ if (!req.params.roomid) {
function (next) { return res.render('chats', {
user.getUidByUserslug(req.params.userslug, next); rooms: recentChats.rooms,
}, uid: uid,
function (_uid, next) { userslug: req.params.userslug,
uid = _uid; nextStart: recentChats.nextStart,
if (!uid) { allowed: true,
return callback(); title: '[[pages:chats]]',
} });
privileges.global.can('chat', req.uid, next); }
}, const room = await messaging.loadRoom(req.uid, { uid: uid, roomId: req.params.roomid });
function (canChat, next) { if (!room) {
if (!canChat) { return next();
return next(new Error('[[error:no-privileges]]')); }
}
messaging.getRecentChats(req.uid, uid, 0, 19, next); room.rooms = recentChats.rooms;
}, room.nextStart = recentChats.nextStart;
function (_recentChats, next) { room.title = room.roomName || room.usernames || '[[pages:chats]]';
recentChats = _recentChats; room.uid = uid;
if (!recentChats) { room.userslug = req.params.userslug;
return callback(); res.render('chats', room);
}
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);
}; };
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) { if (!userslug) {
user.getUserField(req.uid, 'userslug', next); return next();
}, }
function (userslug, next) { const roomid = parseInt(req.params.roomid, 10);
if (!userslug) { helpers.redirect(res, '/user/' + userslug + '/chats' + (roomid ? '/' + roomid : ''));
return next();
}
helpers.redirect(res, '/user/' + userslug + '/chats' + (roomid ? '/' + roomid : ''));
},
], next);
}; };

@ -1,46 +1,30 @@
'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);
if (!userData) {
async.waterfall([ return next();
function (next) { }
accountHelpers.getUserDataByUserSlug(req.params.userslug, req.uid, next); const consented = await db.getObjectField('user:' + userData.uid, 'gdpr_consent');
}, userData.gdpr_consent = parseInt(consented, 10) === 1;
function (_userData, next) { userData.digest = {
userData = _userData; frequency: meta.config.dailyDigestFreq,
if (!userData) { enabled: meta.config.dailyDigestFreq !== 'off',
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',
};
userData.title = '[[user:consent.title]]'; userData.title = '[[user:consent.title]]';
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);
if (!userData) {
var page = parseInt(req.query.page, 10) || 1; return next();
var resultsPerPage = 50; }
var start = Math.max(0, page - 1) * resultsPerPage;
var stop = start + resultsPerPage - 1; const page = parseInt(req.query.page, 10) || 1;
const resultsPerPage = 50;
async.waterfall([ const start = Math.max(0, page - 1) * resultsPerPage;
function (next) { const stop = start + resultsPerPage - 1;
accountHelpers.getUserDataByUserSlug(req.params.userslug, req.uid, next);
}, userData.title = '[[pages:' + tpl + ', ' + userData.username + ']]';
function (data, next) {
userData = data; const method = name === 'following' ? 'getFollowing' : 'getFollowers';
if (!userData) { userData.users = await user[method](userData.uid, start, stop);
return callback();
} const count = name === 'following' ? userData.followingCount : userData.followerCount;
var method = name === 'following' ? 'getFollowing' : 'getFollowers'; const pageCount = Math.ceil(count / resultsPerPage);
user[method](userData.uid, start, stop, next); userData.pagination = pagination.create(page, pageCount);
},
function (users) { userData.breadcrumbs = helpers.buildBreadcrumbs([{ text: userData.username, url: '/user/' + userData.userslug }, { text: '[[user:' + name + ']]' }]);
userData.users = users;
userData.title = '[[pages:' + tpl + ', ' + userData.username + ']]'; res.render(tpl, userData);
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);
} }

@ -1,43 +1,25 @@
'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'); if (!userData) {
return next();
var groupsController = module.exports; }
let groupsData = await groups.getUserGroups([userData.uid]);
groupsController.get = function (req, res, callback) { groupsData = groupsData[0];
var userData; const groupNames = groupsData.filter(Boolean).map(group => group.name);
var groupsData; const members = await groups.getMemberUsers(groupNames, 0, 3);
async.waterfall([ groupsData.forEach(function (group, index) {
function (next) { group.members = members[index];
accountHelpers.getUserDataByUserSlug(req.params.userslug, req.uid, next); });
}, userData.groups = groupsData;
function (_userData, next) { userData.title = '[[pages:account/groups, ' + userData.username + ']]';
userData = _userData; userData.breadcrumbs = helpers.buildBreadcrumbs([{ text: userData.username, url: '/user/' + userData.userslug }, { text: '[[global:header.groups]]' }]);
if (!userData) { res.render('account/groups', 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);
}; };

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