faster getReplies

v1.18.x
Barış Soner Uşaklı 8 years ago
parent feecf0b61c
commit 6485108dfd

@ -169,11 +169,9 @@ module.exports = function (Topics) {
async.apply(posts.getPostsFields, parentPids, ['uid']), async.apply(posts.getPostsFields, parentPids, ['uid']),
function (_parentPosts, next) { function (_parentPosts, next) {
parentPosts = _parentPosts; parentPosts = _parentPosts;
var parentUids = parentPosts.map(function (postObj) { var parentUids = _.uniq(parentPosts.map(function (postObj) {
return parseInt(postObj.uid, 10); return postObj && parseInt(postObj.uid, 10);
}).filter(function (uid, idx, users) { }));
return users.indexOf(uid) === idx;
});
user.getUsersFields(parentUids, ['username'], next); user.getUsersFields(parentUids, ['username'], next);
}, },
@ -391,59 +389,69 @@ module.exports = function (Topics) {
}; };
function getPostReplies(pids, callerUid, callback) { function getPostReplies(pids, callerUid, callback) {
async.map(pids, function (pid, next) { var arrayOfReplyPids;
var replyPids; var replyData;
var uids = []; var uniqueUids;
async.waterfall([ var uniquePids;
function (next) { async.waterfall([
db.getSortedSetRange('pid:' + pid + ':replies', 0, -1, next); function (next) {
}, var keys = pids.map(function (pid) {
function (_replyPids, next) { return 'pid:' + pid + ':replies';
replyPids = _replyPids; });
db.getSortedSetsMembers(keys, next);
var count = 0; },
function (arrayOfPids, next) {
async.until(function () { arrayOfReplyPids = arrayOfPids;
return count === replyPids.length || uids.length === 6;
}, function (next) {
async.waterfall([
function (next) {
posts.getPostField(replyPids[count], 'uid', next);
},
function (uid, next) {
uid = parseInt(uid, 10);
if (uids.indexOf(uid) === -1) {
uids.push(uid);
}
count += 1;
next();
},
], next);
}, next);
},
function (next) {
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));
});
},
}, next);
},
function (replies, next) {
if (replies.users.length > 5) {
replies.users.shift();
replies.hasMore = true;
}
replies.count = replyPids.length; uniquePids = _.uniq(_.flatten(arrayOfPids));
replies.text = replies.count > 1 ? '[[topic:replies_to_this_post, ' + replies.count + ']]' : '[[topic:one_reply_to_this_post]]';
next(null, replies); posts.getPostsFields(uniquePids, ['pid', 'uid', 'timestamp'], next);
}, },
], next); function (_replyData, next) {
}, callback); replyData = _replyData;
var uids = replyData.map(function (replyData) {
return replyData && replyData.uid;
});
uniqueUids = _.uniq(uids);
user.getUsersWithFields(uniqueUids, ['uid', 'username', 'userslug', 'picture'], callerUid, next);
},
function (userData, next) {
var uidMap = _.zipObject(uniqueUids, userData);
var pidMap = _.zipObject(uniquePids, replyData);
var returnData = arrayOfReplyPids.map(function (replyPids) {
var uidsUsed = {};
var currentData = {
hasMore: false,
users: [],
text: replyPids.length > 1 ? '[[topic:replies_to_this_post, ' + replyPids.length + ']]' : '[[topic:one_reply_to_this_post]]',
count: replyPids.length,
timestampISO: replyPids.length ? utils.toISOString(pidMap[replyPids[0]].timestamp) : undefined,
};
replyPids.sort(function(a, b) {
return parseInt(a, 10) - parseInt(b, 10);
});
replyPids.forEach(function (replyPid) {
var replyData = pidMap[replyPid];
if (!uidsUsed[replyData.uid] && currentData.users.length < 6) {
currentData.users.push(uidMap[replyData.uid]);
uidsUsed[replyData.uid] = true;
}
});
if (currentData.users.length > 5) {
currentData.users.pop();
currentData.hasMore = true;
}
return currentData;
});
next(null, returnData);
},
], callback);
} }
}; };

@ -78,12 +78,24 @@ User.getUsersWithFields = function (uids, fields, uid, callback) {
function (results, next) { function (results, next) {
results.userData.forEach(function (user, index) { results.userData.forEach(function (user, index) {
if (user) { if (user) {
user.status = User.getStatus(user);
user.administrator = results.isAdmin[index]; user.administrator = results.isAdmin[index];
user.banned = parseInt(user.banned, 10) === 1;
user.banned_until = parseInt(user['banned:expire'], 10) || 0; if (user.hasOwnProperty('status')) {
user.banned_until_readable = user.banned_until ? new Date(user.banned_until).toString() : 'Not Banned'; user.status = User.getStatus(user);
user['email:confirmed'] = parseInt(user['email:confirmed'], 10) === 1; }
if (user.hasOwnProperty('banned')) {
user.banned = parseInt(user.banned, 10) === 1;
}
if (user.hasOwnProperty('banned:expire')) {
user.banned_until = parseInt(user['banned:expire'], 10) || 0;
user.banned_until_readable = user.banned_until ? new Date(user.banned_until).toString() : 'Not Banned';
}
if (user.hasOwnProperty(['email:confirmed'])) {
user['email:confirmed'] = parseInt(user['email:confirmed'], 10) === 1;
}
} }
}); });
plugins.fireHook('filter:userlist.get', { users: results.userData, uid: uid }, next); plugins.fireHook('filter:userlist.get', { users: results.userData, uid: uid }, next);

Loading…
Cancel
Save