From 776b51fef7dac236175a3c6c3300b342905f862d Mon Sep 17 00:00:00 2001 From: Baris Usakli Date: Mon, 23 Sep 2013 13:43:15 -0400 Subject: [PATCH] closes #323 --- public/src/app.js | 2 +- public/src/forum/users.js | 35 +++++++++++++++++++++++------------ public/templates/users.tpl | 1 + src/plugins.js | 2 +- src/routes/user.js | 25 ++++++++++++++++++++++--- src/websockets.js | 26 ++++++++++++++++---------- 6 files changed, 64 insertions(+), 27 deletions(-) diff --git a/public/src/app.js b/public/src/app.js index 00aaf7c47c..63a57bde7b 100644 --- a/public/src/app.js +++ b/public/src/app.js @@ -275,7 +275,7 @@ var socket, if (active) { jQuery('#main-nav li a').each(function() { var href = this.getAttribute('href'); - if (active == "sort-posts" || active == "sort-reputation" || active == "search" || active== "latest") + if (active == "sort-posts" || active == "sort-reputation" || active == "search" || active == "latest" || active == "online") active = 'users'; if (href && href.match(active)) { jQuery(this.parentNode).addClass('active'); diff --git a/public/src/forum/users.js b/public/src/forum/users.js index bf69cdad9f..37b73d3b34 100644 --- a/public/src/forum/users.js +++ b/public/src/forum/users.js @@ -74,7 +74,10 @@ }); - + socket.on('api:user.isOnline', function(data) { + $('#users-container').empty(); + startLoading('users:online', 0); + }); function onUsersLoaded(users) { var html = templates.prepare(templates['users'].blocks['users']).parse({ @@ -91,24 +94,32 @@ set = 'users:postcount'; } else if (active === 'sort-reputation') { set = 'users:reputation'; + } else if (active === 'online') { + set = 'users:online'; } if (set) { loadingMoreUsers = true; - socket.emit('api:users.loadMore', { - set: set, - after: $('#users-container').children().length - }, function(data) { - if (data.users.length) { - onUsersLoaded(data.users); - } else { - $('#load-more-users-btn').addClass('disabled'); - } - loadingMoreUsers = false; - }); + startLoading(set, $('#users-container').children().length); } } + function startLoading(set, after) { + socket.emit('api:users.loadMore', { + set: set, + after: after + }, function(data) { + if (data.users.length) { + onUsersLoaded(data.users); + $('#load-more-users-btn').removeClass('disabled'); + } else { + $('#load-more-users-btn').addClass('disabled'); + } + loadingMoreUsers = false; + }); + } + + $('#load-more-users-btn').on('click', loadMoreUsers); $(window).off('scroll').on('scroll', function() { diff --git a/public/templates/users.tpl b/public/templates/users.tpl index a6319080c1..ebc00cc62d 100644 --- a/public/templates/users.tpl +++ b/public/templates/users.tpl @@ -3,6 +3,7 @@
  • Latest Users
  • Top Posters
  • Most Reputation
  • +
  • Online
  • Search
  • diff --git a/src/plugins.js b/src/plugins.js index 1afdc3b211..c8e77de3f7 100644 --- a/src/plugins.js +++ b/src/plugins.js @@ -137,7 +137,7 @@ var fs = require('fs'), hookList = this.loadedHooks[hook]; if (hookList && Array.isArray(hookList)) { - if (global.env === 'development') winston.info('[plugins] Firing hook: \'' + hook + '\''); + //if (global.env === 'development') winston.info('[plugins] Firing hook: \'' + hook + '\''); var hookType = hook.split(':')[0]; switch (hookType) { case 'filter': diff --git a/src/routes/user.js b/src/routes/user.js index 78cd6da6c6..e6870823b8 100644 --- a/src/routes/user.js +++ b/src/routes/user.js @@ -25,7 +25,6 @@ var user = require('./../user.js'), }); } }); - }); app.get('/users', function(req, res) { @@ -64,6 +63,15 @@ var user = require('./../user.js'), }); }); + app.get('/users/online', function(req, res) { + app.build_header({ + req: req, + res: res + }, function(err, header) { + res.send(header + app.create_route("users/online", "users") + templates['footer']); + }); + }); + app.get('/users/search', function(req, res) { app.build_header({ req: req, @@ -136,7 +144,7 @@ var user = require('./../user.js'), app.post('/user/uploadpicture', function(req, res) { if (!req.user) return res.redirect('/403'); - + var uploadSize = meta.config.maximumProfileImageSize || 256; if (req.files.userPhoto.size > uploadSize * 1024) { @@ -342,7 +350,7 @@ var user = require('./../user.js'), return; } - + user.getUserFields(uid, ['username', 'userslug', 'showemail'], function(err, userData) { if (err) return next(err); @@ -436,6 +444,7 @@ var user = require('./../user.js'), app.get('/api/users/sort-posts', getUsersSortedByPosts); app.get('/api/users/sort-reputation', getUsersSortedByReputation); app.get('/api/users/latest', getUsersSortedByJoinDate); + app.get('/api/users/online', getOnlineUsers); app.get('/api/users/search', getUsersForSearch); @@ -469,6 +478,16 @@ var user = require('./../user.js'), }); } + function getOnlineUsers(req, res) { + user.getUsers('users:online', 0, 49, function(err, data) { + res.json({ + search_display: 'none', + loadmore_display: 'block', + users: data + }); + }); + } + function getUsersForSearch(req, res) { res.json({ search_display: 'block', diff --git a/src/websockets.js b/src/websockets.js index 38da997253..eec83d0693 100644 --- a/src/websockets.js +++ b/src/websockets.js @@ -23,6 +23,7 @@ var SocketIO = require('socket.io').listen(global.server, { client: RDB, ttl: 60 * 60 * 24 * 14 }), + nconf = require('nconf'), socketCookieParser = express.cookieParser(nconf.get('secret')), admin = { 'categories': require('./admin/categories.js'), @@ -53,14 +54,17 @@ var SocketIO = require('socket.io').listen(global.server, { userSockets[uid].push(socket); if (uid) { - socket.join('uid_' + uid); - io.sockets. in ('global').emit('api:user.isOnline', isUserOnline(uid)); - - user.getUserField(uid, 'username', function(err, username) { - socket.emit('event:connect', { - status: 1, - username: username, - uid: uid + + RDB.zadd('users:online', Date.now(), uid, function(err, data) { + socket.join('uid_' + uid); + io.sockets. in ('global').emit('api:user.isOnline', isUserOnline(uid)); + + user.getUserField(uid, 'username', function(err, username) { + socket.emit('event:connect', { + status: 1, + username: username, + uid: uid + }); }); }); } @@ -80,7 +84,9 @@ var SocketIO = require('socket.io').listen(global.server, { delete users[sessionID]; delete userSockets[uid]; if (uid) { - io.sockets. in ('global').emit('api:user.isOnline', isUserOnline(uid)); + RDB.zrem('users:online', uid, function(err, data) { + io.sockets. in ('global').emit('api:user.isOnline', isUserOnline(uid)); + }); } } @@ -100,7 +106,7 @@ var SocketIO = require('socket.io').listen(global.server, { socket.on('api:get_all_rooms', function(data) { socket.emit('api:get_all_rooms', io.sockets.manager.rooms); - }) + }); function updateRoomBrowsingText(roomName) {