mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-12-23 17:00:24 +01:00
rooms.getAll test
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
|
||||||
|
var async = require('async');
|
||||||
var os = require('os');
|
var os = require('os');
|
||||||
var nconf = require('nconf');
|
var nconf = require('nconf');
|
||||||
var winston = require('winston');
|
var winston = require('winston');
|
||||||
@@ -68,7 +69,7 @@ SocketRooms.getAll = function (socket, data, callback) {
|
|||||||
category: 0
|
category: 0
|
||||||
};
|
};
|
||||||
|
|
||||||
for(var instance in stats) {
|
for (var instance in stats) {
|
||||||
if (stats.hasOwnProperty(instance)) {
|
if (stats.hasOwnProperty(instance)) {
|
||||||
totals.onlineGuestCount += stats[instance].onlineGuestCount;
|
totals.onlineGuestCount += stats[instance].onlineGuestCount;
|
||||||
totals.onlineRegisteredCount += stats[instance].onlineRegisteredCount;
|
totals.onlineRegisteredCount += stats[instance].onlineRegisteredCount;
|
||||||
@@ -99,30 +100,31 @@ SocketRooms.getAll = function (socket, data, callback) {
|
|||||||
return topic.tid;
|
return topic.tid;
|
||||||
});
|
});
|
||||||
|
|
||||||
topics.getTopicsFields(topTenTids, ['title'], function (err, titles) {
|
async.waterfall([
|
||||||
if (err) {
|
function (next) {
|
||||||
return callback(err);
|
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 = {};
|
], callback);
|
||||||
topTenTopics.forEach(function (topic, index) {
|
|
||||||
totals.topics[topic.tid] = {
|
|
||||||
value: topic.count || 0,
|
|
||||||
title: validator.escape(String(titles[index].title))
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
callback(null, totals);
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
SocketRooms.getOnlineUserCount = function (io) {
|
SocketRooms.getOnlineUserCount = function (io) {
|
||||||
if (!io) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
var count = 0;
|
var count = 0;
|
||||||
for (var key in io.sockets.adapter.rooms) {
|
|
||||||
if (io.sockets.adapter.rooms.hasOwnProperty(key) && key.startsWith('uid_')) {
|
if (io) {
|
||||||
++ count;
|
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) {
|
SocketRooms.getLocalStats = function (callback) {
|
||||||
var io = require('../index').server;
|
var io = require('../index').server;
|
||||||
|
|
||||||
if (!io) {
|
|
||||||
return callback();
|
|
||||||
}
|
|
||||||
|
|
||||||
var roomClients = io.sockets.adapter.rooms;
|
|
||||||
var socketData = {
|
var socketData = {
|
||||||
onlineGuestCount: roomClients.online_guests ? roomClients.online_guests.length : 0,
|
onlineGuestCount: 0,
|
||||||
onlineRegisteredCount: SocketRooms.getOnlineUserCount(io),
|
onlineRegisteredCount: 0,
|
||||||
socketCount: Object.keys(io.sockets.sockets).length,
|
socketCount: 0,
|
||||||
users: {
|
users: {
|
||||||
categories: roomClients.categories ? roomClients.categories.length : 0,
|
categories: 0,
|
||||||
recent: roomClients.recent_topics ? roomClients.recent_topics.length : 0,
|
recent: 0,
|
||||||
unread: roomClients.unread_topics ? roomClients.unread_topics.length : 0,
|
unread: 0,
|
||||||
topics: 0,
|
topics: 0,
|
||||||
category: 0
|
category: 0
|
||||||
},
|
},
|
||||||
topics: {}
|
topics: {}
|
||||||
};
|
};
|
||||||
|
|
||||||
var topTenTopics = [];
|
if (io) {
|
||||||
var tid;
|
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;
|
||||||
|
|
||||||
for (var room in roomClients) {
|
var topTenTopics = [];
|
||||||
if (roomClients.hasOwnProperty(room)) {
|
var tid;
|
||||||
tid = room.match(/^topic_(\d+)/);
|
|
||||||
if (tid) {
|
for (var room in roomClients) {
|
||||||
socketData.users.topics += roomClients[room].length;
|
if (roomClients.hasOwnProperty(room)) {
|
||||||
topTenTopics.push({tid: tid[1], count: roomClients[room].length});
|
tid = room.match(/^topic_(\d+)/);
|
||||||
} else if (room.match(/^category/)) {
|
if (tid) {
|
||||||
socketData.users.category += roomClients[room].length;
|
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);
|
||||||
|
|
||||||
|
socketData.topics = topTenTopics;
|
||||||
}
|
}
|
||||||
|
|
||||||
topTenTopics = topTenTopics.sort(function (a, b) {
|
|
||||||
return b.count - a.count;
|
|
||||||
}).slice(0, 10);
|
|
||||||
|
|
||||||
socketData.topics = topTenTopics;
|
|
||||||
callback(null, socketData);
|
callback(null, socketData);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -374,12 +374,54 @@ describe('socket.io', function () {
|
|||||||
|
|
||||||
it('should return error', function (done) {
|
it('should return error', function (done) {
|
||||||
var socketAdmin = require('../src/socket.io/admin');
|
var socketAdmin = require('../src/socket.io/admin');
|
||||||
socketAdmin.before({uid: 10}, 'someMethod', {}, function (err) {
|
socketAdmin.before({uid: 10}, 'someMethod', {}, function (err) {
|
||||||
assert.equal(err.message, '[[error:no-privileges]]');
|
assert.equal(err.message, '[[error:no-privileges]]');
|
||||||
done();
|
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) {
|
after(function (done) {
|
||||||
db.emptydb(done);
|
db.emptydb(done);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user