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.

409 lines
11 KiB
JavaScript

11 years ago
'use strict';
9 years ago
var async = require('async');
var _ = require('lodash');
6 years ago
var db = require('../database');
var user = require('../user');
var Groups = require('../groups');
var plugins = require('../plugins');
var privileges = require('../privileges');
8 years ago
var Categories = module.exports;
6 years ago
require('./data')(Categories);
require('./create')(Categories);
require('./delete')(Categories);
require('./topics')(Categories);
require('./unread')(Categories);
require('./activeusers')(Categories);
require('./recentreplies')(Categories);
require('./update')(Categories);
8 years ago
Categories.exists = function (cid, callback) {
db.exists('category:' + cid, callback);
8 years ago
};
Categories.getCategoryById = function (data, callback) {
var category;
async.waterfall([
function (next) {
Categories.getCategories([data.cid], data.uid, next);
},
function (categories, next) {
if (!categories[0]) {
8 years ago
return next(new Error('[[error:invalid-cid]]'));
}
8 years ago
category = categories[0];
data.category = category;
8 years ago
async.parallel({
topics: function (next) {
Categories.getCategoryTopics(data, next);
},
topicCount: function (next) {
Categories.getTopicCount(data, next);
8 years ago
},
isIgnored: function (next) {
Categories.isIgnored([data.cid], data.uid, next);
},
}, next);
},
function (results, next) {
category.topics = results.topics.topics;
category.nextStart = results.topics.nextStart;
category.isIgnored = results.isIgnored[0];
category.topic_count = results.topicCount;
plugins.fireHook('filter:category.get', { category: category, uid: data.uid }, next);
},
function (data, next) {
next(null, data.category);
},
], callback);
};
Categories.isIgnored = function (cids, uid, callback) {
if (parseInt(uid, 10) <= 0) {
return setImmediate(callback, null, cids.map(() => false));
}
8 years ago
db.isSortedSetMembers('uid:' + uid + ':ignored:cids', cids, callback);
};
Categories.getAllCategories = function (uid, callback) {
async.waterfall([
function (next) {
db.getSortedSetRange('categories:cid', 0, -1, next);
},
function (cids, next) {
Categories.getCategories(cids, uid, next);
},
], callback);
};
Categories.getCidsByPrivilege = function (set, uid, privilege, callback) {
8 years ago
async.waterfall([
function (next) {
db.getSortedSetRange(set, 0, -1, next);
},
function (cids, next) {
privileges.categories.filterCids(privilege, cids, uid, next);
},
], callback);
};
Categories.getCategoriesByPrivilege = function (set, uid, privilege, callback) {
async.waterfall([
function (next) {
Categories.getCidsByPrivilege(set, uid, privilege, next);
},
8 years ago
function (cids, next) {
Categories.getCategories(cids, uid, next);
},
], callback);
};
Categories.getModerators = function (cid, callback) {
async.waterfall([
function (next) {
8 years ago
Groups.getMembers('cid:' + cid + ':privileges:moderate', 0, -1, next);
8 years ago
},
function (uids, next) {
user.getUsersFields(uids, ['uid', 'username', 'userslug', 'picture'], next);
},
], callback);
};
8 years ago
Categories.getCategories = function (cids, uid, callback) {
if (!Array.isArray(cids)) {
return callback(new Error('[[error:invalid-cid]]'));
}
8 years ago
if (!cids.length) {
return callback(null, []);
}
uid = parseInt(uid, 10);
let results;
8 years ago
async.waterfall([
function (next) {
async.parallel({
categories: function (next) {
Categories.getCategoriesData(cids, next);
},
tagWhitelist: function (next) {
Categories.getTagWhitelist(cids, next);
},
hasRead: function (next) {
Categories.hasReadCategories(cids, uid, next);
},
}, next);
},
function (_results, next) {
results = _results;
Categories.getParentsAndChildren(results.categories, uid, next);
},
function (categories, next) {
categories.forEach(function (category, i) {
8 years ago
if (category) {
category.tagWhitelist = results.tagWhitelist[i];
category['unread-class'] = (category.topic_count === 0 || (results.hasRead[i] && uid !== 0)) ? '' : 'unread';
8 years ago
calculateTopicPostCount(category);
11 years ago
}
8 years ago
});
next(null, categories);
8 years ago
},
], callback);
};
Categories.getTagWhitelist = function (cids, callback) {
const keys = cids.map(cid => 'cid:' + cid + ':tag:whitelist');
8 years ago
db.getSortedSetsMembers(keys, callback);
};
function calculateTopicPostCount(category) {
if (!category) {
return;
}
var postCount = category.post_count;
var topicCount = category.topic_count;
8 years ago
if (!Array.isArray(category.children) || !category.children.length) {
10 years ago
category.totalPostCount = postCount;
category.totalTopicCount = topicCount;
8 years ago
return;
10 years ago
}
8 years ago
category.children.forEach(function (child) {
calculateTopicPostCount(child);
postCount += parseInt(child.totalPostCount, 10) || 0;
topicCount += parseInt(child.totalTopicCount, 10) || 0;
});
category.totalPostCount = postCount;
category.totalTopicCount = topicCount;
}
Categories.getParents = function (cids, callback) {
var categoriesData;
var parentCids;
async.waterfall([
function (next) {
Categories.getCategoriesFields(cids, ['parentCid'], next);
},
function (_categoriesData, next) {
categoriesData = _categoriesData;
parentCids = categoriesData.filter(c => c && c.parentCid).map(c => c.parentCid);
8 years ago
if (!parentCids.length) {
return callback(null, cids.map(() => null));
8 years ago
}
8 years ago
Categories.getCategoriesData(parentCids, next);
},
function (parentData, next) {
const cidToParent = _.zipObject(parentCids, parentData);
parentData = categoriesData.map(category => cidToParent[category.parentCid]);
8 years ago
next(null, parentData);
},
], callback);
};
Categories.getParentsAndChildren = function (categoryData, uid, callback) {
const parentCids = categoryData.filter(c => c && c.parentCid).map(c => c.parentCid);
async.waterfall([
function (next) {
async.parallel({
parents: function (next) {
Categories.getCategoriesData(parentCids, next);
},
children: function (next) {
async.each(categoryData, function (category, next) {
getChildrenRecursive(category, uid, next);
}, next);
},
}, next);
},
function (results, next) {
const cidToParent = _.zipObject(parentCids, results.parents);
categoryData.forEach(function (category) {
category.parent = cidToParent[category.parentCid];
});
next(null, categoryData);
},
], callback);
};
8 years ago
Categories.getChildren = function (cids, uid, callback) {
var categories;
async.waterfall([
function (next) {
Categories.getCategoriesFields(cids, ['parentCid'], next);
},
function (categoryData, next) {
categories = categoryData.map((category, index) => ({ cid: cids[index], parentCid: category.parentCid }));
async.each(categories, function (category, next) {
getChildrenRecursive(category, uid, next);
}, next);
},
function (next) {
next(null, categories.map(c => c && c.children));
},
], callback);
8 years ago
};
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) {
children = children.filter(cid => parseInt(category.cid, 10) !== parseInt(cid, 10));
8 years ago
if (!children.length) {
category.children = [];
return callback();
}
8 years ago
Categories.getCategoriesData(children, next);
},
function (children, next) {
children = children.filter(Boolean);
category.children = children;
var cids = children.map(child => child.cid);
Categories.hasReadCategories(cids, uid, next);
},
function (hasRead, next) {
hasRead.forEach(function (read, i) {
var child = category.children[i];
child['unread-class'] = (child.topic_count === 0 || (read && uid !== 0)) ? '' : 'unread';
});
8 years ago
async.each(category.children, function (child, next) {
if (parseInt(category.parentCid, 10) === parseInt(child.cid, 10)) {
return next();
}
8 years ago
getChildrenRecursive(child, uid, next);
}, next);
},
], callback);
}
Categories.getChildrenCids = function (rootCid, callback) {
6 years ago
var allCids = [];
function recursive(currentCid, callback) {
db.getSortedSetRange('cid:' + currentCid + ':children', 0, -1, function (err, childrenCids) {
if (err) {
return callback(err);
}
if (!childrenCids.length) {
return callback();
}
async.eachSeries(childrenCids, function (childCid, next) {
allCids.push(parseInt(childCid, 10));
recursive(childCid, next);
}, callback);
});
}
6 years ago
recursive(rootCid, function (err) {
callback(err, _.uniq(allCids));
});
6 years ago
};
8 years ago
Categories.flattenCategories = function (allCategories, categoryData) {
categoryData.forEach(function (category) {
if (category) {
if (!category.parent) {
allCategories.push(category);
}
10 years ago
8 years ago
if (Array.isArray(category.children) && category.children.length) {
Categories.flattenCategories(allCategories, category.children);
}
}
8 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 = [];
categories.forEach(function (category) {
if (category) {
if (!category.hasOwnProperty('parentCid') || category.parentCid === null) {
category.parentCid = 0;
}
10 years ago
if (category.parentCid === parentCid) {
tree.push(category);
category.children = Categories.getTree(categories, category.cid);
}
8 years ago
}
});
10 years ago
8 years ago
return tree;
};
8 years ago
Categories.buildForSelect = function (uid, privilege, callback) {
8 years ago
async.waterfall([
function (next) {
8 years ago
Categories.getCategoriesByPrivilege('cid:0:children', uid, privilege, next);
8 years ago
},
function (categories, next) {
Categories.buildForSelectCategories(categories, next);
},
], callback);
};
Categories.buildForSelectCategories = function (categories, callback) {
7 years ago
function recursive(category, categoriesData, level, depth) {
8 years ago
var bullet = level ? '&bull; ' : '';
category.value = category.cid;
8 years ago
category.level = level;
8 years ago
category.text = level + bullet + category.name;
7 years ago
category.depth = depth;
8 years ago
categoriesData.push(category);
category.children.forEach(function (child) {
7 years ago
recursive(child, categoriesData, '&nbsp;&nbsp;&nbsp;&nbsp;' + level, depth + 1);
8 years ago
});
}
8 years ago
var categoriesData = [];
categories = categories.filter(category => category && !category.parentCid);
8 years ago
categories.forEach(function (category) {
7 years ago
recursive(category, categoriesData, '', 0);
8 years ago
});
callback(null, categoriesData);
8 years ago
};
Categories.getIgnorers = function (cid, start, stop, callback) {
db.getSortedSetRevRange('cid:' + cid + ':ignorers', start, stop, callback);
};
Categories.filterIgnoringUids = function (cid, uids, callback) {
async.waterfall([
function (next) {
db.isSortedSetMembers('cid:' + cid + ':ignorers', uids, next);
},
function (isIgnoring, next) {
const readingUids = uids.filter((uid, index) => uid && !isIgnoring[index]);
8 years ago
next(null, readingUids);
},
], callback);
};
6 years ago
Categories.async = require('../promisify')(Categories);