diff --git a/app.js b/app.js index b9759de5bc..fb70d170c8 100644 --- a/app.js +++ b/app.js @@ -17,13 +17,6 @@ global.modules = modules; // global.uid = 1; -process.on('uncaughtException', function(err) { - // handle the error safely - console.log("error message "+err); - global.socket.emit('event:consolelog',{type:'uncaughtException',stack:err.stack,error:err.toString()}); -}); - - (function(config) { config['ROOT_DIRECTORY'] = __dirname; diff --git a/src/posts.js b/src/posts.js index 9e46407864..35fdaa30a1 100644 --- a/src/posts.js +++ b/src/posts.js @@ -60,7 +60,7 @@ var RDB = require('./redis.js'), } - Posts.reply = function(tid, uid, content) { + Posts.reply = function(socket, tid, uid, content) { Posts.create(uid, content, function(pid) { RDB.rpush('tid:' + tid + ':posts', pid); diff --git a/src/topics.js b/src/topics.js index 15cadbb806..ba8da84519 100644 --- a/src/topics.js +++ b/src/topics.js @@ -93,8 +93,8 @@ var RDB = require('./redis.js'), }); } - - Topics.post = function(uid, title, content, category) { + Topics.post = function(socket, uid, title, content, category) { + if (uid === 0) { socket.emit('event:alert', { title: 'Thank you for posting', @@ -144,7 +144,6 @@ var RDB = require('./redis.js'), // User Details - move this out later RDB.lpush('uid:' + uid + ':topics', tid); - socket.emit('event:alert', { title: 'Thank you for posting', message: 'You have successfully posted. Click here to view your post.', diff --git a/src/user.js b/src/user.js index 1d3ae16402..2b077e9163 100644 --- a/src/user.js +++ b/src/user.js @@ -7,7 +7,7 @@ var config = require('../config.js'), (function(User) { - User.get = function(uid, fields) { + User.get = function(socket, uid, fields) { if (uid > 0) { var keys = [], returnData = { @@ -48,7 +48,7 @@ var config = require('../config.js'), } } - User.login = function(user) { + User.login = function(socket, user) { if (user.username == null || user.password == null) { return socket.emit('user.login', {'status': 0, 'message': 'Missing fields'}); } @@ -160,7 +160,7 @@ var config = require('../config.js'), } User.create = function(username, password, email, callback) { - User.exists(username, function(exists) { + User.exists(null, username, function(exists) { if (exists) { return callback('user-exists', 0); } @@ -187,7 +187,7 @@ var config = require('../config.js'), }; - User.exists = function(username, callback) { + User.exists = function(socket, username, callback) { User.get_uid_by_username(username, function(exists) { exists = !!exists; @@ -195,12 +195,14 @@ var config = require('../config.js'), else socket.emit('user.exists', {exists: exists}); }); }; - User.count = function() { + + User.count = function(socket) { RDB.get('user:count', function(count) { socket.emit('user.count', {count: (count === null) ? 0 : count}); }); }; - User.latest = function() { + + User.latest = function(socket) { RDB.lrange('user:users', 0, 0, function(username) { socket.emit('user.latest', {username: username}); }); @@ -233,7 +235,7 @@ var config = require('../config.js'), } User.reset = { - validate: function(code, callback) { + validate: function(socket, code, callback) { if (typeof callback !== 'function') callback = undefined; RDB.get('reset:' + code + ':uid', function(uid) { @@ -256,7 +258,7 @@ var config = require('../config.js'), } }); }, - send: function(email) { + send: function(socket, email) { User.get_uid_by_email(email, function(uid) { if (uid !== null) { // Generate a new reset code @@ -305,7 +307,7 @@ var config = require('../config.js'), } }); }, - commit: function(code, password) { + commit: function(socket, code, password) { this.validate(code, function(validated) { if (validated) { RDB.get('reset:' + code + ':uid', function(uid) { @@ -321,7 +323,7 @@ var config = require('../config.js'), } User.email = { - exists: function(email, callback) { + exists: function(socket, email, callback) { User.get_uid_by_email(email, function(exists) { exists = !!exists; if (typeof callback !== 'function') socket.emit('user.email.exists', { exists: exists }); @@ -331,7 +333,7 @@ var config = require('../config.js'), } User.active = { - get_record : function() { + get_record : function(socket) { RDB.mget(['global:active_user_record', 'global:active_user_record_date'], function(data) { socket.emit('api:user.active.get_record', {record: data[0], timestamp: data[1]}); }); diff --git a/src/websockets.js b/src/websockets.js index b47277af36..b5370b84aa 100644 --- a/src/websockets.js +++ b/src/websockets.js @@ -38,60 +38,66 @@ var SocketIO = require('socket.io').listen(global.server), }); io.sockets.on('connection', function(socket) { - global.socket = socket; if (DEVELOPMENT === true) { // refreshing templates modules.templates.init(); } + + process.on('uncaughtException', function(err) { + // handle the error safely + console.log("error message "+err); + socket.emit('event:consolelog',{type:'uncaughtException', stack:err.stack, error:err.toString()}); + }); + socket.emit('event:connect', {status: 1}); // BEGIN: API calls (todo: organize) // julian: :^) socket.on('api:user.get', function(data) { - modules.user.get(uid, data.fields); + modules.user.get(socket, uid, data.fields); }); socket.on('user.exists', function(data) { - modules.user.exists(data.username); + modules.user.exists(socket, data.username); }); socket.on('user.count', function(data) { - modules.user.count(data); + modules.user.count(socket, data); }); socket.on('user.latest', function(data) { - modules.user.latest(data); + modules.user.latest(socket, data); }); socket.on('user.login', function(data) { data.sessionID = sessionID; - modules.user.login(data); + modules.user.login(socket, data); }); socket.on('user.email.exists', function(data) { - modules.user.email.exists(data.email); + modules.user.email.exists(socket, data.email); }); socket.on('user:reset.send', function(data) { - modules.user.reset.send(data.email); + modules.user.reset.send(socket, data.email); }); socket.on('user:reset.valid', function(data) { - modules.user.reset.validate(data.code); + modules.user.reset.validate(socket, data.code); }); socket.on('user:reset.commit', function(data) { - modules.user.reset.commit(data.code, data.password); + modules.user.reset.commit(socket, data.code, data.password); }); socket.on('api:topics.post', function(data) { - modules.topics.post(uid, data.title, data.content); + modules.topics.post(socket, uid, data.title, data.content); }); socket.on('api:posts.reply', function(data) { - modules.posts.reply(data.topic_id, uid, data.content); + modules.posts.reply(socket, data.topic_id, uid, data.content); }); socket.on('api:user.active.get', function() { @@ -99,7 +105,7 @@ var SocketIO = require('socket.io').listen(global.server), }); socket.on('api:user.active.get_record', function() { - modules.user.active.get_record(); + modules.user.active.get_record(socket); }); });