From c85958626990ab0e1cf9795e9697f9ef7d63df55 Mon Sep 17 00:00:00 2001 From: barisusakli Date: Sat, 7 Feb 2015 00:12:47 -0500 Subject: [PATCH] calculate topic post indices instead of querying db --- src/controllers/topics.js | 8 +++----- src/topics.js | 30 ++++++++++-------------------- src/topics/posts.js | 36 +++++++++++++++++++++++++++++------- 3 files changed, 42 insertions(+), 32 deletions(-) diff --git a/src/controllers/topics.js b/src/controllers/topics.js index f470cfc6d1..83d873b583 100644 --- a/src/controllers/topics.js +++ b/src/controllers/topics.js @@ -116,15 +116,13 @@ topicsController.get = function(req, res, next) { } topicData.pageCount = pageCount; - topicData.currentPage = page; - if(page > 1) { + + if (page > 1) { topicData.posts.splice(0, 1); } - plugins.fireHook('filter:controllers.topic.get', topicData, function(err, topicData) { - next(null, topicData); - }); + plugins.fireHook('filter:controllers.topic.get', topicData, next); }); }, function (topicData, next) { diff --git a/src/topics.js b/src/topics.js index 1e9df9958f..692088ec52 100644 --- a/src/topics.js +++ b/src/topics.js @@ -215,25 +215,11 @@ var async = require('async'), } async.parallel({ + mainPost: function(next) { + Topics.getMainPost(tid, uid, next); + }, posts: function(next) { - posts.getPidsFromSet(set, start, end, reverse, function(err, pids) { - if (err) { - return next(err); - } - - pids = topicData.mainPid ? [topicData.mainPid].concat(pids) : pids; - - if (!pids.length) { - return next(null, []); - } - posts.getPostsByPids(pids, uid, function(err, posts) { - if (err) { - return next(err); - } - - Topics.addPostData(posts, uid, next); - }); - }); + Topics.getTopicPosts(tid, set, start, end, uid, reverse, next); }, category: async.apply(Topics.getCategoryData, tid), threadTools: async.apply(plugins.fireHook, 'filter:topic.thread_tools', []), @@ -244,7 +230,7 @@ var async = require('async'), return callback(err); } - topicData.posts = results.posts; + topicData.posts = results.mainPost ? [results.mainPost].concat(results.posts) : results.posts; topicData.category = results.category; topicData.thread_tools = results.threadTools; topicData.tags = results.tags; @@ -280,7 +266,11 @@ var async = require('async'), if (err) { return callback(err); } - + postData.forEach(function(post) { + if (post) { + post.index = 0; + } + }); Topics.addPostData(postData, uid, callback); }); }); diff --git a/src/topics/posts.js b/src/topics/posts.js index d4809f2a79..6d4cd9be8f 100644 --- a/src/topics/posts.js +++ b/src/topics/posts.js @@ -29,15 +29,28 @@ module.exports = function(Topics) { ], callback); }; - Topics.getTopicPosts = function(tid, set, start, end, uid, reverse, callback) { callback = callback || function() {}; - posts.getPostsByTid(tid, set, start, end, uid, reverse, function(err, postData) { + async.parallel({ + posts: function(next) { + posts.getPostsByTid(tid, set, start, end, uid, reverse, next); + }, + postCount: function(next) { + Topics.getTopicField(tid, 'postcount', next); + } + }, function(err, results) { if (err) { return callback(err); } - Topics.addPostData(postData, uid, callback); + var indices = Topics.calculatePostIndices(start, end, results.postCount, reverse); + results.posts.forEach(function(post, index) { + if (post) { + post.index = indices[index]; + } + }); + + Topics.addPostData(results.posts, uid, callback); }); }; @@ -103,9 +116,6 @@ module.exports = function(Topics) { }, privileges: function(next) { privileges.posts.get(pids, uid, next); - }, - indices: function(next) { - posts.getPostIndices(postData, uid, next); } }, function(err, results) { if (err) { @@ -114,7 +124,6 @@ module.exports = function(Topics) { postData.forEach(function(postObj, i) { if (postObj) { - postObj.index = results.indices[i]; postObj.deleted = parseInt(postObj.deleted, 10) === 1; postObj.user = parseInt(postObj.uid, 10) ? results.userData[postObj.uid] : _.clone(results.userData[postObj.uid]); postObj.editor = postObj.editor ? results.editors[postObj.editor] : null; @@ -141,6 +150,19 @@ module.exports = function(Topics) { }); }; + Topics.calculatePostIndices = function(start, end, postCount, reverse) { + var indices = []; + var count = end - start + 1; + for(var i=0; i