diff --git a/public/src/client/topic.js b/public/src/client/topic.js index d5d3aa28b6..cd8940320b 100644 --- a/public/src/client/topic.js +++ b/public/src/client/topic.js @@ -104,7 +104,7 @@ define('forum/topic', [ function getPostIndex() { var parts = window.location.pathname.split('/'); - return parts[4] ? parseInt(parts[4], 10) : 0; + return parts[parts.length - 1] ? parseInt(parts[parts.length - 1], 10) : 0; } function handleSorting() { diff --git a/src/posts/delete.js b/src/posts/delete.js index fa3efe3ce8..1a9c45ab05 100644 --- a/src/posts/delete.js +++ b/src/posts/delete.js @@ -167,6 +167,9 @@ module.exports = function(Posts) { function (next) { topics.decreasePostCount(postData.tid, next); }, + function(next) { + topics.updateTeaser(postData.tid, next); + }, function(next) { user.incrementUserPostCountBy(postData.uid, -1, next); }, diff --git a/src/topics/posts.js b/src/topics/posts.js index e119b4d14f..b089675430 100644 --- a/src/topics/posts.js +++ b/src/topics/posts.js @@ -23,7 +23,7 @@ module.exports = function(Topics) { }, function(next) { Topics.addPostToTopic(postData.tid, postData.pid, postData.timestamp, 0, next); - } + }, ], callback); }; @@ -134,43 +134,55 @@ module.exports = function(Topics) { }); }; - Topics.getLatestUndeletedPost = function(tid, callback) { - Topics.getLatestUndeletedPid(tid, function(err, pid) { - if(err) { + Topics.getLatestUndeletedPid = function(tid, callback) { + Topics.getLatestUndeletedReply(tid, function(err, pid) { + if (err) { return callback(err); } - - posts.getPostData(pid, callback); + if (parseInt(pid, 10)) { + return callback(null, pid.toString()); + } + Topics.getTopicField(tid, 'mainPid', function(err, mainPid) { + callback(err, parseInt(mainPid, 10) ? mainPid.toString() : null); + }); }); }; - Topics.getLatestUndeletedPid = function(tid, callback) { - async.parallel({ - mainPid: function(next) { - Topics.getTopicField(tid, 'mainPid', next); - }, - pids: function(next) { - db.getSortedSetRevRange('tid:' + tid + ':posts', 0, -1, next); - } - }, function(err, results) { - if(err) { - return callback(err); - } - - if (!results.mainPid && (!Array.isArray(results.pids) || !results.pids.length)) { - return callback(null, null); - } + Topics.getLatestUndeletedReply = function(tid, callback) { + var isDeleted = false; + var done = false; + var latestPid = null; + var index = 0; + async.doWhilst( + function(next) { + db.getSortedSetRevRange('tid:' + tid + ':posts', index, index, function(err, pids) { + if (err) { + return next(err); + } - results.pids.push(results.mainPid); + if (!Array.isArray(pids) || !pids.length) { + done = true; + return next(); + } - async.detectSeries(results.pids, function(pid, next) { - posts.getPostField(pid, 'deleted', function(err, deleted) { - next(parseInt(deleted, 10) === 0); + posts.getPostField(pids[0], 'deleted', function(err, deleted) { + if (err) { + return next(err); + } + latestPid = pids[0]; + isDeleted = deleted; + ++index; + next(); + }); }); - }, function(pid) { - callback(null, pid ? pid.toString() : null); - }); - }); + }, + function() { + return isDeleted && !done; + }, + function(err) { + callback(err, latestPid); + } + ); }; Topics.addPostToTopic = function(tid, pid, timestamp, votes, callback) { @@ -188,7 +200,12 @@ module.exports = function(Topics) { function(next) { db.sortedSetAdd('tid:' + tid + ':posts:votes', votes, pid, next); } - ], callback); + ], function(err) { + if (err) { + return callback(err); + } + Topics.updateTeaser(tid, callback); + }); } }); }; @@ -202,7 +219,10 @@ module.exports = function(Topics) { db.sortedSetRemove('tid:' + tid + ':posts:votes', pid, next); } ], function(err, results) { - callback(err); + if (err) { + return callback(err); + } + Topics.updateTeaser(tid, callback); }); }; diff --git a/src/topics/teaser.js b/src/topics/teaser.js index 0294159a81..3dba7d66c5 100644 --- a/src/topics/teaser.js +++ b/src/topics/teaser.js @@ -17,29 +17,21 @@ module.exports = function(Topics) { return callback(null, []); } - async.parallel({ - topics: function(next) { - Topics.getTopicsFields(tids, ['postcount'], next); - }, - pids: function(next) { - async.map(tids, function(tid, next) { - db.getSortedSetRevRange('tid:' + tid + ':posts', 0, 0, function(err, data) { - next(err, Array.isArray(data) && data.length ? data[0] : null); - }); - }, next); - } - }, function(err, results) { + Topics.getTopicsFields(tids, ['postcount', 'teaserPid'], function(err, topics) { if (err) { return callback(err); } + var counts = []; + var teaserPids = []; - var counts = results.topics.map(function(topic) { - return topic && (parseInt(topic.postcount, 10) || 0); + topics.forEach(function(topic) { + counts.push(topic && (parseInt(topic.postcount, 10) || 0)); + if (topic && topic.teaserPid) { + teaserPids.push(topic.teaserPid); + } }); - results.pids = results.pids.filter(Boolean); - - posts.getPostsFields(results.pids, ['pid', 'uid', 'timestamp', 'tid'], function(err, postData) { + posts.getPostsFields(teaserPids, ['pid', 'uid', 'timestamp', 'tid'], function(err, postData) { if (err) { return callback(err); } @@ -81,42 +73,18 @@ module.exports = function(Topics) { }; Topics.getTeaser = function(tid, callback) { - Topics.getLatestUndeletedPid(tid, function(err, pid) { - if (err || !pid) { + Topics.getTeasers([tid], function(err, teasers) { + callback(err, Array.isArray(teasers) && teasers.length ? teasers[0] : null); + }); + }; + + Topics.updateTeaser = function(tid, callback) { + db.getSortedSetRevRange('tid:' + tid + ':posts', 0, 0, function(err, pids) { + if (err) { return callback(err); } - - async.parallel({ - postData: function(next) { - posts.getPostFields(pid, ['pid', 'uid', 'timestamp'], function(err, postData) { - if (err) { - return next(err); - } else if(!postData || !utils.isNumber(postData.uid)) { - return callback(); - } - - user.getUserFields(postData.uid, ['username', 'userslug', 'picture'], function(err, userData) { - if (err) { - return next(err); - } - postData.user = userData; - next(null, postData); - }); - }); - }, - postCount: function(next) { - Topics.getTopicField(tid, 'post_count', next); - } - }, function(err, results) { - if (err) { - return callback(err); - } - - results.postData.timestamp = utils.toISOString(results.postData.timestamp); - results.postData.index = results.postCount; - - callback(null, results.postData); - }); + var pid = Array.isArray(pids) && pids.length ? pids[0] : null; + Topics.setTopicField(tid, 'teaserPid', pid, callback); }); }; }; \ No newline at end of file diff --git a/src/upgrade.js b/src/upgrade.js index 36ca657011..9e13e78c39 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(2014, 11, 2); + latestSchema = Date.UTC(2014, 11, 12); Upgrade.check = function(callback) { db.get('schemaDate', function(err, value) { @@ -389,6 +389,32 @@ Upgrade.upgrade = function(callback) { winston.info('[2014/12/2] Removing register user fields skipped'); next(); } + }, + function(next) { + thisSchemaDate = Date.UTC(2014, 11, 12); + if (schemaDate < thisSchemaDate) { + winston.info('[2014/12/12] Updating teasers'); + + db.getSortedSetRange('topics:tid', 0, -1, function(err, tids) { + if (err) { + return next(err); + } + + async.eachLimit(tids, 50, function(tid, next) { + Topics.updateTeaser(tid, next); + }, function(err) { + if (err) { + winston.error('[2014/12/12] Error encountered while updating teasers'); + return next(err); + } + winston.info('[2014/12/12] Updating teasers done'); + Upgrade.update(thisSchemaDate, next); + }); + }); + } else { + winston.info('[2014/12/12] Updating teasers skipped skipped'); + next(); + } } // Add new schema updates here