all files / src/categories/ data.js

71.67% Statements 43/60
76.92% Branches 30/39
58.82% Functions 10/17
71.67% Lines 43/60
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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115                                                  11×       11× 11× 11× 11×     11×     11×       11×       27×     10×       10× 10×     10× 10×       10× 10×                                                
'use strict';
 
var async = require('async');
var validator = require('validator');
var winston = require('winston');
 
var db = require('../database');
 
module.exports = function(Categories) {
 
	Categories.getCategoryData = function(cid, callback) {
		db.getObject('category:' + cid, function(err, category) {
			if (err) {
				return callback(err);
			}
 
			modifyCategory(category);
			callback(null, category);
		});
	};
 
	Categories.getCategoriesData = function(cids, callback) {
		Iif (!Array.isArray(cids) || !cids.length) {
			return callback(null, []);
		}
		var keys = cids.map(function(cid) {
			return 'category:' + cid;
		});
 
		db.getObjects(keys, function(err, categories) {
			Iif (err || !Array.isArray(categories) || !categories.length) {
				return callback(err, []);
			}
 
			categories.forEach(modifyCategory);
			callback(null, categories);
		});
	};
 
	function modifyCategory(category) {
		Iif (!category) {
			return;
		}
 
		category.name = validator.escape(category.name || '');
		category.disabled = category.hasOwnProperty('disabled') ? parseInt(category.disabled, 10) === 1 : undefined;
		category.icon = category.icon || 'hidden';
		if (category.hasOwnProperty('post_count')) {
			category.post_count = category.totalPostCount = category.post_count || 0;
		}
 
		if (category.hasOwnProperty('topic_count')) {
			category.topic_count = category.totalTopicCount = category.topic_count || 0;
		}
 
		Iif (category.image) {
			category.backgroundImage = category.image;
		}
 
		if (category.description) {
			category.description = validator.escape(category.description);
			category.descriptionParsed = category.descriptionParsed || category.description;
		}
	}
 
	Categories.getCategoryField = function(cid, field, callback) {
		db.getObjectField('category:' + cid, field, callback);
	};
 
	Categories.getCategoriesFields = function(cids, fields, callback) {
		Iif (!Array.isArray(cids) || !cids.length) {
			return callback(null, []);
		}
 
		var keys = cids.map(function(cid) {
			return 'category:' + cid;
		});
 
		db.getObjectsFields(keys, fields, function(err, categories) {
			Iif (err) {
				return callback(err);
			}
 
			categories.forEach(modifyCategory);
			callback(null, categories);
		});
	};
 
	Categories.getMultipleCategoryFields = function(cids, fields, callback) {
		winston.warn('[deprecated] Categories.getMultipleCategoryFields is deprecated please use Categories.getCategoriesFields');
		Categories.getCategoriesFields(cids, fields, callback);
	};
 
	Categories.getAllCategoryFields = function(fields, callback) {
		async.waterfall([
			async.apply(db.getSortedSetRange, 'categories:cid', 0, -1),
			function(cids, next) {
				Categories.getCategoriesFields(cids, fields, next);
			}
		], callback);
	};
 
	Categories.getCategoryFields = function(cid, fields, callback) {
		db.getObjectFields('category:' + cid, fields, callback);
	};
 
	Categories.setCategoryField = function(cid, field, value, callback) {
		db.setObjectField('category:' + cid, field, value, callback);
	};
 
	Categories.incrementCategoryFieldBy = function(cid, field, value, callback) {
		db.incrObjectFieldBy('category:' + cid, field, value, callback);
	};
 
};