2015-09-25 17:38:58 -04:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
var async = require('async');
|
|
|
|
|
var validator = require('validator');
|
|
|
|
|
|
|
|
|
|
var db = require('../database');
|
|
|
|
|
|
2018-10-21 16:47:51 -04:00
|
|
|
const intFields = [
|
|
|
|
|
'cid', 'parentCid', 'disabled', 'isSection', 'order',
|
|
|
|
|
'topic_count', 'post_count',
|
|
|
|
|
];
|
2015-09-25 17:38:58 -04:00
|
|
|
|
2018-10-20 17:07:32 -04:00
|
|
|
module.exports = function (Categories) {
|
2016-10-13 11:43:39 +02:00
|
|
|
Categories.getCategoriesFields = function (cids, fields, callback) {
|
2015-09-25 17:38:58 -04:00
|
|
|
if (!Array.isArray(cids) || !cids.length) {
|
2018-11-22 10:53:44 -05:00
|
|
|
return setImmediate(callback, null, []);
|
2015-09-25 17:38:58 -04:00
|
|
|
}
|
|
|
|
|
|
2017-05-24 15:07:03 -04:00
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
2018-11-22 18:21:43 -05:00
|
|
|
const keys = cids.map(cid => 'category:' + cid);
|
2017-05-24 15:07:03 -04:00
|
|
|
if (fields.length) {
|
|
|
|
|
db.getObjectsFields(keys, fields, next);
|
|
|
|
|
} else {
|
|
|
|
|
db.getObjects(keys, next);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
function (categories, next) {
|
2018-10-25 19:58:01 -04:00
|
|
|
categories.forEach(category => modifyCategory(category, fields));
|
2017-05-24 15:07:03 -04:00
|
|
|
next(null, categories);
|
|
|
|
|
},
|
|
|
|
|
], callback);
|
2015-09-25 17:38:58 -04:00
|
|
|
};
|
|
|
|
|
|
2018-10-20 17:07:32 -04:00
|
|
|
Categories.getCategoryData = function (cid, callback) {
|
|
|
|
|
Categories.getCategoriesFields([cid], [], function (err, categories) {
|
|
|
|
|
callback(err, categories && categories.length ? categories[0] : null);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Categories.getCategoriesData = function (cids, callback) {
|
|
|
|
|
Categories.getCategoriesFields(cids, [], callback);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Categories.getCategoryField = function (cid, field, callback) {
|
|
|
|
|
Categories.getCategoryFields(cid, [field], function (err, category) {
|
|
|
|
|
callback(err, category ? category[field] : null);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Categories.getCategoryFields = function (cid, fields, callback) {
|
|
|
|
|
Categories.getCategoriesFields([cid], fields, function (err, categories) {
|
|
|
|
|
callback(err, categories ? categories[0] : null);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
Categories.getAllCategoryFields = function (fields, callback) {
|
2015-09-25 17:38:58 -04:00
|
|
|
async.waterfall([
|
|
|
|
|
async.apply(db.getSortedSetRange, 'categories:cid', 0, -1),
|
2016-10-13 11:43:39 +02:00
|
|
|
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);
|
|
|
|
|
};
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
Categories.setCategoryField = function (cid, field, value, callback) {
|
2015-09-25 17:38:58 -04:00
|
|
|
db.setObjectField('category:' + cid, field, value, callback);
|
|
|
|
|
};
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
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
|
|
|
};
|
2018-10-20 17:07:32 -04:00
|
|
|
|
2018-10-25 19:58:01 -04:00
|
|
|
function modifyCategory(category, fields) {
|
2018-10-20 17:07:32 -04:00
|
|
|
if (!category) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-25 19:58:01 -04:00
|
|
|
db.parseIntFields(category, intFields, fields);
|
2018-10-20 17:07:32 -04:00
|
|
|
|
|
|
|
|
if (category.hasOwnProperty('name')) {
|
|
|
|
|
category.name = validator.escape(String(category.name || ''));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (category.hasOwnProperty('icon')) {
|
|
|
|
|
category.icon = category.icon || 'hidden';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (category.hasOwnProperty('post_count')) {
|
|
|
|
|
category.totalPostCount = category.post_count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (category.hasOwnProperty('topic_count')) {
|
|
|
|
|
category.totalTopicCount = category.topic_count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (category.image) {
|
|
|
|
|
category.backgroundImage = category.image;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (category.description) {
|
|
|
|
|
category.description = validator.escape(String(category.description));
|
|
|
|
|
category.descriptionParsed = category.descriptionParsed || category.description;
|
|
|
|
|
}
|
|
|
|
|
}
|