category active users are stored in a sorted set now

v1.18.x
Baris Soner Usakli 11 years ago
parent 8ab539b518
commit 8626122679

@ -138,9 +138,7 @@ var db = require('./database.js'),
db.getSortedSetRevRange('categories:' + cid + ':tid', start, stop, callback); 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) { Categories.getAllCategories = function(current_user, callback) {
db.getListRange('categories:cid', 0, -1, function(err, cids) { 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) { Categories.getCategoryData = function(cid, callback) {
db.exists('category:' + cid, function(err, exists) { 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)) { if(parseInt(uid, 10)) {
db.setAdd('cid:' + cid + ':active_users', uid); db.sortedSetAdd('cid:' + cid + ':active_users', timestamp, uid);
} }
}; };
Categories.removeActiveUser = function(cid, 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) { Categories.onNewPostMade = function(uid, tid, pid, timestamp) {
@ -416,13 +420,7 @@ var db = require('./database.js'),
db.sortedSetAdd('categories:' + cid + ':tid', timestamp, tid); db.sortedSetAdd('categories:' + cid + ':tid', timestamp, tid);
} }
db.setCount('cid:' + cid + ':active_users', function(err, amount) { Categories.addActiveUser(cid, uid, timestamp);
if (amount > 15) {
db.setRemoveRandom('cid:' + cid + ':active_users');
}
Categories.addActiveUser(cid, uid);
});
}); });
} }

@ -14,7 +14,7 @@ var db = require('./database'),
Upgrade.check = function(callback) { Upgrade.check = function(callback) {
// IMPORTANT: REMEMBER TO UPDATE VALUE OF latestSchema // 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) { db.get('schemaDate', function(err, value) {
if (parseInt(value, 10) >= latestSchema) { 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'); 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(); next();
}); });
} else { } else {
@ -156,14 +156,58 @@ Upgrade.upgrade = function(callback) {
db.setObjectField('category:' + cids[cid], 'numRecentReplies', '2'); 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(); next();
}); });
} else { } else {
winston.info('[2014/1/3] categories.numRecentReplies fields skipped'); winston.info('[2014/1/4] categories.numRecentReplies fields skipped');
next(); 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<uids.length; ++i) {
db.sortedSetAdd('cid:' + cid + ':active_users', timestamp, uids[i]);
}
next();
});
});
}
async.each(cids, upgradeCategory, function(err) {
if(err) {
return next(err)
}
winston.info('[2014/1/5] Upgraded categories active users');
next();
});
});
} else {
winston.info('[2014/1/5] categories active users skipped');
next();
}
},
// Add new schema updates here // Add new schema updates here
// IMPORTANT: REMEMBER TO UPDATE VALUE OF latestSchema IN LINE 17!!! // IMPORTANT: REMEMBER TO UPDATE VALUE OF latestSchema IN LINE 17!!!
], function(err) { ], function(err) {

Loading…
Cancel
Save