From 3f6f4e347d69795d3cb5e679beb36a00a739a567 Mon Sep 17 00:00:00 2001 From: Baris Soner Usakli Date: Tue, 30 Jul 2013 18:30:43 -0400 Subject: [PATCH] change some socket calls to use the callback, when user changes their email if they are using a gravatar picture it will be updated too, fixed email updating --- public/src/app.js | 37 +++++++- public/src/forum/account.js | 66 ++++++++++++--- public/src/forum/accountedit.js | 60 +++++++++---- public/src/forum/accountsettings.js | 8 +- public/src/forum/following.js | 25 +++++- public/src/forum/recent.js | 16 +++- public/templates/account.tpl | 3 +- public/templates/following.tpl | 4 +- src/user.js | 125 +++++++++++++--------------- src/websockets.js | 75 +++++------------ 10 files changed, 251 insertions(+), 168 deletions(-) diff --git a/public/src/app.js b/public/src/app.js index 909481ee68..d5e65818a7 100644 --- a/public/src/app.js +++ b/public/src/app.js @@ -195,7 +195,29 @@ var socket, } } - + app.alertSuccess = function(message, timeout) { + if(!timeout) + timeout = 2000; + + app.alert({ + title: 'Success', + message: message, + type: 'success', + timeout: timeout + }); + } + + app.alertError = function(message, timeout) { + if(!timeout) + timeout = 2000; + + app.alert({ + title: 'Error', + message: message, + type: 'error', + timeout: timeout + }); + } app.current_room = null; app.enter_room = function(room) { @@ -254,9 +276,8 @@ var socket, }, 100); } - app.showLoginMessage = function() { - if(location.href.indexOf('loggedin') !== -1) { + function showAlert() { app.alert({ type: 'success', title: 'Welcome Back ' + app.username + '!', @@ -264,6 +285,14 @@ var socket, timeout: 5000 }); } + + if(location.href.indexOf('loggedin') !== -1) { + if(document.readyState !== 'complete') { + $(document).ready(showAlert); + } else { + showAlert(); + } + } } jQuery('document').ready(function() { @@ -272,7 +301,7 @@ var socket, loadConfig(); - + function addTouchEvents() { return; // later. diff --git a/public/src/forum/account.js b/public/src/forum/account.js index be0b58058b..78e4beec4c 100644 --- a/public/src/forum/account.js +++ b/public/src/forum/account.js @@ -12,23 +12,63 @@ postcount.html(app.addCommas(postcount.html())); var followBtn = $('#follow-btn'); - if(yourid === "0") { - followBtn.hide(); - } - else if(yourid !== theirid) { - if(isFollowing) + var unfollowBtn = $('#unfollow-btn'); + + if(yourid !== theirid) { + if(isFollowing) { followBtn.hide(); - else + unfollowBtn.show(); + } else { followBtn.show(); + unfollowBtn.hide(); + } } - else { - followBtn.hide(); - } - + followBtn.on('click', function() { - - followBtn.remove(); - socket.emit('api:user.follow', {uid: theirid}); + socket.emit('api:user.follow', {uid: theirid}, function(success) { + var username = $('.account-username a').html(); + if(success) { + followBtn.hide(); + unfollowBtn.show(); + app.alert({ + title: 'Following', + message: 'You are now following ' + username + '!', + type: 'success', + timeout: 2000 + }); + } else { + app.alert({ + title: 'Error', + message: 'There was an error following' + username + '!', + type: 'error', + timeout: 2000 + }); + } + }); + return false; + }); + + unfollowBtn.on('click', function() { + socket.emit('api:user.unfollow', {uid: theirid}, function(success) { + var username = $('.account-username a').html(); + if(success) { + followBtn.show(); + unfollowBtn.hide(); + app.alert({ + title: 'Unfollowing', + message: 'You are no longer following ' + username + '!', + type: 'success', + timeout: 2000 + }); + } else { + app.alert({ + title: 'Error', + message: 'There was an error unfollowing' + username + '!', + type: 'error', + timeout: 2000 + }); + } + }); return false; }); diff --git a/public/src/forum/accountedit.js b/public/src/forum/accountedit.js index b84d08abd7..cb77d6fbb3 100644 --- a/public/src/forum/accountedit.js +++ b/public/src/forum/accountedit.js @@ -85,7 +85,16 @@ $(document).ready(function() { type: type }; - socket.emit('api:user.changePicture', userData); + socket.emit('api:user.changePicture', userData, function(success) { + if(!success) { + app.alert({ + title: 'Error', + message: 'There was an error changing picture!', + type: 'error', + timeout: 2000 + }); + } + }); } var selectedImageType = ''; @@ -102,7 +111,21 @@ $(document).ready(function() { signature:$('#inputSignature').val() }; - socket.emit('api:user.updateProfile', userData); + socket.emit('api:user.updateProfile', userData, function(data) { + if(data.success) { + app.alertSuccess('Your profile has been updated successfully!'); + if(data.picture) { + $('#user-current-picture').attr('src', data.picture); + $('#user_label img').attr('src', data.picture); + } + if(data.gravatarpicture) { + $('#user-gravatar-picture').attr('src', data.gravatarpicture); + gravatarPicture = data.gravatarpicture; + } + } else { + app.alertError('There was an error updating your profile!'); + } + }); return false; }); @@ -231,24 +254,29 @@ $(document).ready(function() { password_confirm.on('keyup', onPasswordConfirmChanged); $('#changePasswordBtn').on('click', function() { + if(passwordvalid && passwordsmatch && currentPassword.val()) { - socket.emit('api:user.changePassword', { - 'currentPassword': currentPassword.val(), - 'newPassword': password.val() + socket.emit('api:user.changePassword', {'currentPassword': currentPassword.val(),'newPassword': password.val() }, function(data) { + + currentPassword.val(''); + password.val(''); + password_confirm.val(''); + password_notify.html(''); + password_confirm_notify.html(''); + passwordsmatch = false; + passwordvalid = false; + + if(data.err) { + app.alertError(data.err); + return; + } + + app.alertSuccess('Your password is updated!'); + }); } return false; }); - - socket.on('api:user.changePassword', function(data) { - currentPassword.val(''); - password.val(''); - password_confirm.val(''); - password_notify.html(''); - password_confirm_notify.html(''); - passwordsmatch = false; - passwordvalid = false; - }); - + }()); }); \ No newline at end of file diff --git a/public/src/forum/accountsettings.js b/public/src/forum/accountsettings.js index 1bc92cfff6..243f12dbeb 100644 --- a/public/src/forum/accountsettings.js +++ b/public/src/forum/accountsettings.js @@ -9,7 +9,13 @@ $(document).ready(function() { showemail: $('#showemailCheckBox').is(':checked')?1:0 }; - socket.emit('api:user.saveSettings', settings); + socket.emit('api:user.saveSettings', settings, function(success) { + if(success) { + app.alertSuccess('Settings saved!'); + } else { + app.alertError('There was an error saving settings!'); + } + }); return false; }); diff --git a/public/src/forum/following.js b/public/src/forum/following.js index 0b3a550f79..387dd7b512 100644 --- a/public/src/forum/following.js +++ b/public/src/forum/following.js @@ -10,17 +10,34 @@ $('#no-following-notice').show(); } + if(yourid !== theirid) { $('.unfollow-btn').hide(); } else { $('.unfollow-btn').on('click',function() { - - var removeBtn = $(this); + var unfollowBtn = $(this); var followingUid = $(this).attr('followingUid'); - removeBtn.parent().remove(); - socket.emit('api:user.unfollow', {uid: followingUid}); + socket.emit('api:user.unfollow', {uid: followingUid}, function(success) { + var username = unfollowBtn.attr('data-username'); + if(success) { + unfollowBtn.parent().remove(); + app.alert({ + title: 'Unfollowing', + message: 'You are no longer following ' + username + '!', + type: 'success', + timeout: 2000 + }); + } else { + app.alert({ + title: 'Error', + message: 'There was an error unfollowing ' + username + '!', + type: 'error', + timeout: 2000 + }); + } + }); return false; }); } diff --git a/public/src/forum/recent.js b/public/src/forum/recent.js index 067b595fc8..6b0ca925c7 100644 --- a/public/src/forum/recent.js +++ b/public/src/forum/recent.js @@ -47,10 +47,18 @@ }); $('#mark-allread-btn').on('click', function() { - socket.emit('api:topics.markAllRead'); - $(this).remove(); - $('#topics-container').empty(); - $('#category-no-topics').removeClass('hidden'); + var btn = $(this); + socket.emit('api:topics.markAllRead', {} , function(success) { + if(success) { + btn.remove(); + $('#topics-container').empty(); + $('#category-no-topics').removeClass('hidden'); + app.alertSuccess('All topics marked as read!'); + } else { + app.alertError('There was an error marking topics read!'); + } + }); + }); })(); \ No newline at end of file diff --git a/public/templates/account.tpl b/public/templates/account.tpl index e6b98690bb..fe08f3f049 100644 --- a/public/templates/account.tpl +++ b/public/templates/account.tpl @@ -25,7 +25,8 @@ offline
- Follow + Follow + Unfollow
diff --git a/public/templates/following.tpl b/public/templates/following.tpl index 7cc6409230..92124561cb 100644 --- a/public/templates/following.tpl +++ b/public/templates/following.tpl @@ -18,7 +18,6 @@
- - Unfollow + Unfollow
-
This user isn't following anyone :(
diff --git a/src/user.js b/src/user.js index 46dc82e7c7..bf96034650 100644 --- a/src/user.js +++ b/src/user.js @@ -185,9 +185,10 @@ var utils = require('./../public/src/utils.js'), }); } - User.updateProfile = function(socket, uid, data) { + User.updateProfile = function(uid, data, callback) { var fields = ['email', 'fullname', 'website', 'location', 'birthday', 'signature']; + var returnData = {success:false}; function isSignatureValid(next) { if(data['signature'] !== undefined && data['signature'].length > 150) { @@ -198,61 +199,69 @@ var utils = require('./../public/src/utils.js'), } function isEmailAvailable(next) { - if(data['email'] !== undefined) { - User.getUserField(uid, 'email', function(email) { - if(email !== data['email']) { - User.isEmailAvailable(data['email'], function(available) { - if(!available) { - next({error:'Email not available!'}, false); - } - }); - } else { - next(null, true); - } - }); - } else { - next(null, true); + if(!data['email']) { + return next(null, true); } + + User.getUserField(uid, 'email', function(email) { + if(email !== data['email']) { + User.isEmailAvailable(data['email'], function(available) { + if(!available) { + next({error:'Email not available!'}, false); + } else { + next(null, true); + } + }); + } else { + next(null, true); + } + }); } async.series([isSignatureValid, isEmailAvailable], function(err, results) { if(err) { - socket.emit('event:alert', { - title: 'Error', - message: err.error, - type: 'error', - timeout: 2000 - }); + console.log(err); + callback(returnData); } else { - updateFields(); + async.each(fields, updateField, function(err) { + if(err) { + console.log(err); + callback(returnData); + } else { + returnData.success = true; + callback(returnData); + } + }); } }); - function updateFields() { - for(var i = 0, key, ii = fields.length; i < ii; ++i) { - key = fields[i]; + function updateField(field, callback) { + if(data[field] !== undefined) { + if(field === 'email') { + var gravatarpicture = User.createGravatarURLFromEmail(data[field]); + User.setUserField(uid, 'gravatarpicture', gravatarpicture); + User.getUserFields(uid, ['email', 'picture', 'uploadedpicture'], function(userData) { + RDB.del('email:' + userData['email'] + ':uid'); + RDB.set('email:' + data['email'] + ':uid', uid); + User.setUserField(uid, field, data[field]); + if(userData.picture !== userData.uploadedpicture) { + returnData.picture = gravatarpicture; + User.setUserField(uid, 'picture', gravatarpicture); + } + returnData.gravatarpicture = gravatarpicture; + callback(null); + }); + return; + } else if(field === 'signature') { + data[field] = utils.strip_tags(data[field]); + } - if(data[key] !== undefined) { - if(key === 'email') { - User.setUserField(uid, 'gravatarpicture', User.createGravatarURLFromEmail(data[key])); - user.getUserField(uid, 'email', function(email) { - RDB.del('email:' + email + ':uid'); - RDB.set('email:' + data['email'] + ':uid', uid); - }); - } else if(key === 'signature') { - data[key] = utils.strip_tags(data[key]); - } + User.setUserField(uid, field, data[field]); - User.setUserField(uid, key, data[key]); - } + callback(null); + } else { + callback(null); } - - socket.emit('event:alert', { - title: 'Success', - message: 'Your profile has been updated successfully!', - type: 'success', - timeout: 2000 - }); } } @@ -268,15 +277,9 @@ var utils = require('./../public/src/utils.js'), }); } - User.changePassword = function(socket, uid, data, callback) { + User.changePassword = function(uid, data, callback) { if(!utils.isPasswordValid(data.newPassword)) { - socket.emit('event:alert', { - title: 'Error', - message: 'Invalid password!', - type: 'error', - timeout: 2000 - }); - callback(false); + callback({err:'Invalid password!'}); return; } @@ -284,7 +287,7 @@ var utils = require('./../public/src/utils.js'), bcrypt.compare(data.currentPassword, user_password, function(err, res) { if(err) { console.log(err); - callback(false); + callback({err:'bcrpyt compare error!'}); return; } @@ -292,22 +295,10 @@ var utils = require('./../public/src/utils.js'), User.hashPassword(data.newPassword, function(hash) { User.setUserField(uid, 'password', hash); - socket.emit('event:alert', { - title: 'Success', - message: 'Your password is updated!', - type: 'success', - timeout: 2000 - }); - callback(true); + callback({err:null}); }); } else { - socket.emit('event:alert', { - title: 'Warning', - message: 'Your current password is not correct!', - type: 'warning', - timeout: 2000 - }); - callback(false); + callback({err:'Your current password is not correct!'}); } }); }); diff --git a/src/websockets.js b/src/websockets.js index 73761c9afb..2535a602ec 100644 --- a/src/websockets.js +++ b/src/websockets.js @@ -247,19 +247,15 @@ var SocketIO = require('socket.io').listen(global.server, { log:false }), socket.emit('api:user.isOnline', isUserOnline(uid)); }); - socket.on('api:user.changePassword', function(data) { - user.changePassword(socket, uid, data, function(success) { - if(success) { - socket.emit('api:user.changePassword'); - } - }); + socket.on('api:user.changePassword', function(data, callback) { + user.changePassword(uid, data, callback); }); - socket.on('api:user.updateProfile', function(data) { - user.updateProfile(socket, uid, data); + socket.on('api:user.updateProfile', function(data, callback) { + user.updateProfile(uid, data, callback); }); - socket.on('api:user.changePicture', function(data) { + socket.on('api:user.changePicture', function(data, callback) { var type = data.type; @@ -267,71 +263,43 @@ var SocketIO = require('socket.io').listen(global.server, { log:false }), user.getUserFields(uid, ['picture'], function(fields) { fields.uid = uid; socket.emit('api:updateHeader', fields); + callback(true); }); } - if(type === 'gravatar') { + if(type === 'gravatar') { user.getUserField(uid, 'gravatarpicture', function(gravatar) { user.setUserField(uid, 'picture', gravatar); updateHeader(); }); - } - else if(type === 'uploaded') { + } else if(type === 'uploaded') { user.getUserField(uid, 'uploadedpicture', function(uploadedpicture) { user.setUserField(uid, 'picture', uploadedpicture); updateHeader(); }); + } else { + callback(false); } - }); - - socket.on('api:user.follow', function(data) { + socket.on('api:user.follow', function(data, callback) { if(uid) { - user.follow(uid, data.uid, function(success) { - if(success) { - user.getUserField(data.uid, 'username', function(username) { - socket.emit('event:alert', { - title: 'Following', - message: 'You are now following ' + username + '!', - type: 'success', - timeout: 2000 - }); - }); - } - }); + user.follow(uid, data.uid, callback); } }); - socket.on('api:user.unfollow', function(data) { + socket.on('api:user.unfollow', function(data, callback) { if(uid) { - user.unfollow(uid, data.uid, function(success) { - if(success) { - user.getUserField(data.uid, 'username', function(username) { - socket.emit('event:alert', { - title: 'Unfollowed', - message: 'You are no longer following ' + username + '!', - type: 'success', - timeout: 2000 - }); - }); - } - }); + user.unfollow(uid, data.uid, callback); } }); - socket.on('api:user.saveSettings', function(data) { + socket.on('api:user.saveSettings', function(data, callback) { if(uid) { user.setUserFields(uid, { showemail:data.showemail }); - - socket.emit('event:alert', { - title: 'Saved', - message: 'Settings saved!', - type: 'success', - timeout: 2000 - }); + callback(true); } }); @@ -339,15 +307,12 @@ var SocketIO = require('socket.io').listen(global.server, { log:false }), topics.post(socket, uid, data.title, data.content, data.category_id, data.images); }); - socket.on('api:topics.markAllRead', function(data) { + socket.on('api:topics.markAllRead', function(data, callback) { topics.markAllRead(uid, function(err, success) { if(!err && success) { - socket.emit('event:alert', { - title: 'Success', - message: 'All topics marked as read!', - type: 'success', - timeout: 2000 - }); + callback(true); + } else { + callback(false); } }); });