most users ever online block

v1.18.x
psychobunny 12 years ago
parent bb8d5098e4
commit a655ebb290

@ -4,7 +4,7 @@
<!-- START Forum Info --> <!-- START Forum Info -->
<div id="footer" class="container" style="padding-top: 50px;"> <div id="footer" class="container" style="padding-top: 50px;">
<div class="alert alert-info"> <div class="alert alert-info">
<span id="active_users"></span><br /> <span id="active_users"></span>; <span id="active_record"></span><br />
<span id="number_of_users"></span><br /> <span id="number_of_users"></span><br />
<span id="latest_user"></span> <span id="latest_user"></span>
</div> </div>
@ -16,7 +16,8 @@
var num_users = document.getElementById('number_of_users'), var num_users = document.getElementById('number_of_users'),
latest_user = document.getElementById('latest_user'), latest_user = document.getElementById('latest_user'),
active_users = document.getElementById('active_users'), active_users = document.getElementById('active_users'),
user_label = document.getElementById('user_label'); user_label = document.getElementById('user_label'),
active_record = document.getElementById('active_record');
socket.emit('user.count', {}); socket.emit('user.count', {});
socket.on('user.count', function(data) { socket.on('user.count', function(data) {
@ -32,7 +33,14 @@
}); });
socket.emit('api:user.active.get'); socket.emit('api:user.active.get');
socket.on('api:user.active.get', function(data) { socket.on('api:user.active.get', function(data) {
active_users.innerHTML = 'There ' + (parseInt(data.users) !== 1 ? 'are' : 'is') + ' <strong>' + data.users + '</strong> user' + (parseInt(data.users) !== 1 ? 's' : '') + ' and <strong>' + data.anon + '</strong> guest' + (parseInt(data.anon) !== 1 ? 's' : '') + ' online'; var plural_users = parseInt(data.users) !== 1,
plural_anon = parseInt(data.anon) !== 1;
active_users.innerHTML = 'There ' + (plural_users ? 'are' : 'is') + ' <strong>' + data.users + '</strong> user' + (plural_users ? 's' : '') + ' and <strong>' + data.anon + '</strong> guest' + (plural_anon ? 's' : '') + ' online';
});
socket.emit('api:user.active.get_record');
socket.on('api:user.active.get_record', function(data) {
active_record.innerHTML = "most users ever online was <strong>" + data + "</strong>";
}); });
socket.emit('api:user.get', { fields: ['username', 'picture'] }); socket.emit('api:user.get', { fields: ['username', 'picture'] });
socket.on('api:user.get', function(data) { socket.on('api:user.get', function(data) {

@ -27,7 +27,7 @@ var config = require('../config.js'),
for(var x=0,numData=data.length;x<numData;x++) { for(var x=0,numData=data.length;x<numData;x++) {
returnData[fields[x]] = data[x]; returnData[fields[x]] = data[x];
} }
console.log(returnData);
if (returnData.picture !== undefined) { if (returnData.picture !== undefined) {
var md5sum = crypto.createHash('md5'); var md5sum = crypto.createHash('md5');
md5sum.update(returnData.email.toLowerCase()); md5sum.update(returnData.email.toLowerCase());
@ -47,17 +47,17 @@ var config = require('../config.js'),
User.login = function(user) { User.login = function(user) {
if (user.username == null || user.password == null) { if (user.username == null || user.password == null) {
return global.socket.emit('user.login', {'status': 0, 'message': 'Missing fields'}); return socket.emit('user.login', {'status': 0, 'message': 'Missing fields'});
} }
RDB.get('username:' + user.username + ':uid', function(uid) { RDB.get('username:' + user.username + ':uid', function(uid) {
if (uid == null) { if (uid == null) {
return global.socket.emit('user.login', {'status': 0, 'message': 'Username does not exist.'}); return socket.emit('user.login', {'status': 0, 'message': 'Username does not exist.'});
} }
RDB.get('uid:' + uid + ':password', function(password) { RDB.get('uid:' + uid + ':password', function(password) {
if (user.password != password) { if (user.password != password) {
return global.socket.emit('user.login', {'status': 0, 'message': 'Incorrect username / password combination.'}); return socket.emit('user.login', {'status': 0, 'message': 'Incorrect username / password combination.'});
} else { } else {
// Start, replace, or extend a session // Start, replace, or extend a session
RDB.get('sess:' + user.sessionID, function(session) { RDB.get('sess:' + user.sessionID, function(session) {
@ -88,7 +88,7 @@ var config = require('../config.js'),
User.create = function(username, password, email) { User.create = function(username, password, email) {
if (username == null || password == null) { if (username == null || password == null) {
return; global.socket.emit('user.create', {'status': 0, 'message': 'Missing fields'}); return; socket.emit('user.create', {'status': 0, 'message': 'Missing fields'});
} }
@ -111,9 +111,9 @@ var config = require('../config.js'),
RDB.lpush('user:users', username); RDB.lpush('user:users', username);
io.sockets.emit('user.latest', {username: username}); io.sockets.emit('user.latest', {username: username});
global.socket.emit('user.create', {'status': 1}); socket.emit('user.create', {'status': 1});
global.socket.emit('event:alert', { socket.emit('event:alert', {
title: 'Thank you for registering', title: 'Thank you for registering',
message: 'You have successfully registered - welcome to nodebb!', message: 'You have successfully registered - welcome to nodebb!',
type: 'notify', type: 'notify',
@ -127,7 +127,7 @@ var config = require('../config.js'),
User.exists = function(username, callback) { User.exists = function(username, callback) {
User.get_uid_by_username(username, function(exists) { User.get_uid_by_username(username, function(exists) {
exists = !!exists; exists = !!exists;
global.socket.emit('user.exists', {exists: exists}) socket.emit('user.exists', {exists: exists})
if (callback) { if (callback) {
callback(exists); callback(exists);
@ -136,12 +136,12 @@ var config = require('../config.js'),
}; };
User.count = function() { User.count = function() {
RDB.get('user:count', function(count) { RDB.get('user:count', function(count) {
global.socket.emit('user.count', {count: (count === null) ? 0 : count}); socket.emit('user.count', {count: (count === null) ? 0 : count});
}); });
}; };
User.latest = function() { User.latest = function() {
RDB.lrange('user:users', 0, 0, function(username) { RDB.lrange('user:users', 0, 0, function(username) {
global.socket.emit('user.latest', {username: username}); socket.emit('user.latest', {username: username});
}); });
} }
@ -165,18 +165,18 @@ var config = require('../config.js'),
if (uid !== null) { if (uid !== null) {
RDB.get('reset:' + code + ':expiry', function(expiry) { RDB.get('reset:' + code + ':expiry', function(expiry) {
if (expiry >= +new Date()/1000|0) { if (expiry >= +new Date()/1000|0) {
if (!callback) global.socket.emit('user:reset.valid', { valid: true }); if (!callback) socket.emit('user:reset.valid', { valid: true });
else callback(true); else callback(true);
} else { } else {
// Expired, delete from db // Expired, delete from db
RDB.del('reset:' + code + ':uid'); RDB.del('reset:' + code + ':uid');
RDB.del('reset:' + code + ':expiry'); RDB.del('reset:' + code + ':expiry');
if (!callback) global.socket.emit('user:reset.valid', { valid: false }); if (!callback) socket.emit('user:reset.valid', { valid: false });
else callback(false); else callback(false);
} }
}); });
} else { } else {
if (!callback) global.socket.emit('user:reset.valid', { valid: false }); if (!callback) socket.emit('user:reset.valid', { valid: false });
else callback(false); else callback(false);
} }
}); });
@ -208,13 +208,13 @@ var config = require('../config.js'),
emailjsServer.send(message, function(err, success) { emailjsServer.send(message, function(err, success) {
if (err === null) { if (err === null) {
global.socket.emit('user.send_reset', { socket.emit('user.send_reset', {
status: "ok", status: "ok",
message: "code-sent", message: "code-sent",
email: email email: email
}); });
} else { } else {
global.socket.emit('user.send_reset', { socket.emit('user.send_reset', {
status: "error", status: "error",
message: "send-failed" message: "send-failed"
}); });
@ -222,7 +222,7 @@ var config = require('../config.js'),
} }
}); });
} else { } else {
global.socket.emit('user.send_reset', { socket.emit('user.send_reset', {
status: "error", status: "error",
message: "invalid-email", message: "invalid-email",
email: email email: email
@ -238,7 +238,7 @@ var config = require('../config.js'),
RDB.del('reset:' + code + ':uid'); RDB.del('reset:' + code + ':uid');
RDB.del('reset:' + code + ':expiry'); RDB.del('reset:' + code + ':expiry');
global.socket.emit('user:reset.commit', { status: 'ok' }); socket.emit('user:reset.commit', { status: 'ok' });
}); });
} }
}); });
@ -249,14 +249,28 @@ var config = require('../config.js'),
exists: function(email, callback) { exists: function(email, callback) {
User.get_uid_by_email(email, function(exists) { User.get_uid_by_email(email, function(exists) {
exists = !!exists; exists = !!exists;
if (typeof callback !== 'function') global.socket.emit('user.email.exists', { exists: exists }); if (typeof callback !== 'function') socket.emit('user.email.exists', { exists: exists });
else callback(exists); else callback(exists);
}); });
} }
} }
User.active = { User.active = {
get_record : function() {
RDB.get('global:active_user_record', function(record) {
socket.emit('api:user.active.get_record', record);
});
},
get: function(callback) { get: function(callback) {
function user_record(total) {
RDB.get('global:active_user_record', function(record) {
if (total > record) {
RDB.set('global:active_user_record', total);
}
});
}
RDB.keys('active:*', function(active) { RDB.keys('active:*', function(active) {
var returnObj = { var returnObj = {
users: 0, users: 0,
@ -282,6 +296,8 @@ var config = require('../config.js'),
} }
} }
user_record(returnObj.anon + returnObj.users);
if (callback === undefined) { if (callback === undefined) {
io.sockets.emit('api:user.active.get', returnObj) io.sockets.emit('api:user.active.get', returnObj)
} else { } else {

@ -96,6 +96,10 @@ var SocketIO = require('socket.io').listen(global.server),
socket.on('api:user.active.get', function() { socket.on('api:user.active.get', function() {
modules.user.active.get(); modules.user.active.get();
}); });
socket.on('api:user.active.get_record', function() {
modules.user.active.get_record();
});
}); });
}(SocketIO)); }(SocketIO));

Loading…
Cancel
Save