From 4f02775bdfb7a356a594d9733c4344304c475659 Mon Sep 17 00:00:00 2001 From: barisusakli Date: Thu, 9 Feb 2017 14:30:02 +0300 Subject: [PATCH] replies change --- src/topics/posts.js | 76 +++++++++++++++++++++++++++------------------ 1 file changed, 45 insertions(+), 31 deletions(-) diff --git a/src/topics/posts.js b/src/topics/posts.js index 96b1992fc9..9d7ea65828 100644 --- a/src/topics/posts.js +++ b/src/topics/posts.js @@ -389,43 +389,57 @@ module.exports = function (Topics) { }; 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) { + var uids = []; + var count = 0; + var replyPids; + async.map(pids, function (pid, _next) { + async.waterfall([ + function (next) { + db.getSortedSetRange('pid:' + pid + ':replies', 0, -1, next); + }, + function (_replyPids, next) { + replyPids = _replyPids; + if (!replyPids.length) { + return _next(null, {count: 0}); + } + async.until(function () { + return count === replyPids.length || uids.length === 6; + }, function (next) { + posts.getPostField(replyPids[count], 'uid', function (err, uid) { + if (err) { + return next(err); + } + uid = parseInt(uid, 10); + if (uids.indexOf(uid) === -1) { + uids.push(uid); + } + count++; + next(); + }); + }, next); + }, + function (next) { async.parallel({ - "users": function (next) { + 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; + timestampISO: function (next) { + posts.getPostField(replyPids[replyPids.length - 1], 'timestamp', next); } + }, next); + }, + function (replies, next) { + if (replies.users.length > 5) { + replies.users.shift(); + replies.hasMore = true; + } - replies.count = replyPids.length; + replies.count = replyPids.length; + replies.timestampISO = utils.toISOString(replies.timestampISO); - next(err, replies); - }); - }); - }); + next(null, replies); + } + ], next); }, callback); } };