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.
nodebb/src/categories.js

408 lines
11 KiB
JavaScript

11 years ago
'use strict';
var async = require('async'),
nconf = require('nconf'),
db = require('./database'),
11 years ago
user = require('./user'),
Groups = require('./groups'),
plugins = require('./plugins'),
11 years ago
validator = require('validator'),
privileges = require('./privileges');
(function(Categories) {
require('./categories/create')(Categories);
require('./categories/delete')(Categories);
require('./categories/topics')(Categories);
require('./categories/unread')(Categories);
require('./categories/activeusers')(Categories);
require('./categories/recentreplies')(Categories);
require('./categories/update')(Categories);
11 years ago
Categories.exists = function(cid, callback) {
db.isSortedSetMember('categories:cid', cid, callback);
};
Categories.getCategoryById = function(data, callback) {
Categories.getCategories([data.cid], data.uid, function(err, categories) {
11 years ago
if (err || !Array.isArray(categories) || !categories[0]) {
return callback(err || new Error('[[error:invalid-cid]]'));
}
11 years ago
var category = categories[0];
if (parseInt(data.uid, 10)) {
Categories.markAsRead([data.cid], data.uid);
11 years ago
}
async.parallel({
topics: function(next) {
Categories.getCategoryTopics(data, next);
},
pageCount: function(next) {
Categories.getPageCount(data.cid, data.uid, next);
11 years ago
},
isIgnored: function(next) {
Categories.isIgnored([data.cid], data.uid, next);
}
}, function(err, results) {
if(err) {
return callback(err);
}
category.topics = results.topics.topics;
category.nextStart = results.topics.nextStart;
category.pageCount = results.pageCount;
11 years ago
category.isIgnored = results.isIgnored[0];
plugins.fireHook('filter:category.get', {category: category, uid: data.uid}, function(err, data) {
callback(err, data ? data.category : null);
});
});
});
};
11 years ago
Categories.isIgnored = function(cids, uid, callback) {
user.getIgnoredCategories(uid, function(err, ignoredCids) {
if (err) {
return callback(err);
}
cids = cids.map(function(cid) {
return ignoredCids.indexOf(cid.toString()) !== -1;
});
callback(null, cids);
});
};
Categories.getPageCount = function(cid, uid, callback) {
10 years ago
async.parallel({
topicCount: async.apply(Categories.getCategoryField, cid, 'topic_count'),
settings: async.apply(user.getSettings, uid)
}, function(err, results) {
if (err) {
return callback(err);
}
10 years ago
if (!parseInt(results.topicCount, 10)) {
return callback(null, 1);
}
10 years ago
callback(null, Math.ceil(parseInt(results.topicCount, 10) / results.settings.topicsPerPage));
});
};
11 years ago
Categories.getAllCategories = function(uid, callback) {
db.getSortedSetRange('categories:cid', 0, -1, function(err, cids) {
if (err || !Array.isArray(cids) || !cids.length) {
return callback(err, []);
11 years ago
}
11 years ago
11 years ago
Categories.getCategories(cids, uid, callback);
});
};
Categories.getCategoriesByPrivilege = function(set, uid, privilege, callback) {
10 years ago
async.waterfall([
function(next) {
db.getSortedSetRange(set, 0, -1, next);
10 years ago
},
function(cids, next) {
privileges.categories.filterCids(privilege, cids, uid, next);
},
function(cids, next) {
Categories.getCategories(cids, uid, next);
}
10 years ago
], callback);
};
Categories.getModerators = function(cid, callback) {
Groups.getMembers('cid:' + cid + ':privileges:mods', 0, -1, function(err, uids) {
10 years ago
if (err || !Array.isArray(uids) || !uids.length) {
return callback(err, []);
}
10 years ago
user.getMultipleUserFields(uids, ['uid', 'username', 'userslug', 'picture'], callback);
});
};
11 years ago
Categories.getCategoryData = function(cid, callback) {
Categories.getCategoriesData([cid], function(err, categories) {
callback(err, categories ? categories[0] : null);
});
};
12 years ago
11 years ago
Categories.getCategoriesData = function(cids, callback) {
10 years ago
if (!Array.isArray(cids) || !cids.length) {
return callback(null, []);
}
var keys = cids.map(function(cid) {
return 'category:' + cid;
});
db.getObjects(keys, function(err, categories) {
if (err || !Array.isArray(categories) || !categories.length) {
return callback(err, []);
}
async.map(categories, modifyCategory, callback);
});
11 years ago
};
function modifyCategory(category, callback) {
if (!category) {
return callback(null, null);
}
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')) {
10 years ago
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) {
plugins.fireHook('filter:parse.raw', category.description, function(err, parsedDescription) {
if (err) {
return callback(err);
}
category.descriptionParsed = parsedDescription;
category.description = validator.escape(category.description);
callback(null, category);
});
} else {
callback(null, category);
}
}
Categories.getCategoryField = function(cid, field, callback) {
11 years ago
db.getObjectField('category:' + cid, field, callback);
};
Categories.getMultipleCategoryFields = function(cids, fields, callback) {
10 years ago
if (!Array.isArray(cids) || !cids.length) {
return callback(null, []);
}
var keys = cids.map(function(cid) {
return 'category:' + cid;
});
10 years ago
db.getObjectsFields(keys, fields, function(err, categories) {
if (err) {
return callback(err);
}
async.map(categories, modifyCategory, callback);
});
};
10 years ago
Categories.getAllCategoryFields = function(fields, callback) {
async.waterfall([
async.apply(db.getSortedSetRange, 'categories:cid', 0, -1),
function(cids, next) {
Categories.getMultipleCategoryFields(cids, fields, next);
}
], callback);
};
Categories.getCategoryFields = function(cid, fields, callback) {
11 years ago
db.getObjectFields('category:' + cid, fields, callback);
};
12 years ago
11 years ago
Categories.setCategoryField = function(cid, field, value, callback) {
11 years ago
db.setObjectField('category:' + cid, field, value, callback);
};
11 years ago
Categories.incrementCategoryFieldBy = function(cid, field, value, callback) {
11 years ago
db.incrObjectFieldBy('category:' + cid, field, value, callback);
};
Categories.getCategories = function(cids, uid, callback) {
if (!Array.isArray(cids)) {
return callback(new Error('[[error:invalid-cid]]'));
}
12 years ago
if (!cids.length) {
10 years ago
return callback(null, []);
}
async.parallel({
categories: function(next) {
Categories.getCategoriesData(cids, next);
},
11 years ago
children: function(next) {
Categories.getChildren(cids, uid, next);
},
parents: function(next) {
Categories.getParents(cids, next);
},
hasRead: function(next) {
Categories.hasReadCategories(cids, uid, next);
}
}, function(err, results) {
if (err) {
return callback(err);
}
12 years ago
var categories = results.categories;
var hasRead = results.hasRead;
uid = parseInt(uid, 10);
for(var i=0; i<results.categories.length; ++i) {
11 years ago
if (categories[i]) {
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] || undefined;
10 years ago
calculateTopicPostCount(categories[i]);
11 years ago
}
}
11 years ago
callback(null, categories);
});
12 years ago
};
10 years ago
function calculateTopicPostCount(category) {
10 years ago
if (!category) {
10 years ago
return;
}
var postCount = parseInt(category.post_count, 10) || 0;
var topicCount = parseInt(category.topic_count, 10) || 0;
10 years ago
if (!Array.isArray(category.children) || !category.children.length) {
category.totalPostCount = postCount;
category.totalTopicCount = topicCount;
return;
}
10 years ago
category.children.forEach(function(child) {
calculateTopicPostCount(child);
postCount += parseInt(child.totalPostCount, 10) || 0;
topicCount += parseInt(child.totalTopicCount, 10) || 0;
10 years ago
});
10 years ago
category.totalPostCount = postCount;
category.totalTopicCount = topicCount;
10 years ago
}
11 years ago
Categories.getParents = function(cids, callback) {
var categoriesData;
var parentCids;
async.waterfall([
function (next) {
Categories.getMultipleCategoryFields(cids, ['parentCid'], next);
},
function (_categoriesData, next) {
categoriesData = _categoriesData;
parentCids = categoriesData.filter(function(category) {
10 years ago
return category && category.hasOwnProperty('parentCid') && parseInt(category.parentCid, 10);
}).map(function(category) {
return parseInt(category.parentCid, 10);
});
11 years ago
if (!parentCids.length) {
return callback(null, cids.map(function() {return null;}));
}
11 years ago
Categories.getCategoriesData(parentCids, next);
},
function (parentData, next) {
parentData = categoriesData.map(function(category) {
return parentData[parentCids.indexOf(parseInt(category.parentCid, 10))];
});
next(null, parentData);
}
], callback);
11 years ago
};
Categories.getChildren = function(cids, uid, callback) {
var categories = cids.map(function(cid) {
return {cid: cid};
});
async.each(categories, function(category, next) {
getChildrenRecursive(category, uid, next);
}, function (err) {
callback(err, categories.map(function(c) {
return c && c.children;
}));
});
};
function getChildrenRecursive(category, uid, callback) {
async.waterfall([
function (next) {
db.getSortedSetRange('cid:' + category.cid + ':children', 0, -1, next);
},
function (children, next) {
privileges.categories.filterCids('find', children, uid, next);
},
function (children, next) {
10 years ago
children = children.filter(function(cid) {
return parseInt(category.cid, 10) !== parseInt(cid, 10);
});
if (!children.length) {
category.children = [];
return callback();
}
Categories.getCategoriesData(children, next);
11 years ago
},
function (childrenData, next) {
category.children = childrenData;
async.each(category.children, function(child, next) {
getChildrenRecursive(child, uid, next);
11 years ago
}, next);
}
], callback);
}
Categories.flattenCategories = function(allCategories, categoryData) {
categoryData.forEach(function(category) {
if (!category) {
return;
}
if (!category.parent) {
allCategories.push(category);
}
if (Array.isArray(category.children) && category.children.length) {
Categories.flattenCategories(allCategories, category.children);
}
});
};
10 years ago
/**
* Recursively build tree
*
* @param categories {array} flat list of categories
* @param parentCid {number} start from 0 to build full tree
*/
Categories.getTree = function(categories, parentCid) {
var tree = [], i = 0, len = categories.length, category;
for (i; i < len; ++i) {
category = categories[i];
if (!category.hasOwnProperty('parentCid')) {
category.parentCid = 0;
}
10 years ago
if (category.parentCid == parentCid){
tree.push(category);
category.children = Categories.getTree(categories, category.cid);
}
}
10 years ago
return tree;
10 years ago
};
}(exports));