From dc8d721c46c5aea0b232c619ba96f3969b5d01f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Thu, 3 Oct 2019 23:31:42 -0400 Subject: [PATCH] refactor: async/await --- src/user/admin.js | 14 ++++++------ src/user/auth.js | 57 ++++++++++++++++++++-------------------------- src/user/blocks.js | 9 ++++---- src/user/create.js | 15 ++++++------ src/user/data.js | 28 +++++++++++------------ src/user/delete.js | 28 +++++++++++------------ 6 files changed, 71 insertions(+), 80 deletions(-) diff --git a/src/user/admin.js b/src/user/admin.js index 356e8b0ce1..9880af3edd 100644 --- a/src/user/admin.js +++ b/src/user/admin.js @@ -1,18 +1,18 @@ 'use strict'; -var winston = require('winston'); -var validator = require('validator'); +const winston = require('winston'); +const validator = require('validator'); -var db = require('../database'); -var plugins = require('../plugins'); +const db = require('../database'); +const plugins = require('../plugins'); module.exports = function (User) { User.logIP = async function (uid, ip) { if (!(parseInt(uid, 10) > 0)) { return; } - var now = Date.now(); + const now = Date.now(); const bulk = [ ['uid:' + uid + ':ip', now, ip || 'Unknown'], ]; @@ -29,8 +29,8 @@ module.exports = function (User) { User.getUsersCSV = async function () { winston.verbose('[user/getUsersCSV] Compiling User CSV data'); - var csvContent = ''; - var uids = await db.getSortedSetRange('users:joindate', 0, -1); + let csvContent = ''; + const uids = await db.getSortedSetRange('users:joindate', 0, -1); const data = await plugins.fireHook('filter:user.csvFields', { fields: ['uid', 'email', 'username'] }); const usersData = await User.getUsersFields(uids, data.fields); usersData.forEach(function (user) { diff --git a/src/user/auth.js b/src/user/auth.js index 7696ba4580..5cbd1a2edd 100644 --- a/src/user/auth.js +++ b/src/user/auth.js @@ -1,15 +1,14 @@ 'use strict'; -var async = require('async'); -var winston = require('winston'); -var validator = require('validator'); +const winston = require('winston'); +const validator = require('validator'); const util = require('util'); const _ = require('lodash'); -var db = require('../database'); -var meta = require('../meta'); -var events = require('../events'); -var batch = require('../batch'); -var utils = require('../utils'); +const db = require('../database'); +const meta = require('../meta'); +const events = require('../events'); +const batch = require('../batch'); +const utils = require('../utils'); module.exports = function (User) { User.auth = {}; @@ -28,7 +27,7 @@ module.exports = function (User) { } // Lock out the account await db.set('lockout:' + uid, ''); - var duration = 1000 * 60 * meta.config.lockoutDuration; + const duration = 1000 * 60 * meta.config.lockoutDuration; await db.delete('loginAttempts:' + uid); await db.pexpire('lockout:' + uid, duration); @@ -44,7 +43,7 @@ module.exports = function (User) { if (!(parseInt(uid, 10) > 0)) { return; } - var _token = await db.getObjectField('user:' + uid, 'rss_token'); + const _token = await db.getObjectField('user:' + uid, 'rss_token'); const token = _token || utils.generateUUID(); if (!_token) { await User.setUserField(uid, 'rss_token', token); @@ -63,9 +62,12 @@ module.exports = function (User) { ]); }; + const getSessionFromStore = util.promisify((sid, callback) => db.sessionStore.get(sid, (err, sessObj) => callback(err, sessObj || null))); + const sessionStoreDestroy = util.promisify((sid, callback) => db.sessionStore.destroy(sid, err => callback(err))); + User.auth.getSessions = async function (uid, curSessionId) { const sids = await db.getSortedSetRevRange('uid:' + uid + ':sessions', 0, 19); - let sessions = await async.map(sids, db.sessionStore.get.bind(db.sessionStore)); + let sessions = await Promise.all(sids.map(sid => getSessionFromStore(sid))); sessions.forEach(function (sessionObj, idx) { if (sessionObj && sessionObj.meta) { sessionObj.meta.current = curSessionId === sids[idx]; @@ -73,11 +75,9 @@ module.exports = function (User) { }); // Revoke any sessions that have expired, return filtered list - var expiredSids = []; - var expired; - + const expiredSids = []; sessions = sessions.filter(function (sessionObj, idx) { - expired = !sessionObj || !sessionObj.hasOwnProperty('passport') || + const expired = !sessionObj || !sessionObj.hasOwnProperty('passport') || !sessionObj.passport.hasOwnProperty('user') || parseInt(sessionObj.passport.user, 10) !== parseInt(uid, 10); @@ -106,21 +106,15 @@ module.exports = function (User) { await db.sortedSetAdd('uid:' + uid + ':sessions', Date.now(), sessionId); }; - const getSessionFromStore = util.promisify(function (sessionId, callback) { - db.sessionStore.get(sessionId, function (err, sessionObj) { - callback(err, sessionObj || null); - }); - }); - User.auth.revokeSession = async function (sessionId, uid) { winston.verbose('[user.auth] Revoking session ' + sessionId + ' for user ' + uid); const sessionObj = await getSessionFromStore(sessionId); if (sessionObj && sessionObj.meta && sessionObj.meta.uuid) { await db.deleteObjectField('uid:' + uid + ':sessionUUID:sessionId', sessionObj.meta.uuid); } - await async.parallel([ - async.apply(db.sortedSetRemove, 'uid:' + uid + ':sessions', sessionId), - async.apply(db.sessionStore.destroy.bind(db.sessionStore), sessionId), + await Promise.all([ + db.sortedSetRemove('uid:' + uid + ':sessions', sessionId), + sessionStoreDestroy(sessionId), ]); }; @@ -139,14 +133,13 @@ module.exports = function (User) { const sessionKeys = uids.map(uid => 'uid:' + uid + ':sessions'); const sessionUUIDKeys = uids.map(uid => 'uid:' + uid + ':sessionUUID:sessionId'); const sids = _.flatten(await db.getSortedSetRange(sessionKeys, 0, -1)); - await async.parallel([ - async.apply(db.deleteAll, sessionKeys.concat(sessionUUIDKeys)), - function (next) { - async.each(sids, function (sid, next) { - db.sessionStore.destroy(sid, next); - }, next); - }, - ]); + + const promises = [ + db.deleteAll(sessionKeys.concat(sessionUUIDKeys)), + ...sids.map(sid => sessionStoreDestroy(sid)), + ]; + console.log('epic', promises); + await Promise.all(promises); }, { batch: 1000 }); }; }; diff --git a/src/user/blocks.js b/src/user/blocks.js index 2c41e61192..0065fd52e4 100644 --- a/src/user/blocks.js +++ b/src/user/blocks.js @@ -1,11 +1,10 @@ 'use strict'; -var async = require('async'); -var LRU = require('lru-cache'); +const async = require('async'); +const LRU = require('lru-cache'); - -var db = require('../database'); -var pubsub = require('../pubsub'); +const db = require('../database'); +const pubsub = require('../pubsub'); module.exports = function (User) { User.blocks = { diff --git a/src/user/create.js b/src/user/create.js index 7312ac1408..4137facaf0 100644 --- a/src/user/create.js +++ b/src/user/create.js @@ -1,12 +1,11 @@ 'use strict'; -var zxcvbn = require('zxcvbn'); -var db = require('../database'); -var utils = require('../utils'); -var plugins = require('../plugins'); -var groups = require('../groups'); -var meta = require('../meta'); - +const zxcvbn = require('zxcvbn'); +const db = require('../database'); +const utils = require('../utils'); +const plugins = require('../plugins'); +const groups = require('../groups'); +const meta = require('../meta'); module.exports = function (User) { User.create = async function (data) { @@ -146,7 +145,7 @@ module.exports = function (User) { throw new Error('[[error:password-too-long]]'); } - var strength = zxcvbn(password); + const strength = zxcvbn(password); if (strength.score < minStrength) { throw new Error('[[user:weak_password]]'); } diff --git a/src/user/data.js b/src/user/data.js index b997249f55..b3cf2d0444 100644 --- a/src/user/data.js +++ b/src/user/data.js @@ -1,14 +1,13 @@ 'use strict'; -var async = require('async'); -var validator = require('validator'); -var nconf = require('nconf'); -var _ = require('lodash'); +const validator = require('validator'); +const nconf = require('nconf'); +const _ = require('lodash'); -var db = require('../database'); -var meta = require('../meta'); -var plugins = require('../plugins'); -var utils = require('../utils'); +const db = require('../database'); +const meta = require('../meta'); +const plugins = require('../plugins'); +const utils = require('../utils'); const intFields = [ 'uid', 'postcount', 'topiccount', 'reputation', 'profileviews', @@ -17,13 +16,13 @@ const intFields = [ ]; module.exports = function (User) { - var iconBackgrounds = [ + const iconBackgrounds = [ '#f44336', '#e91e63', '#9c27b0', '#673ab7', '#3f51b5', '#2196f3', '#009688', '#1b5e20', '#33691e', '#827717', '#e65100', '#ff5722', '#795548', '#607d8b', ]; - var fieldWhitelist = [ + const fieldWhitelist = [ 'uid', 'username', 'userslug', 'email', 'email:confirmed', 'joindate', 'lastonline', 'picture', 'fullname', 'location', 'birthday', 'website', 'aboutme', 'signature', 'uploadedpicture', 'profileviews', 'reputation', @@ -130,7 +129,7 @@ module.exports = function (User) { }; async function modifyUserData(users, requestedFields, fieldsToRemove) { - users = await async.map(users, async function (user) { + users = await Promise.all(users.map(async function (user) { if (!user) { return user; } @@ -173,7 +172,7 @@ module.exports = function (User) { user.status = User.getStatus(user); } - for (var i = 0; i < fieldsToRemove.length; i += 1) { + for (let i = 0; i < fieldsToRemove.length; i += 1) { user[fieldsToRemove[i]] = undefined; } @@ -204,7 +203,8 @@ module.exports = function (User) { } } return user; - }); + })); + return await plugins.fireHook('filter:users.get', users); } @@ -244,7 +244,7 @@ module.exports = function (User) { User.setUserFields = async function (uid, data) { await db.setObject('user:' + uid, data); - for (var field in data) { + for (const field in data) { if (data.hasOwnProperty(field)) { plugins.fireHook('action:user.set', { uid: uid, field: field, value: data[field], type: 'set' }); } diff --git a/src/user/delete.js b/src/user/delete.js index 586e170bf3..156dafdb1e 100644 --- a/src/user/delete.js +++ b/src/user/delete.js @@ -1,21 +1,21 @@ 'use strict'; -var async = require('async'); -var _ = require('lodash'); -var path = require('path'); -var nconf = require('nconf'); - -var db = require('../database'); -var posts = require('../posts'); -var topics = require('../topics'); -var groups = require('../groups'); -var messaging = require('../messaging'); -var plugins = require('../plugins'); -var batch = require('../batch'); -var file = require('../file'); +const async = require('async'); +const _ = require('lodash'); +const path = require('path'); +const nconf = require('nconf'); + +const db = require('../database'); +const posts = require('../posts'); +const topics = require('../topics'); +const groups = require('../groups'); +const messaging = require('../messaging'); +const plugins = require('../plugins'); +const batch = require('../batch'); +const file = require('../file'); module.exports = function (User) { - var deletesInProgress = {}; + const deletesInProgress = {}; User.delete = async function (callerUid, uid) { if (parseInt(uid, 10) <= 0) {