v1.18.x
barisusakli 11 years ago
parent 5a42b6a7ea
commit 95c839579e

@ -67,12 +67,13 @@ var db = require('./database'),
}; };
Categories.getCategoryById = function(cid, start, end, uid, callback) { Categories.getCategoryById = function(cid, start, end, uid, callback) {
Categories.getCategoryData(cid, function(err, category) { Categories.getCategories([cid], uid, function(err, categories) {
if(err || !category) { if (err || !Array.isArray(categories) || !categories[0]) {
return callback(err || new Error('[[error:invalid-cid]]')); return callback(err || new Error('[[error:invalid-cid]]'));
} }
var category = categories[0];
if(parseInt(uid, 10)) { if (parseInt(uid, 10)) {
Categories.markAsRead(cid, uid); Categories.markAsRead(cid, uid);
} }
@ -174,7 +175,7 @@ var db = require('./database'),
}); });
}; };
Categories.getAllCategories = function(callback) { Categories.getAllCategories = function(uid, callback) {
db.getSortedSetRange('categories:cid', 0, -1, function(err, cids) { db.getSortedSetRange('categories:cid', 0, -1, function(err, cids) {
if (err) { if (err) {
return callback(err); return callback(err);
@ -184,7 +185,7 @@ var db = require('./database'),
return callback(null, []); return callback(null, []);
} }
Categories.getCategoriesData(cids, callback); Categories.getCategories(cids, uid, callback);
}); });
}; };
@ -257,25 +258,13 @@ var db = require('./database'),
db.isSetMember('cid:' + cid + ':read_by_uid', uid, callback); db.isSetMember('cid:' + cid + ':read_by_uid', uid, callback);
}; };
Categories.getCategoryData = function(cid, expandFamily, callback) { Categories.getCategoryData = function(cid, callback) {
// expandFamily is optional, default: true Categories.getCategoriesData([cid], function(err, categories) {
if (typeof expandFamily === 'function' && !callback) {
callback = expandFamily;
expandFamily = true;
}
Categories.getCategoriesData([cid], expandFamily, function(err, categories) {
callback(err, categories ? categories[0] : null); callback(err, categories ? categories[0] : null);
}); });
}; };
Categories.getCategoriesData = function(cids, expandFamily, callback) { Categories.getCategoriesData = function(cids, callback) {
// expandFamily is optional, default: true
if (typeof expandFamily === 'function' && !callback) {
callback = expandFamily;
expandFamily = true;
}
var keys = cids.map(function(cid) { var keys = cids.map(function(cid) {
return 'category:' + cid; return 'category:' + cid;
}); });
@ -298,21 +287,7 @@ var db = require('./database'),
category.backgroundImage = category.image ? nconf.get('relative_path') + category.image : ''; category.backgroundImage = category.image ? nconf.get('relative_path') + category.image : '';
category.disabled = category.disabled ? parseInt(category.disabled, 10) !== 0 : false; category.disabled = category.disabled ? parseInt(category.disabled, 10) !== 0 : false;
if (expandFamily !== false) {
async.parallel({
children: async.apply(Categories.getChildren, category.cid),
parent: async.apply(Categories.getCategoryData, category.parentCid, false)
}, function(err, data) {
if (!err) {
category.parent = data.parent;
category.children = data.children;
}
next(null, category); next(null, category);
});
} else {
next(null, category);
}
}, callback); }, callback);
}); });
}; };
@ -353,6 +328,12 @@ var db = require('./database'),
categories: function(next) { categories: function(next) {
Categories.getCategoriesData(cids, next); Categories.getCategoriesData(cids, next);
}, },
children: function(next) {
Categories.getChildren(cids, uid, next);
},
parents: function(next) {
Categories.getParents(cids, next);
},
hasRead: function(next) { hasRead: function(next) {
Categories.hasReadCategories(cids, uid, next); Categories.hasReadCategories(cids, uid, next);
} }
@ -366,27 +347,47 @@ var db = require('./database'),
uid = parseInt(uid, 10); uid = parseInt(uid, 10);
for(var i=0; i<results.categories.length; ++i) { for(var i=0; i<results.categories.length; ++i) {
categories[i]['unread-class'] = (parseInt(categories[i].topic_count, 10) === 0 || (hasRead[i] && uid !== 0)) ? '' : 'unread'; categories[i]['unread-class'] = (parseInt(categories[i].topic_count, 10) === 0 || (hasRead[i] && uid !== 0)) ? '' : 'unread';
categories[i].children = results.children[i];
categories[i].parent = results.parents[i];
} }
callback(null, categories); callback(null, categories);
}); });
}; };
Categories.getChildren = function(cid, callback) { Categories.getParents = function(cids, callback) {
var keys = cids.map(function(cid) {
return 'category:' + cid;
});
db.getObjectsFields(keys, ['parentCid'], function(err, data) {
if (err) {
return callback(err);
}
var parentCids = data.map(function(category) {
return category && category.parentCid;
});
Categories.getCategoriesData(parentCids, callback);
});
};
Categories.getChildren = function(cids, uid, callback) {
async.waterfall([ async.waterfall([
async.apply(db.getSortedSetRange, 'categories:cid', 0, -1), async.apply(db.getSortedSetRange, 'categories:cid', 0, -1),
function(cids, next) {
privileges.categories.filter('find', cids, uid, next);
},
function (cids, next) { function (cids, next) {
Categories.getMultipleCategoryFields(cids, ['cid', 'parentCid'], next); Categories.getCategoriesData(cids, next);
}, },
function (data, next) { function (categories, next) {
next(null, data.filter(function(category) { async.map(cids, function(cid, next) {
if (parseInt(category.parentCid, 10) === parseInt(cid, 10)) return true; next(null, categories.filter(function(category) {
}).map(function(category) { return parseInt(category.parentCid, 10) === parseInt(cid, 10);
return category.cid;
})); }));
}, }, next);
function (cids, next) {
Categories.getCategoriesData(cids, false, next);
} }
], callback); ], callback);
}; };

@ -117,7 +117,12 @@ adminController.categories.disabled = function(req, res, next) {
}; };
function filterAndRenderCategories(req, res, next, active) { function filterAndRenderCategories(req, res, next, active) {
categories.getAllCategories(function (err, categoryData) { var uid = req.user ? parseInt(req.user.uid, 10) : 0;
categories.getAllCategories(uid, function (err, categoryData) {
if (err) {
return next(err);
}
categoryData = categoryData.filter(function (category) { categoryData = categoryData.filter(function (category) {
return active ? !category.disabled : category.disabled; return active ? !category.disabled : category.disabled;
}); });

@ -379,7 +379,7 @@ function createAdmin(callback) {
function createCategories(next) { function createCategories(next) {
var Categories = require('./categories'); var Categories = require('./categories');
Categories.getAllCategories(function (err, categoryData) { Categories.getAllCategories(0, function (err, categoryData) {
if (err) { if (err) {
return next(err); return next(err);
} }

Loading…
Cancel
Save