improved threaded view design... again

v1.18.x
psychobunny 8 years ago
parent 69dd1e3aca
commit f7d1e58d65

@ -10,6 +10,7 @@ var user = require('../user');
var posts = require('../posts'); var posts = require('../posts');
var meta = require('../meta'); var meta = require('../meta');
var plugins = require('../plugins'); var plugins = require('../plugins');
var utils = require('../../public/src/utils');
module.exports = function (Topics) { module.exports = function (Topics) {
Topics.onNewPostMade = function (postData, callback) { Topics.onNewPostMade = function (postData, callback) {
@ -103,6 +104,9 @@ module.exports = function (Topics) {
parents: function (next) { parents: function (next) {
Topics.addParentPosts(postData, next); Topics.addParentPosts(postData, next);
}, },
replies: function (next) {
+getPostReplies(pids, uid, next);
},
}, next); }, next);
}, },
function (results, next) { function (results, next) {
@ -115,7 +119,7 @@ module.exports = function (Topics) {
postObj.upvoted = results.voteData.upvotes[i]; postObj.upvoted = results.voteData.upvotes[i];
postObj.downvoted = results.voteData.downvotes[i]; postObj.downvoted = results.voteData.downvotes[i];
postObj.votes = postObj.votes || 0; 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); postObj.selfPost = !!parseInt(uid, 10) && parseInt(uid, 10) === parseInt(postObj.uid, 10);
// Username override for guests, if enabled // Username override for guests, if enabled
@ -385,4 +389,45 @@ module.exports = function (Topics) {
Topics.getPostCount = function (tid, callback) { Topics.getPostCount = function (tid, callback) {
db.getObjectField('topic:' + tid, 'postcount', 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);
}
}; };

Loading…
Cancel
Save