From afe97233aade254c84a938de82f57c7f3e9235d7 Mon Sep 17 00:00:00 2001 From: Baris Soner Usakli Date: Thu, 16 Jan 2014 17:32:33 -0500 Subject: [PATCH 1/7] more fixes --- public/src/forum/register.js | 6 +++++- src/socket.io/admin.js | 3 ++- src/user.js | 2 +- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/public/src/forum/register.js b/public/src/forum/register.js index 58f340b54c..aa8fc66b59 100644 --- a/public/src/forum/register.js +++ b/public/src/forum/register.js @@ -44,7 +44,11 @@ define(function() { } else { socket.emit('user.emailExists', { email: emailEl.val() - }, function(exists) { + }, function(err, exists) { + if(err) { + return app.alertError(err.message); + } + if (exists) { showError(email_notify, 'Email address already taken!'); } else { diff --git a/src/socket.io/admin.js b/src/socket.io/admin.js index 0a6523fcb4..1fc9e91fac 100644 --- a/src/socket.io/admin.js +++ b/src/socket.io/admin.js @@ -15,6 +15,7 @@ var groups = require('../groups'), async = require('async'), winston = require('winston'), + index = require('./index'), SocketAdmin = {}; @@ -252,7 +253,7 @@ SocketAdmin.config.set = function(socket, data, callback) { }); } - logger.monitorConfig({io: socket.server}, data); + logger.monitorConfig({io: index.server}, data); }); }; diff --git a/src/user.js b/src/user.js index 8c12611149..da0257b870 100644 --- a/src/user.js +++ b/src/user.js @@ -933,7 +933,7 @@ var bcrypt = require('bcrypt'), }, exists: function(email, callback) { User.getUidByEmail(email, function(err, exists) { - callback(!!exists); + callback(err, !!exists); }); }, confirm: function(code, callback) { From e061978bd5e4ef38fec038ad490326c8f9764188 Mon Sep 17 00:00:00 2001 From: Baris Soner Usakli Date: Thu, 16 Jan 2014 17:52:46 -0500 Subject: [PATCH 2/7] more fixes --- public/src/forum/account.js | 2 +- public/src/forum/accountedit.js | 10 +++++----- public/src/forum/users.js | 2 +- public/src/modules/chat.js | 2 +- src/socket.io/user.js | 32 +++++++++++++++++--------------- src/user.js | 12 ++++-------- 6 files changed, 29 insertions(+), 31 deletions(-) diff --git a/public/src/forum/account.js b/public/src/forum/account.js index 10ec2cd3b9..0b49d71cb4 100644 --- a/public/src/forum/account.js +++ b/public/src/forum/account.js @@ -85,7 +85,7 @@ define(['forum/accountheader'], function(header) { }); }; - Account.handleUserOnline = function(data) { + Account.handleUserOnline = function(err, data) { var onlineStatus = $('.account-online-status'); if (data.online) { diff --git a/public/src/forum/accountedit.js b/public/src/forum/accountedit.js index 1f80904a3f..d12b98f634 100644 --- a/public/src/forum/accountedit.js +++ b/public/src/forum/accountedit.js @@ -36,7 +36,7 @@ define(['forum/accountheader', 'uploader'], function(header, uploader) { gravatarPicture = data.gravatarpicture; } } else { - app.alertError('There was an error updating your profile! ' + err.error); + app.alertError('There was an error updating your profile! ' + err.message); } }); return false; @@ -186,7 +186,7 @@ define(['forum/accountheader', 'uploader'], function(header, uploader) { passwordvalid = false; if (err) { - app.alertError(err.error); + app.alertError(err.message); return; } @@ -206,9 +206,9 @@ define(['forum/accountheader', 'uploader'], function(header, uploader) { type: type }; - socket.emit('user.changePicture', userData, function(success) { - if (!success) { - app.alertError('There was an error changing picture!'); + socket.emit('user.changePicture', userData, function(err) { + if(err) { + app.alertError(err.message); } }); } diff --git a/public/src/forum/users.js b/public/src/forum/users.js index 72f58fd17d..611c71436f 100644 --- a/public/src/forum/users.js +++ b/public/src/forum/users.js @@ -80,7 +80,7 @@ define(function() { }, 250); }); - socket.on('user.isOnline', function(data) { + socket.on('user.isOnline', function(err, data) { if(getActiveSection() == 'online' && !loadingMoreUsers) { startLoading('users:online', 0, true); socket.emit('user.getOnlineAnonCount', {} , function(anonCount) { diff --git a/public/src/modules/chat.js b/public/src/modules/chat.js index 3efb695d66..68bb927523 100644 --- a/public/src/modules/chat.js +++ b/public/src/modules/chat.js @@ -90,7 +90,7 @@ define(['taskbar', 'string'], function(taskbar, S) { } function checkStatus(chatModal, callback) { - socket.emit('user.isOnline', chatModal.touid, function(data) { + socket.emit('user.isOnline', chatModal.touid, function(err, data) { if(data.online !== chatModal.online) { if(data.online) { module.appendChatMessage(chatModal, chatModal.username + ' is currently online.\n', data.timestamp); diff --git a/src/socket.io/user.js b/src/socket.io/user.js index 8d4e8b3da4..6a4d1f4382 100644 --- a/src/socket.io/user.js +++ b/src/socket.io/user.js @@ -33,7 +33,7 @@ SocketUser.reset.commit = function(socket, data, callback) { }; SocketUser.isOnline = function(socket, uid, callback) { - callback({ + callback(null, { online: module.parent.exports.isUserOnline(uid), uid: uid, timestamp: Date.now() @@ -54,29 +54,31 @@ SocketUser.changePicture = function(socket, data, callback) { function updateHeader() { user.getUserFields(socket.uid, ['picture'], function(err, fields) { - if (!err && fields) { + if(err) { + return callback(err); + } + + if (fields) { fields.uid = socket.uid; - socket.emit('meta.updateHeader', fields); - callback(true); - } else { - callback(false); + socket.emit('meta.updateHeader', null, fields); } + + callback(null); }); } if (type === 'gravatar') { - user.getUserField(socket.uid, 'gravatarpicture', function(err, gravatar) { - user.setUserField(socket.uid, 'picture', gravatar); - updateHeader(); - }); + type = 'gravatarpicture'; } else if (type === 'uploaded') { - user.getUserField(socket.uid, 'uploadedpicture', function(err, uploadedpicture) { - user.setUserField(socket.uid, 'picture', uploadedpicture); - updateHeader(); - }); + type = 'uploadedpicture'; } else { - callback(false); + return callback(new Error('invalid-image-type')); } + + user.getUserField(socket.uid, type, function(err, picture) { + user.setUserField(socket.uid, 'picture', picture); + updateHeader(); + }); }; SocketUser.follow = function(socket, data, callback) { diff --git a/src/user.js b/src/user.js index da0257b870..47f60cd88e 100644 --- a/src/user.js +++ b/src/user.js @@ -347,13 +347,11 @@ var bcrypt = require('bcrypt'), User.changePassword = function(uid, data, callback) { if (!utils.isPasswordValid(data.newPassword)) { - return callback({ - error: 'Invalid password!' - }); + return callback(new Error('Invalid password!')); } - User.getUserField(uid, 'password', function(err, user_password) { - bcrypt.compare(data.currentPassword, user_password, function(err, res) { + User.getUserField(uid, 'password', function(err, currentPassword) { + bcrypt.compare(data.currentPassword, currentPassword, function(err, res) { if (err) { return callback(err); } @@ -365,9 +363,7 @@ var bcrypt = require('bcrypt'), callback(null); }); } else { - callback({ - error: 'Your current password is not correct!' - }); + callback(new Error('Your current password is not correct!')); } }); }); From 39b631a3a8f3dc6d78bc779e37f6f01b06bdb93c Mon Sep 17 00:00:00 2001 From: Baris Soner Usakli Date: Thu, 16 Jan 2014 18:06:19 -0500 Subject: [PATCH 3/7] more fixes --- public/src/app.js | 2 +- public/src/forum/account.js | 28 ++++++++++++++-------------- public/src/forum/accountsettings.js | 7 +++---- public/src/forum/admin/footer.js | 11 ++++++++++- public/src/forum/admin/settings.js | 23 ++++++++++++----------- src/socket.io/admin.js | 24 ++++++++++-------------- src/socket.io/user.js | 8 +++----- src/user.js | 26 ++++++++------------------ 8 files changed, 61 insertions(+), 68 deletions(-) diff --git a/public/src/app.js b/public/src/app.js index 2cc6f99f1e..bce01992af 100644 --- a/public/src/app.js +++ b/public/src/app.js @@ -253,7 +253,7 @@ var socket, uids.push(this.getAttribute('data-uid')); }); - socket.emit('user.get_online_users', uids, function (users) { + socket.emit('user.getOnlineUsers', uids, function (err, users) { jQuery('a.username-field').each(function () { if (this.processed === true) return; diff --git a/public/src/forum/account.js b/public/src/forum/account.js index 0b49d71cb4..d3c639cf66 100644 --- a/public/src/forum/account.js +++ b/public/src/forum/account.js @@ -37,14 +37,14 @@ define(['forum/accountheader'], function(header) { followBtn.on('click', function() { socket.emit('user.follow', { uid: theirid - }, function(success) { - if (success) { - followBtn.addClass('hide'); - unfollowBtn.removeClass('hide'); - app.alertSuccess('You are now following ' + username + '!'); - } else { - app.alertError('There was an error following' + username + '!'); + }, function(err) { + if(err) { + return app.alertError('There was an error following' + username + '!'); } + + followBtn.addClass('hide'); + unfollowBtn.removeClass('hide'); + app.alertSuccess('You are now following ' + username + '!'); }); return false; }); @@ -52,14 +52,14 @@ define(['forum/accountheader'], function(header) { unfollowBtn.on('click', function() { socket.emit('user.unfollow', { uid: theirid - }, function(success) { - if (success) { - followBtn.removeClass('hide'); - unfollowBtn.addClass('hide'); - app.alertSuccess('You are no longer following ' + username + '!'); - } else { - app.alertError('There was an error unfollowing ' + username + '!'); + }, function(err) { + if(err) { + return app.alertError('There was an error unfollowing ' + username + '!'); } + + followBtn.removeClass('hide'); + unfollowBtn.addClass('hide'); + app.alertSuccess('You are no longer following ' + username + '!'); }); return false; }); diff --git a/public/src/forum/accountsettings.js b/public/src/forum/accountsettings.js index c87a9c8da1..db21bb4efa 100644 --- a/public/src/forum/accountsettings.js +++ b/public/src/forum/accountsettings.js @@ -11,11 +11,10 @@ define(['forum/accountheader'], function(header) { }; socket.emit('user.saveSettings', settings, function(err) { - if (!err) { - app.alertSuccess('Settings saved!'); - } else { - app.alertError('There was an error saving settings!'); + if (err) { + return app.alertError('There was an error saving settings!'); } + app.alertSuccess('Settings saved!'); }); return false; }); diff --git a/public/src/forum/admin/footer.js b/public/src/forum/admin/footer.js index ea0fedbd56..f81e23d110 100644 --- a/public/src/forum/admin/footer.js +++ b/public/src/forum/admin/footer.js @@ -15,6 +15,15 @@ jQuery('document').ready(function() { }, false); }); -socket.emit('admin.config.get', function(config) { +socket.emit('admin.config.get', function(err, config) { + if(err) { + return app.alert({ + alert_id: 'config_status', + timeout: 2500, + title: 'Error', + message: 'NodeBB encountered a problem getting config', + type: 'danger' + }); + } app.config = config; }); diff --git a/public/src/forum/admin/settings.js b/public/src/forum/admin/settings.js index 03325086d7..777b82bb9f 100644 --- a/public/src/forum/admin/settings.js +++ b/public/src/forum/admin/settings.js @@ -73,17 +73,9 @@ define(['uploader'], function(uploader) { socket.emit('admin.config.set', { key: key, value: value - }, function(data) { - if (data.status === 'ok') { - app.alert({ - alert_id: 'config_status', - timeout: 2500, - title: 'Changes Saved', - message: 'Your changes to the NodeBB configuration have been saved.', - type: 'success' - }); - } else { - app.alert({ + }, function(err) { + if(err) { + return app.alert({ alert_id: 'config_status', timeout: 2500, title: 'Changes Not Saved', @@ -91,6 +83,15 @@ define(['uploader'], function(uploader) { type: 'danger' }); } + + app.alert({ + alert_id: 'config_status', + timeout: 2500, + title: 'Changes Saved', + message: 'Your changes to the NodeBB configuration have been saved.', + type: 'success' + }); + }); } }); diff --git a/src/socket.io/admin.js b/src/socket.io/admin.js index 1fc9e91fac..33e188b4c5 100644 --- a/src/socket.io/admin.js +++ b/src/socket.io/admin.js @@ -233,26 +233,22 @@ SocketAdmin.plugins.toggle = function(socket, plugin_id) { SocketAdmin.config = {}; SocketAdmin.config.get = function(socket, data, callback) { - meta.configs.list(function(err, config) { - if (!err) { - callback(config); - } - }); + meta.configs.list(callback); }; SocketAdmin.config.set = function(socket, data, callback) { meta.configs.set(data.key, data.value, function(err) { - if (!err) { - callback({ - status: 'ok' - }); - - plugins.fireHook('action:config.set', { - key: data.key, - value: data.value - }); + if(err) { + return callback(err); } + callback(null); + + plugins.fireHook('action:config.set', { + key: data.key, + value: data.value + }); + logger.monitorConfig({io: index.server}, data); }); }; diff --git a/src/socket.io/user.js b/src/socket.io/user.js index 6a4d1f4382..1237f418b9 100644 --- a/src/socket.io/user.js +++ b/src/socket.io/user.js @@ -97,13 +97,11 @@ SocketUser.saveSettings = function(socket, data, callback) { if (socket.uid) { user.setUserFields(socket.uid, { showemail: data.showemail - }, function(err, result) { - callback(err); - }); + }, callback); } }; -SocketUser.get_online_users = function(socket, data, callback) { +SocketUser.getOnlineUsers = function(socket, data, callback) { var returnData = []; for (var i = 0; i < data.length; ++i) { @@ -115,7 +113,7 @@ SocketUser.get_online_users = function(socket, data, callback) { } } - callback(returnData); + callback(null, returnData); }; SocketUser.getOnlineAnonCount = function(socket, data, callback) { diff --git a/src/user.js b/src/user.js index 47f60cd88e..b17e612ba9 100644 --- a/src/user.js +++ b/src/user.js @@ -537,31 +537,21 @@ var bcrypt = require('bcrypt'), User.follow = function(uid, followid, callback) { db.setAdd('following:' + uid, followid, function(err, data) { - if (!err) { - db.setAdd('followers:' + followid, uid, function(err, data) { - if (!err) { - callback(true); - } else { - console.log(err); - callback(false); - } - }); - } else { - console.log(err); - callback(false); + if(err) { + return callback(err); } + + db.setAdd('followers:' + followid, uid, callback); }); }; User.unfollow = function(uid, unfollowid, callback) { db.setRemove('following:' + uid, unfollowid, function(err, data) { - if (!err) { - db.setRemove('followers:' + unfollowid, uid, function(err, data) { - callback(data); - }); - } else { - console.log(err); + if(err) { + return callback(err); } + + db.setRemove('followers:' + unfollowid, uid, callback); }); }; From 4366c11ec0c4e9731abc7c42c491009b3b4e5bbc Mon Sep 17 00:00:00 2001 From: Baris Soner Usakli Date: Thu, 16 Jan 2014 18:10:38 -0500 Subject: [PATCH 4/7] users.js fix --- public/src/forum/footer.js | 2 +- public/src/forum/users.js | 2 +- src/socket.io/user.js | 16 ++++++++-------- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/public/src/forum/footer.js b/public/src/forum/footer.js index c4b447f7ca..1bcdac57bf 100644 --- a/public/src/forum/footer.js +++ b/public/src/forum/footer.js @@ -8,7 +8,7 @@ define(['notifications', 'chat'], function(Notifications, Chat) { Chat.prepareDOM(); translator.prepareDOM(); - function updateUnreadCount(count) { + function updateUnreadCount(err, count) { $('#unread-count').toggleClass('unread-count', count > 0); $('#unread-count').attr('data-content', count > 20 ? '20+' : count); } diff --git a/public/src/forum/users.js b/public/src/forum/users.js index 611c71436f..28bc10a67b 100644 --- a/public/src/forum/users.js +++ b/public/src/forum/users.js @@ -83,7 +83,7 @@ define(function() { socket.on('user.isOnline', function(err, data) { if(getActiveSection() == 'online' && !loadingMoreUsers) { startLoading('users:online', 0, true); - socket.emit('user.getOnlineAnonCount', {} , function(anonCount) { + socket.emit('user.getOnlineAnonCount', {} , function(err, anonCount) { if(parseInt(anonCount, 10) > 0) { $('#users-container .anon-user').removeClass('hide'); $('#online_anon_count').html(anonCount); diff --git a/src/socket.io/user.js b/src/socket.io/user.js index 1237f418b9..b629d1a648 100644 --- a/src/socket.io/user.js +++ b/src/socket.io/user.js @@ -117,12 +117,12 @@ SocketUser.getOnlineUsers = function(socket, data, callback) { }; SocketUser.getOnlineAnonCount = function(socket, data, callback) { - callback(module.parent.exports.getOnlineAnonCount()); + callback(null, module.parent.exports.getOnlineAnonCount()); }; SocketUser.getUnreadCount = function(socket, data, callback) { topics.getUnreadTids(socket.uid, 0, 19, function(err, tids) { - callback(tids.length); + callback(err, tids?tids.length:0); }); }; @@ -135,13 +135,13 @@ SocketUser.loadMore = function(socket, data, callback) { end = start + 19; user.getUsers(data.set, start, end, function(err, data) { - if (err) { - winston.err(err); - } else { - callback({ - users: data - }); + if(err) { + return callback(err); } + + callback(null, { + users: data + }); }); }; From ddb9a4a60e0090580e92f9d3b553dc477e10776a Mon Sep 17 00:00:00 2001 From: Baris Soner Usakli Date: Thu, 16 Jan 2014 18:12:31 -0500 Subject: [PATCH 5/7] fixed user load more --- public/src/forum/admin/users.js | 4 ++-- public/src/forum/users.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/public/src/forum/admin/users.js b/public/src/forum/admin/users.js index d95228b9a3..802431735c 100644 --- a/public/src/forum/admin/users.js +++ b/public/src/forum/admin/users.js @@ -245,8 +245,8 @@ define(function() { socket.emit('user.loadMore', { set: set, after: $('#users-container').children().length - }, function(data) { - if (data.users.length) { + }, function(err, data) { + if (data && data.users.length) { onUsersLoaded(data.users); } loadingMoreUsers = false; diff --git a/public/src/forum/users.js b/public/src/forum/users.js index 28bc10a67b..de00f3bc38 100644 --- a/public/src/forum/users.js +++ b/public/src/forum/users.js @@ -126,8 +126,8 @@ define(function() { socket.emit('user.loadMore', { set: set, after: after - }, function(data) { - if (data.users.length) { + }, function(err, data) { + if (data && data.users.length) { onUsersLoaded(data.users, emptyContainer); $('#load-more-users-btn').removeClass('disabled'); } else { From ba8e98520cde9d98859d205d7b57912ba45cee9a Mon Sep 17 00:00:00 2001 From: Baris Soner Usakli Date: Thu, 16 Jan 2014 18:18:42 -0500 Subject: [PATCH 6/7] added data checks to user calls --- src/socket.io/user.js | 68 +++++++++++++++++++++++++++---------------- 1 file changed, 43 insertions(+), 25 deletions(-) diff --git a/src/socket.io/user.js b/src/socket.io/user.js index b629d1a648..0888f57eed 100644 --- a/src/socket.io/user.js +++ b/src/socket.io/user.js @@ -14,22 +14,30 @@ SocketUser.count = function(socket, data, callback) { }; SocketUser.emailExists = function(socket, data, callback) { - user.email.exists(data.email, callback); + if(data && data.email) { + user.email.exists(data.email, callback); + } }; // Password Reset SocketUser.reset = {}; SocketUser.reset.send = function(socket, data, callback) { - user.reset.send(socket, data.email); + if(data && data.email) { + user.reset.send(socket, data.email); + } }; SocketUser.reset.valid = function(socket, data, callback) { - user.reset.validate(socket, data.code); + if(data && data.code) { + user.reset.validate(socket, data.code); + } }; SocketUser.reset.commit = function(socket, data, callback) { - user.reset.commit(socket, data.code, data.password); + if(data && data.code && data.password) { + user.reset.commit(socket, data.code, data.password); + } }; SocketUser.isOnline = function(socket, uid, callback) { @@ -41,14 +49,21 @@ SocketUser.isOnline = function(socket, uid, callback) { }; SocketUser.changePassword = function(socket, data, callback) { - user.changePassword(socket.uid, data, callback); + if(data) { + user.changePassword(socket.uid, data, callback); + } }; SocketUser.updateProfile = function(socket, data, callback) { - user.updateProfile(socket.uid, data, callback); + if(data) { + user.updateProfile(socket.uid, data, callback); + } }; SocketUser.changePicture = function(socket, data, callback) { + if(!data) { + return; + } var type = data.type; @@ -82,19 +97,19 @@ SocketUser.changePicture = function(socket, data, callback) { }; SocketUser.follow = function(socket, data, callback) { - if (socket.uid) { + if (socket.uid && data) { user.follow(socket.uid, data.uid, callback); } }; SocketUser.unfollow = function(socket, data, callback) { - if (socket.uid) { + if (socket.uid && data) { user.unfollow(socket.uid, data.uid, callback); } }; SocketUser.saveSettings = function(socket, data, callback) { - if (socket.uid) { + if (socket.uid && data) { user.setUserFields(socket.uid, { showemail: data.showemail }, callback); @@ -103,13 +118,14 @@ SocketUser.saveSettings = function(socket, data, callback) { SocketUser.getOnlineUsers = function(socket, data, callback) { var returnData = []; - - for (var i = 0; i < data.length; ++i) { - var uid = data[i]; - if (module.parent.exports.isUserOnline(uid)) { - returnData.push(uid); - } else { - returnData.push(0); + if(data) { + for (var i = 0; i < data.length; ++i) { + var uid = data[i]; + if (module.parent.exports.isUserOnline(uid)) { + returnData.push(uid); + } else { + returnData.push(0); + } } } @@ -131,18 +147,20 @@ SocketUser.getActiveUsers = function(socket, data, callback) { }; SocketUser.loadMore = function(socket, data, callback) { - var start = data.after, - end = start + 19; + if(data) { + var start = data.after, + end = start + 19; - user.getUsers(data.set, start, end, function(err, data) { - if(err) { - return callback(err); - } + user.getUsers(data.set, start, end, function(err, data) { + if(err) { + return callback(err); + } - callback(null, { - users: data + callback(null, { + users: data + }); }); - }); + } }; /* Exports */ From 6da6baa213ebe23d518cda0ac68719abc8abfdb9 Mon Sep 17 00:00:00 2001 From: Baris Soner Usakli Date: Thu, 16 Jan 2014 18:50:45 -0500 Subject: [PATCH 7/7] more crash fixes to socketS --- src/socket.io/index.js | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/socket.io/index.js b/src/socket.io/index.js index 8c27bbaa14..91e7dea07a 100644 --- a/src/socket.io/index.js +++ b/src/socket.io/index.js @@ -140,8 +140,11 @@ Sockets.init = function() { }); } + if(!payload.name) { + return winston.warn('[socket.io] Empty method name'); + } - var parts = payload.name.split('.'), + var parts = payload.name.toString().split('.'), namespace = parts.slice(0, 1), methodToCall = parts.reduce(function(prev, cur) { if (prev !== null && prev[cur]) { @@ -151,17 +154,16 @@ Sockets.init = function() { } }, Namespaces); - if (methodToCall !== null) { + if(!methodToCall) { + return winston.warn('[socket.io] Unrecognized message: ' + payload.name); + } - if (Namespaces[namespace].before) { - Namespaces[namespace].before(socket, function() { - callMethod(methodToCall); - }); - } else { + if (Namespaces[namespace].before) { + Namespaces[namespace].before(socket, function() { callMethod(methodToCall); - } + }); } else { - winston.warn('[socket.io] Unrecognized message: ' + payload.name); + callMethod(methodToCall); } }); });