mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-01 03:26:04 +01:00
*shenans* refactored Posts.get to use async module instead of callback
pyramid. Call time net decrease = 0, rofl.
This commit is contained in:
192
src/posts.js
192
src/posts.js
@@ -19,66 +19,31 @@ var RDB = require('./redis.js'),
|
|||||||
if (start == null) start = 0;
|
if (start == null) start = 0;
|
||||||
if (end == null) end = start + 10;
|
if (end == null) end = start + 10;
|
||||||
|
|
||||||
// async.parallel({
|
async.parallel({
|
||||||
// details: function(callback) {
|
details: function(callback) {
|
||||||
// RDB.get('tid:' + tid + ':title', function(topic_name) {
|
RDB.get('tid:' + tid + ':title', function(topic_name) {
|
||||||
// callback(null, {
|
callback(null, {
|
||||||
// 'topic_name': topic_name
|
'topic_name': topic_name
|
||||||
// });
|
});
|
||||||
// });
|
});
|
||||||
// },
|
},
|
||||||
// posts: function(callback) {
|
posts: function(callback) {
|
||||||
// var participant_uids = [],
|
var participant_uids = [],
|
||||||
// post_calls = [];
|
post_calls = [];
|
||||||
// RDB.lrange('tid:' + tid + ':posts', start, end, function(pids) {
|
|
||||||
// var content = [],
|
|
||||||
// uid = [],
|
|
||||||
// timestamp = [],
|
|
||||||
// pid = [],
|
|
||||||
// post_rep = [];
|
|
||||||
|
|
||||||
// for (var i=0, ii=pids.length; i<ii; i++) {
|
async.waterfall([
|
||||||
// content.push('pid:' + pids[i] + ':content');
|
function(next) {
|
||||||
// uid.push('pid:' + pids[i] + ':uid');
|
|
||||||
// timestamp.push('pid:' + pids[i] + ':timestamp');
|
|
||||||
// post_rep.push('pid:' + pids[i] + ':rep');
|
|
||||||
// pid.push(pids[i]);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// if (pids.length > 0) {
|
|
||||||
// RDB.multi()
|
|
||||||
// .mget(content)
|
|
||||||
// .mget(uid)
|
|
||||||
// .mget(timestamp)
|
|
||||||
// .mget(post_rep)
|
|
||||||
// .exec(function(err, replies) {
|
|
||||||
// content = replies[0];
|
|
||||||
// uid = replies[1];
|
|
||||||
// timestamp = replies[2];
|
|
||||||
// post_rep = replies[3];
|
|
||||||
|
|
||||||
// callback(null, replies);
|
|
||||||
// }
|
|
||||||
// );
|
|
||||||
// }
|
|
||||||
// });
|
|
||||||
// }
|
|
||||||
// }, function(err, results) {
|
|
||||||
// callback(results);
|
|
||||||
// });
|
|
||||||
// return;
|
|
||||||
|
|
||||||
RDB.get('tid:' + tid + ':title', function(topic_name) { //do these asynch later
|
|
||||||
RDB.lrange('tid:' + tid + ':posts', start, end, function(pids) {
|
RDB.lrange('tid:' + tid + ':posts', start, end, function(pids) {
|
||||||
var content = [],
|
var content = [],
|
||||||
uid = [],
|
uids = [],
|
||||||
|
participants = [],
|
||||||
timestamp = [],
|
timestamp = [],
|
||||||
pid = [],
|
pid = [],
|
||||||
post_rep = [];
|
post_rep = [];
|
||||||
|
|
||||||
for (var i=0, ii=pids.length; i<ii; i++) {
|
for (var i=0, ii=pids.length; i<ii; i++) {
|
||||||
content.push('pid:' + pids[i] + ':content');
|
content.push('pid:' + pids[i] + ':content');
|
||||||
uid.push('pid:' + pids[i] + ':uid');
|
uids.push('pid:' + pids[i] + ':uid');
|
||||||
timestamp.push('pid:' + pids[i] + ':timestamp');
|
timestamp.push('pid:' + pids[i] + ':timestamp');
|
||||||
post_rep.push('pid:' + pids[i] + ':rep');
|
post_rep.push('pid:' + pids[i] + ':rep');
|
||||||
pid.push(pids[i]);
|
pid.push(pids[i]);
|
||||||
@@ -87,54 +52,101 @@ var RDB = require('./redis.js'),
|
|||||||
if (pids.length > 0) {
|
if (pids.length > 0) {
|
||||||
RDB.multi()
|
RDB.multi()
|
||||||
.mget(content)
|
.mget(content)
|
||||||
.mget(uid)
|
.mget(uids)
|
||||||
.mget(timestamp)
|
.mget(timestamp)
|
||||||
.mget(post_rep)
|
.mget(post_rep)
|
||||||
.exec(function(err, replies) {
|
.exec(function(err, replies) {
|
||||||
content = replies[0];
|
// Populate uids array
|
||||||
uid = replies[1];
|
for(var x=0,numReplies=replies[1].length;x<numReplies;x++) {
|
||||||
timestamp = replies[2];
|
var uid = parseInt(replies[1][x]);
|
||||||
post_rep = replies[3];
|
if (participants.indexOf(uid) === -1) participants.push(uid);
|
||||||
|
}
|
||||||
|
|
||||||
user.get_user_postdetails(uid, function(user_details) {
|
// Construct return object
|
||||||
user.get_gravatars_by_uids(uid, '', function(gravatars) {
|
next(null, {
|
||||||
var posts = [];
|
replies: replies,
|
||||||
var callbacks = content.length;
|
pids: pids,
|
||||||
|
participants: participants
|
||||||
|
});
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}, function(returnObj, next) {
|
||||||
|
// Get user details
|
||||||
|
var details = {},
|
||||||
|
calls = [];
|
||||||
|
|
||||||
for (var i=0, ii=content.length; i<ii; i++) {
|
for(var x=0,numParticipants=returnObj.participants.length;x<numParticipants;x++) {
|
||||||
(function(i) {
|
(function(uid) {
|
||||||
Posts.hasFavourited(pid[i], current_user, function(hasFavourited) {
|
calls.push(function(next) {
|
||||||
|
// Get individual participant's details
|
||||||
|
user.getUserData(uid, function(userData) {
|
||||||
|
next(null, userData);
|
||||||
|
})
|
||||||
|
});
|
||||||
|
})(returnObj.participants[x]);
|
||||||
|
}
|
||||||
|
async.parallel(calls, function(err, results) {
|
||||||
|
for(var x=0,numResults=results.length;x<numResults;x++) {
|
||||||
|
details[returnObj.participants[x]] = results[x];
|
||||||
|
}
|
||||||
|
returnObj.participants = details;
|
||||||
|
next(null, returnObj);
|
||||||
|
});
|
||||||
|
}, function(returnObj, next) {
|
||||||
|
// Favourited?
|
||||||
|
var calls = [],
|
||||||
|
numPosts = returnObj.pids.length;
|
||||||
|
|
||||||
|
for(var x=0;x<numPosts;x++) {
|
||||||
|
(function(pid) {
|
||||||
|
calls.push(function(callback) {
|
||||||
|
Posts.hasFavourited(pid, current_user, function(hasFavourited) {
|
||||||
|
callback(null, hasFavourited);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
})(returnObj.pids[x]);
|
||||||
|
}
|
||||||
|
async.parallel(calls, function(err, results) {
|
||||||
|
returnObj.favourites = results;
|
||||||
|
next(null, returnObj);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
], function(err, returnObj) {
|
||||||
|
callback(null, returnObj);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}, function(err, results) {
|
||||||
|
// Construct posts array
|
||||||
|
var posts = [],
|
||||||
|
participant_details = results.posts.participants;
|
||||||
|
for(var x=0,numPosts=results.posts.pids.length;x<numPosts;x++) {
|
||||||
|
var uid = results.posts.replies[1][x],
|
||||||
|
participant = participant_details[uid];
|
||||||
posts.push({
|
posts.push({
|
||||||
'pid' : pid[i],
|
pid: results.posts.pids[x],
|
||||||
'content' : marked(content[i] || ''),
|
content: marked(results.posts.replies[0][x] || ''),
|
||||||
'uid' : uid[i],
|
uid: uid,
|
||||||
'username' : user_details.username[i] || 'anonymous',
|
timestamp: results.posts.replies[2][x],
|
||||||
'user_rep' : user_details.rep[i] || 0,
|
relativeTime: utils.relativeTime(results.posts.replies[2][x]),
|
||||||
'post_rep' : post_rep[i] || 0,
|
post_rep: results.posts.replies[3][x],
|
||||||
'gravatar' : gravatars[i],
|
display_moderator_tools: results.posts.replies[1][x] === current_user ? 'show' : 'hide',
|
||||||
'timestamp' : timestamp[i],
|
username: participant.username,
|
||||||
'relativeTime': utils.relativeTime(timestamp[i]),
|
gravatar: participant.picture,
|
||||||
'fav_star_class' : hasFavourited ? 'icon-star' : 'icon-star-empty',
|
user_rep: participant.reputation,
|
||||||
'display_moderator_tools' : uid[i] === current_user ? 'show' : 'hide'
|
fav_star_class: results.posts.favourites[x] ? 'icon-star' : 'icon-star-empty',
|
||||||
});
|
});
|
||||||
|
|
||||||
callbacks--;
|
|
||||||
if (callbacks == 0) {
|
|
||||||
callback({'topic_name':topic_name, 'topic_id': tid, 'posts': posts});
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
}(i));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
callback({});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
|
// Construct return object
|
||||||
|
callback({
|
||||||
|
'topic_name': results.details.topic_name,
|
||||||
|
'topic_id': tid,
|
||||||
|
posts: posts,
|
||||||
|
uids: results.posts.participants
|
||||||
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -59,7 +59,6 @@ var config = require('../config.js'),
|
|||||||
for(var i=0, ii=uids.length; i<ii; ++i) {
|
for(var i=0, ii=uids.length; i<ii; ++i) {
|
||||||
|
|
||||||
User.getUserField(uids[i], 'picture', function(picture) {
|
User.getUserField(uids[i], 'picture', function(picture) {
|
||||||
console.log(picture);
|
|
||||||
gravatars.push(picture);
|
gravatars.push(picture);
|
||||||
if(gravatars.length >= uids.length)
|
if(gravatars.length >= uids.length)
|
||||||
callback(gravatars);
|
callback(gravatars);
|
||||||
@@ -239,7 +238,7 @@ var config = require('../config.js'),
|
|||||||
User.createGravatarURLFromEmail = function(email) {
|
User.createGravatarURLFromEmail = function(email) {
|
||||||
if(email) {
|
if(email) {
|
||||||
var md5sum = crypto.createHash('md5');
|
var md5sum = crypto.createHash('md5');
|
||||||
md5sum.update(email.toLowerCase());
|
md5sum.update((email || '').toLowerCase());
|
||||||
var gravatarURL = 'http://www.gravatar.com/avatar/' + md5sum.digest('hex');
|
var gravatarURL = 'http://www.gravatar.com/avatar/' + md5sum.digest('hex');
|
||||||
return gravatarURL;
|
return gravatarURL;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user