refactor: async/await

v1.18.x
Barış Soner Uşaklı 5 years ago
parent 1f0c6f3d60
commit dc8d721c46

@ -1,18 +1,18 @@
'use strict'; 'use strict';
var winston = require('winston'); const winston = require('winston');
var validator = require('validator'); const validator = require('validator');
var db = require('../database'); const db = require('../database');
var plugins = require('../plugins'); const plugins = require('../plugins');
module.exports = function (User) { module.exports = function (User) {
User.logIP = async function (uid, ip) { User.logIP = async function (uid, ip) {
if (!(parseInt(uid, 10) > 0)) { if (!(parseInt(uid, 10) > 0)) {
return; return;
} }
var now = Date.now(); const now = Date.now();
const bulk = [ const bulk = [
['uid:' + uid + ':ip', now, ip || 'Unknown'], ['uid:' + uid + ':ip', now, ip || 'Unknown'],
]; ];
@ -29,8 +29,8 @@ module.exports = function (User) {
User.getUsersCSV = async function () { User.getUsersCSV = async function () {
winston.verbose('[user/getUsersCSV] Compiling User CSV data'); winston.verbose('[user/getUsersCSV] Compiling User CSV data');
var csvContent = ''; let csvContent = '';
var uids = await db.getSortedSetRange('users:joindate', 0, -1); const uids = await db.getSortedSetRange('users:joindate', 0, -1);
const data = await plugins.fireHook('filter:user.csvFields', { fields: ['uid', 'email', 'username'] }); const data = await plugins.fireHook('filter:user.csvFields', { fields: ['uid', 'email', 'username'] });
const usersData = await User.getUsersFields(uids, data.fields); const usersData = await User.getUsersFields(uids, data.fields);
usersData.forEach(function (user) { usersData.forEach(function (user) {

@ -1,15 +1,14 @@
'use strict'; 'use strict';
var async = require('async'); const winston = require('winston');
var winston = require('winston'); const validator = require('validator');
var validator = require('validator');
const util = require('util'); const util = require('util');
const _ = require('lodash'); const _ = require('lodash');
var db = require('../database'); const db = require('../database');
var meta = require('../meta'); const meta = require('../meta');
var events = require('../events'); const events = require('../events');
var batch = require('../batch'); const batch = require('../batch');
var utils = require('../utils'); const utils = require('../utils');
module.exports = function (User) { module.exports = function (User) {
User.auth = {}; User.auth = {};
@ -28,7 +27,7 @@ module.exports = function (User) {
} }
// Lock out the account // Lock out the account
await db.set('lockout:' + uid, ''); 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.delete('loginAttempts:' + uid);
await db.pexpire('lockout:' + uid, duration); await db.pexpire('lockout:' + uid, duration);
@ -44,7 +43,7 @@ module.exports = function (User) {
if (!(parseInt(uid, 10) > 0)) { if (!(parseInt(uid, 10) > 0)) {
return; return;
} }
var _token = await db.getObjectField('user:' + uid, 'rss_token'); const _token = await db.getObjectField('user:' + uid, 'rss_token');
const token = _token || utils.generateUUID(); const token = _token || utils.generateUUID();
if (!_token) { if (!_token) {
await User.setUserField(uid, 'rss_token', 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) { User.auth.getSessions = async function (uid, curSessionId) {
const sids = await db.getSortedSetRevRange('uid:' + uid + ':sessions', 0, 19); 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) { sessions.forEach(function (sessionObj, idx) {
if (sessionObj && sessionObj.meta) { if (sessionObj && sessionObj.meta) {
sessionObj.meta.current = curSessionId === sids[idx]; sessionObj.meta.current = curSessionId === sids[idx];
@ -73,11 +75,9 @@ module.exports = function (User) {
}); });
// Revoke any sessions that have expired, return filtered list // Revoke any sessions that have expired, return filtered list
var expiredSids = []; const expiredSids = [];
var expired;
sessions = sessions.filter(function (sessionObj, idx) { sessions = sessions.filter(function (sessionObj, idx) {
expired = !sessionObj || !sessionObj.hasOwnProperty('passport') || const expired = !sessionObj || !sessionObj.hasOwnProperty('passport') ||
!sessionObj.passport.hasOwnProperty('user') || !sessionObj.passport.hasOwnProperty('user') ||
parseInt(sessionObj.passport.user, 10) !== parseInt(uid, 10); 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); 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) { User.auth.revokeSession = async function (sessionId, uid) {
winston.verbose('[user.auth] Revoking session ' + sessionId + ' for user ' + uid); winston.verbose('[user.auth] Revoking session ' + sessionId + ' for user ' + uid);
const sessionObj = await getSessionFromStore(sessionId); const sessionObj = await getSessionFromStore(sessionId);
if (sessionObj && sessionObj.meta && sessionObj.meta.uuid) { if (sessionObj && sessionObj.meta && sessionObj.meta.uuid) {
await db.deleteObjectField('uid:' + uid + ':sessionUUID:sessionId', sessionObj.meta.uuid); await db.deleteObjectField('uid:' + uid + ':sessionUUID:sessionId', sessionObj.meta.uuid);
} }
await async.parallel([ await Promise.all([
async.apply(db.sortedSetRemove, 'uid:' + uid + ':sessions', sessionId), db.sortedSetRemove('uid:' + uid + ':sessions', sessionId),
async.apply(db.sessionStore.destroy.bind(db.sessionStore), sessionId), sessionStoreDestroy(sessionId),
]); ]);
}; };
@ -139,14 +133,13 @@ module.exports = function (User) {
const sessionKeys = uids.map(uid => 'uid:' + uid + ':sessions'); const sessionKeys = uids.map(uid => 'uid:' + uid + ':sessions');
const sessionUUIDKeys = uids.map(uid => 'uid:' + uid + ':sessionUUID:sessionId'); const sessionUUIDKeys = uids.map(uid => 'uid:' + uid + ':sessionUUID:sessionId');
const sids = _.flatten(await db.getSortedSetRange(sessionKeys, 0, -1)); const sids = _.flatten(await db.getSortedSetRange(sessionKeys, 0, -1));
await async.parallel([
async.apply(db.deleteAll, sessionKeys.concat(sessionUUIDKeys)), const promises = [
function (next) { db.deleteAll(sessionKeys.concat(sessionUUIDKeys)),
async.each(sids, function (sid, next) { ...sids.map(sid => sessionStoreDestroy(sid)),
db.sessionStore.destroy(sid, next); ];
}, next); console.log('epic', promises);
}, await Promise.all(promises);
]);
}, { batch: 1000 }); }, { batch: 1000 });
}; };
}; };

@ -1,11 +1,10 @@
'use strict'; 'use strict';
var async = require('async'); const async = require('async');
var LRU = require('lru-cache'); const LRU = require('lru-cache');
const db = require('../database');
var db = require('../database'); const pubsub = require('../pubsub');
var pubsub = require('../pubsub');
module.exports = function (User) { module.exports = function (User) {
User.blocks = { User.blocks = {

@ -1,12 +1,11 @@
'use strict'; 'use strict';
var zxcvbn = require('zxcvbn'); const zxcvbn = require('zxcvbn');
var db = require('../database'); const db = require('../database');
var utils = require('../utils'); const utils = require('../utils');
var plugins = require('../plugins'); const plugins = require('../plugins');
var groups = require('../groups'); const groups = require('../groups');
var meta = require('../meta'); const meta = require('../meta');
module.exports = function (User) { module.exports = function (User) {
User.create = async function (data) { User.create = async function (data) {
@ -146,7 +145,7 @@ module.exports = function (User) {
throw new Error('[[error:password-too-long]]'); throw new Error('[[error:password-too-long]]');
} }
var strength = zxcvbn(password); const strength = zxcvbn(password);
if (strength.score < minStrength) { if (strength.score < minStrength) {
throw new Error('[[user:weak_password]]'); throw new Error('[[user:weak_password]]');
} }

@ -1,14 +1,13 @@
'use strict'; 'use strict';
var async = require('async'); const validator = require('validator');
var validator = require('validator'); const nconf = require('nconf');
var nconf = require('nconf'); const _ = require('lodash');
var _ = require('lodash');
var db = require('../database'); const db = require('../database');
var meta = require('../meta'); const meta = require('../meta');
var plugins = require('../plugins'); const plugins = require('../plugins');
var utils = require('../utils'); const utils = require('../utils');
const intFields = [ const intFields = [
'uid', 'postcount', 'topiccount', 'reputation', 'profileviews', 'uid', 'postcount', 'topiccount', 'reputation', 'profileviews',
@ -17,13 +16,13 @@ const intFields = [
]; ];
module.exports = function (User) { module.exports = function (User) {
var iconBackgrounds = [ const iconBackgrounds = [
'#f44336', '#e91e63', '#9c27b0', '#673ab7', '#3f51b5', '#2196f3', '#f44336', '#e91e63', '#9c27b0', '#673ab7', '#3f51b5', '#2196f3',
'#009688', '#1b5e20', '#33691e', '#827717', '#e65100', '#ff5722', '#009688', '#1b5e20', '#33691e', '#827717', '#e65100', '#ff5722',
'#795548', '#607d8b', '#795548', '#607d8b',
]; ];
var fieldWhitelist = [ const fieldWhitelist = [
'uid', 'username', 'userslug', 'email', 'email:confirmed', 'joindate', 'uid', 'username', 'userslug', 'email', 'email:confirmed', 'joindate',
'lastonline', 'picture', 'fullname', 'location', 'birthday', 'website', 'lastonline', 'picture', 'fullname', 'location', 'birthday', 'website',
'aboutme', 'signature', 'uploadedpicture', 'profileviews', 'reputation', 'aboutme', 'signature', 'uploadedpicture', 'profileviews', 'reputation',
@ -130,7 +129,7 @@ module.exports = function (User) {
}; };
async function modifyUserData(users, requestedFields, fieldsToRemove) { 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) { if (!user) {
return user; return user;
} }
@ -173,7 +172,7 @@ module.exports = function (User) {
user.status = User.getStatus(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; user[fieldsToRemove[i]] = undefined;
} }
@ -204,7 +203,8 @@ module.exports = function (User) {
} }
} }
return user; return user;
}); }));
return await plugins.fireHook('filter:users.get', users); return await plugins.fireHook('filter:users.get', users);
} }
@ -244,7 +244,7 @@ module.exports = function (User) {
User.setUserFields = async function (uid, data) { User.setUserFields = async function (uid, data) {
await db.setObject('user:' + uid, data); await db.setObject('user:' + uid, data);
for (var field in data) { for (const field in data) {
if (data.hasOwnProperty(field)) { if (data.hasOwnProperty(field)) {
plugins.fireHook('action:user.set', { uid: uid, field: field, value: data[field], type: 'set' }); plugins.fireHook('action:user.set', { uid: uid, field: field, value: data[field], type: 'set' });
} }

@ -1,21 +1,21 @@
'use strict'; 'use strict';
var async = require('async'); const async = require('async');
var _ = require('lodash'); const _ = require('lodash');
var path = require('path'); const path = require('path');
var nconf = require('nconf'); const nconf = require('nconf');
var db = require('../database'); const db = require('../database');
var posts = require('../posts'); const posts = require('../posts');
var topics = require('../topics'); const topics = require('../topics');
var groups = require('../groups'); const groups = require('../groups');
var messaging = require('../messaging'); const messaging = require('../messaging');
var plugins = require('../plugins'); const plugins = require('../plugins');
var batch = require('../batch'); const batch = require('../batch');
var file = require('../file'); const file = require('../file');
module.exports = function (User) { module.exports = function (User) {
var deletesInProgress = {}; const deletesInProgress = {};
User.delete = async function (callerUid, uid) { User.delete = async function (callerUid, uid) {
if (parseInt(uid, 10) <= 0) { if (parseInt(uid, 10) <= 0) {

Loading…
Cancel
Save