From 8f5bf1a7edc991f7b40c3f8520cec87b8b10b8f8 Mon Sep 17 00:00:00 2001 From: barisusakli Date: Wed, 14 Jan 2015 15:29:57 -0500 Subject: [PATCH] closes #2587 --- src/topics/delete.js | 21 ++++++++++++++------- src/upgrade.js | 41 +++++++++++++++++++++++++++++++++++++++-- src/user.js | 5 ++++- src/user/create.js | 1 + 4 files changed, 58 insertions(+), 10 deletions(-) diff --git a/src/topics/delete.js b/src/topics/delete.js index de86eb7bbc..1bb8e0798c 100644 --- a/src/topics/delete.js +++ b/src/topics/delete.js @@ -3,6 +3,7 @@ var async = require('async'), db = require('../database'), + user = require('../user'), plugins = require('../plugins'); @@ -77,13 +78,19 @@ module.exports = function(Topics) { if (err) { return callback(err); } - - db.sortedSetsRemove([ - 'cid:' + topicData.cid + ':tids', - 'cid:' + topicData.cid + ':tids:posts', - 'cid:' + topicData.cid + ':uid:' + topicData.uid + ':tids', - 'uid:' + topicData.uid + ':topics' - ], tid, callback); + async.parallel([ + function(next) { + db.sortedSetsRemove([ + 'cid:' + topicData.cid + ':tids', + 'cid:' + topicData.cid + ':tids:posts', + 'cid:' + topicData.cid + ':uid:' + topicData.uid + ':tids', + 'uid:' + topicData.uid + ':topics' + ], tid, next); + }, + function(next) { + user.decrementUserFieldBy(topicData.uid, 'topiccount', 1, next); + } + ], callback); }); } diff --git a/src/upgrade.js b/src/upgrade.js index 10ca9fef14..35f3acaa87 100644 --- a/src/upgrade.js +++ b/src/upgrade.js @@ -21,7 +21,7 @@ var db = require('./database'), schemaDate, thisSchemaDate, // IMPORTANT: REMEMBER TO UPDATE VALUE OF latestSchema - latestSchema = Date.UTC(2015, 0, 14); + latestSchema = Date.UTC(2015, 0, 15); Upgrade.check = function(callback) { db.get('schemaDate', function(err, value) { @@ -630,7 +630,44 @@ Upgrade.upgrade = function(callback) { winston.info('[2015/01/14] Upgrading follow sets to sorted sets skipped'); next(); } - } + }, + function(next) { + thisSchemaDate = Date.UTC(2015, 0, 15); + if (schemaDate < thisSchemaDate) { + winston.info('[2015/01/15] Creating topiccount for users'); + + db.getSortedSetRange('users:joindate', 0, -1, function(err, uids) { + if (err) { + winston.error('[2014/01/15] Error encountered while Creating topiccount for users'); + return next(err); + } + + async.eachLimit(uids, 50, function(uid, next) { + db.sortedSetCard('uid:' + uid + ':topics', function(err, count) { + if (err) { + return next(err); + } + + if (parseInt(count, 10)) { + db.setObjectField('user:' + uid, 'topiccount', count, next); + } else { + next(); + } + }); + }, function(err) { + if (err) { + winston.error('[2015/01/15] Error encountered while Creating topiccount for users'); + return next(err); + } + winston.info('[2015/01/15] Creating topiccount for users done'); + Upgrade.update(thisSchemaDate, next); + }); + }); + } else { + winston.info('[2015/01/15] Creating topiccount for users skipped'); + next(); + } + }, // Add new schema updates here // IMPORTANT: REMEMBER TO UPDATE VALUE OF latestSchema IN LINE 22!!! diff --git a/src/user.js b/src/user.js index a640b946d7..0a83994964 100644 --- a/src/user.js +++ b/src/user.js @@ -287,7 +287,10 @@ var async = require('async'), }; User.addTopicIdToUser = function(uid, tid, timestamp, callback) { - db.sortedSetAdd('uid:' + uid + ':topics', timestamp, tid, callback); + async.parallel([ + async.apply(db.sortedSetAdd, 'uid:' + uid + ':topics', timestamp, tid), + async.apply(User.incrementUserFieldBy, uid, 'topiccount', 1) + ], callback); }; User.exists = function(userslug, callback) { diff --git a/src/user/create.js b/src/user/create.js index e43c612488..d02635a550 100644 --- a/src/user/create.js +++ b/src/user/create.js @@ -43,6 +43,7 @@ module.exports = function(User) { 'profileviews': 0, 'reputation': 0, 'postcount': 0, + 'topiccount': 0, 'lastposttime': 0, 'banned': 0, 'status': 'online'