online page changes

v1.18.x
Baris Usakli 11 years ago
parent 52f2e193d6
commit 1155eaf1f1

@ -77,8 +77,11 @@ define(function() {
socket.on('api:user.isOnline', function(data) { socket.on('api:user.isOnline', function(data) {
if(active == 'online' && !loadingMoreUsers) { if(active == 'online' && !loadingMoreUsers) {
$('#users-container').empty(); $('#users-container .registered-user').remove();
startLoading('users:online', 0); startLoading('users:online', 0);
socket.emit('api:user.getOnlineAnonCount', {} , function(anonCount) {
$('#online_anon_count').html(anonCount);
});
} }
}); });
@ -86,7 +89,7 @@ define(function() {
var html = templates.prepare(templates['users'].blocks['users']).parse({ var html = templates.prepare(templates['users'].blocks['users']).parse({
users: users users: users
}); });
$('#users-container').append(html); $('#users-container').prepend(html);
} }
function loadMoreUsers() { function loadMoreUsers() {

@ -20,7 +20,7 @@
<ul id="users-container" class="users-container"> <ul id="users-container" class="users-container">
<!-- BEGIN users --> <!-- BEGIN users -->
<div class="users-box"> <div class="users-box registered-user">
<a href="/user/{users.userslug}"> <a href="/user/{users.userslug}">
<img src="{users.picture}" class="img-thumbnail"/> <img src="{users.picture}" class="img-thumbnail"/>
</a> </a>
@ -39,6 +39,17 @@
</div> </div>
</div> </div>
<!-- END users --> <!-- END users -->
<div class="users-box {show_anon}">
<a href="#">
<img src="" class="img-thumbnail"/>
</a>
<br/>
<div class="user-info">
<span id="online_anon_count">{anonymousUserCount}</span>
<br/>
<a href="#">Anonymous</a>
</div>
</div>
</ul> </ul>
<div class="text-center {loadmore_display}"> <div class="text-center {loadmore_display}">

@ -6,7 +6,10 @@ var user = require('./../user.js'),
path = require('path'), path = require('path'),
winston = require('winston'), winston = require('winston'),
nconf = require('nconf'), nconf = require('nconf'),
meta = require('./../meta'); meta = require('./../meta'),
async= require('async'),
RDB = require('./../redis'),
websockets = require('./../websockets.js');
(function (User) { (function (User) {
User.create_routes = function (app) { User.create_routes = function (app) {
@ -457,7 +460,8 @@ var user = require('./../user.js'),
res.json({ res.json({
search_display: 'none', search_display: 'none',
loadmore_display: 'block', loadmore_display: 'block',
users: data users: data,
show_anon: 'hide'
}); });
}); });
} }
@ -467,7 +471,8 @@ var user = require('./../user.js'),
res.json({ res.json({
search_display: 'none', search_display: 'none',
loadmore_display: 'block', loadmore_display: 'block',
users: data users: data,
show_anon: 'hide'
}); });
}); });
} }
@ -477,17 +482,36 @@ var user = require('./../user.js'),
res.json({ res.json({
search_display: 'none', search_display: 'none',
loadmore_display: 'block', loadmore_display: 'block',
users: data users: data,
show_anon: 'hide'
}); });
}); });
} }
function getOnlineUsers(req, res) { function getOnlineUsers(req, res) {
user.getUsers('users:online', 0, 49, function (err, data) { user.getUsers('users:online', 0, 49, function (err, data) {
res.json({
search_display: 'none', var onlineUsers = [];
loadmore_display: 'block',
users: data function iterator(user, callback) {
if(websockets.isUserOnline(user.uid)) {
onlineUsers.push(user);
} else {
RDB.zrem('users:online', user.uid);
}
callback(null);
}
var anonymousUserCount = websockets.getOnlineAnonCount();
async.each(data, iterator, function(err) {
res.json({
search_display: 'none',
loadmore_display: 'block',
users: onlineUsers,
anonymousUserCount: anonymousUserCount,
show_anon: anonymousUserCount?'':'hide'
});
}); });
}); });
} }
@ -496,7 +520,8 @@ var user = require('./../user.js'),
res.json({ res.json({
search_display: 'block', search_display: 'block',
loadmore_display: 'none', loadmore_display: 'none',
users: [] users: [],
show_anon: 'hide'
}); });
} }

@ -68,7 +68,6 @@ module.exports.init = function(io) {
RDB.zadd('users:online', Date.now(), uid, function(err, data) { RDB.zadd('users:online', Date.now(), uid, function(err, data) {
socket.join('uid_' + uid); socket.join('uid_' + uid);
io.sockets. in ('global').emit('api:user.isOnline', isUserOnline(uid));
user.getUserField(uid, 'username', function(err, username) { user.getUserField(uid, 'username', function(err, username) {
socket.emit('event:connect', { socket.emit('event:connect', {
@ -79,6 +78,8 @@ module.exports.init = function(io) {
}); });
}); });
} }
io.sockets. in ('global').emit('api:user.isOnline', isUserOnline(uid));
}); });
}); });
@ -96,11 +97,12 @@ module.exports.init = function(io) {
delete userSockets[uid]; delete userSockets[uid];
if (uid) { if (uid) {
RDB.zrem('users:online', uid, function(err, data) { RDB.zrem('users:online', uid, function(err, data) {
io.sockets. in ('global').emit('api:user.isOnline', isUserOnline(uid));
}); });
} }
} }
io.sockets. in ('global').emit('api:user.isOnline', isUserOnline(uid));
emitOnlineUserCount(); emitOnlineUserCount();
for (var roomName in rooms) { for (var roomName in rooms) {
@ -266,6 +268,8 @@ module.exports.init = function(io) {
return !!userSockets[uid] && userSockets[uid].length > 0; return !!userSockets[uid] && userSockets[uid].length > 0;
} }
module.exports.isUserOnline = isUserOnline;
socket.on('api:user.get_online_users', function(data) { socket.on('api:user.get_online_users', function(data) {
var returnData = []; var returnData = [];
@ -282,6 +286,7 @@ module.exports.init = function(io) {
socket.on('api:user.isOnline', function(uid, callback) { socket.on('api:user.isOnline', function(uid, callback) {
callback({ callback({
online: isUserOnline(uid), online: isUserOnline(uid),
uid: uid,
timestamp: Date.now() timestamp: Date.now()
}); });
}); });
@ -438,6 +443,14 @@ module.exports.init = function(io) {
}); });
}); });
socket.on('api:user.getOnlineAnonCount', function(data, callback) {
callback(module.exports.getOnlineAnonCount());
});
module.exports.getOnlineAnonCount = function () {
return userSockets[0] ? userSockets[0].length : 0;
}
function emitOnlineUserCount() { function emitOnlineUserCount() {
var anon = userSockets[0] ? userSockets[0].length : 0; var anon = userSockets[0] ? userSockets[0].length : 0;
var registered = Object.keys(userSockets).length; var registered = Object.keys(userSockets).length;
@ -445,7 +458,8 @@ module.exports.init = function(io) {
registered = registered - 1; registered = registered - 1;
var returnObj = { var returnObj = {
users: registered + anon users: registered + anon,
anon: anon
}; };
io.sockets.emit('api:user.active.get', returnObj) io.sockets.emit('api:user.active.get', returnObj)
} }

Loading…
Cancel
Save