| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | var	RDB = require('./redis.js'), | 
					
						
							|  |  |  | 	posts = require('./posts.js'), | 
					
						
							| 
									
										
										
										
											2013-05-23 13:21:38 -04:00
										 |  |  | 	utils = require('./../public/src/utils.js'), | 
					
						
							| 
									
										
										
										
											2013-05-18 14:59:50 -04:00
										 |  |  | 	user = require('./user.js'), | 
					
						
							| 
									
										
										
										
											2013-05-21 21:07:26 -04:00
										 |  |  | 	async = require('async'), | 
					
						
							|  |  |  | 	topics = require('./topics.js'); | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | (function(Categories) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-24 11:18:28 -04:00
										 |  |  | 	Categories.getCategoryById = function(category_id, current_user, callback) { | 
					
						
							| 
									
										
										
										
											2013-05-23 16:53:19 -04:00
										 |  |  | 		RDB.smembers('categories:' + category_id + ':tid', function(err, tids) { | 
					
						
							|  |  |  | 			RDB.multi() | 
					
						
							| 
									
										
										
										
											2013-05-21 21:07:26 -04:00
										 |  |  | 				.get('cid:' + category_id + ':name') | 
					
						
							| 
									
										
										
										
											2013-05-23 16:53:19 -04:00
										 |  |  | 				.smembers('cid:' + category_id + ':active_users') | 
					
						
							| 
									
										
										
										
											2013-06-24 17:05:23 -04:00
										 |  |  | 				.get('cid:' + category_id + ':slug') | 
					
						
							| 
									
										
										
										
											2013-05-23 16:53:19 -04:00
										 |  |  | 				.exec(function(err, replies) { | 
					
						
							| 
									
										
										
										
											2013-06-24 17:05:23 -04:00
										 |  |  | 					var	category_name = replies[0], | 
					
						
							|  |  |  | 						active_users = replies[1], | 
					
						
							|  |  |  | 						category_slug = replies[2]; | 
					
						
							| 
									
										
										
										
											2013-05-23 16:53:19 -04:00
										 |  |  | 					 | 
					
						
							|  |  |  | 					if (category_name === null) { | 
					
						
							|  |  |  | 						callback(false); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					var categoryData = { | 
					
						
							|  |  |  | 							'category_name' : category_name, | 
					
						
							| 
									
										
										
										
											2013-06-11 16:36:33 -04:00
										 |  |  | 							'show_sidebar' : 'show', | 
					
						
							|  |  |  | 							'show_topic_button': 'show', | 
					
						
							|  |  |  | 							'no_topics_message': 'hidden', | 
					
						
							| 
									
										
										
										
											2013-05-24 11:44:26 -04:00
										 |  |  | 							'topic_row_size': 'span9', | 
					
						
							| 
									
										
										
										
											2013-05-23 16:53:19 -04:00
										 |  |  | 							'category_id': category_id, | 
					
						
							| 
									
										
										
										
											2013-06-24 14:33:53 -04:00
										 |  |  | 							'active_users': [], | 
					
						
							| 
									
										
										
										
											2013-06-24 17:05:23 -04:00
										 |  |  | 							'topics' : [], | 
					
						
							| 
									
										
										
										
											2013-06-25 21:15:24 -04:00
										 |  |  | 							'twitter-intent-url': 'https://twitter.com/intent/tweet?url=' + encodeURIComponent(global.config.url + 'category/' + category_slug) + '&text=' + encodeURIComponent(category_name), | 
					
						
							|  |  |  | 							'facebook-share-url': 'https://www.facebook.com/sharer/sharer.php?u=' + encodeURIComponent(global.config.url + 'category/' + category_slug), | 
					
						
							|  |  |  | 							'google-share-url': 'https://plus.google.com/share?url=' + encodeURIComponent(global.config.url + 'category/' + category_slug) | 
					
						
							| 
									
										
										
										
											2013-05-23 16:53:19 -04:00
										 |  |  | 						}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					function getTopics(next) { | 
					
						
							|  |  |  | 						Categories.getTopicsByTids(tids, current_user, function(topics) { | 
					
						
							| 
									
										
										
										
											2013-06-25 17:31:51 -04:00
										 |  |  | 							// Float pinned topics to the top
 | 
					
						
							|  |  |  | 							topics = topics.sort(function(a, b) { | 
					
						
							|  |  |  | 								if (a.pinned !== b.pinned) return b.pinned - a.pinned; | 
					
						
							|  |  |  | 								else { | 
					
						
							| 
									
										
										
										
											2013-06-25 21:16:37 -04:00
										 |  |  | 									return b.lastposttime - a.lastposttime; | 
					
						
							| 
									
										
										
										
											2013-06-25 17:31:51 -04:00
										 |  |  | 								} | 
					
						
							|  |  |  | 							}); | 
					
						
							| 
									
										
										
										
											2013-05-23 16:53:19 -04:00
										 |  |  | 							next(null, topics); | 
					
						
							| 
									
										
										
										
											2013-06-25 17:31:51 -04:00
										 |  |  | 							 | 
					
						
							| 
									
										
										
										
											2013-05-23 16:53:19 -04:00
										 |  |  | 						}, category_id); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					 | 
					
						
							|  |  |  | 					function getModerators(next) { | 
					
						
							|  |  |  | 						Categories.getModerators(category_id, function(moderators) { | 
					
						
							|  |  |  | 							next(null, moderators); | 
					
						
							|  |  |  | 						}); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-24 14:33:53 -04:00
										 |  |  | 					function getActiveUsers(next) { | 
					
						
							| 
									
										
										
										
											2013-07-02 19:02:23 -04:00
										 |  |  | 						user.getMultipleUserFields(active_users, ['username', 'userslug'], function(users) { | 
					
						
							| 
									
										
										
										
											2013-06-24 14:33:53 -04:00
										 |  |  | 							var activeUserData = []; | 
					
						
							|  |  |  | 							for(var uid in users) { | 
					
						
							|  |  |  | 								activeUserData.push(users[uid]); | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 							next(null, activeUserData); | 
					
						
							|  |  |  | 						}); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-24 07:52:15 -04:00
										 |  |  | 					if (tids.length === 0) { | 
					
						
							|  |  |  | 						getModerators(function(err, moderators) { | 
					
						
							|  |  |  | 							categoryData.moderator_block_class = moderators.length > 0 ? '' : 'none'; | 
					
						
							|  |  |  | 							categoryData.moderators = moderators; | 
					
						
							| 
									
										
										
										
											2013-06-11 16:36:33 -04:00
										 |  |  | 							categoryData.show_sidebar = 'hidden'; | 
					
						
							|  |  |  | 							categoryData.no_topics_message = 'show'; | 
					
						
							| 
									
										
										
										
											2013-05-24 07:52:15 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 							callback(categoryData); | 
					
						
							|  |  |  | 						}); | 
					
						
							|  |  |  | 					} else { | 
					
						
							| 
									
										
										
										
											2013-06-24 14:33:53 -04:00
										 |  |  | 						async.parallel([getTopics, getModerators, getActiveUsers], function(err, results) { | 
					
						
							| 
									
										
										
										
											2013-05-24 07:52:15 -04:00
										 |  |  | 							categoryData.topics = results[0]; | 
					
						
							|  |  |  | 							categoryData.moderator_block_class = results[1].length > 0 ? '' : 'none'; | 
					
						
							|  |  |  | 							categoryData.moderators = results[1]; | 
					
						
							| 
									
										
										
										
											2013-06-24 14:33:53 -04:00
										 |  |  | 							categoryData.active_users = results[2]; | 
					
						
							| 
									
										
										
										
											2013-05-24 07:52:15 -04:00
										 |  |  | 							callback(categoryData); | 
					
						
							|  |  |  | 						}); | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2013-05-23 16:53:19 -04:00
										 |  |  | 				}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-23 17:38:49 -04:00
										 |  |  | 	// not the permanent location for this function
 | 
					
						
							|  |  |  | 	Categories.getLatestTopics = function(current_user, start, end, callback) { | 
					
						
							| 
									
										
										
										
											2013-06-25 14:38:17 -04:00
										 |  |  | 		RDB.zrevrange('topics:recent', 0, -1, function(err, tids) { | 
					
						
							| 
									
										
										
										
											2013-05-23 17:38:49 -04:00
										 |  |  | 			var latestTopics = { | 
					
						
							|  |  |  | 				'category_name' : 'Recent', | 
					
						
							| 
									
										
										
										
											2013-06-11 16:36:33 -04:00
										 |  |  | 				'show_sidebar' : 'hidden', | 
					
						
							|  |  |  | 				'show_topic_button' : 'hidden', | 
					
						
							|  |  |  | 				'no_topics_message' : 'hidden', | 
					
						
							| 
									
										
										
										
											2013-05-24 11:44:26 -04:00
										 |  |  | 				'topic_row_size': 'span12', | 
					
						
							| 
									
										
										
										
											2013-05-23 17:38:49 -04:00
										 |  |  | 				'category_id': false, | 
					
						
							|  |  |  | 				'topics' : [] | 
					
						
							|  |  |  | 			}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (!tids.length) { | 
					
						
							|  |  |  | 				callback(latestTopics); | 
					
						
							|  |  |  | 				return; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			Categories.getTopicsByTids(tids, current_user, function(topicData) { | 
					
						
							|  |  |  | 				latestTopics.topics = topicData; | 
					
						
							|  |  |  | 				callback(latestTopics); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-24 11:18:28 -04:00
										 |  |  | 	// not the permanent location for this function
 | 
					
						
							| 
									
										
										
										
											2013-05-23 16:53:19 -04:00
										 |  |  | 	Categories.getTopicsByTids = function(tids, current_user, callback, category_id /*temporary*/) { | 
					
						
							| 
									
										
										
										
											2013-07-02 17:01:49 -04:00
										 |  |  | 		var retrieved_topics = []; | 
					
						
							| 
									
										
										
										
											2013-07-02 19:05:28 -04:00
										 |  |  | 		var topicCountToLoad = tids.length; | 
					
						
							| 
									
										
										
										
											2013-07-02 17:01:49 -04:00
										 |  |  | 		 | 
					
						
							| 
									
										
										
										
											2013-07-02 19:02:23 -04:00
										 |  |  | 		function getTopicInfo(topicData, callback) { | 
					
						
							| 
									
										
										
										
											2013-07-02 17:01:49 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-02 18:02:02 -04:00
										 |  |  | 			function getUserName(next) { | 
					
						
							| 
									
										
										
										
											2013-07-02 17:01:49 -04:00
										 |  |  | 				user.getUserField(topicData.uid, 'username', function(username) { | 
					
						
							|  |  |  | 					next(null, username); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-02 18:02:02 -04:00
										 |  |  | 			function hasReadTopic(next) { | 
					
						
							| 
									
										
										
										
											2013-07-02 17:01:49 -04:00
										 |  |  | 				topics.hasReadTopics([topicData.tid], current_user, function(hasRead) { | 
					
						
							|  |  |  | 					next(null, hasRead); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			function getTeaserInfo(next) { | 
					
						
							| 
									
										
										
										
											2013-07-03 12:21:24 -04:00
										 |  |  | 				topics.getTeaser(topicData.tid, function(teaser) { | 
					
						
							| 
									
										
										
										
											2013-07-02 18:02:02 -04:00
										 |  |  | 					next(null, teaser); | 
					
						
							| 
									
										
										
										
											2013-07-02 17:01:49 -04:00
										 |  |  | 				}); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// temporary. I don't think this call should belong here
 | 
					
						
							|  |  |  | 			function getPrivileges(next) { | 
					
						
							|  |  |  | 				Categories.privileges(category_id, current_user, function(user_privs) { | 
					
						
							|  |  |  | 					next(null, user_privs); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-02 18:02:02 -04:00
										 |  |  | 			async.parallel([getUserName, hasReadTopic, getTeaserInfo, getPrivileges], function(err, results) { | 
					
						
							| 
									
										
										
										
											2013-07-02 17:01:49 -04:00
										 |  |  | 				var username = results[0], | 
					
						
							|  |  |  | 					hasReadTopic = results[1], | 
					
						
							|  |  |  | 					teaserInfo = results[2], | 
					
						
							|  |  |  | 					privileges = results[3]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				callback({ | 
					
						
							|  |  |  | 					username: username, | 
					
						
							| 
									
										
										
										
											2013-07-02 19:02:23 -04:00
										 |  |  | 					hasread: hasReadTopic, | 
					
						
							| 
									
										
										
										
											2013-07-02 17:01:49 -04:00
										 |  |  | 					teaserInfo: teaserInfo, | 
					
						
							|  |  |  | 					privileges: privileges | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2013-05-23 16:53:19 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-07-02 19:02:23 -04:00
										 |  |  | 		 | 
					
						
							| 
									
										
										
										
											2013-07-02 19:05:28 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-23 16:53:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-02 17:01:49 -04:00
										 |  |  | 		for(var i=0; i<tids.length; ++i) { | 
					
						
							| 
									
										
										
										
											2013-05-21 21:07:26 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-02 17:01:49 -04:00
										 |  |  | 			topics.getTopicData(tids[i], function(topicData) { | 
					
						
							| 
									
										
										
										
											2013-05-21 21:07:26 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-02 19:02:23 -04:00
										 |  |  | 				getTopicInfo(topicData, function(topicInfo) { | 
					
						
							| 
									
										
										
										
											2013-05-23 16:53:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-02 17:01:49 -04:00
										 |  |  | 					topicData['pin-icon'] = topicData.pinned === '1' ? 'icon-pushpin' : 'none'; | 
					
						
							|  |  |  | 					topicData['lock-icon'] = topicData.locked === '1' ? 'icon-lock' : 'none'; | 
					
						
							|  |  |  | 					topicData['deleted-class'] = topicData.deleted === '1' ? 'deleted' : ''; | 
					
						
							| 
									
										
										
										
											2013-05-21 21:07:26 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-02 17:01:49 -04:00
										 |  |  | 					topicData.relativeTime = utils.relativeTime(topicData.timestamp); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					topicData.username = topicInfo.username; | 
					
						
							|  |  |  | 					topicData.badgeclass = (topicInfo.hasread && current_user != 0) ? '' : 'badge-important'; | 
					
						
							|  |  |  | 					topicData.teaser_text = topicInfo.teaserInfo.text, | 
					
						
							|  |  |  | 					topicData.teaser_username = topicInfo.teaserInfo.username; | 
					
						
							|  |  |  | 					topicData.teaser_timestamp = utils.relativeTime(topicInfo.teaserInfo.timestamp); | 
					
						
							| 
									
										
										
										
											2013-05-21 21:07:26 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-02 19:02:23 -04:00
										 |  |  | 					if (!topicData.deleted || (topicData.deleted && topicInfo.privileges.view_deleted) || topicData.uid === current_user) | 
					
						
							|  |  |  | 						retrieved_topics.push(topicData); | 
					
						
							|  |  |  | 					else | 
					
						
							|  |  |  | 						--topicCountToLoad; | 
					
						
							| 
									
										
										
										
											2013-07-02 17:01:49 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-02 19:05:28 -04:00
										 |  |  | 					if(retrieved_topics.length === topicCountToLoad) | 
					
						
							| 
									
										
										
										
											2013-07-02 17:01:49 -04:00
										 |  |  | 						callback(retrieved_topics); | 
					
						
							| 
									
										
										
										
											2013-05-21 21:07:26 -04:00
										 |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2013-07-02 17:01:49 -04:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-21 21:07:26 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Categories.getAllCategories = function(callback, current_user) { | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 		RDB.lrange('categories:cid', 0, -1, function(err, cids) { | 
					
						
							|  |  |  | 			RDB.handle(err); | 
					
						
							| 
									
										
										
										
											2013-05-24 08:34:13 -04:00
										 |  |  | 			Categories.getCategories(cids, callback, current_user); | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-16 12:49:39 -04:00
										 |  |  | 	Categories.getModerators = function(cid, callback) { | 
					
						
							|  |  |  | 		RDB.smembers('cid:' + cid + ':moderators', function(err, mods) { | 
					
						
							| 
									
										
										
										
											2013-05-16 13:14:23 -04:00
										 |  |  | 			if (mods.length === 0) return callback([]); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-16 12:49:39 -04:00
										 |  |  | 			user.getMultipleUserFields(mods, ['username'], function(details) { | 
					
						
							|  |  |  | 				var moderators = []; | 
					
						
							|  |  |  | 				for(u in details) { | 
					
						
							|  |  |  | 					if (details.hasOwnProperty(u)) { | 
					
						
							|  |  |  | 						moderators.push({ username: details[u].username }); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				callback(moderators); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-21 18:12:39 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-24 11:18:28 -04:00
										 |  |  | 	Categories.privileges = function(cid, uid, callback) { | 
					
						
							|  |  |  | 		function isModerator(next) { | 
					
						
							|  |  |  | 			user.isModerator(uid, cid, function(isMod) { | 
					
						
							|  |  |  | 					next(null, isMod); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		function isAdministrator(next) { | 
					
						
							|  |  |  | 			user.isAdministrator(uid, function(isAdmin) { | 
					
						
							|  |  |  | 					next(null, isAdmin); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		async.parallel([isModerator, isAdministrator], function(err, results) { | 
					
						
							|  |  |  | 			callback({ | 
					
						
							|  |  |  | 				editable: results.indexOf(true) !== -1 ? true : false, | 
					
						
							|  |  |  | 				view_deleted: results.indexOf(true) !== -1 ? true : false | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-28 12:40:15 -04:00
										 |  |  | 	Categories.isTopicsRead = function(cid, uid, callback) { | 
					
						
							|  |  |  | 		RDB.smembers('categories:' + cid + ':tid', function(err, tids) { | 
					
						
							| 
									
										
										
										
											2013-05-24 11:18:28 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-28 12:40:15 -04:00
										 |  |  | 			topics.hasReadTopics(tids, uid, function(hasRead) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				var allread = true; | 
					
						
							|  |  |  | 				for (var i=0, ii=tids.length; i<ii; i++) { | 
					
						
							|  |  |  | 					if(hasRead[i] === 0) { | 
					
						
							|  |  |  | 						allread = false; | 
					
						
							|  |  |  | 						break; | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				callback(allread);				 | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Categories.markAsRead = function(cid, uid) { | 
					
						
							|  |  |  | 		RDB.sadd('cid:' + cid + ':read_by_uid', uid);			 | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-05-24 11:18:28 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-21 18:12:39 -04:00
										 |  |  | 	Categories.hasReadCategories = function(cids, uid, callback) { | 
					
						
							|  |  |  | 		var batch = RDB.multi(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		for (var i=0, ii=cids.length; i<ii; i++) { | 
					
						
							|  |  |  | 			batch.sismember('cid:' + cids[i] + ':read_by_uid', uid);	 | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		 | 
					
						
							|  |  |  | 		batch.exec(function(err, hasRead) { | 
					
						
							|  |  |  | 			callback(hasRead); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-24 12:46:19 -04:00
										 |  |  | 	Categories.getRecentReplies = function(cid, callback) { | 
					
						
							| 
									
										
										
										
											2013-06-21 16:46:31 -04:00
										 |  |  | 		RDB.zrevrange('categories:recent_posts:cid:' + cid, 0, 4, function(err, pids) { | 
					
						
							| 
									
										
										
										
											2013-07-02 19:46:58 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-24 15:05:59 -04:00
										 |  |  | 			if (pids.length == 0) { | 
					
						
							|  |  |  | 				callback(false); | 
					
						
							|  |  |  | 				return; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-05-24 14:34:55 -04:00
										 |  |  | 			posts.getPostSummaryByPids(pids, function(posts) { | 
					
						
							|  |  |  | 				callback(posts); | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2013-05-24 12:46:19 -04:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-05-21 18:12:39 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-24 08:34:13 -04:00
										 |  |  | 	Categories.getCategories = function(cids, callback, current_user) { | 
					
						
							| 
									
										
										
										
											2013-05-24 11:18:28 -04:00
										 |  |  | 		if (cids.length === 0) { | 
					
						
							|  |  |  | 			callback({'categories' : []}); | 
					
						
							|  |  |  | 			return; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		 | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 		var name = [], | 
					
						
							|  |  |  | 			description = [], | 
					
						
							|  |  |  | 			icon = [], | 
					
						
							|  |  |  | 			blockclass = [], | 
					
						
							| 
									
										
										
										
											2013-05-21 18:12:39 -04:00
										 |  |  | 			slug = [], | 
					
						
							|  |  |  | 			topic_count = [], | 
					
						
							|  |  |  | 			has_read = {}; | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		for (var i=0, ii=cids.length; i<ii; i++) { | 
					
						
							|  |  |  | 			name.push('cid:' + cids[i] + ':name'); | 
					
						
							|  |  |  | 			description.push('cid:' + cids[i] + ':description'); | 
					
						
							|  |  |  | 			icon.push('cid:' + cids[i] + ':icon'); | 
					
						
							|  |  |  | 			blockclass.push('cid:' + cids[i] + ':blockclass'); | 
					
						
							|  |  |  | 			slug.push('cid:' + cids[i] + ':slug'); | 
					
						
							| 
									
										
										
										
											2013-05-21 18:12:39 -04:00
										 |  |  | 			topic_count.push('cid:' + cids[i] + ':topiccount'); | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-05-24 11:18:28 -04:00
										 |  |  | 		 | 
					
						
							|  |  |  | 		RDB.multi() | 
					
						
							|  |  |  | 			.mget(name) | 
					
						
							|  |  |  | 			.mget(description) | 
					
						
							|  |  |  | 			.mget(icon) | 
					
						
							|  |  |  | 			.mget(blockclass) | 
					
						
							|  |  |  | 			.mget(slug) | 
					
						
							|  |  |  | 			.mget(topic_count) | 
					
						
							|  |  |  | 			.exec(function(err, replies) { | 
					
						
							|  |  |  | 				name = replies[0]; | 
					
						
							|  |  |  | 				description = replies[1]; | 
					
						
							|  |  |  | 				icon = replies[2]; | 
					
						
							|  |  |  | 				blockclass = replies[3]; | 
					
						
							|  |  |  | 				slug = replies[4]; | 
					
						
							|  |  |  | 				topic_count = replies[5]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				 | 
					
						
							|  |  |  | 				function generateCategories() { | 
					
						
							|  |  |  | 					var categories = []; | 
					
						
							|  |  |  | 					for (var i=0, ii=cids.length; i<ii; i++) { | 
					
						
							|  |  |  | 						categories.push({ | 
					
						
							|  |  |  | 							'name' : name[i], | 
					
						
							|  |  |  | 							'cid' : cids[i], | 
					
						
							|  |  |  | 							'slug' : slug[i], | 
					
						
							|  |  |  | 							'description' : description[i], | 
					
						
							|  |  |  | 							'blockclass' : blockclass[i], | 
					
						
							|  |  |  | 							'icon' : icon[i], | 
					
						
							|  |  |  | 							'badgeclass' : (!topic_count[i] || (has_read[i] && current_user !=0)) ? '' : 'badge-important', | 
					
						
							|  |  |  | 							'topic_count' : topic_count[i] || 0 | 
					
						
							|  |  |  | 						}); | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 					} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-24 11:18:28 -04:00
										 |  |  | 					callback({'categories': categories}); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				Categories.hasReadCategories(cids, current_user, function(read_data) { | 
					
						
							|  |  |  | 					has_read = read_data; | 
					
						
							|  |  |  | 					generateCategories(); | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2013-05-24 11:18:28 -04:00
										 |  |  | 				 | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-06 22:06:01 +00:00
										 |  |  | }(exports)); |