Skip to content

Node.js based forum software built for the modern web

rooms.getAll test

barisusaklif8d958e06508
diff --git a/src/socket.io/admin/rooms.js b/src/socket.io/admin/rooms.js
index a4ebfeb44e..2f6ab50698 100644
--- a/src/socket.io/admin/rooms.js
+++ b/src/socket.io/admin/rooms.js
@@ -1,6 +1,7 @@
'use strict';
+var async = require('async');
var os = require('os');
var nconf = require('nconf');
var winston = require('winston');
@@ -68,7 +69,7 @@ SocketRooms.getAll = function (socket, data, callback) {
category: 0
};
- for(var instance in stats) {
+ for (var instance in stats) {
if (stats.hasOwnProperty(instance)) {
totals.onlineGuestCount += stats[instance].onlineGuestCount;
totals.onlineRegisteredCount += stats[instance].onlineRegisteredCount;
@@ -99,30 +100,31 @@ SocketRooms.getAll = function (socket, data, callback) {
return topic.tid;
});
- topics.getTopicsFields(topTenTids, ['title'], function (err, titles) {
- if (err) {
- return callback(err);
+ async.waterfall([
+ function (next) {
+ topics.getTopicsFields(topTenTids, ['title'], next);
+ },
+ function (titles, next) {
+ totals.topics = {};
+ topTenTopics.forEach(function (topic, index) {
+ totals.topics[topic.tid] = {
+ value: topic.count || 0,
+ title: validator.escape(String(titles[index].title))
+ };
+ });
+ next(null, totals);
}
- totals.topics = {};
- topTenTopics.forEach(function (topic, index) {
- totals.topics[topic.tid] = {
- value: topic.count || 0,
- title: validator.escape(String(titles[index].title))
- };
- });
-
- callback(null, totals);
- });
+ ], callback);
};
SocketRooms.getOnlineUserCount = function (io) {
- if (!io) {
- return 0;
- }
var count = 0;
- for (var key in io.sockets.adapter.rooms) {
- if (io.sockets.adapter.rooms.hasOwnProperty(key) && key.startsWith('uid_')) {
- ++ count;
+
+ if (io) {
+ for (var key in io.sockets.adapter.rooms) {
+ if (io.sockets.adapter.rooms.hasOwnProperty(key) && key.startsWith('uid_')) {
+ ++ count;
+ }
}
}
@@ -132,45 +134,51 @@ SocketRooms.getOnlineUserCount = function (io) {
SocketRooms.getLocalStats = function (callback) {
var io = require('../index').server;
- if (!io) {
- return callback();
- }
-
- var roomClients = io.sockets.adapter.rooms;
var socketData = {
- onlineGuestCount: roomClients.online_guests ? roomClients.online_guests.length : 0,
- onlineRegisteredCount: SocketRooms.getOnlineUserCount(io),
- socketCount: Object.keys(io.sockets.sockets).length,
+ onlineGuestCount: 0,
+ onlineRegisteredCount: 0,
+ socketCount: 0,
users: {
- categories: roomClients.categories ? roomClients.categories.length : 0,
- recent: roomClients.recent_topics ? roomClients.recent_topics.length : 0,
- unread: roomClients.unread_topics ? roomClients.unread_topics.length : 0,
+ categories: 0,
+ recent: 0,
+ unread: 0,
topics: 0,
category: 0
},
topics: {}
};
- var topTenTopics = [];
- var tid;
-
- for (var room in roomClients) {
- if (roomClients.hasOwnProperty(room)) {
- tid = room.match(/^topic_(\d+)/);
- if (tid) {
- socketData.users.topics += roomClients[room].length;
- topTenTopics.push({tid: tid[1], count: roomClients[room].length});
- } else if (room.match(/^category/)) {
- socketData.users.category += roomClients[room].length;
+ if (io) {
+ var roomClients = io.sockets.adapter.rooms;
+ socketData.onlineGuestCount = roomClients.online_guests ? roomClients.online_guests.length : 0;
+ socketData.onlineRegisteredCount = SocketRooms.getOnlineUserCount(io);
+ socketData.socketCount = Object.keys(io.sockets.sockets).length;
+ socketData.users.categories = roomClients.categories ? roomClients.categories.length : 0;
+ socketData.users.recent = roomClients.recent_topics ? roomClients.recent_topics.length : 0;
+ socketData.users.unread = roomClients.unread_topics ? roomClients.unread_topics.length : 0;
+
+ var topTenTopics = [];
+ var tid;
+
+ for (var room in roomClients) {
+ if (roomClients.hasOwnProperty(room)) {
+ tid = room.match(/^topic_(\d+)/);
+ if (tid) {
+ socketData.users.topics += roomClients[room].length;
+ topTenTopics.push({tid: tid[1], count: roomClients[room].length});
+ } else if (room.match(/^category/)) {
+ socketData.users.category += roomClients[room].length;
+ }
}
}
- }
- topTenTopics = topTenTopics.sort(function (a, b) {
- return b.count - a.count;
- }).slice(0, 10);
+ topTenTopics = topTenTopics.sort(function (a, b) {
+ return b.count - a.count;
+ }).slice(0, 10);
+
+ socketData.topics = topTenTopics;
+ }
- socketData.topics = topTenTopics;
callback(null, socketData);
};
diff --git a/test/socket.io.js b/test/socket.io.js
index d4eea47df4..807b371048 100644
--- a/test/socket.io.js
+++ b/test/socket.io.js
@@ -374,12 +374,54 @@ describe('socket.io', function () {
it('should return error', function (done) {
var socketAdmin = require('../src/socket.io/admin');
- socketAdmin.before({uid: 10}, 'someMethod', {}, function (err) {
- assert.equal(err.message, '[[error:no-privileges]]');
- done();
- });
+ socketAdmin.before({uid: 10}, 'someMethod', {}, function (err) {
+ assert.equal(err.message, '[[error:no-privileges]]');
+ done();
+ });
+ });
+
+ it('should get room stats', function (done) {
+ var socketAdmin = require('../src/socket.io/admin');
+
+ io.emit('meta.rooms.enter', {enter: 'topic_1'}, function (err) {
+ assert.ifError(err);
+ socketAdmin.rooms.getAll({uid: 10}, {}, function (err) {
+ assert.ifError(err);
+ setTimeout(function () {
+ socketAdmin.rooms.getAll({uid: 10}, {}, function (err, data) {
+ assert.ifError(err);
+ assert(data.hasOwnProperty('onlineGuestCount'));
+ assert(data.hasOwnProperty('onlineRegisteredCount'));
+ assert(data.hasOwnProperty('socketCount'));
+ assert(data.hasOwnProperty('topics'));
+ assert(data.hasOwnProperty('users'));
+ assert.equal(data.topics['1'].title, 'test topic title')
+ done();
+ });
+ }, 1000);
+ });
+ });
});
+ it('should get room stats', function (done) {
+ var socketAdmin = require('../src/socket.io/admin');
+
+ io.emit('meta.rooms.enter', {enter: 'category_1'}, function (err) {
+ assert.ifError(err);
+ socketAdmin.rooms.getAll({uid: 10}, {}, function (err) {
+ assert.ifError(err);
+ setTimeout(function () {
+ socketAdmin.rooms.getAll({uid: 10}, {}, function (err, data) {
+ assert.ifError(err);
+ assert.equal(data.users.category, 1);
+ done();
+ });
+ }, 1000);
+ });
+ });
+ });
+
+
after(function (done) {
db.emptydb(done);
});