refactor: async/await

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

@ -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) {

@ -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 });
};
};

@ -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 = {

@ -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]]');
}

@ -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' });
}

@ -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) {

Loading…
Cancel
Save