From 0c5881b51f84f14cb3979db3c2089a5c5b6cb56a Mon Sep 17 00:00:00 2001 From: barisusakli Date: Fri, 28 Nov 2014 20:31:47 -0500 Subject: [PATCH] update online users regardless of status --- src/middleware/middleware.js | 3 ++- src/user.js | 15 ++++++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/middleware/middleware.js b/src/middleware/middleware.js index ad2b6eb408..3281b4fd08 100644 --- a/src/middleware/middleware.js +++ b/src/middleware/middleware.js @@ -42,8 +42,9 @@ middleware.applyCSRF = csrf(); middleware.ensureLoggedIn = ensureLoggedIn.ensureLoggedIn(); middleware.updateLastOnlineTime = function(req, res, next) { - if(req.user) { + if (req.user) { user.updateLastOnlineTime(req.user.uid); + user.updateOnlineUsers(req.user.uid); } db.sortedSetScore('ip:recent', req.ip, function(err, score) { diff --git a/src/user.js b/src/user.js index 72694e8bfe..7a16ead789 100644 --- a/src/user.js +++ b/src/user.js @@ -138,16 +138,25 @@ var async = require('async'), callback = callback || function() {}; User.getUserFields(uid, ['status', 'lastonline'], function(err, userData) { var now = Date.now(); - if(err || userData.status === 'offline' || now - parseInt(userData.lastonline, 10) < 300000) { + if (err || userData.status === 'offline' || now - parseInt(userData.lastonline, 10) < 300000) { return callback(err); } - db.sortedSetAdd('users:online', now, uid); - User.setUserField(uid, 'lastonline', now, callback); }); }; + User.updateOnlineUsers = function(uid, callback) { + callback = callback || function() {}; + db.sortedSetScore('users:online', uid, function(err, score) { + var now = Date.now(); + if (err || now - parseInt(score, 10) < 300000) { + return callback(err); + } + db.sortedSetAdd('users:online', now, uid, callback); + }); + } + User.setUserField = function(uid, field, value, callback) { plugins.fireHook('action:user.set', {field: field, value: value, type: 'set'}); db.setObjectField('user:' + uid, field, value, callback);