| 
									
										
										
										
											2014-03-01 16:59:04 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-17 20:15:14 +02:00
										 |  |  | var async = require('async'); | 
					
						
							| 
									
										
										
										
											2018-11-06 13:34:29 -05:00
										 |  |  | var _ = require('lodash'); | 
					
						
							| 
									
										
										
										
											2014-11-08 23:54:21 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-23 15:03:32 -04:00
										 |  |  | var db = require('../database'); | 
					
						
							|  |  |  | var user = require('../user'); | 
					
						
							|  |  |  | var Groups = require('../groups'); | 
					
						
							|  |  |  | var plugins = require('../plugins'); | 
					
						
							|  |  |  | var privileges = require('../privileges'); | 
					
						
							| 
									
										
										
										
											2018-11-27 19:38:28 -05:00
										 |  |  | const cache = require('../cache'); | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-26 16:56:26 -04:00
										 |  |  | var Categories = module.exports; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-23 15:03:32 -04:00
										 |  |  | require('./data')(Categories); | 
					
						
							|  |  |  | require('./create')(Categories); | 
					
						
							|  |  |  | require('./delete')(Categories); | 
					
						
							|  |  |  | require('./topics')(Categories); | 
					
						
							|  |  |  | require('./unread')(Categories); | 
					
						
							|  |  |  | require('./activeusers')(Categories); | 
					
						
							|  |  |  | require('./recentreplies')(Categories); | 
					
						
							|  |  |  | require('./update')(Categories); | 
					
						
							| 
									
										
										
										
											2018-12-14 16:24:17 -05:00
										 |  |  | require('./watch')(Categories); | 
					
						
							| 
									
										
										
										
											2017-05-26 16:56:26 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | Categories.exists = function (cid, callback) { | 
					
						
							| 
									
										
										
										
											2018-10-26 14:31:30 -04:00
										 |  |  | 	db.exists('category:' + cid, callback); | 
					
						
							| 
									
										
										
										
											2017-05-26 16:56:26 -04:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Categories.getCategoryById = function (data, callback) { | 
					
						
							|  |  |  | 	var category; | 
					
						
							|  |  |  | 	async.waterfall([ | 
					
						
							|  |  |  | 		function (next) { | 
					
						
							|  |  |  | 			Categories.getCategories([data.cid], data.uid, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function (categories, next) { | 
					
						
							| 
									
										
										
										
											2017-05-27 00:47:04 -04:00
										 |  |  | 			if (!categories[0]) { | 
					
						
							| 
									
										
										
										
											2018-12-01 17:15:38 -05:00
										 |  |  | 				return callback(null, null); | 
					
						
							| 
									
										
										
										
											2014-01-24 22:26:11 -05:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-05-26 16:56:26 -04:00
										 |  |  | 			category = categories[0]; | 
					
						
							| 
									
										
										
										
											2017-06-06 16:40:32 -04:00
										 |  |  | 			data.category = category; | 
					
						
							| 
									
										
										
										
											2017-05-26 16:56:26 -04:00
										 |  |  | 			async.parallel({ | 
					
						
							|  |  |  | 				topics: function (next) { | 
					
						
							|  |  |  | 					Categories.getCategoryTopics(data, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				topicCount: function (next) { | 
					
						
							| 
									
										
										
										
											2017-06-06 16:40:32 -04:00
										 |  |  | 					Categories.getTopicCount(data, next); | 
					
						
							| 
									
										
										
										
											2017-05-26 16:56:26 -04:00
										 |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2018-12-14 16:24:17 -05:00
										 |  |  | 				watchState: function (next) { | 
					
						
							|  |  |  | 					Categories.getWatchState([data.cid], data.uid, next); | 
					
						
							| 
									
										
										
										
											2017-05-26 16:56:26 -04:00
										 |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2018-11-25 19:08:38 -05:00
										 |  |  | 				parent: function (next) { | 
					
						
							|  |  |  | 					if (category.parentCid) { | 
					
						
							|  |  |  | 						Categories.getCategoryData(category.parentCid, next); | 
					
						
							|  |  |  | 					} else { | 
					
						
							|  |  |  | 						next(); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				children: function (next) { | 
					
						
							|  |  |  | 					getChildrenTree(category, data.uid, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2017-05-26 16:56:26 -04:00
										 |  |  | 			}, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function (results, next) { | 
					
						
							|  |  |  | 			category.topics = results.topics.topics; | 
					
						
							|  |  |  | 			category.nextStart = results.topics.nextStart; | 
					
						
							|  |  |  | 			category.topic_count = results.topicCount; | 
					
						
							| 
									
										
										
										
											2018-12-14 16:24:17 -05:00
										 |  |  | 			category.isWatched = results.watchState[0] === Categories.watchStates.watching; | 
					
						
							|  |  |  | 			category.isNotWatched = results.watchState[0] === Categories.watchStates.notwatching; | 
					
						
							|  |  |  | 			category.isIgnored = results.watchState[0] === Categories.watchStates.ignoring; | 
					
						
							| 
									
										
										
										
											2018-11-25 19:08:38 -05:00
										 |  |  | 			category.parent = results.parent; | 
					
						
							| 
									
										
										
										
											2017-05-26 16:56:26 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-25 19:08:38 -05:00
										 |  |  | 			calculateTopicPostCount(category); | 
					
						
							| 
									
										
										
										
											2017-05-26 16:56:26 -04:00
										 |  |  | 			plugins.fireHook('filter:category.get', { category: category, uid: data.uid }, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function (data, next) { | 
					
						
							|  |  |  | 			next(null, data.category); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	], callback); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-27 19:38:28 -05:00
										 |  |  | Categories.getAllCidsFromSet = function (key, callback) { | 
					
						
							|  |  |  | 	const cids = cache.get(key); | 
					
						
							|  |  |  | 	if (cids) { | 
					
						
							|  |  |  | 		return setImmediate(callback, null, cids.slice()); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	db.getSortedSetRange(key, 0, -1, function (err, cids) { | 
					
						
							|  |  |  | 		if (err) { | 
					
						
							|  |  |  | 			return callback(err); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		cache.set(key, cids); | 
					
						
							|  |  |  | 		callback(null, cids.slice()); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-26 16:56:26 -04:00
										 |  |  | Categories.getAllCategories = function (uid, callback) { | 
					
						
							|  |  |  | 	async.waterfall([ | 
					
						
							|  |  |  | 		function (next) { | 
					
						
							| 
									
										
										
										
											2018-12-05 11:22:44 -05:00
										 |  |  | 			Categories.getAllCidsFromSet('categories:cid', next); | 
					
						
							| 
									
										
										
										
											2017-05-26 16:56:26 -04:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		function (cids, next) { | 
					
						
							|  |  |  | 			Categories.getCategories(cids, uid, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	], callback); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-20 14:34:42 -04:00
										 |  |  | Categories.getCidsByPrivilege = function (set, uid, privilege, callback) { | 
					
						
							| 
									
										
										
										
											2017-05-26 16:56:26 -04:00
										 |  |  | 	async.waterfall([ | 
					
						
							|  |  |  | 		function (next) { | 
					
						
							| 
									
										
										
										
											2018-11-27 19:38:28 -05:00
										 |  |  | 			Categories.getAllCidsFromSet(set, next); | 
					
						
							| 
									
										
										
										
											2017-05-26 16:56:26 -04:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		function (cids, next) { | 
					
						
							|  |  |  | 			privileges.categories.filterCids(privilege, cids, uid, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2018-07-20 14:34:42 -04:00
										 |  |  | 	], callback); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Categories.getCategoriesByPrivilege = function (set, uid, privilege, callback) { | 
					
						
							|  |  |  | 	async.waterfall([ | 
					
						
							|  |  |  | 		function (next) { | 
					
						
							|  |  |  | 			Categories.getCidsByPrivilege(set, uid, privilege, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2017-05-26 16:56:26 -04:00
										 |  |  | 		function (cids, next) { | 
					
						
							|  |  |  | 			Categories.getCategories(cids, uid, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	], callback); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Categories.getModerators = function (cid, callback) { | 
					
						
							|  |  |  | 	async.waterfall([ | 
					
						
							|  |  |  | 		function (next) { | 
					
						
							| 
									
										
										
										
											2017-05-26 23:21:46 -04:00
										 |  |  | 			Groups.getMembers('cid:' + cid + ':privileges:moderate', 0, -1, next); | 
					
						
							| 
									
										
										
										
											2017-05-26 16:56:26 -04:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		function (uids, next) { | 
					
						
							|  |  |  | 			user.getUsersFields(uids, ['uid', 'username', 'userslug', 'picture'], next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	], callback); | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2013-07-03 12:59:45 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-26 16:56:26 -04:00
										 |  |  | Categories.getCategories = function (cids, uid, callback) { | 
					
						
							|  |  |  | 	if (!Array.isArray(cids)) { | 
					
						
							|  |  |  | 		return callback(new Error('[[error:invalid-cid]]')); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-07-29 21:51:46 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-26 16:56:26 -04:00
										 |  |  | 	if (!cids.length) { | 
					
						
							|  |  |  | 		return callback(null, []); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-11-22 10:53:44 -05:00
										 |  |  | 	uid = parseInt(uid, 10); | 
					
						
							| 
									
										
										
										
											2017-05-26 16:56:26 -04:00
										 |  |  | 	async.waterfall([ | 
					
						
							|  |  |  | 		function (next) { | 
					
						
							|  |  |  | 			async.parallel({ | 
					
						
							|  |  |  | 				categories: function (next) { | 
					
						
							|  |  |  | 					Categories.getCategoriesData(cids, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				tagWhitelist: function (next) { | 
					
						
							|  |  |  | 					Categories.getTagWhitelist(cids, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				hasRead: function (next) { | 
					
						
							|  |  |  | 					Categories.hasReadCategories(cids, uid, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			}, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2018-11-25 19:08:38 -05:00
										 |  |  | 		function (results, next) { | 
					
						
							|  |  |  | 			results.categories.forEach(function (category, i) { | 
					
						
							| 
									
										
										
										
											2016-12-09 18:53:08 +03:00
										 |  |  | 				if (category) { | 
					
						
							|  |  |  | 					category.tagWhitelist = results.tagWhitelist[i]; | 
					
						
							| 
									
										
										
										
											2018-10-23 22:28:37 -04:00
										 |  |  | 					category['unread-class'] = (category.topic_count === 0 || (results.hasRead[i] && uid !== 0)) ? '' : 'unread'; | 
					
						
							| 
									
										
										
										
											2014-09-01 18:46:42 -04:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2016-12-09 18:53:08 +03:00
										 |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2018-11-25 19:08:38 -05:00
										 |  |  | 			next(null, results.categories); | 
					
						
							| 
									
										
										
										
											2017-05-26 16:56:26 -04:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 	], callback); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Categories.getTagWhitelist = function (cids, callback) { | 
					
						
							| 
									
										
										
										
											2018-12-09 16:03:41 -05:00
										 |  |  | 	const cachedData = {}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	const nonCachedCids = cids.filter((cid) => { | 
					
						
							|  |  |  | 		const data = cache.get('cid:' + cid + ':tag:whitelist'); | 
					
						
							|  |  |  | 		const isInCache = data !== undefined; | 
					
						
							|  |  |  | 		if (isInCache) { | 
					
						
							|  |  |  | 			cachedData[cid] = data; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return !isInCache; | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!nonCachedCids.length) { | 
					
						
							|  |  |  | 		return setImmediate(callback, null, _.clone(cids.map(cid => cachedData[cid]))); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	const keys = nonCachedCids.map(cid => 'cid:' + cid + ':tag:whitelist'); | 
					
						
							|  |  |  | 	db.getSortedSetsMembers(keys, function (err, data) { | 
					
						
							|  |  |  | 		if (err) { | 
					
						
							|  |  |  | 			return callback(err); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		nonCachedCids.forEach((cid, index) => { | 
					
						
							|  |  |  | 			cachedData[cid] = data[index]; | 
					
						
							|  |  |  | 			cache.set('cid:' + cid + ':tag:whitelist', data[index]); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 		callback(null, _.clone(cids.map(cid => cachedData[cid]))); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2017-05-26 16:56:26 -04:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function calculateTopicPostCount(category) { | 
					
						
							|  |  |  | 	if (!category) { | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-08-18 14:17:16 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-23 22:28:37 -04:00
										 |  |  | 	var postCount = category.post_count; | 
					
						
							|  |  |  | 	var topicCount = category.topic_count; | 
					
						
							| 
									
										
										
										
											2017-05-26 16:56:26 -04:00
										 |  |  | 	if (!Array.isArray(category.children) || !category.children.length) { | 
					
						
							| 
									
										
										
										
											2015-05-17 15:53:42 -04:00
										 |  |  | 		category.totalPostCount = postCount; | 
					
						
							|  |  |  | 		category.totalTopicCount = topicCount; | 
					
						
							| 
									
										
										
										
											2017-05-26 16:56:26 -04:00
										 |  |  | 		return; | 
					
						
							| 
									
										
										
										
											2015-04-19 15:18:55 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-26 16:56:26 -04:00
										 |  |  | 	category.children.forEach(function (child) { | 
					
						
							|  |  |  | 		calculateTopicPostCount(child); | 
					
						
							|  |  |  | 		postCount += parseInt(child.totalPostCount, 10) || 0; | 
					
						
							|  |  |  | 		topicCount += parseInt(child.totalTopicCount, 10) || 0; | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	category.totalPostCount = postCount; | 
					
						
							|  |  |  | 	category.totalTopicCount = topicCount; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Categories.getParents = function (cids, callback) { | 
					
						
							|  |  |  | 	var categoriesData; | 
					
						
							|  |  |  | 	var parentCids; | 
					
						
							|  |  |  | 	async.waterfall([ | 
					
						
							|  |  |  | 		function (next) { | 
					
						
							|  |  |  | 			Categories.getCategoriesFields(cids, ['parentCid'], next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function (_categoriesData, next) { | 
					
						
							|  |  |  | 			categoriesData = _categoriesData; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-23 22:28:37 -04:00
										 |  |  | 			parentCids = categoriesData.filter(c => c && c.parentCid).map(c => c.parentCid); | 
					
						
							| 
									
										
										
										
											2015-08-18 14:17:16 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-26 16:56:26 -04:00
										 |  |  | 			if (!parentCids.length) { | 
					
						
							| 
									
										
										
										
											2018-10-23 22:28:37 -04:00
										 |  |  | 				return callback(null, cids.map(() => null)); | 
					
						
							| 
									
										
										
										
											2017-05-26 16:56:26 -04:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2015-08-18 14:17:16 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-26 16:56:26 -04:00
										 |  |  | 			Categories.getCategoriesData(parentCids, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function (parentData, next) { | 
					
						
							| 
									
										
										
										
											2018-11-10 11:24:10 -05:00
										 |  |  | 			const cidToParent = _.zipObject(parentCids, parentData); | 
					
						
							|  |  |  | 			parentData = categoriesData.map(category => cidToParent[category.parentCid]); | 
					
						
							| 
									
										
										
										
											2017-05-26 16:56:26 -04:00
										 |  |  | 			next(null, parentData); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	], callback); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Categories.getChildren = function (cids, uid, callback) { | 
					
						
							| 
									
										
										
										
											2018-11-22 18:21:43 -05:00
										 |  |  | 	var categories; | 
					
						
							|  |  |  | 	async.waterfall([ | 
					
						
							|  |  |  | 		function (next) { | 
					
						
							|  |  |  | 			Categories.getCategoriesFields(cids, ['parentCid'], next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function (categoryData, next) { | 
					
						
							|  |  |  | 			categories = categoryData.map((category, index) => ({ cid: cids[index], parentCid: category.parentCid })); | 
					
						
							|  |  |  | 			async.each(categories, function (category, next) { | 
					
						
							| 
									
										
										
										
											2018-11-25 19:08:38 -05:00
										 |  |  | 				getChildrenTree(category, uid, next); | 
					
						
							| 
									
										
										
										
											2018-11-22 18:21:43 -05:00
										 |  |  | 			}, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function (next) { | 
					
						
							|  |  |  | 			next(null, categories.map(c => c && c.children)); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	], callback); | 
					
						
							| 
									
										
										
										
											2017-05-26 16:56:26 -04:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-25 19:08:38 -05:00
										 |  |  | function getChildrenTree(category, uid, callback) { | 
					
						
							|  |  |  | 	let children; | 
					
						
							| 
									
										
										
										
											2017-05-26 16:56:26 -04:00
										 |  |  | 	async.waterfall([ | 
					
						
							|  |  |  | 		function (next) { | 
					
						
							| 
									
										
										
										
											2018-11-25 19:08:38 -05:00
										 |  |  | 			Categories.getChildrenCids(category.cid, next); | 
					
						
							| 
									
										
										
										
											2017-05-26 16:56:26 -04:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		function (children, next) { | 
					
						
							|  |  |  | 			privileges.categories.filterCids('find', children, uid, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function (children, next) { | 
					
						
							| 
									
										
										
										
											2018-11-22 10:53:44 -05:00
										 |  |  | 			children = children.filter(cid => parseInt(category.cid, 10) !== parseInt(cid, 10)); | 
					
						
							| 
									
										
										
										
											2017-05-26 16:56:26 -04:00
										 |  |  | 			if (!children.length) { | 
					
						
							|  |  |  | 				category.children = []; | 
					
						
							|  |  |  | 				return callback(); | 
					
						
							| 
									
										
										
										
											2015-08-18 14:17:16 -04:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-05-26 16:56:26 -04:00
										 |  |  | 			Categories.getCategoriesData(children, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2018-11-25 19:08:38 -05:00
										 |  |  | 		function (_children, next) { | 
					
						
							|  |  |  | 			children = _children.filter(Boolean); | 
					
						
							| 
									
										
										
										
											2017-11-16 23:40:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-25 19:08:38 -05:00
										 |  |  | 			const cids = children.map(child => child.cid); | 
					
						
							| 
									
										
										
										
											2017-11-16 23:40:39 +00:00
										 |  |  | 			Categories.hasReadCategories(cids, uid, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function (hasRead, next) { | 
					
						
							|  |  |  | 			hasRead.forEach(function (read, i) { | 
					
						
							| 
									
										
										
										
											2018-11-25 19:08:38 -05:00
										 |  |  | 				const child = children[i]; | 
					
						
							| 
									
										
										
										
											2018-10-23 22:28:37 -04:00
										 |  |  | 				child['unread-class'] = (child.topic_count === 0 || (read && uid !== 0)) ? '' : 'unread'; | 
					
						
							| 
									
										
										
										
											2017-11-16 23:40:39 +00:00
										 |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2018-11-25 19:08:38 -05:00
										 |  |  | 			Categories.getTree([category].concat(children), category.parentCid); | 
					
						
							|  |  |  | 			next(); | 
					
						
							| 
									
										
										
										
											2017-05-26 16:56:26 -04:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 	], callback); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-06 13:34:29 -05:00
										 |  |  | Categories.getChildrenCids = function (rootCid, callback) { | 
					
						
							| 
									
										
										
										
											2018-11-27 19:38:28 -05:00
										 |  |  | 	let allCids = []; | 
					
						
							| 
									
										
										
										
											2018-11-25 19:08:38 -05:00
										 |  |  | 	function recursive(keys, callback) { | 
					
						
							|  |  |  | 		db.getSortedSetRange(keys, 0, -1, function (err, childrenCids) { | 
					
						
							| 
									
										
										
										
											2018-11-06 13:34:29 -05:00
										 |  |  | 			if (err) { | 
					
						
							|  |  |  | 				return callback(err); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (!childrenCids.length) { | 
					
						
							|  |  |  | 				return callback(); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2018-11-25 19:08:38 -05:00
										 |  |  | 			const keys = childrenCids.map(cid => 'cid:' + cid + ':children'); | 
					
						
							|  |  |  | 			childrenCids.forEach(cid => allCids.push(parseInt(cid, 10))); | 
					
						
							|  |  |  | 			recursive(keys, callback); | 
					
						
							| 
									
										
										
										
											2018-11-06 13:34:29 -05:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-11-27 19:38:28 -05:00
										 |  |  | 	const key = 'cid:' + rootCid + ':children'; | 
					
						
							|  |  |  | 	const childrenCids = cache.get(key); | 
					
						
							|  |  |  | 	if (childrenCids) { | 
					
						
							|  |  |  | 		return setImmediate(callback, null, childrenCids.slice()); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-11-06 13:35:55 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-27 19:38:28 -05:00
										 |  |  | 	recursive(key, function (err) { | 
					
						
							|  |  |  | 		if (err) { | 
					
						
							|  |  |  | 			return callback(err); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		allCids = _.uniq(allCids); | 
					
						
							|  |  |  | 		cache.set(key, allCids); | 
					
						
							|  |  |  | 		callback(null, allCids.slice()); | 
					
						
							| 
									
										
										
										
											2018-11-06 13:34:29 -05:00
										 |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2018-11-06 13:35:55 -05:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2018-11-06 13:34:29 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-26 16:56:26 -04:00
										 |  |  | Categories.flattenCategories = function (allCategories, categoryData) { | 
					
						
							|  |  |  | 	categoryData.forEach(function (category) { | 
					
						
							|  |  |  | 		if (category) { | 
					
						
							| 
									
										
										
										
											2018-11-25 19:08:38 -05:00
										 |  |  | 			allCategories.push(category); | 
					
						
							| 
									
										
										
										
											2015-05-30 18:44:31 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-26 16:56:26 -04:00
										 |  |  | 			if (Array.isArray(category.children) && category.children.length) { | 
					
						
							|  |  |  | 				Categories.flattenCategories(allCategories, category.children); | 
					
						
							| 
									
										
										
										
											2015-06-04 14:24:33 -04:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-05-26 16:56:26 -04:00
										 |  |  | 	}); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2018-11-27 11:01:46 -05:00
										 |  |  |  * build tree from flat list of categories | 
					
						
							| 
									
										
										
										
											2017-05-26 16:56:26 -04:00
										 |  |  |  * | 
					
						
							|  |  |  |  * @param categories {array} flat list of categories | 
					
						
							|  |  |  |  * @param parentCid {number} start from 0 to build full tree | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | Categories.getTree = function (categories, parentCid) { | 
					
						
							| 
									
										
										
										
											2018-11-27 11:01:46 -05:00
										 |  |  | 	parentCid = parentCid || 0; | 
					
						
							| 
									
										
										
										
											2018-11-29 07:55:56 -05:00
										 |  |  | 	const cids = categories.map(category => category && category.cid); | 
					
						
							| 
									
										
										
										
											2018-11-27 11:01:46 -05:00
										 |  |  | 	const cidToCategory = {}; | 
					
						
							|  |  |  | 	const parents = {}; | 
					
						
							|  |  |  | 	cids.forEach((cid, index) => { | 
					
						
							| 
									
										
										
										
											2018-11-29 07:55:56 -05:00
										 |  |  | 		if (cid) { | 
					
						
							|  |  |  | 			cidToCategory[cid] = categories[index]; | 
					
						
							|  |  |  | 			parents[cid] = _.clone(categories[index]); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-11-27 11:01:46 -05:00
										 |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2018-11-25 19:08:38 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-27 11:01:46 -05:00
										 |  |  | 	const tree = []; | 
					
						
							| 
									
										
										
										
											2015-05-30 18:44:31 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-27 11:01:46 -05:00
										 |  |  | 	categories.forEach(function (category) { | 
					
						
							|  |  |  | 		if (category) { | 
					
						
							|  |  |  | 			category.children = category.children || []; | 
					
						
							|  |  |  | 			if (!category.cid) { | 
					
						
							|  |  |  | 				return; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			if (!category.hasOwnProperty('parentCid') || category.parentCid === null) { | 
					
						
							|  |  |  | 				category.parentCid = 0; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			if (category.parentCid === parentCid) { | 
					
						
							|  |  |  | 				tree.push(category); | 
					
						
							|  |  |  | 				category.parent = parents[parentCid]; | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				const parent = cidToCategory[category.parentCid]; | 
					
						
							|  |  |  | 				if (parent && parent.cid !== category.cid) { | 
					
						
							|  |  |  | 					category.parent = parents[category.parentCid]; | 
					
						
							|  |  |  | 					parent.children = parent.children || []; | 
					
						
							|  |  |  | 					parent.children.push(category); | 
					
						
							| 
									
										
										
										
											2018-11-25 19:08:38 -05:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2018-06-10 10:28:17 -04:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2018-11-27 11:01:46 -05:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 	function sortTree(tree) { | 
					
						
							| 
									
										
										
										
											2018-11-25 19:08:38 -05:00
										 |  |  | 		tree.sort((a, b) => a.order - b.order); | 
					
						
							| 
									
										
										
										
											2018-11-27 11:01:46 -05:00
										 |  |  | 		if (tree.children) { | 
					
						
							|  |  |  | 			sortTree(tree.children); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-11-25 19:08:38 -05:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-11-27 11:01:46 -05:00
										 |  |  | 	sortTree(tree); | 
					
						
							| 
									
										
										
										
											2015-05-30 18:44:31 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-25 19:08:38 -05:00
										 |  |  | 	categories.forEach(c => calculateTopicPostCount(c)); | 
					
						
							| 
									
										
										
										
											2017-05-26 16:56:26 -04:00
										 |  |  | 	return tree; | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2016-09-16 14:20:07 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-30 17:21:30 -04:00
										 |  |  | Categories.buildForSelect = function (uid, privilege, callback) { | 
					
						
							| 
									
										
										
										
											2017-05-30 14:10:12 -04:00
										 |  |  | 	async.waterfall([ | 
					
						
							|  |  |  | 		function (next) { | 
					
						
							| 
									
										
										
										
											2018-11-25 19:08:38 -05:00
										 |  |  | 			Categories.getCategoriesByPrivilege('categories:cid', uid, privilege, next); | 
					
						
							| 
									
										
										
										
											2017-05-30 14:10:12 -04:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		function (categories, next) { | 
					
						
							| 
									
										
										
										
											2018-11-25 19:08:38 -05:00
										 |  |  | 			categories = Categories.getTree(categories); | 
					
						
							| 
									
										
										
										
											2017-05-30 14:10:12 -04:00
										 |  |  | 			Categories.buildForSelectCategories(categories, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	], callback); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Categories.buildForSelectCategories = function (categories, callback) { | 
					
						
							| 
									
										
										
										
											2018-01-05 14:44:18 -05:00
										 |  |  | 	function recursive(category, categoriesData, level, depth) { | 
					
						
							| 
									
										
										
										
											2017-05-26 16:56:26 -04:00
										 |  |  | 		var bullet = level ? '• ' : ''; | 
					
						
							|  |  |  | 		category.value = category.cid; | 
					
						
							| 
									
										
										
										
											2017-05-30 17:21:30 -04:00
										 |  |  | 		category.level = level; | 
					
						
							| 
									
										
										
										
											2017-05-26 16:56:26 -04:00
										 |  |  | 		category.text = level + bullet + category.name; | 
					
						
							| 
									
										
										
										
											2018-01-05 14:44:18 -05:00
										 |  |  | 		category.depth = depth; | 
					
						
							| 
									
										
										
										
											2017-05-26 16:56:26 -04:00
										 |  |  | 		categoriesData.push(category); | 
					
						
							| 
									
										
										
										
											2018-11-25 19:08:38 -05:00
										 |  |  | 		if (Array.isArray(category.children)) { | 
					
						
							|  |  |  | 			category.children.forEach(function (child) { | 
					
						
							|  |  |  | 				recursive(child, categoriesData, '    ' + level, depth + 1); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-05-26 16:56:26 -04:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-09-16 14:20:07 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-30 14:10:12 -04:00
										 |  |  | 	var categoriesData = []; | 
					
						
							| 
									
										
										
										
											2016-09-16 14:20:07 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-23 22:28:37 -04:00
										 |  |  | 	categories = categories.filter(category => category && !category.parentCid); | 
					
						
							| 
									
										
										
										
											2017-05-30 14:10:12 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	categories.forEach(function (category) { | 
					
						
							| 
									
										
										
										
											2018-01-05 14:44:18 -05:00
										 |  |  | 		recursive(category, categoriesData, '', 0); | 
					
						
							| 
									
										
										
										
											2017-05-30 14:10:12 -04:00
										 |  |  | 	}); | 
					
						
							|  |  |  | 	callback(null, categoriesData); | 
					
						
							| 
									
										
										
										
											2017-05-26 16:56:26 -04:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-23 15:03:32 -04:00
										 |  |  | Categories.async = require('../promisify')(Categories); |