Files
NodeBB/src/user.js

1083 lines
27 KiB
JavaScript
Raw Normal View History

2014-01-21 20:56:12 -07:00
var bcrypt = require('bcryptjs'),
2013-08-29 13:40:04 -04:00
async = require('async'),
nconf = require('nconf'),
winston = require('winston'),
gravatar = require('gravatar'),
2013-11-22 14:08:02 -05:00
check = require('validator').check,
sanitize = require('validator').sanitize,
S = require('string'),
2013-12-01 16:21:19 -05:00
utils = require('./../public/src/utils'),
plugins = require('./plugins'),
2013-12-02 17:10:26 -05:00
db = require('./database'),
meta = require('./meta'),
2013-12-02 19:40:11 -05:00
groups = require('./groups'),
notifications = require('./notifications'),
topics = require('./topics'),
2013-12-21 19:42:07 -05:00
events = require('./events'),
Emailer = require('./emailer');
2013-11-22 14:08:02 -05:00
(function(User) {
'use strict';
User.create = function(userData, callback) {
userData = userData || {};
userData.userslug = utils.slugify(userData.username);
userData.username = userData.username.trim();
if (userData.email !== undefined) {
userData.email = userData.email.trim();
}
async.parallel([
function(next) {
if (userData.email) {
next(!utils.isEmailValid(userData.email) ? new Error('Invalid Email!') : null);
} else {
next();
}
},
function(next) {
next((!utils.isUserNameValid(userData.username) || !userData.userslug) ? new Error('Invalid Username!') : null);
},
function(next) {
if (userData.password) {
next(!utils.isPasswordValid(userData.password) ? new Error('Invalid Password!') : null);
} else {
next();
}
},
function(next) {
User.exists(userData.userslug, function(err, exists) {
2014-01-16 16:50:41 -05:00
if (err) {
return next(err);
}
next(exists ? new Error('Username taken!') : null);
});
},
function(next) {
if (userData.email) {
User.isEmailAvailable(userData.email, function(err, available) {
if (err) {
2013-09-11 15:24:01 -04:00
return next(err);
}
2013-07-24 12:04:32 -04:00
next(!available ? new Error('Email taken!') : null);
});
} else {
next();
}
},
function(next) {
plugins.fireHook('filter:user.create', userData, function(err, filteredUserData){
next(err, utils.merge(userData, filteredUserData));
});
}
], function(err, results) {
userData = results[results.length - 1];
if (err) {
2013-12-02 17:10:26 -05:00
return callback(err);
}
2013-12-02 21:58:37 -05:00
db.incrObjectField('global', 'nextUid', function(err, uid) {
2013-12-02 17:10:26 -05:00
if(err) {
return callback(err);
}
var gravatar = User.createGravatarURLFromEmail(userData.email);
var timestamp = Date.now();
var password = userData.password;
2013-08-23 13:14:36 -04:00
userData = {
'uid': uid,
'username': userData.username,
'userslug': userData.userslug,
'fullname': '',
2013-09-17 13:09:37 -04:00
'location': '',
'birthday': '',
'website': '',
'email': userData.email || '',
2013-09-17 13:09:37 -04:00
'signature': '',
'joindate': timestamp,
'picture': gravatar,
2013-09-17 13:09:37 -04:00
'gravatarpicture': gravatar,
'uploadedpicture': '',
'profileviews': 0,
'reputation': 0,
'postcount': 0,
'lastposttime': 0,
'banned': 0,
2014-01-31 15:13:52 -05:00
'status': 'online',
2013-07-25 14:30:12 -04:00
'showemail': 0
};
db.setObject('user:' + uid, userData);
2013-08-23 13:14:36 -04:00
db.setObjectField('username:uid', userData.username, uid);
db.setObjectField('userslug:uid', userData.userslug, uid);
if (userData.email !== undefined) {
db.setObjectField('email:uid', userData.email, uid);
if (parseInt(uid, 10) !== 1) {
User.email.verify(uid, userData.email);
}
2013-07-24 12:04:32 -04:00
}
plugins.fireHook('action:user.create', userData);
2013-12-02 21:33:35 -05:00
db.incrObjectField('global', 'userCount');
2013-12-02 17:10:26 -05:00
db.sortedSetAdd('users:joindate', timestamp, uid);
db.sortedSetAdd('users:postcount', 0, uid);
db.sortedSetAdd('users:reputation', 0, uid);
2013-08-23 13:14:36 -04:00
groups.joinByGroupName('registered-users', uid);
if (password) {
User.hashPassword(password, function(err, hash) {
User.setUserField(uid, 'password', hash);
callback(null, uid);
});
} else {
callback(null, uid);
}
});
});
};
2013-08-23 13:14:36 -04:00
User.ban = function(uid, callback) {
User.setUserField(uid, 'banned', 1, callback);
};
User.unban = function(uid, callback) {
2013-08-23 13:14:36 -04:00
User.setUserField(uid, 'banned', 0, callback);
};
2013-08-23 13:14:36 -04:00
User.getUserField = function(uid, field, callback) {
2013-12-02 17:10:26 -05:00
db.getObjectField('user:' + uid, field, callback);
};
2013-07-05 16:37:45 -05:00
User.getUserFields = function(uid, fields, callback) {
2013-12-02 17:10:26 -05:00
db.getObjectFields('user:' + uid, fields, callback);
};
User.getMultipleUserFields = function(uids, fields, callback) {
2013-09-17 13:09:37 -04:00
if (uids.length === 0) {
2013-09-11 12:49:54 -04:00
return callback(null, []);
}
2014-02-02 15:02:27 -05:00
function getFields(uid, next) {
User.getUserFields(uid, fields, next);
}
2013-07-05 19:08:59 -04:00
2014-02-02 15:02:27 -05:00
async.map(uids, getFields, callback);
};
User.getUserData = function(uid, callback) {
2013-12-02 17:10:26 -05:00
db.getObject('user:' + uid, function(err, data) {
if(err) {
return callback(err);
}
2013-09-11 13:02:55 -04:00
if (data && data.password) {
delete data.password;
}
2013-09-11 13:02:55 -04:00
callback(err, data);
});
};
2014-02-10 14:15:54 -05:00
User.getSettings = function(uid, callback) {
function sendDefaultSettings() {
callback(null, {
showemail: false,
usePagination: parseInt(meta.config.usePagination, 10) !== 0,
topicsPerPage: parseInt(meta.config.topicsPerPage, 10) || 20,
postsPerPage: parseInt(meta.config.postsPerPage, 10) || 10
});
}
if(!parseInt(uid, 10)) {
return sendDefaultSettings();
}
db.getObject('user:' + uid + ':settings', function(err, settings) {
if(err) {
return callback(err);
}
if(!settings) {
settings = {}
2014-02-10 14:15:54 -05:00
}
settings.showemail = settings.showemail ? parseInt(settings.showemail, 10) !== 0 : parseInt(meta.config.usePagination, 10) !== 0;
settings.usePagination = settings.usePagination ? parseInt(settings.usePagination, 10) !== 0 : parseInt(meta.config.usePagination, 10) !== 0;
settings.topicsPerPage = settings.topicsPerPage ? parseInt(settings.topicsPerPage, 10) : parseInt(meta.config.topicsPerPage, 10) || 20;
settings.postsPerPage = settings.postsPerPage ? parseInt(settings.postsPerPage, 10) : parseInt(meta.config.postsPerPage, 10) || 10;
2014-02-10 14:15:54 -05:00
callback(null, settings);
});
}
User.saveSettings = function(uid, data, callback) {
2014-02-12 13:15:13 -05:00
if(!data.topicsPerPage || !data.postsPerPage || parseInt(data.topicsPerPage, 10) <= 0 || parseInt(data.postsPerPage, 10) <= 0) {
return callback(new Error('Invalid pagination value!'));
}
db.setObject('user:' + uid + ':settings', {
showemail: data.showemail,
usePagination: data.usePagination,
topicsPerPage: data.topicsPerPage,
postsPerPage: data.postsPerPage
}, callback);
2014-02-10 14:15:54 -05:00
}
User.updateProfile = function(uid, data, callback) {
2014-01-08 21:19:32 -05:00
var fields = ['username', 'email', 'fullname', 'website', 'location', 'birthday', 'signature'];
2013-09-17 13:09:37 -04:00
var returnData = {
success: false
};
2013-07-08 14:14:18 -04:00
function isSignatureValid(next) {
2013-12-19 14:57:59 -05:00
if (data.signature !== undefined && data.signature.length > meta.config.maximumSignatureLength) {
2014-01-16 16:50:41 -05:00
next(new Error('Signature can\'t be longer than ' + meta.config.maximumSignatureLength + ' characters!'), false);
2013-07-08 14:14:18 -04:00
} else {
2013-08-23 13:14:36 -04:00
next(null, true);
}
2013-07-08 14:14:18 -04:00
}
2013-08-23 13:14:36 -04:00
2013-07-08 14:14:18 -04:00
function isEmailAvailable(next) {
if (!data.email) {
return next(null, true);
2013-07-08 14:14:18 -04:00
}
2013-08-26 11:58:52 -04:00
User.getUserField(uid, 'email', function(err, email) {
2014-01-08 21:19:32 -05:00
if(email === data.email) {
return next(null, true);
}
2014-01-08 21:19:32 -05:00
User.isEmailAvailable(data.email, function(err, available) {
if (err) {
return next(err, null);
}
if (!available) {
2014-01-16 16:50:41 -05:00
next(new Error('Email not available!'), false);
2014-01-08 21:19:32 -05:00
} else {
next(null, true);
}
});
});
2013-05-21 13:44:53 -04:00
}
2013-08-23 13:14:36 -04:00
2014-01-08 21:19:32 -05:00
function isUsernameAvailable(next) {
User.getUserFields(uid, ['username', 'userslug'], function(err, userData) {
var userslug = utils.slugify(data.username);
if(userslug === userData.userslug) {
return next(null, true);
}
if(!utils.isUserNameValid(data.username) || !userslug) {
2014-01-16 16:50:41 -05:00
return next(new Error('Invalid Username!'), false);
2014-01-08 21:19:32 -05:00
}
2014-01-16 16:50:41 -05:00
User.exists(userslug, function(err, exists) {
if(err) {
return next(err);
}
2014-01-08 21:19:32 -05:00
if(exists) {
2014-01-16 16:50:41 -05:00
next(new Error('Username not available!'), false);
} else {
2014-01-08 21:19:32 -05:00
next(null, true);
}
});
2014-01-08 21:19:32 -05:00
});
}
async.series([isSignatureValid, isEmailAvailable, isUsernameAvailable], function(err, results) {
if (err) {
return callback(err, returnData);
2013-07-08 14:14:18 -04:00
}
2014-01-08 21:19:32 -05:00
async.each(fields, updateField, function(err) {
if (err) {
return callback(err, returnData);
}
returnData.success = true;
callback(null, returnData);
});
2013-07-08 14:14:18 -04:00
});
2013-09-11 13:53:42 -04:00
function updateField(field, next) {
if (!(data[field] !== undefined && typeof data[field] === 'string')) {
return next();
}
2013-08-23 13:14:36 -04:00
data[field] = data[field].trim();
data[field] = sanitize(data[field]).escape();
if (field === 'email') {
User.getUserFields(uid, ['email', 'picture', 'uploadedpicture'], function(err, userData) {
if (err) {
return next(err);
}
if(userData.email === data.email) {
return next();
}
var gravatarpicture = User.createGravatarURLFromEmail(data.email);
User.setUserField(uid, 'gravatarpicture', gravatarpicture);
2014-01-08 21:19:32 -05:00
db.deleteObjectField('email:uid', userData.email);
db.setObjectField('email:uid', data.email, uid);
User.setUserField(uid, 'email', data.email);
if (userData.picture !== userData.uploadedpicture) {
returnData.picture = gravatarpicture;
User.setUserField(uid, 'picture', gravatarpicture);
}
returnData.gravatarpicture = gravatarpicture;
2014-01-08 21:19:32 -05:00
events.logEmailChange(uid, userData.email, data.email);
next();
});
return;
} else if (field === 'username') {
2014-01-08 21:19:32 -05:00
User.getUserFields(uid, ['username', 'userslug'], function(err, userData) {
var userslug = utils.slugify(data.username);
2014-01-08 21:19:32 -05:00
if(data.username !== userData.username) {
User.setUserField(uid, 'username', data.username);
db.deleteObjectField('username:uid', userData.username);
db.setObjectField('username:uid', data.username, uid);
events.logUsernameChange(uid, userData.username, data.username);
}
2014-01-08 21:19:32 -05:00
if(userslug !== userData.userslug) {
User.setUserField(uid, 'userslug', userslug);
db.deleteObjectField('userslug:uid', userData.userslug);
db.setObjectField('userslug:uid', userslug, uid);
returnData.userslug = userslug;
2013-11-22 14:08:02 -05:00
}
2013-07-05 16:37:45 -05:00
next();
});
2013-07-08 14:14:18 -04:00
return;
} else if (field === 'signature') {
data[field] = S(data[field]).stripTags().s;
} else if (field === 'website') {
if(data[field].substr(0, 7) !== 'http://' && data[field].substr(0, 8) !== 'https://') {
data[field] = 'http://' + data[field];
}
}
User.setUserField(uid, field, data[field]);
next();
}
};
2013-07-05 16:37:45 -05:00
2014-01-23 19:01:30 -05:00
User.isReadyToPost = function(uid, callback) {
User.getUserField(uid, 'lastposttime', function(err, lastposttime) {
if(err) {
return callback(err);
}
if(!lastposttime) {
lastposttime = 0;
}
if (Date.now() - parseInt(lastposttime, 10) < parseInt(meta.config.postDelay, 10) * 1000) {
return callback(new Error('too-many-posts'));
}
callback();
});
}
2013-07-08 14:14:18 -04:00
User.isEmailAvailable = function(email, callback) {
2013-12-02 19:40:11 -05:00
db.isObjectField('email:uid', email, function(err, exists) {
callback(err, !exists);
2013-07-08 14:14:18 -04:00
});
};
User.changePassword = function(uid, data, callback) {
2013-09-17 13:09:37 -04:00
if (!utils.isPasswordValid(data.newPassword)) {
2014-01-16 17:52:46 -05:00
return callback(new Error('Invalid password!'));
2013-07-08 12:10:21 -04:00
}
2014-01-16 17:52:46 -05:00
User.getUserField(uid, 'password', function(err, currentPassword) {
bcrypt.compare(data.currentPassword, currentPassword, function(err, res) {
2013-09-17 13:09:37 -04:00
if (err) {
2013-09-11 15:24:01 -04:00
return callback(err);
2013-07-08 12:10:21 -04:00
}
if (res) {
User.hashPassword(data.newPassword, function(err, hash) {
2013-07-08 12:10:21 -04:00
User.setUserField(uid, 'password', hash);
2013-12-21 19:42:07 -05:00
events.logPasswordChange(uid);
2013-09-11 15:24:01 -04:00
callback(null);
2013-07-08 12:10:21 -04:00
});
} else {
2014-01-16 17:52:46 -05:00
callback(new Error('Your current password is not correct!'));
2013-07-08 12:10:21 -04:00
}
});
});
};
2013-07-08 12:10:21 -04:00
User.setUserField = function(uid, field, value, callback) {
2013-12-02 19:40:11 -05:00
db.setObjectField('user:' + uid, field, value, callback);
};
2013-11-27 15:02:09 -05:00
User.setUserFields = function(uid, data, callback) {
2013-12-02 19:40:11 -05:00
db.setObject('user:' + uid, data, callback);
};
2013-07-24 13:12:56 -04:00
User.incrementUserFieldBy = function(uid, field, value, callback) {
2013-12-02 19:40:11 -05:00
db.incrObjectFieldBy('user:' + uid, field, value, callback);
};
User.decrementUserFieldBy = function(uid, field, value, callback) {
2013-12-02 19:40:11 -05:00
db.incrObjectFieldBy('user:' + uid, field, -value, callback);
};
User.getUsers = function(set, start, stop, callback) {
2013-08-23 13:14:36 -04:00
2013-12-02 19:40:11 -05:00
db.getSortedSetRevRange(set, start, stop, function(err, uids) {
2013-09-17 13:09:37 -04:00
if (err) {
return callback(err, null);
}
2013-08-23 13:14:36 -04:00
function getUserData(uid, callback) {
2013-09-30 16:24:00 -04:00
User.getUserData(uid, function(err, userData) {
if(!userData.status) {
2014-02-06 18:40:01 -05:00
userData.status = 'online';
}
User.isAdministrator(uid, function(err, isAdmin) {
2013-09-30 16:24:00 -04:00
if (userData) {
userData.administrator = isAdmin ? '1':'0';
2013-09-30 16:24:00 -04:00
}
if(set === 'users:online') {
return callback(null, userData);
}
db.sortedSetScore('users:online', uid, function(err, score) {
if(!score) {
userData.status = 'offline';
}
callback(null, userData);
});
});
});
2013-09-30 16:24:00 -04:00
}
async.map(uids, getUserData, callback);
});
};
User.createGravatarURLFromEmail = function(email) {
2013-07-12 16:23:35 -04:00
var options = {
size: '128',
default: 'identicon',
rating: 'pg'
2014-01-25 12:05:48 -05:00
};
2013-08-23 13:14:36 -04:00
if (!email) {
2013-07-12 16:23:35 -04:00
email = '';
options.forcedefault = 'y';
}
2013-07-12 16:23:35 -04:00
2014-01-25 12:05:48 -05:00
return gravatar.url(email, options, true);
};
User.hashPassword = function(password, callback) {
2013-09-17 13:09:37 -04:00
if (!password) {
callback(password);
return;
}
2013-08-23 13:14:36 -04:00
bcrypt.genSalt(nconf.get('bcrypt_rounds'), function(err, salt) {
bcrypt.hash(password, salt, callback);
});
};
// thanks to @akhoury
User.getUsersCSV = function(callback) {
var csvContent = "";
db.getObjectValues('username:uid', function(err, uids) {
async.each(uids, function(uid, next) {
User.getUserFields(uid, ['email', 'username'], function(err, userData) {
if(err) {
return next(err);
}
csvContent += userData.email+ ',' + userData.username + ',' + uid +'\n';
next();
});
}, function(err) {
if (err) {
throw err;
}
callback(err, csvContent);
});
});
}
2014-01-19 21:46:39 -05:00
User.search = function(query, callback) {
if (!query || query.length === 0) {
2014-01-25 20:36:55 -05:00
return callback(null, {timing:0, users:[]});
}
var start = process.hrtime();
2014-01-08 22:53:55 -05:00
2014-01-19 21:46:39 -05:00
db.getObject('username:uid', function(err, usernamesHash) {
2013-09-17 13:09:37 -04:00
if (err) {
2014-01-25 20:36:55 -05:00
return callback(null, {timing: 0, users:[]});
2013-08-29 13:40:04 -04:00
}
2014-01-14 18:04:54 -05:00
query = query.toLowerCase();
2014-01-19 21:46:39 -05:00
var usernames = Object.keys(usernamesHash),
results = [];
results = usernames.filter(function(username) {
return username.toLowerCase().indexOf(query) === 0;
})
.slice(0, 10)
.sort(function(a, b) {
2014-01-19 21:46:39 -05:00
return a > b;
})
.map(function(username) {
return usernamesHash[username];
});
2014-01-19 21:46:39 -05:00
User.getDataForUsers(results, function(err, userdata) {
2014-01-25 20:36:55 -05:00
var diff = process.hrtime(start);
var timing = (diff[0] * 1e3 + diff[1] / 1e6).toFixed(1);
callback(null, {timing: timing, users: userdata});
});
2013-06-26 12:32:30 -04:00
});
};
2013-06-26 12:32:30 -04:00
2013-07-02 16:24:13 -04:00
User.onNewPostMade = function(uid, tid, pid, timestamp) {
2014-01-07 17:30:29 -05:00
User.addPostIdToUser(uid, pid, timestamp);
User.incrementUserFieldBy(uid, 'postcount', 1, function(err, newpostcount) {
2013-12-02 19:40:11 -05:00
db.sortedSetAdd('users:postcount', newpostcount, uid);
});
2013-08-23 13:14:36 -04:00
2013-07-02 16:24:13 -04:00
User.setUserField(uid, 'lastposttime', timestamp);
};
2013-07-02 16:24:13 -04:00
2014-01-07 17:30:29 -05:00
User.addPostIdToUser = function(uid, pid, timestamp) {
db.sortedSetAdd('uid:' + uid + ':posts', timestamp, pid);
};
2013-07-02 16:24:13 -04:00
2014-01-07 17:30:29 -05:00
User.addTopicIdToUser = function(uid, tid, timestamp) {
db.sortedSetAdd('uid:' + uid + ':topics', timestamp, tid);
};
2013-07-02 16:24:13 -04:00
2013-12-02 19:40:11 -05:00
User.getPostIds = function(uid, start, stop, callback) {
2014-01-07 17:30:29 -05:00
db.getSortedSetRevRange('uid:' + uid + ':posts', start, stop, function(err, pids) {
if(err) {
return callback(err);
}
if (pids && pids.length) {
callback(null, pids);
} else {
callback(null, []);
2013-07-02 16:24:13 -04:00
}
});
};
2013-07-02 16:24:13 -04:00
User.follow = function(uid, followid, callback) {
2013-12-02 19:40:11 -05:00
db.setAdd('following:' + uid, followid, function(err, data) {
2014-01-16 18:06:19 -05:00
if(err) {
return callback(err);
}
2014-01-16 18:06:19 -05:00
db.setAdd('followers:' + followid, uid, callback);
});
};
User.unfollow = function(uid, unfollowid, callback) {
2013-12-02 19:40:11 -05:00
db.setRemove('following:' + uid, unfollowid, function(err, data) {
2014-01-16 18:06:19 -05:00
if(err) {
return callback(err);
}
2014-01-16 18:06:19 -05:00
db.setRemove('followers:' + unfollowid, uid, callback);
});
};
User.getFollowing = function(uid, callback) {
2013-12-02 19:40:11 -05:00
db.getSetMembers('following:' + uid, function(err, userIds) {
if(err) {
return callback(err);
}
User.getDataForUsers(userIds, callback);
});
};
User.getFollowers = function(uid, callback) {
2013-12-02 19:40:11 -05:00
db.getSetMembers('followers:' + uid, function(err, userIds) {
if(err) {
return callback(err);
}
User.getDataForUsers(userIds, callback);
2013-05-15 11:46:41 -04:00
});
};
2013-07-05 16:37:45 -05:00
User.getFollowingCount = function(uid, callback) {
2013-12-02 19:40:11 -05:00
db.getSetMembers('following:' + uid, function(err, userIds) {
2013-10-11 14:29:34 -04:00
if (err) {
return callback(err);
}
userIds = userIds.filter(function(value) {
return parseInt(value, 10) !== 0;
});
callback(null, userIds.length);
});
};
2013-07-05 16:37:45 -05:00
User.getFollowerCount = function(uid, callback) {
2013-12-02 19:40:11 -05:00
db.getSetMembers('followers:' + uid, function(err, userIds) {
2013-10-11 14:29:34 -04:00
if(err) {
return callback(err);
}
userIds = userIds.filter(function(value) {
return parseInt(value, 10) !== 0;
});
callback(null, userIds.length);
});
};
2013-07-05 16:37:45 -05:00
User.getFollowStats = function (uid, callback) {
async.parallel({
followingCount: function(next) {
User.getFollowingCount(uid, next);
},
followerCount : function(next) {
User.getFollowerCount(uid, next);
}
}, callback);
}
2013-07-05 19:08:59 -04:00
User.getDataForUsers = function(uids, callback) {
2013-09-17 13:09:37 -04:00
if (!uids || !Array.isArray(uids) || uids.length === 0) {
return callback(null, []);
}
function getUserData(uid, next) {
2013-12-05 13:11:27 -05:00
if(parseInt(uid, 10) === 0) {
return next(null, null);
}
2013-10-11 14:29:34 -04:00
User.getUserData(uid, next);
}
2013-08-23 13:14:36 -04:00
async.map(uids, getUserData, callback);
};
2013-07-05 16:37:45 -05:00
User.sendPostNotificationToFollowers = function(uid, tid, pid) {
2013-08-26 11:58:52 -04:00
User.getUserField(uid, 'username', function(err, username) {
2013-12-02 19:40:11 -05:00
db.getSetMembers('followers:' + uid, function(err, followers) {
topics.getTopicField(tid, 'slug', function(err, slug) {
2013-08-27 13:04:18 -04:00
var message = '<strong>' + username + '</strong> made a new post';
2013-06-18 10:47:43 -04:00
notifications.create(message, nconf.get('relative_path') + '/topic/' + slug + '#' + pid, 'topic:' + tid, function(nid) {
2013-09-17 13:09:37 -04:00
notifications.push(nid, followers);
2013-06-18 10:47:43 -04:00
});
});
});
});
};
2013-05-15 11:46:41 -04:00
User.isFollowing = function(uid, theirid, callback) {
2013-12-04 15:11:17 -05:00
db.isSetMember('following:' + uid, theirid, function(err, isMember) {
2013-09-17 13:09:37 -04:00
if (!err) {
2013-12-04 15:11:17 -05:00
callback(isMember);
} else {
console.log(err);
}
});
};
User.exists = function(userslug, callback) {
2013-10-30 18:31:36 -04:00
User.getUidByUserslug(userslug, function(err, exists) {
2014-01-16 16:50:41 -05:00
callback(err, !! exists);
});
};
2013-07-05 16:37:45 -05:00
2014-01-09 21:27:50 -05:00
User.count = function(callback) {
2013-12-02 21:33:35 -05:00
db.getObjectField('global', 'userCount', function(err, count) {
2013-12-02 19:40:11 -05:00
if(err) {
2014-01-16 17:07:34 -05:00
return callback(err);
2013-12-02 19:40:11 -05:00
}
2014-01-16 17:07:34 -05:00
callback(null, {
2013-09-17 13:09:37 -04:00
count: count ? count : 0
});
});
};
2013-07-05 16:37:45 -05:00
2013-10-30 18:31:36 -04:00
User.getUidByUsername = function(username, callback) {
2013-12-02 19:40:11 -05:00
db.getObjectField('username:uid', username, callback);
};
2013-10-30 18:31:36 -04:00
User.getUidByUserslug = function(userslug, callback) {
2013-12-02 19:40:11 -05:00
db.getObjectField('userslug:uid', userslug, callback);
};
2013-06-24 14:33:53 -04:00
2013-10-30 18:31:36 -04:00
User.getUsernamesByUids = function(uids, callback) {
if (!Array.isArray(uids)) {
2014-02-11 23:14:41 -05:00
return callback(null, []);
}
2013-07-05 16:37:45 -05:00
2014-02-11 23:14:41 -05:00
function getUserName(uid, next) {
User.getUserField(uid, 'username', next);
}
2013-07-05 19:11:27 -04:00
async.map(uids, getUserName, callback);
};
2013-06-24 14:33:53 -04:00
2013-10-30 18:31:36 -04:00
User.getUserSlugsByUids = function(uids, callback) {
2013-06-24 14:33:53 -04:00
if (!Array.isArray(uids)) {
2014-02-11 23:14:41 -05:00
return callback(null, []);
}
2013-07-05 16:37:45 -05:00
2014-02-11 23:14:41 -05:00
function getUserSlug(uid, next) {
User.getUserField(uid, 'userslug', next);
2013-07-05 18:19:55 -04:00
}
2014-02-11 23:14:41 -05:00
async.map(uids, getUserSlug, callback);
};
User.getUsernameByUserslug = function(slug, callback) {
async.waterfall([
function(next) {
User.getUidByUserslug(slug, next);
},
function(uid, next) {
User.getUserField(uid, 'username', next);
}
], callback);
};
2013-10-30 18:31:36 -04:00
User.getUidByEmail = function(email, callback) {
2013-12-02 19:40:11 -05:00
db.getObjectField('email:uid', email, function(err, data) {
if (err) {
2013-12-02 19:40:11 -05:00
return callback(err);
}
2013-12-02 19:40:11 -05:00
callback(null, data);
});
};
2013-05-16 12:49:39 -04:00
User.isModerator = function(uid, cid, callback) {
groups.isMemberByGroupName(uid, 'cid:' + cid + ':privileges:mod', function(err, isMember) {
2013-12-02 19:40:11 -05:00
if(err) {
return calback(err);
}
2014-02-07 11:21:23 -05:00
callback(err, isMember);
});
};
User.isAdministrator = function(uid, callback) {
groups.getGidFromName('administrators', function(err, gid) {
2014-01-19 18:18:47 -05:00
if(err) {
return callback(err);
}
groups.isMember(uid, gid, callback);
2013-06-20 14:45:38 -04:00
});
};
2013-06-20 14:45:38 -04:00
User.reset = {
validate: function(socket, code, callback) {
2013-08-23 13:14:36 -04:00
2013-12-02 19:40:11 -05:00
db.getObjectField('reset:uid', code, function(err, uid) {
if (err) {
2014-01-16 21:19:29 -05:00
return callback(err);
}
if (uid !== null) {
2013-12-02 19:40:11 -05:00
db.getObjectField('reset:expiry', code, function(err, expiry) {
if (err) {
2014-01-16 21:19:29 -05:00
return callback(err);
}
2013-09-17 13:09:37 -04:00
if (expiry >= +Date.now() / 1000 | 0) {
2014-01-16 21:19:29 -05:00
callback(null, true);
} else {
// Expired, delete from db
2013-12-02 19:40:11 -05:00
db.deleteObjectField('reset:uid', code);
db.deleteObjectField('reset:expiry', code);
2014-01-16 21:19:29 -05:00
callback(null, false);
}
});
} else {
2014-01-16 21:19:29 -05:00
callback(null, false);
}
});
},
2014-01-16 21:19:29 -05:00
send: function(socket, email, callback) {
2013-12-02 19:40:11 -05:00
User.getUidByEmail(email, function(err, uid) {
2014-01-16 21:19:29 -05:00
if(err) {
return callback(err);
}
2014-01-16 21:19:29 -05:00
if(!uid) {
return callback(new Error('invalid-email'));
}
2014-01-16 21:19:29 -05:00
// Generate a new reset code
var reset_code = utils.generateUUID();
db.setObjectField('reset:uid', reset_code, uid);
db.setObjectField('reset:expiry', reset_code, (60 * 60) + new Date() / 1000 | 0); // Active for one hour
2014-01-22 14:04:04 -05:00
var reset_link = nconf.get('url') + '/reset/' + reset_code;
2013-07-05 16:37:45 -05:00
2014-01-16 21:19:29 -05:00
Emailer.send('reset', uid, {
'site_title': (meta.config['title'] || 'NodeBB'),
'reset_link': reset_link,
subject: 'Password Reset Requested - ' + (meta.config['title'] || 'NodeBB') + '!',
template: 'reset',
uid: uid
});
callback(null);
});
},
2014-01-16 21:19:29 -05:00
commit: function(socket, code, password, callback) {
this.validate(socket, code, function(err, validated) {
if(err) {
return callback(err);
}
if (validated) {
2013-12-02 19:40:11 -05:00
db.getObjectField('reset:uid', code, function(err, uid) {
if (err) {
2014-01-16 21:19:29 -05:00
return callback(err);
}
User.hashPassword(password, function(err, hash) {
2013-08-19 13:31:04 -04:00
User.setUserField(uid, 'password', hash);
2013-12-21 19:42:07 -05:00
events.logPasswordReset(uid);
2013-08-19 13:31:04 -04:00
});
2013-12-02 19:40:11 -05:00
db.deleteObjectField('reset:uid', code);
db.deleteObjectField('reset:expiry', code);
2014-01-16 21:19:29 -05:00
callback(null);
});
}
});
}
};
User.pushNotifCount = function(uid) {
var websockets = require('./socket.io');
User.notifications.getUnreadCount(uid, function(err, count) {
if (!err) {
websockets.in('uid_' + uid).emit('event:notifications.updateCount', count);
} else {
winston.warn('[User.pushNotifCount] Count not retrieve unread notifications count to push to uid ' + uid + '\'s client(s)');
}
});
};
User.email = {
verify: function(uid, email) {
if (!plugins.hasListeners('action:email.send')) {
return;
}
var confirm_code = utils.generateUUID(),
2014-01-22 14:04:04 -05:00
confirm_link = nconf.get('url') + '/confirm/' + confirm_code;
async.series([
function(next) {
db.setObject('confirm:' + confirm_code, {
email: email,
uid: uid
}, next);
},
function(next) {
db.expireAt('confirm:' + confirm_code, Math.floor(Date.now() / 1000 + 60 * 60 * 2), next);
}
], function(err) {
// Send intro email w/ confirm code
User.getUserField(uid, 'username', function(err, username) {
Emailer.send('welcome', uid, {
'site_title': (meta.config['title'] || 'NodeBB'),
username: username,
'confirm_link': confirm_link,
subject: 'Welcome to ' + (meta.config['title'] || 'NodeBB') + '!',
template: 'welcome',
uid: uid
});
});
});
},
2014-01-16 15:46:37 -05:00
exists: function(email, callback) {
2013-12-02 19:40:11 -05:00
User.getUidByEmail(email, function(err, exists) {
2014-01-16 17:32:33 -05:00
callback(err, !!exists);
});
},
confirm: function(code, callback) {
db.getObject('confirm:' + code, function(err, confirmObj) {
if (err) {
return callback({
status:'error'
2013-09-17 13:09:37 -04:00
});
}
if (confirmObj && confirmObj.uid && confirmObj.email) {
db.setObjectField('email:confirmed', confirmObj.email, '1', function() {
callback({
status: 'ok'
});
});
} else {
callback({
status: 'not_ok'
});
}
});
}
};
2013-05-22 21:43:01 -04:00
User.notifications = {
get: function(uid, callback) {
2014-02-11 23:38:25 -05:00
function getNotifications(set, start, stop, iterator, done) {
db.getSortedSetRevRange(set, start, stop, function(err, nids) {
if(err) {
return done(err);
}
if(!nids || nids.length === 0) {
return done(null, []);
}
if (nids.length > maxNotifs) {
nids.length = maxNotifs;
}
async.map(nids, function(nid, next) {
notifications.get(nid, uid, function(notif_data) {
if(!notif_data) {
db.sortedSetRemove(set, nid);
} else {
if(typeof iterator === 'function') {
iterator(notif_data);
}
}
next(null, notif_data);
});
}, done);
});
}
2013-09-17 13:09:37 -04:00
var maxNotifs = 15;
2013-05-22 21:43:01 -04:00
async.parallel({
unread: function(next) {
2014-02-11 23:38:25 -05:00
getNotifications('uid:' + uid + ':notifications:unread', 0, 10, function(notif_data) {
notif_data.readClass = !notif_data.read ? 'label-warning' : '';
}, next);
2013-05-22 21:43:01 -04:00
},
read: function(next) {
2014-02-11 23:38:25 -05:00
getNotifications('uid:' + uid + 'notifications:read', 0, 10, null, next);
2013-05-22 21:43:01 -04:00
}
}, function(err, notifications) {
// Limit the number of notifications to `maxNotifs`, prioritising unread notifications
if (notifications.read.length + notifications.unread.length > maxNotifs) {
notifications.read.length = maxNotifs - notifications.unread.length;
}
2014-01-16 20:29:11 -05:00
callback(err, notifications);
2013-05-22 21:43:01 -04:00
});
},
getAll: function(uid, limit, before, callback) {
var now = new Date();
if (!limit || parseInt(limit,10) <= 0) {
limit = 25;
}
if (before) {
before = new Date(parseInt(before, 10));
}
2013-12-04 16:31:05 -05:00
var args1 = ['uid:' + uid + ':notifications:read', before ? before.getTime(): now.getTime(), -Infinity, 'LIMIT', 0, limit];
var args2 = ['uid:' + uid + ':notifications:unread', before ? before.getTime(): now.getTime(), -Infinity, 'LIMIT', 0, limit];
2013-12-04 16:31:05 -05:00
db.getSortedSetRevRangeByScore(args1, function(err, results1) {
db.getSortedSetRevRangeByScore(args2, function(err, results2) {
var nids = results1.concat(results2);
async.map(nids, function(nid, next) {
notifications.get(nid, uid, function(notif_data) {
next(null, notif_data);
});
}, function(err, notifs) {
2013-11-18 15:09:34 -05:00
notifs = notifs.filter(function(notif) {
2013-11-18 15:56:12 -05:00
return notif !== null;
2013-11-18 15:09:34 -05:00
}).sort(function(a, b) {
return parseInt(b.datetime, 10) - parseInt(a.datetime, 10);
}).map(function(notif) {
notif.datetimeISO = utils.toISOString(notif.datetime);
2014-01-21 10:38:20 -05:00
notif.readClass = !notif.read ? 'label-warning' : '';
return notif;
});
callback(err, notifs);
});
});
2013-12-04 16:31:05 -05:00
});
},
getUnreadCount: function(uid, callback) {
2013-12-05 12:17:01 -05:00
db.sortedSetCount('uid:' + uid + ':notifications:unread', -Infinity, Infinity, callback);
},
getUnreadByUniqueId: function(uid, uniqueId, callback) {
2013-12-02 19:40:11 -05:00
db.getSortedSetRange('uid:' + uid + ':notifications:unread', 0, -1, function(err, nids) {
2013-12-04 18:58:20 -05:00
async.filter(nids, function(nid, next) {
notifications.get(nid, uid, function(notifObj) {
2013-12-04 18:58:20 -05:00
if(!notifObj) {
next(false);
}
if (notifObj.uniqueId === uniqueId) {
next(true);
} else {
next(false);
}
});
}, function(nids) {
callback(null, nids);
});
});
2013-05-22 21:43:01 -04:00
}
};
}(exports));