'use strict'; var bcrypt = require('bcryptjs'), async = require('async'), nconf = require('nconf'), winston = require('winston'), gravatar = require('gravatar'), S = require('string'), utils = require('./../public/src/utils'), plugins = require('./plugins'), db = require('./database'), meta = require('./meta'), groups = require('./groups'), topics = require('./topics'), events = require('./events'), emitter = require('./emitter'), Emailer = require('./emailer'); (function(User) { User.email = require('./user/email'); User.notifications = require('./user/notifications'); User.reset = require('./user/reset'); require('./user/auth')(User); require('./user/create')(User); require('./user/follow')(User); require('./user/profile')(User); require('./user/admin')(User); require('./user/delete')(User); require('./user/settings')(User); require('./user/search')(User); require('./user/jobs')(User); User.getUserField = function(uid, field, callback) { User.getUserFields(uid, [field], function(err, user) { callback(err, user ? user[field] : null); }); }; User.getUserFields = function(uid, fields, callback) { User.getMultipleUserFields([uid], fields, function(err, users) { callback(err, users ? users[0] : null); }); }; User.getMultipleUserFields = function(uids, fields, callback) { var fieldsToRemove = []; function addField(field) { if (fields.indexOf(field) === -1) { fields.push(field); fieldsToRemove.push(field); } } if (!Array.isArray(uids) || !uids.length) { return callback(null, []); } var keys = uids.map(function(uid) { return 'user:' + uid; }); addField('uid'); if (fields.indexOf('picture') !== -1) { addField('email'); addField('gravatarpicture'); addField('uploadedpicture'); } db.getObjectsFields(keys, fields, function(err, users) { if (err) { return callback(err); } callback(null, modifyUserData(users, fieldsToRemove)); }); }; User.getUserData = function(uid, callback) { User.getUsersData([uid], function(err, users) { callback(err, users ? users[0] : null); }); }; User.getUsersData = function(uids, callback) { if (!Array.isArray(uids) || !uids.length) { return callback(null, []); } var keys = uids.map(function(uid) { return 'user:' + uid; }); db.getObjects(keys, function(err, users) { if (err) { return callback(err); } callback(null, modifyUserData(users, [])); }); }; function modifyUserData(users, fieldsToRemove) { users.forEach(function(user) { if (!user) { return; } user.hasPassword = !!user.password; if (user.password) { user.password = null; } if (!parseInt(user.uid, 10)) { user.username = '[[global:guest]]'; user.userslug = ''; } if (user.picture) { if (user.picture === user.uploadedpicture) { user.picture = nconf.get('relative_path') + user.picture; } else { user.picture = User.createGravatarURLFromEmail(user.email); } } else { user.picture = User.createGravatarURLFromEmail(''); } for(var i=0; i