From 2bf38ec84b0660924fadf12b2c97a752a1bfecba Mon Sep 17 00:00:00 2001 From: barisusakli Date: Tue, 22 Jul 2014 17:58:27 -0400 Subject: [PATCH] handle pinned topics --- src/categories/recentreplies.js | 42 ++++++++++++++++++++++++++------- src/topics/posts.js | 15 +++++++++--- 2 files changed, 46 insertions(+), 11 deletions(-) diff --git a/src/categories/recentreplies.js b/src/categories/recentreplies.js index ae6f41fe4c..62dc8929d4 100644 --- a/src/categories/recentreplies.js +++ b/src/categories/recentreplies.js @@ -24,21 +24,47 @@ module.exports = function(Categories) { }; Categories.getRecentTopicReplies = function(cid, uid, count, callback) { - if (!parseInt(count, 10)) { + count = parseInt(count, 10); + if (!count) { return callback(null, []); } - db.getSortedSetRevRange('categories:' + cid + ':tid', 0, count - 1, function(err, tids) { - if (err || !tids || !tids.length) { + db.getSortedSetRevRange('categories:recent_posts:cid:' + cid, 0, 0, function(err, pids) { + if (err || !pids || !pids.length) { return callback(err, []); } - async.map(tids, topics.getLatestUndeletedPid, function(err, pids) { - pids = pids.filter(function(pid) { - return !!pid; - }); + if (count === 1) { + return posts.getPostSummaryByPids(pids, {stripTags: true}, callback); + } - posts.getPostSummaryByPids(pids, {stripTags: true}, callback); + async.parallel({ + pinnedTids: function(next) { + db.getSortedSetRevRangeByScore('categories:' + cid + ':tid', 0, -1, Infinity, Date.now(), next); + }, + tids: function(next) { + db.getSortedSetRevRangeByScore('categories:' + cid + ':tid', 0, Math.max(0, count - 1), Date.now(), 0, next); + } + }, function(err, results) { + if (err) { + return callback(err); + } + + results.tids = results.tids.concat(results.pinnedTids); + + async.map(results.tids, topics.getLatestUndeletedPid, function(err, topicPids) { + pids = pids.concat(topicPids).filter(function(pid, index, array) { + return !!pid && array.indexOf(pid) === index; + }); + + posts.getPostSummaryByPids(pids, {stripTags: true}, function(err, posts) { + posts = posts.sort(function(a, b) { + return parseInt(b.timestamp, 10) - parseInt(a.timestamp, 10); + }).slice(0, count); + + callback(err, posts); + }); + }); }); }); }; diff --git a/src/topics/posts.js b/src/topics/posts.js index 8c755dd393..a61e67915a 100644 --- a/src/topics/posts.js +++ b/src/topics/posts.js @@ -134,16 +134,25 @@ module.exports = function(Topics) { }; Topics.getLatestUndeletedPid = function(tid, callback) { - db.getSortedSetRevRange('tid:' + tid + ':posts', 0, -1, function(err, pids) { + 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 (!pids || !pids.length) { + if (!results.mainPid && (!Array.isArray(results.pids) || !results.pids.length)) { return callback(null, null); } - async.detectSeries(pids, function(pid, next) { + results.pids.push(results.mainPid); + + async.detectSeries(results.pids, function(pid, next) { posts.getPostField(pid, 'deleted', function(err, deleted) { next(parseInt(deleted, 10) === 0); });