simplified calculatePostIndices

v1.18.x
barisusakli 10 years ago
parent 76178e3232
commit 51cdd0800c

@ -274,17 +274,13 @@ var async = require('async'),
if (!posts.length) { if (!posts.length) {
return next(null, []); return next(null, []);
} }
var replies = posts;
if (topic.mainPid) { if (topic.mainPid) {
posts[0].index = 0; posts[0].index = 0;
replies = posts.slice(1);
} }
var indices = Topics.calculatePostIndices(start, stop, topic.postcount, reverse); Topics.calculatePostIndices(replies, start, stop, topic.postcount, reverse);
for (var i=1; i<posts.length; ++i) {
if (posts[i]) {
posts[i].index = indices[i - 1];
}
}
Topics.addPostData(posts, uid, next); Topics.addPostData(posts, uid, next);
} }

@ -44,12 +44,7 @@ module.exports = function(Topics) {
return callback(err); return callback(err);
} }
var indices = Topics.calculatePostIndices(start, stop, results.postCount, reverse); Topics.calculatePostIndices(results.posts, start, stop, results.postCount, reverse);
results.posts.forEach(function(post, index) {
if (post) {
post.index = indices[index];
}
});
Topics.addPostData(results.posts, uid, callback); Topics.addPostData(results.posts, uid, callback);
}); });
@ -151,17 +146,14 @@ module.exports = function(Topics) {
}); });
}; };
Topics.calculatePostIndices = function(start, stop, postCount, reverse) { Topics.calculatePostIndices = function(posts, start, stop, postCount, reverse) {
var indices = []; posts.forEach(function(post, index) {
var count = stop - start + 1;
for(var i=0; i<count; ++i) {
if (reverse) { if (reverse) {
indices.push(postCount - (start + i + 1)); post.index = postCount - (start + index + 1);
} else { } else {
indices.push(start + i + 1); post.index = start + index + 1;
} }
} });
return indices;
}; };
Topics.getLatestUndeletedPid = function(tid, callback) { Topics.getLatestUndeletedPid = function(tid, callback) {

Loading…
Cancel
Save