From bc48ba874d2cb94bf4577c01651aad5862df8d47 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Thu, 16 Jan 2014 20:14:09 -0500 Subject: [PATCH 1/2] fixed module.js callback format --- public/src/modules/chat.js | 6 ++-- public/src/modules/composer.js | 6 ++-- public/src/modules/notifications.js | 14 +++++---- src/socket.io/modules.js | 45 +++++++++++------------------ 4 files changed, 31 insertions(+), 40 deletions(-) diff --git a/public/src/modules/chat.js b/public/src/modules/chat.js index 68bb927523..82855d0d9f 100644 --- a/public/src/modules/chat.js +++ b/public/src/modules/chat.js @@ -13,13 +13,13 @@ define(['taskbar', 'string'], function(taskbar, S) { return; } - socket.emit('modules.chats.list', function(chats) { + socket.emit('modules.chats.list', function(err, chats) { var chatsFrag = document.createDocumentFragment(), chatEl = document.createElement('li'), numChats = chats.length, x, userObj; - if (numChats > 0) { + if (!err && numChats > 0) { for(x=0;x 0) { titleEl.val(postData.title); titleEl.prop('readOnly', true); - socket.emit('modules.composer.editCheck', postData.pid, function(editCheck) { - if (editCheck.titleEditable) { + socket.emit('modules.composer.editCheck', postData.pid, function(err, editCheck) { + if (!err && editCheck.titleEditable) { postContainer.find('input').prop('readonly', false); } }); diff --git a/public/src/modules/notifications.js b/public/src/modules/notifications.js index 171e5f32f7..c5e172f165 100644 --- a/public/src/modules/notifications.js +++ b/public/src/modules/notifications.js @@ -51,13 +51,15 @@ define(function() { notifIcon.toggleClass('active', false); } - socket.emit('modules.notifications.mark_all_read', null, function() { - notifIcon.toggleClass('active', false); - app.refreshTitle(); + socket.emit('modules.notifications.mark_all_read', null, function(err) { + if (!err) { + notifIcon.toggleClass('active', false); + app.refreshTitle(); - // Update favicon + local count - Tinycon.setBubble(0); - localStorage.setItem('notifications:count', 0); + // Update favicon + local count + Tinycon.setBubble(0); + localStorage.setItem('notifications:count', 0); + } }); }); } diff --git a/src/socket.io/modules.js b/src/socket.io/modules.js index fe1c423999..8cbbda3095 100644 --- a/src/socket.io/modules.js +++ b/src/socket.io/modules.js @@ -11,6 +11,7 @@ var posts = require('../posts'), async = require('async'), S = require('string'), winston = require('winston'), + server = require('./'), SocketModules = {}; @@ -32,7 +33,7 @@ SocketModules.composer.push = function(socket, data, callback) { }); } ], function(err, results) { - callback({ + callback(err, { title: results[1], pid: data.pid, body: results[0].content @@ -40,7 +41,7 @@ SocketModules.composer.push = function(socket, data, callback) { }); } } else { - callback({ + callback(null, { error: 'no-uid' }); } @@ -48,8 +49,12 @@ SocketModules.composer.push = function(socket, data, callback) { SocketModules.composer.editCheck = function(socket, pid, callback) { posts.getPostField(pid, 'tid', function(err, tid) { + if (err) { + return callback(err); + } + postTools.isMain(pid, tid, function(err, isMain) { - callback({ + callback(err, { titleEditable: isMain }); }); @@ -62,13 +67,7 @@ SocketModules.chats = {}; SocketModules.chats.get = function(socket, data, callback) { var touid = data.touid; - Messaging.getMessages(socket.uid, touid, function(err, messages) { - if (err) { - return callback(null); - } - - callback(messages); - }); + Messaging.getMessages(socket.uid, touid, callback); }; SocketModules.chats.send = function(socket, data) { @@ -102,11 +101,11 @@ SocketModules.chats.send = function(socket, data) { var numSockets = 0, x; - if (socket.userSockets[touid]) { - numSockets = socket.userSockets[touid].length; + if (server.userSockets[touid]) { + numSockets = server.userSockets[touid].length; for (x = 0; x < numSockets; ++x) { - socket.userSockets[touid][x].emit('event:chats.receive', { + server.userSockets[touid][x].emit('event:chats.receive', { fromuid: socket.uid, username: username, // todo this isnt very nice, but can't think of a better way atm @@ -116,12 +115,12 @@ SocketModules.chats.send = function(socket, data) { } } - if (socket.userSockets[socket.uid]) { + if (server.userSockets[socket.uid]) { - numSockets = socket.userSockets[socket.uid].length; + numSockets = server.userSockets[socket.uid].length; for (x = 0; x < numSockets; ++x) { - socket.userSockets[socket.uid][x].emit('event:chats.receive', { + server.userSockets[socket.uid][x].emit('event:chats.receive', { fromuid: touid, username: toUsername, message: parsed, @@ -135,13 +134,7 @@ SocketModules.chats.send = function(socket, data) { }; SocketModules.chats.list = function(socket, data, callback) { - Messaging.getRecentChats(socket.uid, function(err, uids) { - if (err) { - winston.warn('[(socket) chats.list] Problem retrieving chats: ' + err.message); - } - - callback(uids || []); - }); + Messaging.getRecentChats(socket.uid, callback); }; /* Notifications */ @@ -153,11 +146,7 @@ SocketModules.notifications.mark_read = function(socket, nid) { }; SocketModules.notifications.mark_all_read = function(socket, data, callback) { - notifications.mark_all_read(socket.uid, function(err) { - if (!err) { - callback(); - } - }); + notifications.mark_all_read(socket.uid, callback); }; module.exports = SocketModules; \ No newline at end of file From 111776d8781465579404a8e016549c880e1bf6e2 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Thu, 16 Jan 2014 20:29:11 -0500 Subject: [PATCH 2/2] notifications socket callbacks --- public/src/modules/notifications.js | 4 ++-- src/socket.io/notifications.js | 10 ++++------ src/user.js | 2 +- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/public/src/modules/notifications.js b/public/src/modules/notifications.js index c5e172f165..8994ba008a 100644 --- a/public/src/modules/notifications.js +++ b/public/src/modules/notifications.js @@ -11,14 +11,14 @@ define(function() { notifTrigger.addEventListener('click', function(e) { e.preventDefault(); if (notifContainer.className.indexOf('open') === -1) { - socket.emit('notifications.get', null, function(data) { + socket.emit('notifications.get', null, function(err, data) { var notifFrag = document.createDocumentFragment(), notifEl = document.createElement('li'), numRead = data.read.length, numUnread = data.unread.length, x; notifList.innerHTML = ''; - if ((data.read.length + data.unread.length) > 0) { + if (!err && (data.read.length + data.unread.length) > 0) { for (x = 0; x < numUnread; x++) { notifEl.setAttribute('data-nid', data.unread[x].nid); notifEl.className = 'unread'; diff --git a/src/socket.io/notifications.js b/src/socket.io/notifications.js index 3c4d7c352a..3c41f33d5d 100644 --- a/src/socket.io/notifications.js +++ b/src/socket.io/notifications.js @@ -1,17 +1,15 @@ +"use strict"; + var user = require('../user'), SocketNotifs = {}; SocketNotifs.get = function(socket, data, callback) { - user.notifications.get(socket.uid, function(notifs) { - callback(notifs); - }); + user.notifications.get(socket.uid, callback); }; SocketNotifs.getCount = function(socket, data, callback) { - user.notifications.getUnreadCount(socket.uid, function(err, count) { - callback(err ? err.message : null, count); - }); + user.notifications.getUnreadCount(socket.uid, callback); }; module.exports = SocketNotifs; \ No newline at end of file diff --git a/src/user.js b/src/user.js index b17e612ba9..09155e17f7 100644 --- a/src/user.js +++ b/src/user.js @@ -1016,7 +1016,7 @@ var bcrypt = require('bcrypt'), notifications.read.length = maxNotifs - notifications.unread.length; } - callback(notifications); + callback(err, notifications); }); }, getAll: function(uid, limit, before, callback) {