| 
									
										
										
										
											2014-03-01 16:59:04 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-17 20:15:14 +02:00
										 |  |  | var async = require('async'); | 
					
						
							| 
									
										
										
										
											2014-11-08 23:54:21 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-17 20:15:14 +02:00
										 |  |  | var db = require('./database'); | 
					
						
							|  |  |  | var user = require('./user'); | 
					
						
							|  |  |  | var Groups = require('./groups'); | 
					
						
							|  |  |  | var plugins = require('./plugins'); | 
					
						
							| 
									
										
										
										
											2016-08-27 01:52:08 +03:00
										 |  |  | var privileges = require('./privileges'); | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | (function (Categories) { | 
					
						
							| 
									
										
										
										
											2015-09-25 17:38:58 -04:00
										 |  |  | 	require('./categories/data')(Categories); | 
					
						
							| 
									
										
										
										
											2014-11-08 23:54:21 -05:00
										 |  |  | 	require('./categories/create')(Categories); | 
					
						
							| 
									
										
										
										
											2014-06-10 17:48:48 -04:00
										 |  |  | 	require('./categories/delete')(Categories); | 
					
						
							| 
									
										
										
										
											2014-11-08 23:54:21 -05:00
										 |  |  | 	require('./categories/topics')(Categories); | 
					
						
							|  |  |  | 	require('./categories/unread')(Categories); | 
					
						
							| 
									
										
										
										
											2014-03-12 21:41:53 -04:00
										 |  |  | 	require('./categories/activeusers')(Categories); | 
					
						
							|  |  |  | 	require('./categories/recentreplies')(Categories); | 
					
						
							| 
									
										
										
										
											2014-04-01 17:12:03 -04:00
										 |  |  | 	require('./categories/update')(Categories); | 
					
						
							| 
									
										
										
										
											2014-03-12 21:41:53 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	Categories.exists = function (cid, callback) { | 
					
						
							| 
									
										
										
										
											2014-05-15 20:49:47 -04:00
										 |  |  | 		db.isSortedSetMember('categories:cid', cid, callback); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	Categories.getCategoryById = function (data, callback) { | 
					
						
							| 
									
										
										
										
											2016-02-17 20:15:14 +02:00
										 |  |  | 		var category; | 
					
						
							|  |  |  | 		async.waterfall([ | 
					
						
							|  |  |  | 			function (next) { | 
					
						
							|  |  |  | 				Categories.getCategories([data.cid], data.uid, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function (categories, next) { | 
					
						
							|  |  |  | 				if (!Array.isArray(categories) || !categories[0]) { | 
					
						
							|  |  |  | 					return next(new Error('[[error:invalid-cid]]')); | 
					
						
							| 
									
										
										
										
											2014-03-12 20:53:42 -04:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2016-02-17 20:15:14 +02:00
										 |  |  | 				category = categories[0]; | 
					
						
							| 
									
										
										
										
											2014-03-12 20:53:42 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-17 20:15:14 +02:00
										 |  |  | 				async.parallel({ | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 					topics: function (next) { | 
					
						
							| 
									
										
										
										
											2016-02-17 20:15:14 +02:00
										 |  |  | 						Categories.getCategoryTopics(data, next); | 
					
						
							|  |  |  | 					}, | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 					topicCount: function (next) { | 
					
						
							| 
									
										
										
										
											2016-09-19 13:08:31 +03:00
										 |  |  | 						if (Array.isArray(data.set)) { | 
					
						
							|  |  |  | 							db.sortedSetIntersectCard(data.set, next); | 
					
						
							|  |  |  | 						} else { | 
					
						
							|  |  |  | 							next(null, category.topic_count); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 					}, | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 					isIgnored: function (next) { | 
					
						
							| 
									
										
										
										
											2016-02-17 20:15:14 +02:00
										 |  |  | 						Categories.isIgnored([data.cid], data.uid, next); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 					}, | 
					
						
							| 
									
										
										
										
											2016-02-17 20:15:14 +02:00
										 |  |  | 				}, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function (results, next) { | 
					
						
							| 
									
										
										
										
											2014-03-12 20:53:42 -04:00
										 |  |  | 				category.topics = results.topics.topics; | 
					
						
							|  |  |  | 				category.nextStart = results.topics.nextStart; | 
					
						
							| 
									
										
										
										
											2014-08-29 15:57:20 -04:00
										 |  |  | 				category.isIgnored = results.isIgnored[0]; | 
					
						
							| 
									
										
										
										
											2016-09-19 13:08:31 +03:00
										 |  |  | 				category.topic_count = results.topicCount; | 
					
						
							| 
									
										
										
										
											2013-05-24 07:52:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 				plugins.fireHook('filter:category.get', { category: category, uid: data.uid }, next); | 
					
						
							| 
									
										
										
										
											2016-02-17 20:15:14 +02:00
										 |  |  | 			}, | 
					
						
							|  |  |  | 			function (data, next) { | 
					
						
							|  |  |  | 				next(null, data.category); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2016-02-17 20:15:14 +02:00
										 |  |  | 		], callback); | 
					
						
							| 
									
										
										
										
											2013-09-12 14:07:24 -04:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-05-23 16:53:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	Categories.isIgnored = function (cids, uid, callback) { | 
					
						
							| 
									
										
										
										
											2016-11-21 13:47:34 +03:00
										 |  |  | 		db.isSortedSetMembers('uid:' + uid + ':ignored:cids', cids, callback); | 
					
						
							| 
									
										
										
										
											2014-08-29 15:57:20 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	Categories.getPageCount = function (cid, uid, callback) { | 
					
						
							| 
									
										
										
										
											2014-11-09 00:33:26 -05:00
										 |  |  | 		async.parallel({ | 
					
						
							|  |  |  | 			topicCount: async.apply(Categories.getCategoryField, cid, 'topic_count'), | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 			settings: async.apply(user.getSettings, uid), | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		}, function (err, results) { | 
					
						
							| 
									
										
										
										
											2014-10-14 23:12:47 -04:00
										 |  |  | 			if (err) { | 
					
						
							| 
									
										
										
										
											2014-01-24 22:26:11 -05:00
										 |  |  | 				return callback(err); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-09 00:33:26 -05:00
										 |  |  | 			if (!parseInt(results.topicCount, 10)) { | 
					
						
							| 
									
										
										
										
											2014-02-22 18:56:37 -05:00
										 |  |  | 				return callback(null, 1); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-09 00:33:26 -05:00
										 |  |  | 			callback(null, Math.ceil(parseInt(results.topicCount, 10) / results.settings.topicsPerPage)); | 
					
						
							| 
									
										
										
										
											2014-01-24 22:26:11 -05:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2014-01-04 01:15:41 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	Categories.getAllCategories = function (uid, callback) { | 
					
						
							|  |  |  | 		db.getSortedSetRange('categories:cid', 0, -1, function (err, cids) { | 
					
						
							| 
									
										
										
										
											2014-11-09 00:45:18 -05:00
										 |  |  | 			if (err || !Array.isArray(cids) || !cids.length) { | 
					
						
							|  |  |  | 				return callback(err, []); | 
					
						
							| 
									
										
										
										
											2013-11-26 19:09:32 -05:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-11-27 15:02:09 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-22 19:10:26 -04:00
										 |  |  | 			Categories.getCategories(cids, uid, callback); | 
					
						
							| 
									
										
										
										
											2014-05-27 12:44:28 -04:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	Categories.getCategoriesByPrivilege = function (set, uid, privilege, callback) { | 
					
						
							| 
									
										
										
										
											2014-11-09 00:33:26 -05:00
										 |  |  | 		async.waterfall([ | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 			function (next) { | 
					
						
							| 
									
										
										
										
											2015-08-18 14:17:16 -04:00
										 |  |  | 				db.getSortedSetRange(set, 0, -1, next); | 
					
						
							| 
									
										
										
										
											2014-11-09 00:33:26 -05:00
										 |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 			function (cids, next) { | 
					
						
							| 
									
										
										
										
											2014-11-09 00:33:26 -05:00
										 |  |  | 				privileges.categories.filterCids(privilege, cids, uid, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 			function (cids, next) { | 
					
						
							| 
									
										
										
										
											2014-11-09 00:33:26 -05:00
										 |  |  | 				Categories.getCategories(cids, uid, next); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2014-11-09 00:33:26 -05:00
										 |  |  | 		], callback); | 
					
						
							| 
									
										
										
										
											2013-09-12 14:07:24 -04:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	Categories.getModerators = function (cid, callback) { | 
					
						
							|  |  |  | 		Groups.getMembers('cid:' + cid + ':privileges:mods', 0, -1, function (err, uids) { | 
					
						
							| 
									
										
										
										
											2014-11-09 00:33:26 -05:00
										 |  |  | 			if (err || !Array.isArray(uids) || !uids.length) { | 
					
						
							| 
									
										
										
										
											2014-11-09 00:45:18 -05:00
										 |  |  | 				return callback(err, []); | 
					
						
							| 
									
										
										
										
											2013-07-19 16:13:00 -04:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-07-29 21:51:46 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-25 17:38:58 -04:00
										 |  |  | 			user.getUsersFields(uids, ['uid', 'username', 'userslug', 'picture'], callback); | 
					
						
							| 
									
										
										
										
											2014-03-09 23:09:08 -04:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2014-03-10 00:17:06 -04:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2014-03-09 23:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-03 12:59:45 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	Categories.getCategories = function (cids, uid, callback) { | 
					
						
							| 
									
										
										
										
											2014-07-29 21:51:46 -04:00
										 |  |  | 		if (!Array.isArray(cids)) { | 
					
						
							| 
									
										
										
										
											2014-04-09 21:56:30 -04:00
										 |  |  | 			return callback(new Error('[[error:invalid-cid]]')); | 
					
						
							| 
									
										
										
										
											2013-07-22 16:57:18 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-08-20 12:11:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-29 21:51:46 -04:00
										 |  |  | 		if (!cids.length) { | 
					
						
							| 
									
										
										
										
											2014-11-09 00:01:46 -05:00
										 |  |  | 			return callback(null, []); | 
					
						
							| 
									
										
										
										
											2014-07-29 21:51:46 -04:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-09 23:09:08 -04:00
										 |  |  | 		async.parallel({ | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 			categories: function (next) { | 
					
						
							| 
									
										
										
										
											2014-03-09 23:09:08 -04:00
										 |  |  | 				Categories.getCategoriesData(cids, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 			children: function (next) { | 
					
						
							| 
									
										
										
										
											2014-08-22 19:10:26 -04:00
										 |  |  | 				Categories.getChildren(cids, uid, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 			parents: function (next) { | 
					
						
							| 
									
										
										
										
											2014-08-22 19:10:26 -04:00
										 |  |  | 				Categories.getParents(cids, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2016-12-09 18:53:08 +03:00
										 |  |  | 			tagWhitelist: function (next) { | 
					
						
							|  |  |  | 				Categories.getTagWhitelist(cids, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 			hasRead: function (next) { | 
					
						
							| 
									
										
										
										
											2014-03-09 23:09:08 -04:00
										 |  |  | 				Categories.hasReadCategories(cids, uid, next); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		}, function (err, results) { | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 			if (err) { | 
					
						
							| 
									
										
										
										
											2014-02-22 18:56:37 -05:00
										 |  |  | 				return callback(err); | 
					
						
							| 
									
										
										
										
											2013-07-22 16:57:18 -04:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-08-20 12:11:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-09 23:09:08 -04:00
										 |  |  | 			uid = parseInt(uid, 10); | 
					
						
							| 
									
										
										
										
											2016-12-09 18:53:08 +03:00
										 |  |  | 			results.categories.forEach(function (category, i) { | 
					
						
							|  |  |  | 				if (category) { | 
					
						
							|  |  |  | 					category['unread-class'] = (parseInt(category.topic_count, 10) === 0 || (results.hasRead[i] && uid !== 0)) ? '' : 'unread'; | 
					
						
							|  |  |  | 					category.children = results.children[i]; | 
					
						
							|  |  |  | 					category.parent = results.parents[i] || undefined; | 
					
						
							|  |  |  | 					category.tagWhitelist = results.tagWhitelist[i]; | 
					
						
							|  |  |  | 					calculateTopicPostCount(category); | 
					
						
							| 
									
										
										
										
											2014-09-01 18:46:42 -04:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2016-12-09 18:53:08 +03:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			callback(null, results.categories); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-10-07 17:48:11 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-09 18:53:08 +03:00
										 |  |  | 	Categories.getTagWhitelist = function (cids, callback) { | 
					
						
							|  |  |  | 		var keys = cids.map(function (cid) { | 
					
						
							|  |  |  | 			return 'cid:' + cid + ':tag:whitelist'; | 
					
						
							| 
									
										
										
										
											2013-07-22 16:57:18 -04:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2016-12-09 18:53:08 +03:00
										 |  |  | 		db.getSortedSetsMembers(keys, callback); | 
					
						
							| 
									
										
										
										
											2013-08-20 12:11:17 -04:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-07-22 16:57:18 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-19 15:18:55 -04:00
										 |  |  | 	function calculateTopicPostCount(category) { | 
					
						
							| 
									
										
										
										
											2015-05-17 15:53:42 -04:00
										 |  |  | 		if (!category) { | 
					
						
							| 
									
										
										
										
											2015-04-19 15:18:55 -04:00
										 |  |  | 			return; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-08-18 14:17:16 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-19 15:24:11 -04:00
										 |  |  | 		var postCount = parseInt(category.post_count, 10) || 0; | 
					
						
							|  |  |  | 		var topicCount = parseInt(category.topic_count, 10) || 0; | 
					
						
							| 
									
										
										
										
											2015-05-17 15:53:42 -04:00
										 |  |  | 		if (!Array.isArray(category.children) || !category.children.length) { | 
					
						
							|  |  |  | 			category.totalPostCount = postCount; | 
					
						
							|  |  |  | 			category.totalTopicCount = topicCount; | 
					
						
							|  |  |  | 			return; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-04-19 15:18:55 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		category.children.forEach(function (child) { | 
					
						
							| 
									
										
										
										
											2015-08-18 14:17:16 -04:00
										 |  |  | 			calculateTopicPostCount(child); | 
					
						
							|  |  |  | 			postCount += parseInt(child.totalPostCount, 10) || 0; | 
					
						
							|  |  |  | 			topicCount += parseInt(child.totalTopicCount, 10) || 0; | 
					
						
							| 
									
										
										
										
											2015-04-19 15:18:55 -04:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2015-08-18 14:17:16 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-17 15:53:42 -04:00
										 |  |  | 		category.totalPostCount = postCount; | 
					
						
							|  |  |  | 		category.totalTopicCount = topicCount; | 
					
						
							| 
									
										
										
										
											2015-04-19 15:18:55 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	Categories.getParents = function (cids, callback) { | 
					
						
							| 
									
										
										
										
											2015-09-16 18:13:03 -04:00
										 |  |  | 		var categoriesData; | 
					
						
							|  |  |  | 		var parentCids; | 
					
						
							|  |  |  | 		async.waterfall([ | 
					
						
							|  |  |  | 			function (next) { | 
					
						
							| 
									
										
										
										
											2015-09-25 17:38:58 -04:00
										 |  |  | 				Categories.getCategoriesFields(cids, ['parentCid'], next); | 
					
						
							| 
									
										
										
										
											2015-09-16 18:13:03 -04:00
										 |  |  | 			}, | 
					
						
							|  |  |  | 			function (_categoriesData, next) { | 
					
						
							|  |  |  | 				categoriesData = _categoriesData; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 				parentCids = categoriesData.filter(function (category) { | 
					
						
							| 
									
										
										
										
											2015-09-16 18:16:14 -04:00
										 |  |  | 					return category && category.hasOwnProperty('parentCid') && parseInt(category.parentCid, 10); | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 				}).map(function (category) { | 
					
						
							| 
									
										
										
										
											2015-09-16 18:13:03 -04:00
										 |  |  | 					return parseInt(category.parentCid, 10); | 
					
						
							|  |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2014-08-22 19:10:26 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-16 18:13:03 -04:00
										 |  |  | 				if (!parentCids.length) { | 
					
						
							| 
									
										
										
										
											2017-02-18 01:21:34 -07:00
										 |  |  | 					return callback(null, cids.map(function () { return null; })); | 
					
						
							| 
									
										
										
										
											2015-04-13 16:12:13 -04:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2014-08-22 19:10:26 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-16 18:13:03 -04:00
										 |  |  | 				Categories.getCategoriesData(parentCids, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function (parentData, next) { | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 				parentData = categoriesData.map(function (category) { | 
					
						
							| 
									
										
										
										
											2015-09-16 18:13:03 -04:00
										 |  |  | 					return parentData[parentCids.indexOf(parseInt(category.parentCid, 10))]; | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 				next(null, parentData); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2015-09-16 18:13:03 -04:00
										 |  |  | 		], callback); | 
					
						
							| 
									
										
										
										
											2014-08-22 19:10:26 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	Categories.getChildren = function (cids, uid, callback) { | 
					
						
							|  |  |  | 		var categories = cids.map(function (cid) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			return { cid: cid }; | 
					
						
							| 
									
										
										
										
											2015-08-18 14:17:16 -04:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		async.each(categories, function (category, next) { | 
					
						
							| 
									
										
										
										
											2015-08-19 13:37:48 -04:00
										 |  |  | 			getChildrenRecursive(category, uid, next); | 
					
						
							| 
									
										
										
										
											2015-08-18 14:17:16 -04:00
										 |  |  | 		}, function (err) { | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 			callback(err, categories.map(function (c) { | 
					
						
							| 
									
										
										
										
											2015-08-18 14:17:16 -04:00
										 |  |  | 				return c && c.children; | 
					
						
							|  |  |  | 			})); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-19 13:37:48 -04:00
										 |  |  | 	function getChildrenRecursive(category, uid, callback) { | 
					
						
							| 
									
										
										
										
											2014-08-21 09:52:21 -04:00
										 |  |  | 		async.waterfall([ | 
					
						
							| 
									
										
										
										
											2015-08-18 14:17:16 -04:00
										 |  |  | 			function (next) { | 
					
						
							| 
									
										
										
										
											2015-08-19 13:37:48 -04:00
										 |  |  | 				db.getSortedSetRange('cid:' + category.cid + ':children', 0, -1, next); | 
					
						
							| 
									
										
										
										
											2014-08-21 09:52:21 -04:00
										 |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2015-08-18 14:17:16 -04:00
										 |  |  | 			function (children, next) { | 
					
						
							|  |  |  | 				privileges.categories.filterCids('find', children, uid, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function (children, next) { | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 				children = children.filter(function (cid) { | 
					
						
							| 
									
										
										
										
											2015-09-15 12:58:19 -04:00
										 |  |  | 					return parseInt(category.cid, 10) !== parseInt(cid, 10); | 
					
						
							|  |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2015-08-18 14:17:16 -04:00
										 |  |  | 				if (!children.length) { | 
					
						
							|  |  |  | 					category.children = []; | 
					
						
							|  |  |  | 					return callback(); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				Categories.getCategoriesData(children, next); | 
					
						
							| 
									
										
										
										
											2014-08-22 19:10:26 -04:00
										 |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2015-08-18 14:17:16 -04:00
										 |  |  | 			function (childrenData, next) { | 
					
						
							| 
									
										
										
										
											2015-09-18 15:33:48 -04:00
										 |  |  | 				childrenData = childrenData.filter(Boolean); | 
					
						
							| 
									
										
										
										
											2015-08-18 14:17:16 -04:00
										 |  |  | 				category.children = childrenData; | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 				async.each(category.children, function (child, next) { | 
					
						
							| 
									
										
										
										
											2015-08-19 13:37:48 -04:00
										 |  |  | 					getChildrenRecursive(child, uid, next); | 
					
						
							| 
									
										
										
										
											2014-08-22 19:10:26 -04:00
										 |  |  | 				}, next); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2014-08-21 09:52:21 -04:00
										 |  |  | 		], callback); | 
					
						
							| 
									
										
										
										
											2015-08-18 14:17:16 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	Categories.flattenCategories = function (allCategories, categoryData) { | 
					
						
							|  |  |  | 		categoryData.forEach(function (category) { | 
					
						
							| 
									
										
										
										
											2017-05-25 20:01:07 -04:00
										 |  |  | 			if (category) { | 
					
						
							|  |  |  | 				if (!category.parent) { | 
					
						
							|  |  |  | 					allCategories.push(category); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2015-08-18 14:17:16 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-25 20:01:07 -04:00
										 |  |  | 				if (Array.isArray(category.children) && category.children.length) { | 
					
						
							|  |  |  | 					Categories.flattenCategories(allCategories, category.children); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2015-08-18 14:17:16 -04:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2015-08-19 13:37:48 -04:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2014-08-21 09:52:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-30 18:44:31 +03:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * Recursively build tree | 
					
						
							|  |  |  | 	 * | 
					
						
							|  |  |  | 	 * @param categories {array} flat list of categories | 
					
						
							|  |  |  | 	 * @param parentCid {number} start from 0 to build full tree | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	Categories.getTree = function (categories, parentCid) { | 
					
						
							| 
									
										
										
										
											2017-02-17 20:20:42 -07:00
										 |  |  | 		var tree = []; | 
					
						
							|  |  |  | 		var i = 0; | 
					
						
							|  |  |  | 		var len = categories.length; | 
					
						
							|  |  |  | 		var category; | 
					
						
							| 
									
										
										
										
											2015-05-30 18:44:31 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-18 01:12:18 -07:00
										 |  |  | 		for (i; i < len; i += 1) { | 
					
						
							| 
									
										
										
										
											2015-06-04 14:24:33 -04:00
										 |  |  | 			category = categories[i]; | 
					
						
							| 
									
										
										
										
											2016-09-15 20:31:47 +03:00
										 |  |  | 			if (!category.hasOwnProperty('parentCid') || category.parentCid === null) { | 
					
						
							| 
									
										
										
										
											2015-06-04 14:24:33 -04:00
										 |  |  | 				category.parentCid = 0; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2015-05-30 18:44:31 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-25 21:34:47 +02:00
										 |  |  | 			if (parseInt(category.parentCid, 10) === parseInt(parentCid, 10)) { | 
					
						
							| 
									
										
										
										
											2015-06-04 14:24:33 -04:00
										 |  |  | 				tree.push(category); | 
					
						
							|  |  |  | 				category.children = Categories.getTree(categories, category.cid); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-05-30 18:44:31 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-04 14:24:33 -04:00
										 |  |  | 		return tree; | 
					
						
							| 
									
										
										
										
											2015-05-30 18:44:31 +03:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	Categories.buildForSelect = function (uid, callback) { | 
					
						
							| 
									
										
										
										
											2016-09-16 14:20:07 +03:00
										 |  |  | 		function recursive(category, categoriesData, level) { | 
					
						
							|  |  |  | 			if (category.link) { | 
					
						
							|  |  |  | 				return; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			var bullet = level ? '• ' : ''; | 
					
						
							|  |  |  | 			category.value = category.cid; | 
					
						
							| 
									
										
										
										
											2016-10-25 21:34:47 +02:00
										 |  |  | 			category.text = level + bullet + category.name; | 
					
						
							| 
									
										
										
										
											2016-09-16 14:20:07 +03:00
										 |  |  | 			categoriesData.push(category); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 			category.children.forEach(function (child) { | 
					
						
							| 
									
										
										
										
											2016-09-16 14:20:07 +03:00
										 |  |  | 				recursive(child, categoriesData, '    ' + level); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		Categories.getCategoriesByPrivilege('cid:0:children', uid, 'read', function (err, categories) { | 
					
						
							| 
									
										
										
										
											2016-09-16 14:20:07 +03:00
										 |  |  | 			if (err) { | 
					
						
							|  |  |  | 				return callback(err); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			var categoriesData = []; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 			categories = categories.filter(function (category) { | 
					
						
							| 
									
										
										
										
											2016-09-16 14:20:07 +03:00
										 |  |  | 				return category && !category.link && !parseInt(category.parentCid, 10); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 			categories.forEach(function (category) { | 
					
						
							| 
									
										
										
										
											2016-09-16 14:20:07 +03:00
										 |  |  | 				recursive(category, categoriesData, ''); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 			callback(null, categoriesData); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	Categories.getIgnorers = function (cid, start, stop, callback) { | 
					
						
							| 
									
										
										
										
											2016-05-18 19:02:43 +03:00
										 |  |  | 		db.getSortedSetRevRange('cid:' + cid + ':ignorers', start, stop, callback); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	Categories.filterIgnoringUids = function (cid, uids, callback) { | 
					
						
							| 
									
										
										
										
											2016-05-18 19:02:43 +03:00
										 |  |  | 		async.waterfall([ | 
					
						
							| 
									
										
										
										
											2016-10-25 21:34:47 +02:00
										 |  |  | 			function (next) { | 
					
						
							| 
									
										
										
										
											2017-02-09 13:15:37 +03:00
										 |  |  | 				db.isSortedSetMembers('cid:' + cid + ':ignorers', uids, next); | 
					
						
							| 
									
										
										
										
											2016-05-18 19:02:43 +03:00
										 |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2017-02-09 13:15:37 +03:00
										 |  |  | 			function (isIgnoring, next) { | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 				var readingUids = uids.filter(function (uid, index) { | 
					
						
							| 
									
										
										
										
											2017-02-09 13:15:37 +03:00
										 |  |  | 					return uid && !isIgnoring[index]; | 
					
						
							| 
									
										
										
										
											2016-05-18 19:02:43 +03:00
										 |  |  | 				}); | 
					
						
							|  |  |  | 				next(null, readingUids); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2016-05-18 19:02:43 +03:00
										 |  |  | 		], callback); | 
					
						
							|  |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2014-04-10 20:31:57 +01:00
										 |  |  | }(exports)); |