From 8626122679c9569fcb0e5e7aebc4647014ce02d8 Mon Sep 17 00:00:00 2001 From: Baris Soner Usakli Date: Sat, 4 Jan 2014 01:15:41 -0500 Subject: [PATCH] category active users are stored in a sorted set now --- src/categories.js | 60 +++++++++++++++++++++++------------------------ src/upgrade.js | 54 ++++++++++++++++++++++++++++++++++++++---- 2 files changed, 78 insertions(+), 36 deletions(-) diff --git a/src/categories.js b/src/categories.js index 29a4ce09f4..33912387f3 100644 --- a/src/categories.js +++ b/src/categories.js @@ -138,9 +138,7 @@ var db = require('./database.js'), db.getSortedSetRevRange('categories:' + cid + ':tid', start, stop, callback); }; - Categories.getActiveUsers = function(cid, callback) { - db.getSetMembers('cid:' + cid + ':active_users', callback); - }; + Categories.getAllCategories = function(current_user, callback) { db.getListRange('categories:cid', 0, -1, function(err, cids) { @@ -268,25 +266,7 @@ var db = require('./database.js'), }); }; - Categories.moveActiveUsers = function(tid, oldCid, cid, callback) { - function updateUser(uid) { - Categories.addActiveUser(cid, uid); - Categories.isUserActiveIn(oldCid, uid, function(err, active) { - - if (!err && !active) { - Categories.removeActiveUser(oldCid, uid); - } - }); - } - topics.getUids(tid, function(err, uids) { - if (!err && uids) { - for (var i = 0; i < uids.length; ++i) { - updateUser(uids[i]); - } - } - }); - }; Categories.getCategoryData = function(cid, callback) { db.exists('category:' + cid, function(err, exists) { @@ -395,14 +375,38 @@ var db = require('./database.js'), }); }; - Categories.addActiveUser = function(cid, uid) { + Categories.addActiveUser = function(cid, uid, timestamp) { if(parseInt(uid, 10)) { - db.setAdd('cid:' + cid + ':active_users', uid); + db.sortedSetAdd('cid:' + cid + ':active_users', timestamp, uid); } }; Categories.removeActiveUser = function(cid, uid) { - db.setRemove('cid:' + cid + ':active_users', uid); + db.sortedSetRemove('cid:' + cid + ':active_users', uid); + }; + + Categories.getActiveUsers = function(cid, callback) { + db.getSortedSetRevRange('cid:' + cid + ':active_users', 0, 15, callback); + }; + + Categories.moveActiveUsers = function(tid, oldCid, cid, callback) { + function updateUser(uid) { + Categories.addActiveUser(cid, uid); + Categories.isUserActiveIn(oldCid, uid, function(err, active) { + + if (!err && !active) { + Categories.removeActiveUser(oldCid, uid); + } + }); + } + + topics.getUids(tid, function(err, uids) { + if (!err && uids) { + for (var i = 0; i < uids.length; ++i) { + updateUser(uids[i]); + } + } + }); }; Categories.onNewPostMade = function(uid, tid, pid, timestamp) { @@ -416,13 +420,7 @@ var db = require('./database.js'), db.sortedSetAdd('categories:' + cid + ':tid', timestamp, tid); } - db.setCount('cid:' + cid + ':active_users', function(err, amount) { - if (amount > 15) { - db.setRemoveRandom('cid:' + cid + ':active_users'); - } - - Categories.addActiveUser(cid, uid); - }); + Categories.addActiveUser(cid, uid, timestamp); }); } diff --git a/src/upgrade.js b/src/upgrade.js index 7e0c621793..9eae872964 100644 --- a/src/upgrade.js +++ b/src/upgrade.js @@ -14,7 +14,7 @@ var db = require('./database'), Upgrade.check = function(callback) { // IMPORTANT: REMEMBER TO UPDATE VALUE OF latestSchema - var latestSchema = new Date(2014, 0, 4).getTime(); + var latestSchema = new Date(2014, 0, 5).getTime(); db.get('schemaDate', function(err, value) { if (parseInt(value, 10) >= latestSchema) { @@ -133,7 +133,7 @@ Upgrade.upgrade = function(callback) { db.setObjectField('category:' + cids[cid], 'class', 'col-md-3 col-xs-6'); } - winston.info('[2013/12/31] Added categories.class, categories.link fields'); + winston.info('[2014/1/3] Added categories.class, categories.link fields'); next(); }); } else { @@ -156,14 +156,58 @@ Upgrade.upgrade = function(callback) { db.setObjectField('category:' + cids[cid], 'numRecentReplies', '2'); } - winston.info('[2013/12/31] Added categories.numRecentReplies fields'); + winston.info('[2014/1/4] Added categories.numRecentReplies fields'); next(); }); } else { - winston.info('[2014/1/3] categories.numRecentReplies fields skipped'); + winston.info('[2014/1/4] categories.numRecentReplies fields skipped'); next(); } - } + }, + function(next) { + thisSchemaDate = new Date(2014, 0, 5).getTime(); + if (schemaDate < thisSchemaDate) { + updatesMade = true; + + db.getListRange('categories:cid', 0, -1, function(err, cids) { + if(err) { + return next(err); + } + + var timestamp = Date.now(); + + function upgradeCategory(cid, next) { + db.getSetMembers('cid:' + cid + ':active_users', function(err, uids) { + if(err) { + return next(err); + } + + db.delete('cid:' + cid + ':active_users', function(err) { + if(err) { + return next(err); + } + + for(var i=0; i