diff --git a/src/topics/posts.js b/src/topics/posts.js index 909de0caa4..1e0fd20f42 100644 --- a/src/topics/posts.js +++ b/src/topics/posts.js @@ -10,6 +10,7 @@ var user = require('../user'); var posts = require('../posts'); var meta = require('../meta'); var plugins = require('../plugins'); +var utils = require('../../public/src/utils'); module.exports = function (Topics) { Topics.onNewPostMade = function (postData, callback) { @@ -103,6 +104,9 @@ module.exports = function (Topics) { parents: function (next) { Topics.addParentPosts(postData, next); }, + replies: function (next) { + +getPostReplies(pids, uid, next); + }, }, next); }, function (results, next) { @@ -115,7 +119,7 @@ module.exports = function (Topics) { postObj.upvoted = results.voteData.upvotes[i]; postObj.downvoted = results.voteData.downvotes[i]; postObj.votes = postObj.votes || 0; - postObj.replies = postObj.replies || 0; + postObj.replies = results.replies[i]; postObj.selfPost = !!parseInt(uid, 10) && parseInt(uid, 10) === parseInt(postObj.uid, 10); // Username override for guests, if enabled @@ -385,4 +389,45 @@ module.exports = function (Topics) { Topics.getPostCount = function (tid, callback) { db.getObjectField('topic:' + tid, 'postcount', callback); }; + + function getPostReplies(pids, callerUid, callback) { + async.map(pids, function (pid, next) { + db.getSortedSetRange('pid:' + pid + ':replies', 0, -1, function (err, replyPids) { + var uids = [], +count = 0; + + async.until(function () { + return count === replyPids.length || uids.length === 6; + }, function (next) { + posts.getPostField(replyPids[count], 'uid', function (err, uid) { + uid = parseInt(uid, 10); + if (uids.indexOf(uid) === -1) { + uids.push(uid); + } + count++; + next(err); + }); + }, function (err) { + async.parallel({ + 'users': function (next) { + user.getUsersWithFields(uids, ['uid', 'username', 'userslug', 'picture'], callerUid, next); + }, + 'timestampISO': function (next) { + posts.getPostField(replyPids[0], 'timestamp', function (err, timestamp) { + next(err, utils.toISOString(timestamp)); + }); + }, + }, function (err, replies) { + if (replies.users.length > 5) { + replies.users.shift(); + replies.hasMore = true; + } + + replies.count = replyPids.length; + next(err, replies); + }); + }); + }); + }, callback); + } };