all files / src/controllers/ categories.js

29.03% Statements 9/31
0% Branches 0/19
0% Functions 0/5
29.03% Lines 9/31
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81                                                                                                                                               
"use strict";
 
var async = require('async');
var nconf = require('nconf');
var validator = require('validator');
 
var categories = require('../categories');
var meta = require('../meta');
var helpers = require('./helpers');
 
var categoriesController = {};
 
categoriesController.list = function(req, res, next) {
	res.locals.metaTags = [{
		name: "title",
		content: validator.escape(meta.config.title || 'NodeBB')
	}, {
		name: "description",
		content: validator.escape(meta.config.description || '')
	}, {
		property: 'og:title',
		content: '[[pages:categories]]'
	}, {
		property: 'og:type',
		content: 'website'
	}];
 
	if (meta.config['brand:logo']) {
		var brandLogo = meta.config['brand:logo'];
		if (!brandLogo.startsWith('http')) {
			brandLogo = nconf.get('url') + brandLogo;
		}
		res.locals.metaTags.push({
			property: 'og:image',
			content: brandLogo
		});
	}
 
	var categoryData;
	async.waterfall([
		function (next) {
			categories.getCategoriesByPrivilege('cid:0:children', req.uid, 'find', next);
		},
		function (_categoryData, next) {
			categoryData = _categoryData;
 
			var allCategories = [];
			categories.flattenCategories(allCategories, categoryData);
 
			categories.getRecentTopicReplies(allCategories, req.uid, next);
		}
	], function(err) {
		if (err) {
			return next(err);
		}
 
		var data = {
			title: '[[pages:categories]]',
			categories: categoryData
		};
 
		if (req.path.startsWith('/api/categories') || req.path.startsWith('/categories')) {
			data.breadcrumbs = helpers.buildBreadcrumbs([{text: data.title}]);
		}
 
		data.categories.forEach(function(category) {
			if (category && Array.isArray(category.posts) && category.posts.length) {
				category.teaser = {
					url: nconf.get('relative_path') + '/topic/' + category.posts[0].topic.slug + '/' + category.posts[0].index,
					timestampISO: category.posts[0].timestampISO,
					pid: category.posts[0].pid
				};
			}
		});
 
		res.render('categories', data);
	});
};
 
module.exports = categoriesController;