| 
									
										
										
										
											2014-11-15 23:22:57 -05:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-05 20:24:03 +03:00
										 |  |  | var nconf = require('nconf'); | 
					
						
							|  |  |  | var async = require('async'); | 
					
						
							|  |  |  | var validator = require('validator'); | 
					
						
							| 
									
										
										
										
											2016-07-11 13:03:47 +03:00
										 |  |  | var winston = require('winston'); | 
					
						
							| 
									
										
										
										
											2018-06-18 14:37:32 -04:00
										 |  |  | var querystring = require('querystring'); | 
					
						
							| 
									
										
										
										
											2014-12-11 22:55:00 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-03 13:06:21 +03:00
										 |  |  | var user = require('../user'); | 
					
						
							|  |  |  | var privileges = require('../privileges'); | 
					
						
							| 
									
										
										
										
											2016-05-05 20:24:03 +03:00
										 |  |  | var categories = require('../categories'); | 
					
						
							|  |  |  | var plugins = require('../plugins'); | 
					
						
							|  |  |  | var meta = require('../meta'); | 
					
						
							| 
									
										
										
										
											2017-05-12 17:53:23 -04:00
										 |  |  | var middleware = require('../middleware'); | 
					
						
							| 
									
										
										
										
											2014-11-15 23:22:57 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-12 17:53:23 -04:00
										 |  |  | var helpers = module.exports; | 
					
						
							| 
									
										
										
										
											2014-11-15 23:22:57 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-20 08:51:04 -04:00
										 |  |  | helpers.noScriptErrors = function (req, res, error, httpStatus) { | 
					
						
							|  |  |  | 	if (req.body.noscript !== 'true') { | 
					
						
							|  |  |  | 		return res.status(httpStatus).send(error); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var middleware = require('../middleware'); | 
					
						
							|  |  |  | 	var httpStatusString = httpStatus.toString(); | 
					
						
							|  |  |  | 	middleware.buildHeader(req, res, function () { | 
					
						
							|  |  |  | 		res.status(httpStatus).render(httpStatusString, { | 
					
						
							|  |  |  | 			path: req.path, | 
					
						
							| 
									
										
										
										
											2018-01-31 15:20:17 -05:00
										 |  |  | 			loggedIn: req.loggedIn, | 
					
						
							| 
									
										
										
										
											2017-07-20 08:51:04 -04:00
										 |  |  | 			error: error, | 
					
						
							| 
									
										
										
										
											2017-09-11 15:37:18 -04:00
										 |  |  | 			returnLink: true, | 
					
						
							| 
									
										
										
										
											2017-07-20 08:51:04 -04:00
										 |  |  | 			title: '[[global:' + httpStatusString + '.title]]', | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-19 13:53:05 -04:00
										 |  |  | helpers.validFilters = { '': true, new: true, watched: true, unreplied: true }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-18 14:37:32 -04:00
										 |  |  | helpers.terms = { | 
					
						
							|  |  |  | 	daily: 'day', | 
					
						
							|  |  |  | 	weekly: 'week', | 
					
						
							|  |  |  | 	monthly: 'month', | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | helpers.buildQueryString = function (cid, filter, term) { | 
					
						
							|  |  |  | 	var qs = {}; | 
					
						
							|  |  |  | 	if (cid) { | 
					
						
							|  |  |  | 		qs.cid = cid; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (filter) { | 
					
						
							|  |  |  | 		qs.filter = filter; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (term) { | 
					
						
							|  |  |  | 		qs.term = term; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (Object.keys(qs).length) { | 
					
						
							|  |  |  | 		return '?' + querystring.stringify(qs); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return ''; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | helpers.buildFilters = function (url, filter, query) { | 
					
						
							| 
									
										
										
										
											2017-10-19 13:53:05 -04:00
										 |  |  | 	return [{ | 
					
						
							|  |  |  | 		name: '[[unread:all-topics]]', | 
					
						
							| 
									
										
										
										
											2018-06-18 14:37:32 -04:00
										 |  |  | 		url: url + helpers.buildQueryString(query.cid, '', query.term), | 
					
						
							| 
									
										
										
										
											2017-10-19 13:53:05 -04:00
										 |  |  | 		selected: filter === '', | 
					
						
							|  |  |  | 		filter: '', | 
					
						
							|  |  |  | 	}, { | 
					
						
							|  |  |  | 		name: '[[unread:new-topics]]', | 
					
						
							| 
									
										
										
										
											2018-06-18 14:37:32 -04:00
										 |  |  | 		url: url + helpers.buildQueryString(query.cid, 'new', query.term), | 
					
						
							| 
									
										
										
										
											2017-10-19 13:53:05 -04:00
										 |  |  | 		selected: filter === 'new', | 
					
						
							|  |  |  | 		filter: 'new', | 
					
						
							|  |  |  | 	}, { | 
					
						
							|  |  |  | 		name: '[[unread:watched-topics]]', | 
					
						
							| 
									
										
										
										
											2018-06-18 14:37:32 -04:00
										 |  |  | 		url: url + helpers.buildQueryString(query.cid, 'watched', query.term), | 
					
						
							| 
									
										
										
										
											2017-10-19 13:53:05 -04:00
										 |  |  | 		selected: filter === 'watched', | 
					
						
							|  |  |  | 		filter: 'watched', | 
					
						
							|  |  |  | 	}, { | 
					
						
							|  |  |  | 		name: '[[unread:unreplied-topics]]', | 
					
						
							| 
									
										
										
										
											2018-06-18 14:37:32 -04:00
										 |  |  | 		url: url + helpers.buildQueryString(query.cid, 'unreplied', query.term), | 
					
						
							| 
									
										
										
										
											2017-10-19 13:53:05 -04:00
										 |  |  | 		selected: filter === 'unreplied', | 
					
						
							|  |  |  | 		filter: 'unreplied', | 
					
						
							|  |  |  | 	}]; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-18 14:37:32 -04:00
										 |  |  | helpers.buildTerms = function (url, term, query) { | 
					
						
							|  |  |  | 	return [{ | 
					
						
							|  |  |  | 		name: '[[recent:alltime]]', | 
					
						
							|  |  |  | 		url: url + helpers.buildQueryString(query.cid, query.filter, ''), | 
					
						
							|  |  |  | 		selected: term === 'alltime', | 
					
						
							|  |  |  | 		term: 'alltime', | 
					
						
							|  |  |  | 	}, { | 
					
						
							|  |  |  | 		name: '[[recent:day]]', | 
					
						
							|  |  |  | 		url: url + helpers.buildQueryString(query.cid, query.filter, 'daily'), | 
					
						
							|  |  |  | 		selected: term === 'day', | 
					
						
							|  |  |  | 		term: 'day', | 
					
						
							|  |  |  | 	}, { | 
					
						
							|  |  |  | 		name: '[[recent:week]]', | 
					
						
							|  |  |  | 		url: url + helpers.buildQueryString(query.cid, query.filter, 'weekly'), | 
					
						
							|  |  |  | 		selected: term === 'week', | 
					
						
							|  |  |  | 		term: 'week', | 
					
						
							|  |  |  | 	}, { | 
					
						
							|  |  |  | 		name: '[[recent:month]]', | 
					
						
							|  |  |  | 		url: url + helpers.buildQueryString(query.cid, query.filter, 'monthly'), | 
					
						
							|  |  |  | 		selected: term === 'month', | 
					
						
							|  |  |  | 		term: 'month', | 
					
						
							|  |  |  | 	}]; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | helpers.notAllowed = function (req, res, error) { | 
					
						
							| 
									
										
										
										
											2016-06-22 14:58:46 -04:00
										 |  |  | 	plugins.fireHook('filter:helpers.notAllowed', { | 
					
						
							|  |  |  | 		req: req, | 
					
						
							|  |  |  | 		res: res, | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 		error: error, | 
					
						
							| 
									
										
										
										
											2017-02-18 15:05:36 -07:00
										 |  |  | 	}, function (err) { | 
					
						
							| 
									
										
										
										
											2016-07-11 13:03:47 +03:00
										 |  |  | 		if (err) { | 
					
						
							|  |  |  | 			return winston.error(err); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-01-31 15:20:17 -05:00
										 |  |  | 		if (req.loggedIn) { | 
					
						
							| 
									
										
										
										
											2016-06-22 14:58:46 -04:00
										 |  |  | 			if (res.locals.isAPI) { | 
					
						
							|  |  |  | 				res.status(403).json({ | 
					
						
							|  |  |  | 					path: req.path.replace(/^\/api/, ''), | 
					
						
							| 
									
										
										
										
											2018-01-31 15:20:17 -05:00
										 |  |  | 					loggedIn: req.loggedIn, | 
					
						
							| 
									
										
										
										
											2016-12-02 14:05:54 +03:00
										 |  |  | 					error: error, | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 					title: '[[global:403.title]]', | 
					
						
							| 
									
										
										
										
											2016-06-22 14:58:46 -04:00
										 |  |  | 				}); | 
					
						
							|  |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2017-05-12 17:53:23 -04:00
										 |  |  | 				middleware.buildHeader(req, res, function () { | 
					
						
							|  |  |  | 					res.status(403).render('403', { | 
					
						
							|  |  |  | 						path: req.path, | 
					
						
							| 
									
										
										
										
											2018-01-31 15:20:17 -05:00
										 |  |  | 						loggedIn: req.loggedIn, | 
					
						
							| 
									
										
										
										
											2017-05-12 17:53:23 -04:00
										 |  |  | 						error: error, | 
					
						
							|  |  |  | 						title: '[[global:403.title]]', | 
					
						
							|  |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2016-06-22 14:58:46 -04:00
										 |  |  | 				}); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-02-18 14:32:35 -07:00
										 |  |  | 		} else if (res.locals.isAPI) { | 
					
						
							|  |  |  | 			req.session.returnTo = nconf.get('relative_path') + req.url.replace(/^\/api/, ''); | 
					
						
							|  |  |  | 			res.status(401).json('not-authorized'); | 
					
						
							| 
									
										
										
										
											2014-11-15 23:22:57 -05:00
										 |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2017-02-18 14:32:35 -07:00
										 |  |  | 			req.session.returnTo = nconf.get('relative_path') + req.url; | 
					
						
							|  |  |  | 			res.redirect(nconf.get('relative_path') + '/login'); | 
					
						
							| 
									
										
										
										
											2014-11-15 23:22:57 -05:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-06-22 14:58:46 -04:00
										 |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2014-11-15 23:22:57 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | helpers.redirect = function (res, url) { | 
					
						
							| 
									
										
										
										
											2015-03-09 18:22:44 -04:00
										 |  |  | 	if (res.locals.isAPI) { | 
					
						
							| 
									
										
										
										
											2017-07-05 11:36:35 -04:00
										 |  |  | 		res.set('X-Redirect', encodeURI(url)).status(200).json(url); | 
					
						
							| 
									
										
										
										
											2015-03-09 18:22:44 -04:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2016-03-20 15:11:32 -05:00
										 |  |  | 		res.redirect(nconf.get('relative_path') + encodeURI(url)); | 
					
						
							| 
									
										
										
										
											2015-03-09 18:22:44 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | helpers.buildCategoryBreadcrumbs = function (cid, callback) { | 
					
						
							| 
									
										
										
										
											2014-12-11 22:55:00 -05:00
										 |  |  | 	var breadcrumbs = []; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	async.whilst(function () { | 
					
						
							| 
									
										
										
										
											2014-12-11 22:55:00 -05:00
										 |  |  | 		return parseInt(cid, 10); | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	}, function (next) { | 
					
						
							| 
									
										
										
										
											2017-05-29 17:12:02 -04:00
										 |  |  | 		categories.getCategoryFields(cid, ['name', 'slug', 'parentCid', 'disabled', 'isSection'], function (err, data) { | 
					
						
							| 
									
										
										
										
											2014-12-11 22:55:00 -05:00
										 |  |  | 			if (err) { | 
					
						
							|  |  |  | 				return next(err); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-29 17:12:02 -04:00
										 |  |  | 			if (!parseInt(data.disabled, 10) && !parseInt(data.isSection, 10)) { | 
					
						
							| 
									
										
										
										
											2015-05-27 19:08:59 -04:00
										 |  |  | 				breadcrumbs.unshift({ | 
					
						
							| 
									
										
										
										
											2016-08-27 15:45:15 +03:00
										 |  |  | 					text: validator.escape(String(data.name)), | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 					url: nconf.get('relative_path') + '/category/' + data.slug, | 
					
						
							| 
									
										
										
										
											2015-05-27 19:08:59 -04:00
										 |  |  | 				}); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-12-11 22:55:00 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			cid = data.parentCid; | 
					
						
							|  |  |  | 			next(); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	}, function (err) { | 
					
						
							| 
									
										
										
										
											2014-12-11 22:55:00 -05:00
										 |  |  | 		if (err) { | 
					
						
							|  |  |  | 			return callback(err); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-22 11:39:39 -05:00
										 |  |  | 		if (meta.config.homePageRoute && meta.config.homePageRoute !== 'categories') { | 
					
						
							| 
									
										
										
										
											2016-04-21 11:40:40 -04:00
										 |  |  | 			breadcrumbs.unshift({ | 
					
						
							|  |  |  | 				text: '[[global:header.categories]]', | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 				url: nconf.get('relative_path') + '/categories', | 
					
						
							| 
									
										
										
										
											2016-04-21 11:40:40 -04:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-29 01:06:48 -05:00
										 |  |  | 		breadcrumbs.unshift({ | 
					
						
							|  |  |  | 			text: '[[global:home]]', | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 			url: nconf.get('relative_path') + '/', | 
					
						
							| 
									
										
										
										
											2014-12-11 22:55:00 -05:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2015-01-29 01:06:48 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		callback(null, breadcrumbs); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | helpers.buildBreadcrumbs = function (crumbs) { | 
					
						
							| 
									
										
										
										
											2015-01-29 01:06:48 -05:00
										 |  |  | 	var breadcrumbs = [ | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			text: '[[global:home]]', | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 			url: nconf.get('relative_path') + '/', | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2015-01-29 01:06:48 -05:00
										 |  |  | 	]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	crumbs.forEach(function (crumb) { | 
					
						
							| 
									
										
										
										
											2015-01-29 01:06:48 -05:00
										 |  |  | 		if (crumb) { | 
					
						
							|  |  |  | 			if (crumb.url) { | 
					
						
							|  |  |  | 				crumb.url = nconf.get('relative_path') + crumb.url; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			breadcrumbs.push(crumb); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-12-11 22:55:00 -05:00
										 |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2015-01-29 01:06:48 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return breadcrumbs; | 
					
						
							| 
									
										
										
										
											2014-12-11 22:55:00 -05:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2014-11-15 23:22:57 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | helpers.buildTitle = function (pageTitle) { | 
					
						
							| 
									
										
										
										
											2015-09-23 01:59:13 -04:00
										 |  |  | 	var titleLayout = meta.config.titleLayout || '{pageTitle} | {browserTitle}'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-27 15:45:15 +03:00
										 |  |  | 	var browserTitle = validator.escape(String(meta.config.browserTitle || meta.config.title || 'NodeBB')); | 
					
						
							| 
									
										
										
										
											2015-09-23 01:59:13 -04:00
										 |  |  | 	pageTitle = pageTitle || ''; | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	var title = titleLayout.replace('{pageTitle}', function () { | 
					
						
							| 
									
										
										
										
											2016-03-22 11:19:33 +02:00
										 |  |  | 		return pageTitle; | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	}).replace('{browserTitle}', function () { | 
					
						
							| 
									
										
										
										
											2016-03-22 11:19:33 +02:00
										 |  |  | 		return browserTitle; | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2015-09-23 01:59:13 -04:00
										 |  |  | 	return title; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-03 13:25:29 +03:00
										 |  |  | helpers.getWatchedCategories = function (uid, selectedCid, callback) { | 
					
						
							| 
									
										
										
										
											2017-10-30 15:26:12 -04:00
										 |  |  | 	if (selectedCid && !Array.isArray(selectedCid)) { | 
					
						
							|  |  |  | 		selectedCid = [selectedCid]; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-11-03 13:06:21 +03:00
										 |  |  | 	async.waterfall([ | 
					
						
							|  |  |  | 		function (next) { | 
					
						
							|  |  |  | 			user.getWatchedCategories(uid, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function (cids, next) { | 
					
						
							|  |  |  | 			privileges.categories.filterCids('read', cids, uid, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function (cids, next) { | 
					
						
							|  |  |  | 			categories.getCategoriesFields(cids, ['cid', 'name', 'slug', 'icon', 'link', 'color', 'bgColor', 'parentCid'], next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function (categoryData, next) { | 
					
						
							|  |  |  | 			categoryData = categoryData.filter(function (category) { | 
					
						
							|  |  |  | 				return category && !category.link; | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2017-10-30 15:26:12 -04:00
										 |  |  | 			var selectedCategory = []; | 
					
						
							|  |  |  | 			var selectedCids = []; | 
					
						
							| 
									
										
										
										
											2016-11-03 13:06:21 +03:00
										 |  |  | 			categoryData.forEach(function (category) { | 
					
						
							| 
									
										
										
										
											2017-10-30 15:26:12 -04:00
										 |  |  | 				category.selected = selectedCid ? selectedCid.indexOf(String(category.cid)) !== -1 : false; | 
					
						
							| 
									
										
										
										
											2016-11-03 13:06:21 +03:00
										 |  |  | 				if (category.selected) { | 
					
						
							| 
									
										
										
										
											2017-10-30 15:26:12 -04:00
										 |  |  | 					selectedCategory.push(category); | 
					
						
							|  |  |  | 					selectedCids.push(parseInt(category.cid, 10)); | 
					
						
							| 
									
										
										
										
											2016-11-03 13:06:21 +03:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2017-10-30 15:26:12 -04:00
										 |  |  | 			selectedCids.sort(function (a, b) { | 
					
						
							|  |  |  | 				return a - b; | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (selectedCategory.length > 1) { | 
					
						
							|  |  |  | 				selectedCategory = { | 
					
						
							|  |  |  | 					icon: 'fa-plus', | 
					
						
							|  |  |  | 					name: '[[unread:multiple-categories-selected]]', | 
					
						
							|  |  |  | 					bgColor: '#ddd', | 
					
						
							|  |  |  | 				}; | 
					
						
							|  |  |  | 			} else if (selectedCategory.length === 1) { | 
					
						
							|  |  |  | 				selectedCategory = selectedCategory[0]; | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				selectedCategory = undefined; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2016-11-03 13:06:21 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			var categoriesData = []; | 
					
						
							|  |  |  | 			var tree = categories.getTree(categoryData, 0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			tree.forEach(function (category) { | 
					
						
							|  |  |  | 				recursive(category, categoriesData, ''); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-30 15:26:12 -04:00
										 |  |  | 			next(null, { categories: categoriesData, selectedCategory: selectedCategory, selectedCids: selectedCids }); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2016-11-03 13:06:21 +03:00
										 |  |  | 	], callback); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function recursive(category, categoriesData, level) { | 
					
						
							|  |  |  | 	category.level = level; | 
					
						
							|  |  |  | 	categoriesData.push(category); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	category.children.forEach(function (child) { | 
					
						
							|  |  |  | 		recursive(child, categoriesData, '    ' + level); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | } |