You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
3.1 KiB
JavaScript

10 years ago
'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) {
8 years ago
async.waterfall([
function (next) {
db.getObject('category:' + cid, next);
},
function (category, next) {
modifyCategory(category);
next(null, category);
},
], callback);
10 years ago
};
Categories.getCategoriesData = function (cids, callback) {
8 years ago
Categories.getCategoriesFields(cids, [], callback);
10 years ago
};
function modifyCategory(category) {
10 years ago
if (!category) {
return;
10 years ago
}
category.name = validator.escape(String(category.name || ''));
10 years ago
category.disabled = category.hasOwnProperty('disabled') ? parseInt(category.disabled, 10) === 1 : undefined;
8 years ago
category.isSection = category.hasOwnProperty('isSection') ? parseInt(category.isSection, 10) === 1 : undefined;
10 years ago
category.icon = category.icon || 'hidden';
if (category.hasOwnProperty('post_count')) {
category.post_count = category.post_count || 0;
category.totalPostCount = category.post_count;
10 years ago
}
if (category.hasOwnProperty('topic_count')) {
category.topic_count = category.topic_count || 0;
category.totalTopicCount = category.topic_count;
10 years ago
}
if (category.image) {
category.backgroundImage = category.image;
}
if (category.description) {
category.description = validator.escape(String(category.description));
category.descriptionParsed = category.descriptionParsed || category.description;
10 years ago
}
}
Categories.getCategoryField = function (cid, field, callback) {
10 years ago
db.getObjectField('category:' + cid, field, callback);
};
Categories.getCategoriesFields = function (cids, fields, callback) {
10 years ago
if (!Array.isArray(cids) || !cids.length) {
return callback(null, []);
}
var keys = cids.map(function (cid) {
10 years ago
return 'category:' + cid;
});
8 years ago
async.waterfall([
function (next) {
if (fields.length) {
db.getObjectsFields(keys, fields, next);
} else {
db.getObjects(keys, next);
}
},
function (categories, next) {
categories.forEach(modifyCategory);
next(null, categories);
},
], callback);
10 years ago
};
Categories.getMultipleCategoryFields = function (cids, fields, callback) {
10 years ago
winston.warn('[deprecated] Categories.getMultipleCategoryFields is deprecated please use Categories.getCategoriesFields');
Categories.getCategoriesFields(cids, fields, callback);
};
Categories.getAllCategoryFields = function (fields, callback) {
10 years ago
async.waterfall([
async.apply(db.getSortedSetRange, 'categories:cid', 0, -1),
function (cids, next) {
10 years ago
Categories.getCategoriesFields(cids, fields, next);
},
10 years ago
], callback);
};
Categories.getCategoryFields = function (cid, fields, callback) {
10 years ago
db.getObjectFields('category:' + cid, fields, callback);
};
Categories.setCategoryField = function (cid, field, value, callback) {
10 years ago
db.setObjectField('category:' + cid, field, value, callback);
};
Categories.incrementCategoryFieldBy = function (cid, field, value, callback) {
10 years ago
db.incrObjectFieldBy('category:' + cid, field, value, callback);
};
8 years ago
};