From 4a1dc28a26781e7d3dcfacf4307b7a0e855c51a4 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Mon, 6 May 2013 12:04:36 -0400 Subject: [PATCH] *shenans* refactored Posts.get to use async module instead of callback pyramid. Call time net decrease = 0, rofl. --- src/posts.js | 230 +++++++++++++++++++++++++++------------------------ src/user.js | 3 +- 2 files changed, 122 insertions(+), 111 deletions(-) diff --git a/src/posts.js b/src/posts.js index d4f6ccf64f..a57ff2644f 100644 --- a/src/posts.js +++ b/src/posts.js @@ -19,122 +19,134 @@ var RDB = require('./redis.js'), if (start == null) start = 0; if (end == null) end = start + 10; - // async.parallel({ - // details: function(callback) { - // RDB.get('tid:' + tid + ':title', function(topic_name) { - // callback(null, { - // 'topic_name': topic_name - // }); - // }); - // }, - // posts: function(callback) { - // var participant_uids = [], - // post_calls = []; - // RDB.lrange('tid:' + tid + ':posts', start, end, function(pids) { - // var content = [], - // uid = [], - // timestamp = [], - // pid = [], - // post_rep = []; + async.parallel({ + details: function(callback) { + RDB.get('tid:' + tid + ':title', function(topic_name) { + callback(null, { + 'topic_name': topic_name + }); + }); + }, + posts: function(callback) { + var participant_uids = [], + post_calls = []; + + async.waterfall([ + function(next) { + RDB.lrange('tid:' + tid + ':posts', start, end, function(pids) { + var content = [], + uids = [], + participants = [], + timestamp = [], + pid = [], + post_rep = []; - // for (var i=0, ii=pids.length; i 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]; + if (pids.length > 0) { + RDB.multi() + .mget(content) + .mget(uids) + .mget(timestamp) + .mget(post_rep) + .exec(function(err, replies) { + // Populate uids array + for(var x=0,numReplies=replies[1].length;x 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]; - - user.get_user_postdetails(uid, function(user_details) { - user.get_gravatars_by_uids(uid, '', function(gravatars) { - var posts = []; - var callbacks = content.length; - - for (var i=0, ii=content.length; i= uids.length) callback(gravatars); @@ -239,7 +238,7 @@ var config = require('../config.js'), User.createGravatarURLFromEmail = function(email) { if(email) { var md5sum = crypto.createHash('md5'); - md5sum.update(email.toLowerCase()); + md5sum.update((email || '').toLowerCase()); var gravatarURL = 'http://www.gravatar.com/avatar/' + md5sum.digest('hex'); return gravatarURL; }