Files
NodeBB/src/categories/data.js

114 lines
3.1 KiB
JavaScript
Raw Normal View History

2015-09-25 17:38:58 -04:00
'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) {
2015-10-27 17:21:28 -04:00
if (err) {
return callback(err);
}
modifyCategory(category);
callback(null, category);
2015-09-25 17:38:58 -04:00
});
};
Categories.getCategoriesData = function (cids, callback) {
2015-09-25 17:38:58 -04:00
if (!Array.isArray(cids) || !cids.length) {
return callback(null, []);
}
var keys = cids.map(function (cid) {
2015-09-25 17:38:58 -04:00
return 'category:' + cid;
});
db.getObjects(keys, function (err, categories) {
2015-09-25 17:38:58 -04:00
if (err || !Array.isArray(categories) || !categories.length) {
return callback(err, []);
}
2015-10-27 17:21:28 -04:00
categories.forEach(modifyCategory);
callback(null, categories);
2015-09-25 17:38:58 -04:00
});
};
2015-10-27 17:21:28 -04:00
function modifyCategory(category) {
2015-09-25 17:38:58 -04:00
if (!category) {
2015-10-27 17:21:28 -04:00
return;
2015-09-25 17:38:58 -04:00
}
category.name = validator.escape(String(category.name || ''));
2015-09-25 17:38:58 -04:00
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;
}
if (category.image) {
category.backgroundImage = category.image;
}
if (category.description) {
category.description = validator.escape(String(category.description));
category.descriptionParsed = category.descriptionParsed || category.description;
2015-09-25 17:38:58 -04:00
}
}
Categories.getCategoryField = function (cid, field, callback) {
2015-09-25 17:38:58 -04:00
db.getObjectField('category:' + cid, field, callback);
};
Categories.getCategoriesFields = function (cids, fields, callback) {
2015-09-25 17:38:58 -04:00
if (!Array.isArray(cids) || !cids.length) {
return callback(null, []);
}
var keys = cids.map(function (cid) {
2015-09-25 17:38:58 -04:00
return 'category:' + cid;
});
db.getObjectsFields(keys, fields, function (err, categories) {
2015-09-25 17:38:58 -04:00
if (err) {
return callback(err);
}
2015-10-27 17:21:28 -04:00
categories.forEach(modifyCategory);
callback(null, categories);
2015-09-25 17:38:58 -04:00
});
};
Categories.getMultipleCategoryFields = function (cids, fields, callback) {
2015-09-25 17:38:58 -04:00
winston.warn('[deprecated] Categories.getMultipleCategoryFields is deprecated please use Categories.getCategoriesFields');
Categories.getCategoriesFields(cids, fields, callback);
};
Categories.getAllCategoryFields = function (fields, callback) {
2015-09-25 17:38:58 -04:00
async.waterfall([
async.apply(db.getSortedSetRange, 'categories:cid', 0, -1),
function (cids, next) {
2015-09-25 17:38:58 -04:00
Categories.getCategoriesFields(cids, fields, next);
2017-02-17 19:31:21 -07:00
},
2015-09-25 17:38:58 -04:00
], callback);
};
Categories.getCategoryFields = function (cid, fields, callback) {
2015-09-25 17:38:58 -04:00
db.getObjectFields('category:' + cid, fields, callback);
};
Categories.setCategoryField = function (cid, field, value, callback) {
2015-09-25 17:38:58 -04:00
db.setObjectField('category:' + cid, field, value, callback);
};
Categories.incrementCategoryFieldBy = function (cid, field, value, callback) {
2015-09-25 17:38:58 -04:00
db.incrObjectFieldBy('category:' + cid, field, value, callback);
};
2017-02-18 02:30:48 -07:00
};