Files
NodeBB/src/socket.io/user.js

519 lines
12 KiB
JavaScript
Raw Normal View History

2014-03-12 22:11:48 -04:00
'use strict';
2014-01-31 15:13:52 -05:00
var async = require('async'),
2014-08-13 19:25:58 -04:00
nconf = require('nconf'),
2014-01-31 15:13:52 -05:00
user = require('../user'),
groups = require('../groups'),
topics = require('../topics'),
2014-09-15 14:34:01 -04:00
posts = require('../posts'),
2014-08-13 19:25:58 -04:00
notifications = require('../notifications'),
2014-07-19 10:33:27 -04:00
messaging = require('../messaging'),
plugins = require('../plugins'),
utils = require('../../public/src/utils'),
websockets = require('./index'),
2014-03-19 21:32:13 -04:00
meta = require('../meta'),
2015-02-01 19:11:58 -05:00
events = require('../events'),
2015-02-08 21:06:38 -05:00
emailer = require('../emailer'),
db = require('../database'),
SocketUser = {};
2014-01-16 15:46:37 -05:00
SocketUser.exists = function(socket, data, callback) {
if (data && data.username) {
meta.userOrGroupExists(utils.slugify(data.username), callback);
}
};
2014-08-26 13:47:48 -04:00
SocketUser.deleteAccount = function(socket, data, callback) {
2015-04-22 10:41:44 -04:00
if (!socket.uid) {
return;
}
user.isAdministrator(socket.uid, function(err, isAdmin) {
if (err || isAdmin) {
return callback(err || new Error('[[error:cant-delete-admin]]'));
}
2015-04-22 10:41:44 -04:00
socket.broadcast.emit('event:user_status_change', {uid: socket.uid, status: 'offline'});
user.deleteAccount(socket.uid, function(err) {
if (err) {
return callback(err);
}
websockets.in('uid_' + socket.uid).emit('event:logout');
callback();
2014-09-24 00:07:24 -04:00
});
2015-04-22 10:41:44 -04:00
});
2014-08-26 13:47:48 -04:00
};
2014-01-16 15:46:37 -05:00
SocketUser.emailExists = function(socket, data, callback) {
2015-04-22 10:41:44 -04:00
if (data && data.email) {
2014-01-16 18:18:42 -05:00
user.email.exists(data.email, callback);
}
};
SocketUser.emailConfirm = function(socket, data, callback) {
if (socket.uid && parseInt(meta.config.requireEmailConfirmation, 10) === 1) {
user.getUserField(socket.uid, 'email', function(err, email) {
if (err) {
return callback(err);
}
if (!email) {
return;
}
2015-04-13 17:29:43 -04:00
user.email.sendValidationEmail(socket.uid, email, callback);
});
}
};
2015-01-07 17:27:09 -05:00
SocketUser.search = function(socket, data, callback) {
if (!data) {
2015-02-01 19:11:58 -05:00
return callback(new Error('[[error:invalid-data]]'));
2015-01-07 17:27:09 -05:00
}
2014-06-23 19:10:59 -04:00
if (!socket.uid) {
return callback(new Error('[[error:not-logged-in]]'));
}
2015-01-12 15:57:45 -05:00
user.search({
query: data.query,
page: data.page,
searchBy: data.searchBy,
sortBy: data.sortBy,
2015-05-19 23:04:28 -04:00
onlineOnly: data.onlineOnly,
uid: socket.uid
2015-01-12 15:57:45 -05:00
}, callback);
};
// Password Reset
SocketUser.reset = {};
2014-04-04 13:11:05 -04:00
SocketUser.reset.send = function(socket, email, callback) {
if (email) {
user.reset.send(email, callback);
2014-01-16 18:18:42 -05:00
}
};
2014-01-16 15:46:37 -05:00
SocketUser.reset.commit = function(socket, data, callback) {
2015-02-11 21:54:20 -05:00
if (!data || !data.code || !data.password) {
return callback(new Error('[[error:invalid-data]]'));
}
2015-02-08 21:06:38 -05:00
2015-02-11 21:54:20 -05:00
async.parallel({
uid: async.apply(db.getObjectField, 'reset:uid', data.code),
reset: async.apply(user.reset.commit, data.code, data.password)
}, function(err, results) {
if (err) {
return callback(err);
}
var uid = results.uid,
now = new Date(),
parsedDate = now.getFullYear() + '/' + (now.getMonth()+1) + '/' + now.getDate();
user.getUserField(uid, 'username', function(err, username) {
emailer.send('reset_notify', uid, {
username: username,
date: parsedDate,
site_title: meta.config.title || 'NodeBB',
subject: '[[email:reset.notify.subject]]'
2015-02-01 19:11:58 -05:00
});
});
2015-02-11 21:54:20 -05:00
events.log({
type: 'password-reset',
uid: uid,
ip: socket.ip
});
callback();
});
};
SocketUser.checkStatus = function(socket, uid, callback) {
if (!socket.uid) {
return callback('[[error:invalid-uid]]');
}
var online = websockets.isUserOnline(uid);
if (!online) {
return callback(null, 'offline');
}
user.getUserField(uid, 'status', function(err, status) {
if (err) {
return callback(err);
}
status = status || 'online';
callback(null, status);
2014-08-14 07:51:21 -04:00
});
};
2014-01-16 15:46:37 -05:00
SocketUser.changePassword = function(socket, data, callback) {
if (!data || !data.uid || data.newPassword.length < meta.config.minimumPasswordLength) {
2015-02-01 19:11:58 -05:00
return callback(new Error('[[error:invalid-data]]'));
2014-01-16 18:18:42 -05:00
}
2015-02-01 19:11:58 -05:00
if (!socket.uid) {
return callback('[[error:invalid-uid]]');
}
user.changePassword(socket.uid, data, function(err) {
if (err) {
return callback(err);
}
events.log({
type: 'password-change',
uid: socket.uid,
targetUid: data.uid,
ip: socket.ip
});
2015-02-11 21:26:26 -05:00
callback();
2015-02-01 19:11:58 -05:00
});
};
2014-01-16 15:46:37 -05:00
SocketUser.updateProfile = function(socket, data, callback) {
2015-02-01 19:11:58 -05:00
function update(oldUserData) {
function done(err, userData) {
if (err) {
return callback(err);
}
if (userData.email !== oldUserData.email) {
events.log({
type: 'email-change',
uid: socket.uid,
targetUid: data.uid,
ip: socket.ip,
oldEmail: oldUserData.email,
newEmail: userData.email
});
}
if (userData.username !== oldUserData.username) {
events.log({
type: 'username-change',
uid: socket.uid,
targetUid: data.uid,
ip: socket.ip,
oldUsername: oldUserData.username,
newUsername: userData.username
});
}
2015-02-05 23:37:11 -05:00
callback(null, userData);
2015-02-01 19:11:58 -05:00
}
if (socket.uid === parseInt(data.uid, 10)) {
return user.updateProfile(socket.uid, data, done);
}
user.isAdministrator(socket.uid, function(err, isAdmin) {
2015-02-05 23:37:11 -05:00
if (err || !isAdmin) {
return callback(err || new Error('[[error:no-privileges]]'));
2015-02-01 19:11:58 -05:00
}
user.updateProfile(data.uid, data, done);
});
}
if (!socket.uid) {
return callback('[[error:invalid-uid]]');
}
if (!data || !data.uid) {
2014-04-09 21:44:00 -04:00
return callback(new Error('[[error:invalid-data]]'));
}
2015-02-01 19:11:58 -05:00
user.getUserFields(data.uid, ['email', 'username'], function(err, oldUserData) {
if (err) {
return callback(err);
}
2014-03-12 22:11:48 -04:00
2015-02-01 19:11:58 -05:00
update(oldUserData, callback);
});
};
2014-01-16 15:46:37 -05:00
SocketUser.changePicture = function(socket, data, callback) {
if (!socket.uid) {
return callback('[[error:invalid-uid]]');
}
if (!data) {
2014-04-09 21:44:00 -04:00
return callback(new Error('[[error:invalid-data]]'));
2014-01-16 18:18:42 -05:00
}
var type = data.type;
2014-02-15 17:12:05 -05:00
function changePicture(uid, callback) {
user.getUserField(uid, type, function(err, picture) {
2014-12-23 18:09:23 -05:00
if (err) {
2014-02-15 17:12:05 -05:00
return callback(err);
}
user.setUserField(uid, 'picture', picture, callback);
});
}
if (type === 'gravatar') {
2014-01-16 17:52:46 -05:00
type = 'gravatarpicture';
} else if (type === 'uploaded') {
2014-01-16 17:52:46 -05:00
type = 'uploadedpicture';
} else {
2015-01-12 17:33:11 -05:00
return callback(new Error('[[error:invalid-image-type, ' + ['gravatar', 'uploadedpicture'].join(', ') + ']]'));
}
2014-01-16 17:52:46 -05:00
2014-12-23 18:09:23 -05:00
if (socket.uid === parseInt(data.uid, 10)) {
return changePicture(socket.uid, callback);
2014-02-15 17:12:05 -05:00
}
user.isAdministrator(socket.uid, function(err, isAdmin) {
2014-12-23 18:09:23 -05:00
if (err || !isAdmin) {
return callback(err || new Error('[[error:no-privileges]]'));
2014-02-15 17:12:05 -05:00
}
changePicture(data.uid, callback);
2014-01-16 17:52:46 -05:00
});
};
2015-04-13 15:01:38 -04:00
SocketUser.uploadProfileImageFromUrl = function(socket, data, callback) {
function upload() {
user.uploadFromUrl(data.uid, data.url, function(err, uploadedImage) {
callback(err, uploadedImage ? uploadedImage.url : null);
});
}
if (!socket.uid || !data.url || !data.uid) {
2014-09-24 17:13:12 -04:00
return;
}
2015-04-13 15:01:38 -04:00
if (parseInt(socket.uid, 10) === parseInt(data.uid, 10)) {
return upload();
}
user.isAdministrator(socket.uid, function(err, isAdmin) {
if (err || !isAdmin) {
return callback(err || new Error('[[error:not-allowed]]'));
2014-09-24 17:13:12 -04:00
}
2015-04-13 15:01:38 -04:00
upload();
2014-09-24 17:13:12 -04:00
});
2014-10-08 12:18:32 -04:00
};
2014-09-24 17:13:12 -04:00
2014-01-16 15:46:37 -05:00
SocketUser.follow = function(socket, data, callback) {
2014-09-08 23:03:37 -04:00
if (!socket.uid || !data) {
return;
}
2015-03-19 15:55:56 -04:00
var userData;
2015-02-27 17:57:09 -05:00
async.waterfall([
function(next) {
toggleFollow('follow', socket.uid, data.uid, next);
},
function(next) {
user.getUserFields(socket.uid, ['username', 'userslug'], next);
},
2015-03-19 15:55:56 -04:00
function(_userData, next) {
userData = _userData;
2014-09-08 23:03:37 -04:00
notifications.create({
bodyShort: '[[notifications:user_started_following_you, ' + userData.username + ']]',
nid: 'follow:' + data.uid + ':uid:' + socket.uid,
2015-03-19 15:55:56 -04:00
from: socket.uid
2015-02-27 17:57:09 -05:00
}, next);
},
function(notification, next) {
2015-03-19 15:55:56 -04:00
notification.user = userData;
2015-02-27 17:57:09 -05:00
notifications.push(notification, [data.uid], next);
}
], callback);
};
2014-01-16 15:46:37 -05:00
SocketUser.unfollow = function(socket, data, callback) {
2014-01-16 18:18:42 -05:00
if (socket.uid && data) {
toggleFollow('unfollow', socket.uid, data.uid, callback);
}
};
function toggleFollow(method, uid, theiruid, callback) {
user[method](uid, theiruid, function(err) {
if (err) {
return callback(err);
}
plugins.fireHook('action:user.' + method, {
fromUid: uid,
toUid: theiruid
});
2014-08-17 00:14:45 -04:00
callback();
});
}
2014-01-16 15:46:37 -05:00
SocketUser.saveSettings = function(socket, data, callback) {
if (!socket.uid || !data) {
return callback(new Error('[[error:invalid-data]]'));
}
if (socket.uid === parseInt(data.uid, 10)) {
return user.saveSettings(socket.uid, data.settings, callback);
}
user.isAdministrator(socket.uid, function(err, isAdmin) {
if (err) {
return callback(err);
}
if (!isAdmin) {
return callback(new Error('[[error:no-privileges]]'));
}
user.saveSettings(data.uid, data.settings, callback);
});
};
SocketUser.setTopicSort = function(socket, sort, callback) {
if (socket.uid) {
user.setSetting(socket.uid, 'topicPostSort', sort, callback);
}
};
2015-01-08 13:47:15 -05:00
SocketUser.setCategorySort = function(socket, sort, callback) {
if (socket.uid) {
user.setSetting(socket.uid, 'categoryTopicSort', sort, callback);
}
};
2014-01-16 15:46:37 -05:00
SocketUser.getOnlineAnonCount = function(socket, data, callback) {
2014-01-16 18:10:38 -05:00
callback(null, module.parent.exports.getOnlineAnonCount());
};
2014-01-16 15:46:37 -05:00
SocketUser.getUnreadCount = function(socket, data, callback) {
if (!socket.uid) {
return callback(null, 0);
}
2014-03-09 14:02:30 -04:00
topics.getTotalUnread(socket.uid, callback);
};
2014-07-19 10:33:27 -04:00
SocketUser.getUnreadChatCount = function(socket, data, callback) {
if (!socket.uid) {
return callback(null, 0);
}
2014-07-19 10:33:27 -04:00
messaging.getUnreadCount(socket.uid, callback);
};
2014-01-16 15:46:37 -05:00
SocketUser.loadMore = function(socket, data, callback) {
if (!data || !data.set || parseInt(data.after, 10) < 0) {
2014-04-09 21:44:00 -04:00
return callback(new Error('[[error:invalid-data]]'));
}
2014-01-10 16:00:03 -05:00
2014-03-19 21:32:13 -04:00
if (!socket.uid && !!parseInt(meta.config.privateUserInfo, 10)) {
2014-04-09 21:44:00 -04:00
return callback(new Error('[[error:no-privileges]]'));
2014-03-19 21:32:13 -04:00
}
2015-01-13 17:15:10 -05:00
var start = parseInt(data.after, 10),
stop = start + 19;
2014-01-16 18:10:38 -05:00
async.parallel({
isAdmin: function(next) {
user.isAdministrator(socket.uid, next);
},
users: function(next) {
user.getUsersFromSet(data.set, socket.uid, start, stop, next);
}
}, function(err, results) {
2015-01-13 17:15:10 -05:00
if (err) {
return callback(err);
}
2014-02-06 17:12:13 -05:00
if (!results.isAdmin && data.set === 'users:online') {
results.users = results.users.filter(function(user) {
return user.status !== 'offline';
2014-02-06 17:12:13 -05:00
});
}
var result = {
users: results.users,
nextStart: stop + 1,
};
result['route_' + data.set] = true;
callback(null, result);
});
2014-01-10 16:00:03 -05:00
};
2015-05-19 23:04:28 -04:00
SocketUser.loadPage = function(socket, data, callback) {
function done(err, result) {
if (err) {
return callback(err);
}
var pageCount = Math.ceil(result.count / resultsPerPage);
var userData = {
matchCount: result.users.length,
timing: (process.elapsedTimeSince(startTime) / 1000).toFixed(2),
2015-05-19 23:04:28 -04:00
users: result.users,
pagination: pagination.create(data.page, pageCount)
};
callback(null, userData);
}
2015-05-19 23:07:01 -04:00
if (!data || !data.page) {
return callback(new Error('[[error:invalid-data]]'));
}
var startTime = process.hrtime();
2015-05-19 23:04:28 -04:00
var controllers = require('../controllers/users');
var pagination = require('../pagination');
var set = '';
data.sortBy = data.sortBy || 'joindate';
var resultsPerPage = parseInt(meta.config.userSearchResultsPerPage, 10) || 20;
var start = Math.max(0, data.page - 1) * resultsPerPage;
var stop = start + resultsPerPage - 1;
if (data.onlineOnly) {
async.parallel({
users: function(next) {
user.getUsersFromSet('users:online', socket.uid, 0, 49, next);
},
count: function(next) {
var now = Date.now();
db.sortedSetCount('users:online', now - 300000, now, next);
}
}, done);
} else if (data.sortBy === 'username') {
async.parallel({
count: function(next) {
db.sortedSetCard('username:sorted', next);
},
users: function(next) {
db.getSortedSetRangeByLex('username:sorted', '-', '+', start, stop - start + 1, function(err, result) {
if (err) {
return next(err);
}
var uids = result.map(function(user) {
return user && user.split(':')[1];
});
user.getUsers(uids, socket.uid, next);
});
}
}, done);
} else {
controllers.getUsersAndCount('users:joindate', socket.uid, start, stop, done);
}
};
2014-01-31 15:13:52 -05:00
SocketUser.setStatus = function(socket, status, callback) {
if (!socket.uid) {
2014-12-21 23:32:15 -05:00
return callback(new Error('[[error:invalid-uid]]'));
}
2014-09-24 13:36:32 -04:00
var allowedStatus = ['online', 'offline', 'dnd', 'away'];
if (allowedStatus.indexOf(status) === -1) {
2014-12-21 23:32:15 -05:00
return callback(new Error('[[error:invalid-user-status]]'));
2014-09-24 13:36:32 -04:00
}
2014-01-31 15:13:52 -05:00
user.setUserField(socket.uid, 'status', status, function(err) {
if (err) {
return callback(err);
}
var data = {
uid: socket.uid,
status: status
};
websockets.server.sockets.emit('event:user_status_change', data);
callback(null, data);
2014-01-31 15:13:52 -05:00
});
};
/* Exports */
2014-04-10 20:31:57 +01:00
module.exports = SocketUser;