From dfa9f203e66db0ae7c3f65cd6f6bd810bfde51b6 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Thu, 9 Jan 2014 21:27:50 -0500 Subject: [PATCH] crashing a lot less now, heh --- public/src/app.js | 72 ++++++++++--------- public/src/forum/home.js | 13 ++-- public/src/forum/register.js | 8 +-- public/src/forum/reset.js | 2 +- public/src/forum/reset_code.js | 10 +-- src/socket.io/index.js | 40 +++++++---- src/user.js | 4 +- src/websockets.js | 122 +-------------------------------- 8 files changed, 81 insertions(+), 190 deletions(-) diff --git a/public/src/app.js b/public/src/app.js index 415f10ae6d..03d6554aa6 100644 --- a/public/src/app.js +++ b/public/src/app.js @@ -111,42 +111,6 @@ var socket, }); }); - socket.on('api:user.get_online_users', function (users) { - jQuery('a.username-field').each(function () { - if (this.processed === true) - return; - - var el = jQuery(this), - uid = el.parents('li').attr('data-uid'); - - if (uid && jQuery.inArray(uid, users) !== -1) { - el.find('i').remove(); - el.prepend(''); - } else { - el.find('i').remove(); - el.prepend(''); - } - - el.processed = true; - }); - jQuery('button .username-field').each(function () { - //DRY FAIL - if (this.processed === true) - return; - - var el = jQuery(this), - uid = el.parents('li').attr('data-uid'); - - if (uid && jQuery.inArray(uid, users) !== -1) { - el.parent().addClass('btn-success'); - } else { - el.parent().addClass('btn-danger'); - } - - el.processed = true; - }); - }); - socket.on('event:banned', function() { app.alert({ title: 'Banned', @@ -287,7 +251,41 @@ var socket, uids.push(this.getAttribute('data-uid')); }); - socket.emit('api:user.get_online_users', uids); + socket.emit('api:user.get_online_users', uids, function (users) { + jQuery('a.username-field').each(function () { + if (this.processed === true) + return; + + var el = jQuery(this), + uid = el.parents('li').attr('data-uid'); + + if (uid && jQuery.inArray(uid, users) !== -1) { + el.find('i').remove(); + el.prepend(''); + } else { + el.find('i').remove(); + el.prepend(''); + } + + el.processed = true; + }); + jQuery('button .username-field').each(function () { + //DRY FAIL + if (this.processed === true) + return; + + var el = jQuery(this), + uid = el.parents('li').attr('data-uid'); + + if (uid && jQuery.inArray(uid, users) !== -1) { + el.parent().addClass('btn-success'); + } else { + el.parent().addClass('btn-danger'); + } + + el.processed = true; + }); + }); }; function highlightNavigationLink() { diff --git a/public/src/forum/home.js b/public/src/forum/home.js index 971be662a6..adcf297d7e 100644 --- a/public/src/forum/home.js +++ b/public/src/forum/home.js @@ -4,24 +4,21 @@ define(function() { home.init = function() { ajaxify.register_events([ - 'user.count', + 'api:user.count', 'post.stats', - 'api:user.active.get' + 'api:user.getActiveUsers' ]); - socket.emit('user.count', {}); - socket.on('user.count', function(data) { + socket.emit('api:user.count', function(data) { $('#stats_users').html(utils.makeNumberHumanReadable(data.count)).attr('title', data.count); }); - socket.emit('post.stats'); - socket.on('post.stats', function(data) { + socket.emit('post.stats', function(data) { $('#stats_topics').html(utils.makeNumberHumanReadable(data.topics)).attr('title', data.topics); $('#stats_posts').html(utils.makeNumberHumanReadable(data.posts)).attr('title', data.posts); }); - socket.emit('api:user.active.get'); - socket.on('api:user.active.get', function(data) { + socket.emit('api:user.getActiveUsers', function(data) { $('#stats_online').html(data.users); }); } diff --git a/public/src/forum/register.js b/public/src/forum/register.js index 65c698c263..881fc13bb2 100644 --- a/public/src/forum/register.js +++ b/public/src/forum/register.js @@ -42,7 +42,7 @@ define(function() { if (!utils.isEmailValid(emailEl.val())) { showError(email_notify, 'Invalid email address.'); } else { - socket.emit('user.email.exists', { + socket.emit('api:user.emailExists', { email: emailEl.val() }); } @@ -117,9 +117,9 @@ define(function() { validatePasswordConfirm(); }); - ajaxify.register_events(['user.exists', 'user.email.exists']); + ajaxify.register_events(['api:user.exists', 'api:user.emailExists']); - socket.on('user.exists', function(data) { + socket.on('api:user.exists', function(data) { if (data.exists === true) { showError(username_notify, 'Username already taken!'); } else { @@ -127,7 +127,7 @@ define(function() { } }); - socket.on('user.email.exists', function(data) { + socket.on('api:user.emailExists', function(data) { if (data.exists === true) { showError(email_notify, 'Email address already taken!'); } else { diff --git a/public/src/forum/reset.js b/public/src/forum/reset.js index 150b282fe0..2d2b797a4b 100644 --- a/public/src/forum/reset.js +++ b/public/src/forum/reset.js @@ -8,7 +8,7 @@ define(function() { document.getElementById('reset').onclick = function() { if (inputEl.value.length > 0 && inputEl.value.indexOf('@') !== -1) { - socket.emit('user:reset.send', { + socket.emit('api:user.reset.send', { email: inputEl.value }); } else { diff --git a/public/src/forum/reset_code.js b/public/src/forum/reset_code.js index f36ff00836..46c7dd9c0a 100644 --- a/public/src/forum/reset_code.js +++ b/public/src/forum/reset_code.js @@ -21,7 +21,7 @@ define(function() { noticeEl.querySelector('p').innerHTML = 'The two passwords you\'ve entered do not match.'; noticeEl.style.display = 'block'; } else { - socket.emit('user:reset.commit', { + socket.emit('api:user.reset.commit', { code: reset_code, password: password.value }); @@ -29,13 +29,13 @@ define(function() { }, false); // Enable the form if the code is valid - socket.emit('user:reset.valid', { + socket.emit('api:user.reset.valid', { code: reset_code }); - ajaxify.register_events(['user:reset.valid', 'user:reset.commit']); - socket.on('user:reset.valid', function(data) { + ajaxify.register_events(['api:user.reset.valid', 'api:user.reset.commit']); + socket.on('api:user.reset.valid', function(data) { if ( !! data.valid) resetEl.disabled = false; else { var formEl = document.getElementById('reset-form'); @@ -45,7 +45,7 @@ define(function() { } }) - socket.on('user:reset.commit', function(data) { + socket.on('api:user.reset.commit', function(data) { if (data.status === 'ok') { $('#error').hide(); $('#notice').hide(); diff --git a/src/socket.io/index.js b/src/socket.io/index.js index 93d6245532..3b676fef59 100644 --- a/src/socket.io/index.js +++ b/src/socket.io/index.js @@ -2,6 +2,7 @@ var SocketIO = require('socket.io'), socketioWildcard = require('socket.io-wildcard'), util = require('util'), async = require('async'), + path = require('path'), fs = require('fs'), nconf = require('nconf'), express = require('express'), @@ -30,6 +31,16 @@ Sockets.init = function() { 'browser client minification': true }); + fs.readdir(__dirname, function(err, files) { + files.splice(files.indexOf('index.js'), 1); + + async.each(files, function(lib, next) { + lib = lib.slice(0, -3); + Namespaces[lib] = require('./' + lib); + next(); + }); + }); + io.sockets.on('connection', function(socket) { var hs = socket.handshake, sessionID, uid, lastPostTime = 0; @@ -123,35 +134,30 @@ Sockets.init = function() { } }); - socket.on('*', function(payload) { + socket.on('*', function(payload, callback) { // Ignore all non-api messages if (payload.name.substr(0, 4) !== 'api:') { return; } else { // Deconstruct the message - var parts = payload.name.split('.'), + var parts = payload.name.slice(4).split('.'), namespace = parts[0], command = parts[1], subcommand = parts[2], // MUST ADD RECURSION (:P) executeHandler = function(args) { + winston.info('[socket.io] Executing: ' + payload.name); if (!subcommand) { - Namespaces[namespace][command](args); + Namespaces[namespace][command].call(Namespaces[namespace], args.length ? args : callback ? callback : undefined, args.length ? callback : undefined); } else { - Namespaces[namespace][command][subcommand](args); + Namespaces[namespace][command][subcommand].call(Namespaces[namespace][command], args, callback); } }; if (Namespaces[namespace]) { + console.log(payload); executeHandler(payload.args); } else { - fs.exists(path.join(__dirname, namespace + '.js'), function(exists) { - if (exists) { - Namespaces[namespace] = require('./' + namespace); - executeHandler(payload.args); - } else { - winston.warn('[socket.io] Unrecognized message: ' + payload.name); - } - }) + winston.warn('[socket.io] Unrecognized message: ' + payload.name); } } console.log('message!', arguments); @@ -251,7 +257,8 @@ function emitTopicPostStats() { }); } -function emitOnlineUserCount() { +Sockets.emitOnlineUserCount = emitOnlineUserCount; +function emitOnlineUserCount(callback) { var anon = userSockets[0] ? userSockets[0].length : 0; var registered = Object.keys(userSockets).length; if (anon) @@ -261,7 +268,12 @@ function emitOnlineUserCount() { users: registered + anon, anon: anon }; - io.sockets.emit('api:user.active.get', returnObj) + + if (callback) { + callback(returnObj); + } else { + io.sockets.emit('api:user.active.get', returnObj) + } } function emitAlert(socket, title, message) { diff --git a/src/user.js b/src/user.js index e712c35e30..f3c737fd62 100644 --- a/src/user.js +++ b/src/user.js @@ -667,13 +667,13 @@ var bcrypt = require('bcrypt'), }); }; - User.count = function(socket) { + User.count = function(callback) { db.getObjectField('global', 'userCount', function(err, count) { if(err) { return; } - socket.emit('user.count', { + callback({ count: count ? count : 0 }); }); diff --git a/src/websockets.js b/src/websockets.js index 24a9411093..8c91a63320 100644 --- a/src/websockets.js +++ b/src/websockets.js @@ -93,121 +93,19 @@ websockets.init = function(io) { }); - socket.on('user.exists', function(data) { - if (data.username) { - user.exists(utils.slugify(data.username), function(exists) { - socket.emit('user.exists', { - exists: exists - }); - }); - } - }); - socket.on('user.count', function(data) { - user.count(socket, data); - }); - socket.on('post.stats', function(data) { - emitTopicPostStats(); - }); - socket.on('user.email.exists', function(data) { - user.email.exists(socket, data.email); - }); - - socket.on('user:reset.send', function(data) { - user.reset.send(socket, data.email); - }); - - socket.on('user:reset.valid', function(data) { - user.reset.validate(socket, data.code); - }); - - socket.on('user:reset.commit', function(data) { - user.reset.commit(socket, data.code, data.password); - }); - socket.on('api:user.get_online_users', function(data) { - var returnData = []; - - for (var i = 0; i < data.length; ++i) { - var uid = data[i]; - if (isUserOnline(uid)) - returnData.push(uid); - else - returnData.push(0); - } - socket.emit('api:user.get_online_users', returnData); - }); - - socket.on('api:user.isOnline', function(uid, callback) { - callback({ - online: isUserOnline(uid), - uid: uid, - timestamp: Date.now() - }); - }); - - socket.on('api:user.changePassword', function(data, callback) { - user.changePassword(uid, data, callback); - }); - - socket.on('api:user.updateProfile', function(data, callback) { - user.updateProfile(uid, data, callback); + socket.on('post.stats', function(data) { + emitTopicPostStats(); }); - socket.on('api:user.changePicture', function(data, callback) { - var type = data.type; - - function updateHeader() { - user.getUserFields(uid, ['picture'], function(err, fields) { - if (!err && fields) { - fields.uid = uid; - socket.emit('api:updateHeader', fields); - callback(true); - } else { - callback(false); - } - }); - } - if (type === 'gravatar') { - user.getUserField(uid, 'gravatarpicture', function(err, gravatar) { - user.setUserField(uid, 'picture', gravatar); - updateHeader(); - }); - } else if (type === 'uploaded') { - user.getUserField(uid, 'uploadedpicture', function(err, uploadedpicture) { - user.setUserField(uid, 'picture', uploadedpicture); - updateHeader(); - }); - } else { - callback(false); - } - }); - socket.on('api:user.follow', function(data, callback) { - if (uid) { - user.follow(uid, data.uid, callback); - } - }); - socket.on('api:user.unfollow', function(data, callback) { - if (uid) { - user.unfollow(uid, data.uid, callback); - } - }); - socket.on('api:user.saveSettings', function(data, callback) { - if (uid) { - user.setUserFields(uid, { - showemail: data.showemail - }, function(err, result) { - callback(err); - }); - } - }); @@ -346,18 +244,8 @@ websockets.init = function(io) { }); }); - socket.on('api:user.getOnlineAnonCount', function(data, callback) { - callback(websockets.getOnlineAnonCount()); - }); - - - - socket.on('api:user.active.get', function() { - emitOnlineUserCount(); - }); - socket.on('api:posts.favourite', function(data) { favourites.favourite(data.pid, data.room_id, uid, socket); }); @@ -995,11 +883,7 @@ websockets.init = function(io) { }); }); - socket.on('api:meta.buildTitle', function(text, callback) { - meta.title.build(text, function(err, title) { - callback(title); - }); - }); + /* GROUPS