Skip to content

Node.js based forum software built for the modern web

removed rooms.js removed browsing users, removed chat syncing across sockets

barisusakli46c2563164b1
diff --git a/public/src/client/topic.js b/public/src/client/topic.js
index adf4eb0d59..c1c58f2862 100644
--- a/public/src/client/topic.js
+++ b/public/src/client/topic.js
@@ -8,12 +8,11 @@ define('forum/topic', [
'forum/topic/threadTools',
'forum/topic/postTools',
'forum/topic/events',
- 'forum/topic/browsing',
'forum/topic/posts',
'navigator',
'sort',
'components'
-], function(infinitescroll, threadTools, postTools, events, browsing, posts, navigator, sort, components) {
+], function(infinitescroll, threadTools, postTools, events, posts, navigator, sort, components) {
var Topic = {},
currentUrl = '';
diff --git a/public/src/client/topic/browsing.js b/public/src/client/topic/browsing.js
deleted file mode 100644
index c3dc3b6a97..0000000000
--- a/public/src/client/topic/browsing.js
+++ /dev/null
@@ -1,111 +0,0 @@
-
-
-'use strict';
-
-/* globals define, app, config, socket, ajaxify */
-
-define('forum/topic/browsing', function() {
-
- var Browsing = {};
-
- Browsing.onUpdateUsersInRoom = function(data) {
- if (data && data.room.indexOf('topic_' + ajaxify.data.tid) !== -1) {
- $('[component="topic/browsing/list"]').parent().toggleClass('hidden', !data.users.length);
- for (var i=0; i<data.users.length; ++i) {
- addUserIcon(data.users[i]);
- }
-
- updateUserCount(data.hidden);
- }
- };
-
- Browsing.onUserEnter = function(data) {
- var browsingList = $('[component="topic/browsing/list"]');
- var user = browsingList.find('a[data-uid="' + data.uid + '"]');
- if (!user.length && browsingList.first().children().length < 10) {
- addUserIcon(data);
- } else if (user.length) {
- user.attr('data-count', parseInt(user.attr('data-count'), 10) + 1);
- } else {
- increaseUserCount(1);
- }
- Browsing.onUserStatusChange(data);
- };
-
- Browsing.onUserLeave = function(uid) {
- if (app.user.uid === parseInt(uid, 10)) {
- return;
- }
- var browsingList = $('[component="topic/browsing/list"]');
- var user = browsingList.find('a[data-uid="' + uid + '"]');
- if (user.length) {
- var count = Math.max(0, parseInt(user.attr('data-count'), 10) - 1);
- user.attr('data-count', count);
- if (count <= 0) {
- user.parent().remove();
- if (!browsingList.children(':not(.hidden)').length) {
- browsingList.parent().addClass('hidden');
- }
- }
- } else {
- increaseUserCount(-1);
- }
- };
-
- Browsing.onUserStatusChange = function(data) {
- app.updateUserStatus($('[data-uid="' + data.uid + '"] [component="user/status"]'), data.status);
-
- updateBrowsingUsers(data);
- };
-
- function updateBrowsingUsers(data) {
- var browsingList = $('[component="topic/browsing/list"]');
- var user = browsingList.find('a[data-uid="'+ data.uid + '"]');
- if (user.length) {
- user.parent().toggleClass('hidden', data.status === 'offline');
- browsingList.parent().toggleClass('hidden', !browsingList.children(':not(.hidden)').length);
- }
- }
-
- function addUserIcon(user) {
- if (!user.userslug) {
- return;
- }
- var browsingList = $('[component="topic/browsing/list"]');
- var userEl = createUserIcon(user.uid, user.picture, user.userslug, user.username, user['icon:bgColor'], user['icon:text']);
- var isSelf = parseInt(user.uid, 10) === parseInt(app.user.uid, 10);
- if (isSelf) {
- browsingList.prepend(userEl);
- } else {
- browsingList.append(userEl);
- }
-
- browsingList.find('a[data-uid]').tooltip({
- placement: 'top'
- });
- }
-
- function createUserIcon(uid, picture, userslug, username, iconBg, iconText) {
- if (!$('[component="topic/browsing/list"]').find('[data-uid="' + uid + '"]').length) {
- var imgOrIcon = picture ?
- '<img src="'+ picture +'" />' :
- '<div class="user-icon" style="background-color: ' + iconBg + ';">' + iconText + '</div>';
-
- return $('<div class="inline-block"><a title="' + username + '" data-uid="' + uid + '" data-count="1" href="' + config.relative_path + '/user/' + userslug + '">' + imgOrIcon + '</a></div>');
- }
- }
-
- function updateUserCount(count) {
- count = parseInt(count, 10);
- if (!count || count < 0) {
- count = 0;
- }
- $('[component="topic/browsing/count"]').text(count).parent().toggleClass('hidden', count === 0);
- }
-
- function increaseUserCount(incr) {
- updateUserCount(parseInt($('[component="topic/browsing/count"]').first().text(), 10) + incr);
- }
-
- return Browsing;
-});
\ No newline at end of file
diff --git a/public/src/modules/chat.js b/public/src/modules/chat.js
index bf9bc79b81..c721c508da 100644
--- a/public/src/modules/chat.js
+++ b/public/src/modules/chat.js
@@ -10,9 +10,6 @@ define('chat', ['components', 'taskbar', 'string', 'sounds', 'forum/chats', 'tra
var chatsToggleEl = components.get('chat/dropdown'),
chatsListEl = components.get('chat/list');
- // Sync open chats between all user socket sessions
- module.sync();
-
chatsToggleEl.on('click', function() {
if (chatsToggleEl.parent().hasClass('open')) {
return;
@@ -94,32 +91,6 @@ define('chat', ['components', 'taskbar', 'string', 'sounds', 'forum/chats', 'tra
app.updateUserStatus(modal.find('[component="user/status"]'), data.status);
});
- socket.on('query:chats.sync', function(data, callback) {
- var chats = Array.prototype.map.call(taskbar.get('chat'), function(chatObj) {
- return {
- username: chatObj.options.title,
- uid: chatObj.options.touid,
- new: chatObj.element.hasClass('new')
- };
- });
-
- callback(null, chats);
- });
-
- socket.on('event:chats.open', function(data) {
- data.silent = true;
- module.createModal(data);
- });
-
- socket.on('event:chats.close', function(uid) {
- module.close(module.getModal(uid), true);
- });
-
- socket.on('event:chats.toggleNew', function(data) {
- var uuid = module.getModal(data.uid).attr('UUID');
- module.toggleNew(uuid, data.state, true);
- });
-
$(window).on('action:taskbar.toggleNew', function(ev, uuid) {
var modal = $('.chat-modal[uuid="' + uuid + '"]'),
touid = modal.attr('touid');
@@ -318,10 +289,6 @@ define('chat', ['components', 'taskbar', 'string', 'sounds', 'forum/chats', 'tra
state: ''
});
- if (!data.silent) {
- socket.emit('modules.chats.open', data);
- }
-
$(window).trigger('action:chat.loaded', chatModal);
if (typeof callback === 'function') {
@@ -343,10 +310,6 @@ define('chat', ['components', 'taskbar', 'string', 'sounds', 'forum/chats', 'tra
taskbar.discard('chat', chatModal.attr('UUID'));
Chats.notifyTyping(chatModal.attr('touid'), false);
- if (!silent) {
- socket.emit('modules.chats.close', chatModal.attr('touid'));
- }
-
if (chatModal.attr('data-mobile')) {
module.disableMobileBehaviour(chatModal);
}
@@ -423,27 +386,7 @@ define('chat', ['components', 'taskbar', 'string', 'sounds', 'forum/chats', 'tra
socket.emit('modules.chats.canMessage', toUid, callback);
};
- module.sync = function() {
- socket.emit('modules.chats.sync', function(err, users) {
- if (err) {
- return app.alertError(err.message);
- }
- users.forEach(function(user) {
- if (!module.modalExists(user.uid)) {
- module.createModal({
- username: user.username,
- touid: user.uid,
- silent: true
- }, function(modal) {
- if (user.new) {
- module.toggleNew(modal.attr('UUID'), true, true);
- }
- });
- }
- });
- });
- };
return module;
});
diff --git a/src/controllers/accounts/helpers.js b/src/controllers/accounts/helpers.js
index 96e4f7c235..b9aeaa0055 100644
--- a/src/controllers/accounts/helpers.js
+++ b/src/controllers/accounts/helpers.js
@@ -86,7 +86,7 @@ helpers.getUserDataByUserSlug = function(userslug, callerUID, callback) {
userData['email:confirmed'] = !!parseInt(userData['email:confirmed'], 10);
userData.profile_links = results.profile_links;
userData.sso = results.sso.associations;
- userData.status = require('../../socket.io').isUserOnline(userData.uid) ? (userData.status || 'online') : 'offline';
+ userData.status = user.getStatus(userData);
userData.banned = parseInt(userData.banned, 10) === 1;
userData.website = validator.escape(userData.website);
userData.websiteLink = !userData.website.startsWith('http') ? 'http://' + userData.website : userData.website;
diff --git a/src/controllers/topics.js b/src/controllers/topics.js
index ab946dab33..f5fb015c15 100644
--- a/src/controllers/topics.js
+++ b/src/controllers/topics.js
@@ -125,6 +125,7 @@ topicsController.get = function(req, res, callback) {
});
},
function (topicData, next) {
+
var breadcrumbs = [
{
text: topicData.category.name,
diff --git a/src/messaging.js b/src/messaging.js
index 0631de61f8..e0a39eac59 100644
--- a/src/messaging.js
+++ b/src/messaging.js
@@ -266,7 +266,7 @@ var db = require('./database'),
db.isSortedSetMembers('uid:' + uid + ':chats:unread', uids, next);
},
users: function(next) {
- user.getUsersFields(uids, ['uid', 'username', 'picture', 'status'] , next);
+ user.getUsersFields(uids, ['uid', 'username', 'picture', 'status', 'lastonline'] , next);
},
teasers: function(next) {
async.map(uids, function(fromuid, next) {
@@ -288,11 +288,11 @@ var db = require('./database'),
return callback(err);
}
- results.users.forEach(function(user, index) {
- if (user && parseInt(user.uid, 10)) {
- user.unread = results.unread[index];
- user.status = sockets.isUserOnline(user.uid) ? user.status : 'offline';
- user.teaser = results.teasers[index];
+ results.users.forEach(function(userData, index) {
+ if (userData && parseInt(userData.uid, 10)) {
+ userData.unread = results.unread[index];
+ userData.status = user.getStatus(userData);
+ userData.teaser = results.teasers[index];
}
});
@@ -416,35 +416,37 @@ var db = require('./database'),
};
function sendNotifications(fromuid, touid, messageObj, callback) {
- if (sockets.isUserOnline(touid)) {
- return callback();
- }
-
- notifications.create({
- bodyShort: '[[notifications:new_message_from, ' + messageObj.fromUser.username + ']]',
- bodyLong: messageObj.content,
- nid: 'chat_' + fromuid + '_' + touid,
- from: fromuid,
- path: '/chats/' + messageObj.fromUser.username
- }, function(err, notification) {
- if (!err && notification) {
- notifications.push(notification, [touid], callback);
+ user.isOnline(touid, function(err, isOnline) {
+ if (err || isOnline) {
+ return callback(err);
}
- });
- user.getSettings(messageObj.toUser.uid, function(err, settings) {
- if (settings.sendChatNotifications && !parseInt(meta.config.disableEmailSubscriptions, 10)) {
- emailer.send('notif_chat', touid, {
- subject: '[[email:notif.chat.subject, ' + messageObj.fromUser.username + ']]',
- username: messageObj.toUser.username,
- userslug: utils.slugify(messageObj.toUser.username),
- summary: '[[notifications:new_message_from, ' + messageObj.fromUser.username + ']]',
- message: messageObj,
- site_title: meta.config.title || 'NodeBB',
- url: nconf.get('url'),
- fromUserslug: utils.slugify(messageObj.fromUser.username)
- });
- }
+ notifications.create({
+ bodyShort: '[[notifications:new_message_from, ' + messageObj.fromUser.username + ']]',
+ bodyLong: messageObj.content,
+ nid: 'chat_' + fromuid + '_' + touid,
+ from: fromuid,
+ path: '/chats/' + messageObj.fromUser.username
+ }, function(err, notification) {
+ if (!err && notification) {
+ notifications.push(notification, [touid], callback);
+ }
+ });
+
+ user.getSettings(messageObj.toUser.uid, function(err, settings) {
+ if (settings.sendChatNotifications && !parseInt(meta.config.disableEmailSubscriptions, 10)) {
+ emailer.send('notif_chat', touid, {
+ subject: '[[email:notif.chat.subject, ' + messageObj.fromUser.username + ']]',
+ username: messageObj.toUser.username,
+ userslug: utils.slugify(messageObj.toUser.username),
+ summary: '[[notifications:new_message_from, ' + messageObj.fromUser.username + ']]',
+ message: messageObj,
+ site_title: meta.config.title || 'NodeBB',
+ url: nconf.get('url'),
+ fromUserslug: utils.slugify(messageObj.fromUser.username)
+ });
+ }
+ });
});
}
diff --git a/src/meta/js.js b/src/meta/js.js
index ec00f5fb18..75b82c856a 100644
--- a/src/meta/js.js
+++ b/src/meta/js.js
@@ -52,7 +52,6 @@ module.exports = function(Meta) {
'public/src/client/recent.js',
'public/src/client/unread.js',
'public/src/client/topic.js',
- 'public/src/client/topic/browsing.js',
'public/src/client/topic/events.js',
'public/src/client/topic/flag.js',
'public/src/client/topic/fork.js',
diff --git a/src/posts/user.js b/src/posts/user.js
index a3ddb3bc12..b27e63517e 100644
--- a/src/posts/user.js
+++ b/src/posts/user.js
@@ -20,10 +20,7 @@ module.exports = function(Posts) {
user.getMultipleUserSettings(uids, next);
},
userData: function(next) {
- user.getUsersFields(uids, ['uid', 'username', 'fullname', 'userslug', 'reputation', 'postcount', 'picture', 'signature', 'banned', 'status'], next);
- },
- online: function(next) {
- require('../socket.io').isUsersOnline(uids, next);
+ user.getUsersFields(uids, ['uid', 'username', 'fullname', 'userslug', 'reputation', 'postcount', 'picture', 'signature', 'banned', 'status', 'lastonline'], next);
}
}, function(err, results) {
if (err) {
@@ -47,7 +44,7 @@ module.exports = function(Posts) {
userData.selectedGroup = userData.groups[index];
}
});
- userData.status = user.getStatus(userData.status, results.online[i]);
+ userData.status = user.getStatus(userData);
});
async.map(userData, function(userData, next) {
diff --git a/src/socket.io/index.js b/src/socket.io/index.js
index a4f2500d7a..e78b19a8a1 100644
--- a/src/socket.io/index.js
+++ b/src/socket.io/index.js
@@ -11,7 +11,6 @@ var SocketIO = require('socket.io'),
user = require('../user'),
logger = require('../logger'),
ratelimit = require('../middleware/ratelimit'),
- rooms = require('./rooms'),
Sockets = {},
Namespaces = {};
@@ -63,8 +62,8 @@ function onConnection(socket) {
function onConnect(socket) {
if (socket.uid) {
- rooms.enter(socket, 'uid_' + socket.uid);
- rooms.enter(socket, 'online_users');
+ socket.join('uid_' + socket.uid);
+ socket.join('online_users');
user.getUserFields(socket.uid, ['status'], function(err, userData) {
if (err || !userData) {
@@ -76,7 +75,7 @@ function onConnect(socket) {
}
});
} else {
- rooms.enter(socket, 'online_guests');
+ socket.join('online_guests');
}
}
@@ -87,7 +86,6 @@ function onDisconnect(socket, data) {
socket.broadcast.emit('event:user_status_change', {uid: socket.uid, status: 'offline'});
}
}
- rooms.leaveAll(socket, data.rooms);
}
function onMessage(socket, payload) {
@@ -249,11 +247,13 @@ Sockets.reqFromSocket = function(socket) {
};
Sockets.isUserOnline = function(uid) {
- return !!rooms.clients('uid_' + uid).length;
+ winston.warn('[deprecated] Sockets.isUserOnline')
+ return false;
};
Sockets.isUsersOnline = function(uids, callback) {
- callback(null, uids.map(Sockets.isUserOnline));
+ winston.warn('[deprecated] Sockets.isUsersOnline')
+ callback(null, uids.map(function() { return false; }));
};
Sockets.getUsersInRoom = function (uid, roomName, start, stop, callback) {
diff --git a/src/socket.io/meta.js b/src/socket.io/meta.js
index f57c679512..39603c8d0c 100644
--- a/src/socket.io/meta.js
+++ b/src/socket.io/meta.js
@@ -1,12 +1,9 @@
'use strict';
-var validator = require('validator'),
-
- meta = require('../meta'),
+var meta = require('../meta'),
user = require('../user'),
topics = require('../topics'),
emitter = require('../emitter'),
- rooms = require('./rooms'),
websockets = require('./'),
@@ -50,7 +47,7 @@ SocketMeta.rooms.enter = function(socket, data, callback) {
leaveCurrentRoom(socket);
if (data.enter) {
- rooms.enter(socket, data.enter);
+ socket.join(data.enter);
socket.currentRoom = data.enter;
}
callback();
@@ -66,7 +63,7 @@ SocketMeta.rooms.leaveCurrent = function(socket, data, callback) {
function leaveCurrentRoom(socket) {
if (socket.currentRoom) {
- rooms.leave(socket, socket.currentRoom);
+ socket.leave(socket.currentRoom);
socket.currentRoom = '';
}
}
diff --git a/src/socket.io/modules.js b/src/socket.io/modules.js
index d7b2c7c03b..08b8a4a26a 100644
--- a/src/socket.io/modules.js
+++ b/src/socket.io/modules.js
@@ -7,7 +7,6 @@ var meta = require('../meta'),
async = require('async'),
server = require('./'),
- rooms = require('./rooms'),
SocketModules = {
chats: {},
@@ -105,37 +104,6 @@ SocketModules.chats.getRecentChats = function(socket, data, callback) {
Messaging.getRecentChats(socket.uid, start, stop, callback);
};
-SocketModules.chats.sync = function(socket, data, callback) {
- var chats = [],
- uids = [],
- socketIds = rooms.clients('uid_' + socket.uid);
-
- rooms.broadcast(socket, 'uid_' + socket.uid, 'query:chats.sync', {}, function(err, sessionData) {
- sessionData.forEach(function(data) {
- data.forEach(function(chat) {
- if (uids.indexOf(chat.uid) === -1) {
- chats.push(chat);
- uids.push(chat.uid);
- }
- });
- });
-
- callback(err, chats);
- });
-};
-
-SocketModules.chats.open = function(socket, data, callback) {
- rooms.broadcast(socket, 'uid_' + socket.uid, 'event:chats.open', data);
-};
-
-SocketModules.chats.close = function(socket, data, callback) {
- rooms.broadcast(socket, 'uid_' + socket.uid, 'event:chats.close', data);
-};
-
-SocketModules.chats.toggleNew = function(socket, data, callback) {
- rooms.broadcast(socket, 'uid_' + socket.uid, 'event:chats.toggleNew', data);
-};
-
/* Sounds */
SocketModules.sounds.getSounds = function(socket, data, callback) {
diff --git a/src/socket.io/rooms.js b/src/socket.io/rooms.js
deleted file mode 100644
index 198288c3a9..0000000000
--- a/src/socket.io/rooms.js
+++ /dev/null
@@ -1,129 +0,0 @@
-'use strict';
-
-
-// Temp solution until
-// https://github.com/NodeBB/NodeBB/issues/2486
-// and
-// https://github.com/Automattic/socket.io/issues/1945
-// are closed.
-// Once they are closed switch to .clients() and async calls
-
-
-var pubsub = require('../pubsub'),
- async = require('async');
-
-var rooms = {};
-
-var clientRooms = {};
-var roomClients = {};
-
-rooms.enter = function(socket, room) {
- socket.join(room);
- //pubsub.publish('socket:join', {id: socket.id, room: room});
-};
-
-rooms.leave = function(socket, room) {
- socket.leave(room);
- //pubsub.publish('socket:leave', {id: socket.id, room: room});
-};
-
-rooms.leaveAll = function(socket, roomsToLeave) {
- roomsToLeave.forEach(function(room) {
- rooms.leave(socket, room);
- });
-};
-
-rooms.broadcast = function(socket, room, msg, data, callback) {
- var io = require('./'),
- socketIds = rooms.clients(room);
-
- callback = callback || function() {};
-
- // Filter out socketIds that aren't actually connected
- socketIds = socketIds.filter(function(id) {
- return io.server.sockets.connected.hasOwnProperty(id);
- });
-
- async.map(socketIds, function(id, next) {
- var timeout,
- timeoutPassed = false;
-
- if (socket.id === id) {
- return setImmediate(next, null, []);
- }
-
- timeout = setTimeout(function() {
- timeoutPassed = true;
- next(null, []);
- }, 500);
-
- io.server.sockets.connected[id].emit(msg, data || {}, function(err, returnData) {
- clearTimeout(timeout);
- if (!timeoutPassed) {
- next(null, returnData);
- }
- });
- }, callback);
-};
-
-pubsub.on('socket:join', onSocketJoin);
-pubsub.on('socket:leave', onSocketLeave);
-
-function onSocketJoin(data) {
- clientRooms[data.id] = clientRooms[data.id] || [];
- if (clientRooms[data.id].indexOf(data.room) === -1) {
- clientRooms[data.id].push(data.room);
- }
-
- roomClients[data.room] = roomClients[data.room] || [];
- if (roomClients[data.room].indexOf(data.id) === -1) {
- roomClients[data.room].push(data.id);
- }
-}
-
-
-function onSocketLeave(data) {
- var index;
- if (Array.isArray(clientRooms[data.id])) {
- index = clientRooms[data.id].indexOf(data.room);
- if (index !== -1) {
- clientRooms[data.id].splice(index, 1);
- if (!clientRooms[data.id].length) {
- delete clientRooms[data.id];
- }
- }
- }
-
- if (Array.isArray(roomClients[data.room])) {
- index = roomClients[data.room].indexOf(data.id);
- if (index !== -1) {
- roomClients[data.room].splice(index, 1);
- if (!roomClients[data.room].length) {
- delete roomClients[data.room];
- }
- }
- }
-}
-
-
-rooms.clients = function(room) {
- return Array.isArray(roomClients[room]) ? roomClients[room] : [];
-};
-
-rooms.clientRooms = function(id) {
- return Array.isArray(clientRooms[id]) ? clientRooms[id] : [];
-};
-
-rooms.socketCount = function() {
- return Object.keys(clientRooms || {}).length;
-};
-
-rooms.roomClients = function() {
- return roomClients;
-};
-
-
-
-
-module.exports = rooms;
-
diff --git a/src/socket.io/user/status.js b/src/socket.io/user/status.js
index 56ea9d14f7..c911ecf588 100644
--- a/src/socket.io/user/status.js
+++ b/src/socket.io/user/status.js
@@ -8,15 +8,12 @@ module.exports = function(SocketUser) {
if (!socket.uid) {
return callback('[[error:invalid-uid]]');
}
- var online = websockets.isUserOnline(uid);
- if (!online) {
- return callback(null, 'offline');
- }
- user.getUserField(uid, 'status', function(err, status) {
+
+ user.getUserFields(uid, ['lastonline', 'status'], function(err, userData) {
if (err) {
return callback(err);
}
- status = status || 'online';
+ var status = user.getStatus(userData);
callback(null, status);
});
};
diff --git a/src/topics/create.js b/src/topics/create.js
index 6ccc06de12..828214df28 100644
--- a/src/topics/create.js
+++ b/src/topics/create.js
@@ -272,13 +272,13 @@ module.exports = function(Topics) {
var tid = postData.tid;
var uid = postData.uid;
async.waterfall([
- function(next) {
+ function (next) {
Topics.markAsUnreadForAll(tid, next);
},
- function(next) {
+ function (next) {
Topics.markAsRead([tid], uid, next);
},
- function(next) {
+ function (markedRead, next) {
async.parallel({
userInfo: function(next) {
posts.getUserInfoForPosts([postData.uid], uid, next);
@@ -294,7 +294,7 @@ module.exports = function(Topics) {
}
}, next);
},
- function(results, next) {
+ function (results, next) {
postData.user = results.userInfo[0];
postData.topic = results.topicInfo;
diff --git a/src/user.js b/src/user.js
index 49c4e06b54..65632217d2 100644
--- a/src/user.js
+++ b/src/user.js
@@ -1,12 +1,9 @@
'use strict';
var async = require('async'),
- nconf = require('nconf'),
- gravatar = require('gravatar'),
plugins = require('./plugins'),
db = require('./database'),
- meta = require('./meta'),
topics = require('./topics'),
privileges = require('./privileges'),
utils = require('../public/src/utils');
@@ -91,7 +88,7 @@ var async = require('async'),
};
User.getUsers = function(uids, uid, callback) {
- var fields = ['uid', 'username', 'userslug', 'picture', 'status', 'banned', 'joindate', 'postcount', 'reputation', 'email:confirmed'];
+ var fields = ['uid', 'username', 'userslug', 'picture', 'status', 'banned', 'joindate', 'postcount', 'reputation', 'email:confirmed', 'lastonline'];
plugins.fireHook('filter:users.addFields', {fields: fields}, function(err, data) {
if (err) {
return callback(err);
@@ -105,9 +102,6 @@ var async = require('async'),
},
isAdmin: function(next) {
User.isAdministrator(uids, next);
- },
- isOnline: function(next) {
- require('./socket.io').isUsersOnline(uids, next);
}
}, function(err, results) {
if (err) {
@@ -118,7 +112,7 @@ var async = require('async'),
if (!user) {
return;
}
- user.status = User.getStatus(user.status, results.isOnline[index]);
+ user.status = User.getStatus(user);
user.joindateISO = utils.toISOString(user.joindate);
user.administrator = results.isAdmin[index];
user.banned = parseInt(user.banned, 10) === 1;
@@ -135,8 +129,19 @@ var async = require('async'),
});
};
- User.getStatus = function(status, isOnline) {
- return isOnline ? (status || 'online') : 'offline';
+ User.getStatus = function(userData) {
+ var isOnline = Date.now() - parseInt(userData.lastonline, 10) < 300000;
+ return isOnline ? (userData.status || 'online') : 'offline';
+ };
+
+ User.isOnline = function(uid, callback) {
+ User.getUserField(uid, 'lastonline', function(err, lastonline) {
+ if (err) {
+ return callback(err);
+ }
+ var isOnline = Date.now() - parseInt(lastonline, 10) < 300000;
+ callback(null, isOnline);
+ });
};
User.exists = function(uid, callback) {
diff --git a/src/user/search.js b/src/user/search.js
index ae91ba1615..322c137738 100644
--- a/src/user/search.js
+++ b/src/user/search.js
@@ -98,29 +98,16 @@ module.exports = function(User) {
function filterAndSortUids(uids, data, callback) {
var sortBy = data.sortBy || 'joindate';
- var fields = ['uid', 'status', sortBy];
+ var fields = ['uid', 'status', 'lastonline', sortBy];
- async.parallel({
- userData: function(next) {
- User.getUsersFields(uids, fields, next);
- },
- isOnline: function(next) {
- if (data.onlineOnly) {
- require('../socket.io').isUsersOnline(uids, next);
- } else {
- next();
- }
- }
- }, function(err, results) {
+ User.getUsersFields(uids, fields, function(err, userData) {
if (err) {
return callback(err);
}
- var userData = results.userData;
-
if (data.onlineOnly) {
- userData = userData.filter(function(user, index) {
- return user && user.status !== 'offline' && results.isOnline[index];
+ userData = userData.filter(function(user) {
+ return user && user.status !== 'offline' && (Date.now() - parseInt(user.lastonline, 10) < 300000);
});
}