From 8da7a6f2f385df7d357c11f013678b41a24c3019 Mon Sep 17 00:00:00 2001 From: Baris Soner Usakli Date: Wed, 26 Feb 2014 15:32:32 -0500 Subject: [PATCH] cleanup --- src/categories.js | 2 +- src/favourites.js | 49 +++++------------------- src/socket.io/posts.js | 46 +++++++++++++---------- src/topics.js | 84 ++++++++++++------------------------------ 4 files changed, 62 insertions(+), 119 deletions(-) diff --git a/src/categories.js b/src/categories.js index da9f84a8af..5eafaff6cb 100644 --- a/src/categories.js +++ b/src/categories.js @@ -424,6 +424,6 @@ var db = require('./database'), Categories.addActiveUser(cid, uid, timestamp); }); - } + }; }(exports)); \ No newline at end of file diff --git a/src/favourites.js b/src/favourites.js index 7792cee454..8084647bdd 100644 --- a/src/favourites.js +++ b/src/favourites.js @@ -153,24 +153,13 @@ var async = require('async'), downvoted: function(next) { db.isSetMember('pid:' + pid + ':downvote', uid, next); } - }, function(err, results) { - callback(err, results) - }); + }, callback); }; Favourites.getVoteStatusByPostIDs = function(pids, uid, callback) { - var data = {}; - - function iterator(pid, next) { - Favourites.hasVoted(pid, uid, function(err, voteStatus) { - data[pid] = voteStatus; - next() - }); - } - - async.each(pids, iterator, function(err) { - callback(data); - }); + async.map(pids, function(pid, next) { + Favourites.hasVoted(pid, uid, next); + }, callback); }; Favourites.favourite = function (pid, room_id, uid, socket) { @@ -248,33 +237,15 @@ var async = require('async'), }; Favourites.getFavouritesByPostIDs = function(pids, uid, callback) { - var data = {}; - - function iterator(pid, next) { - Favourites.hasFavourited(pid, uid, function(err, hasFavourited) { - data[pid] = hasFavourited; - next() - }); - } - - async.each(pids, iterator, function(err) { - callback(data); - }); + async.map(pids, function(pid, next) { + Favourites.hasFavourited(pid, uid, next); + }, callback); }; Favourites.getFavouritedUidsByPids = function(pids, callback) { - var data = {}; - - function getUids(pid, next) { - db.getSetMembers('pid:' + pid + ':users_favourited', function(err, uids) { - data[pid] = uids; - next(); - }); - } - - async.each(pids, getUids, function(err) { - callback(data); - }); + async.map(pids, function(pid, next) { + db.getSetMembers('pid:' + pid + ':users_favourited', next); + }, callback) }; }(exports)); \ No newline at end of file diff --git a/src/socket.io/posts.js b/src/socket.io/posts.js index 7a0475354e..049735b71c 100644 --- a/src/socket.io/posts.js +++ b/src/socket.io/posts.js @@ -207,30 +207,38 @@ SocketPosts.getPrivileges = function(socket, pid, callback) { SocketPosts.getFavouritedUsers = function(socket, pid, callback) { - favourites.getFavouritedUidsByPids([pid], function(data) { + favourites.getFavouritedUidsByPids([pid], function(err, data) { + + if(err) { + return callback(err); + } + + if(!Array.isArray(data) || !data.length) { + callback(null, ""); + } + + console.log(data); var max = 5; //hardcoded var usernames = ""; - var pid_uids = data[pid]; + var pid_uids = data[0]; var rest_amount = 0; - if (data.hasOwnProperty(pid) && pid_uids.length > 0) { - if (pid_uids.length > max) { - rest_amount = pid_uids.length - max; - pid_uids = pid_uids.slice(0, max); - } - user.getUsernamesByUids(pid_uids, function(err, result) { - if(err) { - return callback(err); - } - - usernames = result.join(', ') + (rest_amount > 0 - ? " and " + rest_amount + (rest_amount > 1 ? " others" : " other") - : ""); - callback(null, usernames); - }); - } else { - callback(null, ""); + + if (pid_uids.length > max) { + rest_amount = pid_uids.length - max; + pid_uids = pid_uids.slice(0, max); } + + user.getUsernamesByUids(pid_uids, function(err, result) { + if(err) { + return callback(err); + } + + usernames = result.join(', ') + (rest_amount > 0 + ? " and " + rest_amount + (rest_amount > 1 ? " others" : " other") + : ""); + callback(null, usernames); + }); }); }; diff --git a/src/topics.js b/src/topics.js index 05d12787b8..fe062b8b52 100644 --- a/src/topics.js +++ b/src/topics.js @@ -350,12 +350,7 @@ var async = require('async'), }); }; - Topics.getTopicPosts = function(tid, start, end, current_user, reverse, callback) { - if (typeof reverse === 'function') { - callback = reverse; - reverse = false; - } - + Topics.getTopicPosts = function(tid, start, end, uid, reverse, callback) { posts.getPostsByTid(tid, start, end, reverse, function(err, postData) { if(err) { return callback(err); @@ -373,65 +368,34 @@ var async = require('async'), return post.pid; }); - function getFavouritesData(next) { - favourites.getFavouritesByPostIDs(pids, current_user, function(fav_data) { - next(null, fav_data); - }); - } - - function getVoteStatusData(next) { - favourites.getVoteStatusByPostIDs(pids, current_user, function(vote_data) { - next(null, vote_data); - }) - } - - function addUserInfoToPosts(next) { - function iterator(post, callback) { - posts.addUserInfoToPost(post, function() { - callback(null); - }); - } - - async.each(postData, iterator, function(err) { - next(err, null); - }); - } - - function getPrivileges(next) { - var privs = {}; - async.each(pids, getPostPrivileges, function(err) { - next(err, privs); - }); - - function getPostPrivileges(pid, next) { - postTools.privileges(pid, current_user, function(err, postPrivileges) { - if(err) { - return next(err); - } - privs[pid] = postPrivileges; - next(); - }); + async.parallel({ + favourites : function(next) { + favourites.getFavouritesByPostIDs(pids, uid, next); + }, + voteData : function(next) { + favourites.getVoteStatusByPostIDs(pids, uid, next); + }, + userData : function(next) { + async.each(postData, posts.addUserInfoToPost, next); + }, + privileges : function(next) { + async.map(pids, function (pid, next) { + postTools.privileges(pid, uid, next); + }, next); } - } - - async.parallel([getFavouritesData, addUserInfoToPosts, getPrivileges, getVoteStatusData], function(err, results) { + }, function(err, results) { if(err) { return callback(err); } - var fav_data = results[0], - privileges = results[2], - voteStatus = results[3]; - for (var i = 0; i < postData.length; ++i) { - var pid = postData[i].pid; - postData[i].favourited = fav_data[pid]; - postData[i].upvoted = voteStatus[pid].upvoted; - postData[i].downvoted = voteStatus[pid].downvoted; + postData[i].favourited = results.favourites[i]; + postData[i].upvoted = results.voteData[i].upvoted; + postData[i].downvoted = results.voteData[i].downvoted; postData[i].votes = postData[i].votes || 0; - postData[i].display_moderator_tools = (current_user != 0) && privileges[pid].editable; - postData[i].display_move_tools = privileges[pid].move; - if(parseInt(postData[i].deleted, 10) === 1 && !privileges[pid].view_deleted) { + postData[i].display_moderator_tools = (uid != 0) && results.privileges[i].editable; + postData[i].display_move_tools = results.privileges[i].move; + if(parseInt(postData[i].deleted, 10) === 1 && !results.privileges[i].view_deleted) { postData[i].content = 'This post is deleted!'; } } @@ -696,7 +660,7 @@ var async = require('async'), function isTopicVisible(topicData, topicInfo) { var deleted = parseInt(topicData.deleted, 10) !== 0; - return !deleted || (deleted && topicInfo.privileges.view_deleted) || topicData.uid === current_user; + return !deleted || (deleted && topicInfo.privileges.view_deleted) || parseInt(topicData.uid, 10) === parseInt(current_user, 10); } function loadTopic(tid, next) { @@ -765,7 +729,7 @@ var async = require('async'), } function getTopicPosts(next) { - Topics.getTopicPosts(tid, start, end, current_user, next); + Topics.getTopicPosts(tid, start, end, current_user, false, next); } function getPrivileges(next) {