From 8e114c2093a62878995814b62cab950518549689 Mon Sep 17 00:00:00 2001
From: barisusakli <barisusakli@gmail.com>
Date: Sun, 16 Nov 2014 16:15:49 -0500
Subject: [PATCH] moved isAdminOrMod check out of getTopicsByTids

---
 src/categories/topics.js     | 71 +++++++++++++++++++----------------
 src/controllers/tags.js      |  6 +--
 src/privileges/categories.js | 22 -----------
 src/topics.js                | 72 +++++++++++++-----------------------
 src/topics/recent.js         | 23 +++++-------
 src/topics/teaser.js         | 10 ++---
 6 files changed, 82 insertions(+), 122 deletions(-)

diff --git a/src/categories/topics.js b/src/categories/topics.js
index f248ee6188..12b59263cf 100644
--- a/src/categories/topics.js
+++ b/src/categories/topics.js
@@ -2,48 +2,57 @@
 
 var async = require('async'),
 	db = require('../database'),
+	user = require('../user'),
 	topics = require('../topics'),
 	plugins = require('../plugins');
 
 module.exports = function(Categories) {
 
 	Categories.getCategoryTopics = function(data, callback) {
-		var tids;
-		async.waterfall([
-			async.apply(plugins.fireHook, 'filter:category.topics.prepare', data),
-			function(data, next) {
-				Categories.getTopicIds(data.targetUid ? 'cid:' + data.cid + ':uid:' + data.targetUid + ':tids' : 'cid:' + data.cid + ':tids', data.start, data.stop, next);
+		async.parallel({
+			isAdmin: function(next) {
+				user.isAdministrator(data.uid, next);
 			},
-			function(topicIds, next) {
-				tids = topicIds;
-				topics.getTopicsByTids(tids, data.uid, next);
+			isModerator: function(next) {
+				user.isModerator(data.uid, data.cid, next);
 			},
-			function(topics, next) {
-				if (!Array.isArray(topics) || !topics.length) {
-					return next(null, {
-						topics: [],
-						nextStart: 1
-					});
-				}
+			topics: function(next) {
+				async.waterfall([
+					function(next) {
+						plugins.fireHook('filter:category.topics.prepare', data, next);
+					},
+					function(data, next) {
+						Categories.getTopicIds(data.targetUid ? 'cid:' + data.cid + ':uid:' + data.targetUid + ':tids' : 'cid:' + data.cid + ':tids', data.start, data.stop, next);
+					},
+					function(tids, next) {
+						topics.getTopicsByTids(tids, data.uid, next);
+					},
+					function(topics, next) {
+						if (!Array.isArray(topics) || !topics.length) {
+							return next(null, []);
+						}
 
-				var indices = {},
-					i = 0;
-				for(i=0; i<tids.length; ++i) {
-					indices[tids[i]] = data.start + i;
-				}
+						for (var i=0; i<topics.length; ++i) {
+							topics[i].index = data.start + i;
+						}
 
-				for(i=0; i<topics.length; ++i) {
-					topics[i].index = indices[topics[i].tid];
-				}
-
-				plugins.fireHook('filter:category.topics.get', {topics: topics, uid: data.uid}, function(err, params) {
-					next(null, {
-						topics: params.topics,
-						nextStart: data.stop + 1
-					});
-				});
+						plugins.fireHook('filter:category.topics.get', {topics: topics, uid: data.uid}, function(err, params) {
+							next(null, params.topics);
+						});
+					}
+				], next);
 			}
-		], callback);
+		}, function(err, results) {
+			if (err) {
+				return callback(err);
+			}
+			var isAdminOrMod = results.isAdmin || results.isModerator;
+			results.topics = results.topics.filter(function(topic) {
+				return (!topic.deleted || isAdminOrMod || topic.isOwner);
+			});
+
+			callback(null, {topics: results.topics, nextStart: data.stop + 1});
+		});
 	};
 
 	Categories.getTopicIds = function(set, start, stop, callback) {
diff --git a/src/controllers/tags.js b/src/controllers/tags.js
index fa73eaf19e..269e5a3599 100644
--- a/src/controllers/tags.js
+++ b/src/controllers/tags.js
@@ -22,7 +22,7 @@ tagsController.getTag = function(req, res, next) {
 			return res.render('tag', {topics: [], tag: tag});
 		}
 
-		topics.getTopics(tids, uid, function(err, data) {
+		topics.getTopics(tids, uid, function(err, topics) {
 			if (err) {
 				return next(err);
 			}
@@ -42,9 +42,7 @@ tagsController.getTag = function(req, res, next) {
 				}
 			];
 
-			data.tag = tag;
-			data.nextStart = end + 1;
-			res.render('tag', data);
+			res.render('tag', {topics: topics, tag: tag, nextStart: end + 1});
 		});
 	});
 };
diff --git a/src/privileges/categories.js b/src/privileges/categories.js
index 449ec40e6f..7662ef5913 100644
--- a/src/privileges/categories.js
+++ b/src/privileges/categories.js
@@ -143,28 +143,6 @@ module.exports = function(privileges) {
 		}, callback);
 	};
 
-	privileges.categories.isAdminOrMod = function(cids, uid, callback) {
-		async.parallel({
-			isModerators: function(next) {
-				user.isModerator(uid, cids, next);
-			},
-			isAdmin: function(next) {
-				user.isAdministrator(uid, next);
-			}
-		}, function(err, results) {
-			if (err) {
-				return callback(err);
-			}
-
-			var returnData = new Array(cids.length);
-			for (var i=0; i<cids.length; ++i) {
-				returnData[i] = results.isAdmin || results.isModerators[i];
-			}
-
-			callback(null, returnData);
-		});
-	};
-
 	privileges.categories.canMoveAllTopics = function(currentCid, targetCid, uid, callback) {
 		async.parallel({
 			isAdministrator: function(next) {
diff --git a/src/topics.js b/src/topics.js
index bcdd87354e..59a843e610 100644
--- a/src/topics.js
+++ b/src/topics.js
@@ -99,7 +99,7 @@ var async = require('async'),
 
 	Topics.getCategoryData = function(tid, callback) {
 		Topics.getTopicField(tid, 'cid', function(err, cid) {
-			if(err) {
+			if (err) {
 				callback(err);
 			}
 
@@ -107,43 +107,29 @@ var async = require('async'),
 		});
 	};
 
-	Topics.getTopics = function(tids, uid, callback) {
-		var returnTopics = {
-			topics: [],
-			nextStart: 0
-		};
-
-		privileges.topics.filter('read', tids, uid, function(err, tids) {
-			if (err) {
-				return callback(err);
+	Topics.getTopicsFromSet = function(set, uid, start, end, callback) {
+		async.waterfall([
+			function(next) {
+				db.getSortedSetRevRange(set, start, end, next);
+			},
+			function(tids, next) {
+				Topics.getTopics(tids, uid, next);
+			},
+			function(topics, next) {
+				next(null, {topics: topics, nextStart: end + 1});
 			}
-
-			Topics.getTopicsByTids(tids, uid, function(err, topicData) {
-				if (err) {
-					return callback(err);
-				}
-
-				returnTopics.topics = topicData;
-				callback(null, returnTopics);
-			});
-		});
+		], callback);
 	};
 
-	Topics.getTopicsFromSet = function(set, uid, start, end, callback) {
-		db.getSortedSetRevRange(set, start, end, function(err, tids) {
-			if (err) {
-				return callback(err);
+	Topics.getTopics = function(tids, uid, callback) {
+		async.waterfall([
+			function(next) {
+				privileges.topics.filter('read', tids, uid, next);
+			},
+			function(tids, next) {
+				Topics.getTopicsByTids(tids, uid, next);
 			}
-
-			Topics.getTopics(tids, uid, function(err, data) {
-				if (err) {
-					return callback(err);
-				}
-
-				data.nextStart = end + 1;
-				callback(null, data);
-			});
-		});
+		], callback);
 	};
 
 	Topics.getTopicsByTids = function(tids, uid, callback) {
@@ -169,7 +155,7 @@ var async = require('async'),
 
 			async.parallel({
 				teasers: function(next) {
-					Topics.getTeasers(tids, uid, next);
+					Topics.getTeasers(tids, next);
 				},
 				users: function(next) {
 					user.getMultipleUserFields(uids, ['uid', 'username', 'userslug', 'picture'], next);
@@ -180,9 +166,6 @@ var async = require('async'),
 				hasRead: function(next) {
 					Topics.hasReadTopics(tids, uid, next);
 				},
-				isAdminOrMod: function(next) {
-					privileges.categories.isAdminOrMod(cids, uid, next);
-				},
 				tags: function(next) {
 					Topics.getTopicsTagsObjects(tids, next);
 				}
@@ -193,30 +176,25 @@ var async = require('async'),
 
 				var users = _.object(uids, results.users);
 				var categories = _.object(cids, results.categories);
-				var isAdminOrMod = {};
-				cids.forEach(function(cid, index) {
-					isAdminOrMod[cid] = results.isAdminOrMod[index];
-				});
 
 				for (var i=0; i<topics.length; ++i) {
 					if (topics[i]) {
-						topics[i].category = categories[topics[i].cid] || {};
+						topics[i].category = categories[topics[i].cid];
 						topics[i].user = users[topics[i].uid];
 						topics[i].teaser = results.teasers[i];
 						topics[i].tags = results.tags[i];
 
+						topics[i].isOwner = parseInt(topics[i].uid, 10) === parseInt(uid, 10);
 						topics[i].pinned = parseInt(topics[i].pinned, 10) === 1;
 						topics[i].locked = parseInt(topics[i].locked, 10) === 1;
 						topics[i].deleted = parseInt(topics[i].deleted, 10) === 1;
-						topics[i].unread = !(results.hasRead[i] && parseInt(uid, 10) !== 0);
+						topics[i].unread = !results.hasRead[i];
 						topics[i].unreplied = parseInt(topics[i].postcount, 10) <= 1;
 					}
 				}
 
 				topics = topics.filter(function(topic) {
-					return topic &&	!topic.category.disabled &&
-						(!topic.deleted || (topic.deleted && isAdminOrMod[topic.cid]) ||
-						parseInt(topic.uid, 10) === parseInt(uid, 10));
+					return topic &&	topic.category && !topic.category.disabled;
 				});
 
 				plugins.fireHook('filter:topics.get', {topics: topics, uid: uid}, function(err, topicData) {
diff --git a/src/topics/recent.js b/src/topics/recent.js
index 9a2b0f95d0..b9a5a8d798 100644
--- a/src/topics/recent.js
+++ b/src/topics/recent.js
@@ -20,20 +20,17 @@ module.exports = function(Topics) {
 	};
 
 	Topics.getLatestTopics = function(uid, start, end, term, callback) {
-		Topics.getLatestTids(start, end, term, function(err, tids) {
-			if (err) {
-				return callback(err);
+		async.waterfall([
+			function (next) {
+				Topics.getLatestTids(start, end, term, next);
+			},
+			function(tids, next) {
+				Topics.getTopics(tids, uid, next);
+			},
+			function(topics, next) {
+				next(null, {topics: topics, nextStart: end + 1});
 			}
-
-			Topics.getTopics(tids, uid, function(err, data) {
-				if (err) {
-					return callback(err);
-				}
-
-				data.nextStart = end + 1;
-				callback(null, data);
-			});
-		});
+		], callback);
 	};
 
 	Topics.getLatestTids = function(start, end, term, callback) {
diff --git a/src/topics/teaser.js b/src/topics/teaser.js
index ca5187c474..580c9a4615 100644
--- a/src/topics/teaser.js
+++ b/src/topics/teaser.js
@@ -12,7 +12,7 @@ var async = require('async'),
 
 module.exports = function(Topics) {
 
-	Topics.getTeasers = function(tids, uid, callback) {
+	Topics.getTeasers = function(tids, callback) {
 		if (!Array.isArray(tids) || !tids.length) {
 			return callback(null, []);
 		}
@@ -85,7 +85,7 @@ module.exports = function(Topics) {
 		});
 	};
 
-	Topics.getTeaser = function(tid, uid, callback) {
+	Topics.getTeaser = function(tid, callback) {
 		Topics.getLatestUndeletedPid(tid, function(err, pid) {
 			if (err || !pid) {
 				return callback(err);
@@ -109,8 +109,8 @@ module.exports = function(Topics) {
 						});
 					});
 				},
-				postIndex: function(next) {
-					posts.getPidIndex(pid, uid, next);
+				postCount: function(next) {
+					Topics.getTopicField(tid, 'post_count', next);
 				}
 			}, function(err, results) {
 				if (err) {
@@ -118,7 +118,7 @@ module.exports = function(Topics) {
 				}
 
 				results.postData.timestamp = utils.toISOString(results.postData.timestamp);
-				results.postData.index = results.postIndex;
+				results.postData.index = results.postCount;
 
 				callback(null, results.postData);
 			});