Files
NodeBB/src/categories.js

364 lines
9.9 KiB
JavaScript
Raw Normal View History

2014-03-01 16:59:04 -05:00
'use strict';
2016-02-17 20:15:14 +02:00
var async = require('async');
2014-11-08 23:54:21 -05:00
2016-02-17 20:15:14 +02:00
var db = require('./database');
var user = require('./user');
var Groups = require('./groups');
var plugins = require('./plugins');
2016-08-27 01:52:08 +03:00
var privileges = require('./privileges');
(function (Categories) {
2015-09-25 17:38:58 -04:00
require('./categories/data')(Categories);
2014-11-08 23:54:21 -05:00
require('./categories/create')(Categories);
require('./categories/delete')(Categories);
2014-11-08 23:54:21 -05:00
require('./categories/topics')(Categories);
require('./categories/unread')(Categories);
2014-03-12 21:41:53 -04:00
require('./categories/activeusers')(Categories);
require('./categories/recentreplies')(Categories);
require('./categories/update')(Categories);
2014-03-12 21:41:53 -04:00
Categories.exists = function (cid, callback) {
2014-05-15 20:49:47 -04:00
db.isSortedSetMember('categories:cid', cid, callback);
};
Categories.getCategoryById = function (data, callback) {
2016-02-17 20:15:14 +02:00
var category;
async.waterfall([
function (next) {
Categories.getCategories([data.cid], data.uid, next);
},
function (categories, next) {
if (!Array.isArray(categories) || !categories[0]) {
return next(new Error('[[error:invalid-cid]]'));
}
2016-02-17 20:15:14 +02:00
category = categories[0];
2016-02-17 20:15:14 +02:00
async.parallel({
topics: function (next) {
2016-02-17 20:15:14 +02:00
Categories.getCategoryTopics(data, next);
},
topicCount: function (next) {
2016-09-19 13:08:31 +03:00
if (Array.isArray(data.set)) {
db.sortedSetIntersectCard(data.set, next);
} else {
next(null, category.topic_count);
}
},
isIgnored: function (next) {
2016-02-17 20:15:14 +02:00
Categories.isIgnored([data.cid], data.uid, next);
2017-02-17 19:31:21 -07:00
},
2016-02-17 20:15:14 +02:00
}, next);
},
function (results, next) {
category.topics = results.topics.topics;
category.nextStart = results.topics.nextStart;
2014-08-29 15:57:20 -04:00
category.isIgnored = results.isIgnored[0];
2016-09-19 13:08:31 +03:00
category.topic_count = results.topicCount;
2013-05-24 07:52:15 -04:00
2017-02-18 12:30:49 -07:00
plugins.fireHook('filter:category.get', { category: category, uid: data.uid }, next);
2016-02-17 20:15:14 +02:00
},
function (data, next) {
next(null, data.category);
2017-02-17 19:31:21 -07:00
},
2016-02-17 20:15:14 +02:00
], callback);
2013-09-12 14:07:24 -04:00
};
Categories.isIgnored = function (cids, uid, callback) {
2016-11-21 13:47:34 +03:00
db.isSortedSetMembers('uid:' + uid + ':ignored:cids', cids, callback);
2014-08-29 15:57:20 -04:00
};
Categories.getPageCount = function (cid, uid, callback) {
2014-11-09 00:33:26 -05:00
async.parallel({
topicCount: async.apply(Categories.getCategoryField, cid, 'topic_count'),
2017-02-17 19:31:21 -07:00
settings: async.apply(user.getSettings, uid),
}, function (err, results) {
2014-10-14 23:12:47 -04:00
if (err) {
return callback(err);
}
2014-11-09 00:33:26 -05:00
if (!parseInt(results.topicCount, 10)) {
return callback(null, 1);
}
2014-11-09 00:33:26 -05:00
callback(null, Math.ceil(parseInt(results.topicCount, 10) / results.settings.topicsPerPage));
});
};
Categories.getAllCategories = function (uid, callback) {
db.getSortedSetRange('categories:cid', 0, -1, function (err, cids) {
2014-11-09 00:45:18 -05:00
if (err || !Array.isArray(cids) || !cids.length) {
return callback(err, []);
2013-11-26 19:09:32 -05:00
}
2013-11-27 15:02:09 -05:00
2014-08-22 19:10:26 -04:00
Categories.getCategories(cids, uid, callback);
2014-05-27 12:44:28 -04:00
});
};
Categories.getCategoriesByPrivilege = function (set, uid, privilege, callback) {
2014-11-09 00:33:26 -05:00
async.waterfall([
function (next) {
db.getSortedSetRange(set, 0, -1, next);
2014-11-09 00:33:26 -05:00
},
function (cids, next) {
2014-11-09 00:33:26 -05:00
privileges.categories.filterCids(privilege, cids, uid, next);
},
function (cids, next) {
2014-11-09 00:33:26 -05:00
Categories.getCategories(cids, uid, next);
2017-02-17 19:31:21 -07:00
},
2014-11-09 00:33:26 -05:00
], callback);
2013-09-12 14:07:24 -04:00
};
Categories.getModerators = function (cid, callback) {
Groups.getMembers('cid:' + cid + ':privileges:mods', 0, -1, function (err, uids) {
2014-11-09 00:33:26 -05:00
if (err || !Array.isArray(uids) || !uids.length) {
2014-11-09 00:45:18 -05:00
return callback(err, []);
2013-07-19 16:13:00 -04:00
}
2015-09-25 17:38:58 -04:00
user.getUsersFields(uids, ['uid', 'username', 'userslug', 'picture'], callback);
2014-03-09 23:09:08 -04:00
});
2014-03-10 00:17:06 -04:00
};
2014-03-09 23:09:08 -04:00
2013-07-03 12:59:45 -04:00
Categories.getCategories = function (cids, uid, callback) {
if (!Array.isArray(cids)) {
2014-04-09 21:56:30 -04:00
return callback(new Error('[[error:invalid-cid]]'));
}
2013-08-20 12:11:17 -04:00
if (!cids.length) {
2014-11-09 00:01:46 -05:00
return callback(null, []);
}
2014-03-09 23:09:08 -04:00
async.parallel({
categories: function (next) {
2014-03-09 23:09:08 -04:00
Categories.getCategoriesData(cids, next);
},
children: function (next) {
2014-08-22 19:10:26 -04:00
Categories.getChildren(cids, uid, next);
},
parents: function (next) {
2014-08-22 19:10:26 -04:00
Categories.getParents(cids, next);
},
2016-12-09 18:53:08 +03:00
tagWhitelist: function (next) {
Categories.getTagWhitelist(cids, next);
},
hasRead: function (next) {
2014-03-09 23:09:08 -04:00
Categories.hasReadCategories(cids, uid, next);
2017-02-17 19:31:21 -07:00
},
}, function (err, results) {
2013-09-17 13:09:37 -04:00
if (err) {
return callback(err);
}
2013-08-20 12:11:17 -04:00
2014-03-09 23:09:08 -04:00
uid = parseInt(uid, 10);
2016-12-09 18:53:08 +03:00
results.categories.forEach(function (category, i) {
if (category) {
category['unread-class'] = (parseInt(category.topic_count, 10) === 0 || (results.hasRead[i] && uid !== 0)) ? '' : 'unread';
category.children = results.children[i];
category.parent = results.parents[i] || undefined;
category.tagWhitelist = results.tagWhitelist[i];
calculateTopicPostCount(category);
2014-09-01 18:46:42 -04:00
}
2016-12-09 18:53:08 +03:00
});
callback(null, results.categories);
});
};
2013-10-07 17:48:11 -04:00
2016-12-09 18:53:08 +03:00
Categories.getTagWhitelist = function (cids, callback) {
var keys = cids.map(function (cid) {
return 'cid:' + cid + ':tag:whitelist';
});
2016-12-09 18:53:08 +03:00
db.getSortedSetsMembers(keys, callback);
2013-08-20 12:11:17 -04:00
};
2015-04-19 15:18:55 -04:00
function calculateTopicPostCount(category) {
2015-05-17 15:53:42 -04:00
if (!category) {
2015-04-19 15:18:55 -04:00
return;
}
var postCount = parseInt(category.post_count, 10) || 0;
var topicCount = parseInt(category.topic_count, 10) || 0;
2015-05-17 15:53:42 -04:00
if (!Array.isArray(category.children) || !category.children.length) {
category.totalPostCount = postCount;
category.totalTopicCount = topicCount;
return;
}
2015-04-19 15:18:55 -04:00
category.children.forEach(function (child) {
calculateTopicPostCount(child);
postCount += parseInt(child.totalPostCount, 10) || 0;
topicCount += parseInt(child.totalTopicCount, 10) || 0;
2015-04-19 15:18:55 -04:00
});
2015-05-17 15:53:42 -04:00
category.totalPostCount = postCount;
category.totalTopicCount = topicCount;
2015-04-19 15:18:55 -04:00
}
Categories.getParents = function (cids, callback) {
var categoriesData;
var parentCids;
async.waterfall([
function (next) {
2015-09-25 17:38:58 -04:00
Categories.getCategoriesFields(cids, ['parentCid'], next);
},
function (_categoriesData, next) {
categoriesData = _categoriesData;
parentCids = categoriesData.filter(function (category) {
2015-09-16 18:16:14 -04:00
return category && category.hasOwnProperty('parentCid') && parseInt(category.parentCid, 10);
}).map(function (category) {
return parseInt(category.parentCid, 10);
});
2014-08-22 19:10:26 -04:00
if (!parentCids.length) {
2017-02-18 01:21:34 -07:00
return callback(null, cids.map(function () { return null; }));
}
2014-08-22 19:10:26 -04:00
Categories.getCategoriesData(parentCids, next);
},
function (parentData, next) {
parentData = categoriesData.map(function (category) {
return parentData[parentCids.indexOf(parseInt(category.parentCid, 10))];
});
next(null, parentData);
2017-02-17 19:31:21 -07:00
},
], callback);
2014-08-22 19:10:26 -04:00
};
Categories.getChildren = function (cids, uid, callback) {
var categories = cids.map(function (cid) {
2017-02-18 12:30:49 -07:00
return { cid: cid };
});
async.each(categories, function (category, next) {
2015-08-19 13:37:48 -04:00
getChildrenRecursive(category, uid, next);
}, function (err) {
callback(err, categories.map(function (c) {
return c && c.children;
}));
});
};
2015-08-19 13:37:48 -04:00
function getChildrenRecursive(category, uid, callback) {
async.waterfall([
function (next) {
2015-08-19 13:37:48 -04:00
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(function (cid) {
2015-09-15 12:58:19 -04:00
return parseInt(category.cid, 10) !== parseInt(cid, 10);
});
if (!children.length) {
category.children = [];
return callback();
}
Categories.getCategoriesData(children, next);
2014-08-22 19:10:26 -04:00
},
function (childrenData, next) {
2015-09-18 15:33:48 -04:00
childrenData = childrenData.filter(Boolean);
category.children = childrenData;
async.each(category.children, function (child, next) {
2015-08-19 13:37:48 -04:00
getChildrenRecursive(child, uid, next);
2014-08-22 19:10:26 -04:00
}, next);
2017-02-17 19:31:21 -07:00
},
], callback);
}
Categories.flattenCategories = function (allCategories, categoryData) {
categoryData.forEach(function (category) {
2017-05-25 20:01:07 -04:00
if (category) {
if (!category.parent) {
allCategories.push(category);
}
2017-05-25 20:01:07 -04:00
if (Array.isArray(category.children) && category.children.length) {
Categories.flattenCategories(allCategories, category.children);
}
}
});
2015-08-19 13:37:48 -04:00
};
2015-05-30 18:44:31 +03:00
/**
* 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) {
2017-02-17 20:20:42 -07:00
var tree = [];
var i = 0;
var len = categories.length;
var category;
2015-05-30 18:44:31 +03:00
for (i; i < len; i += 1) {
category = categories[i];
2016-09-15 20:31:47 +03:00
if (!category.hasOwnProperty('parentCid') || category.parentCid === null) {
category.parentCid = 0;
}
2015-05-30 18:44:31 +03:00
if (parseInt(category.parentCid, 10) === parseInt(parentCid, 10)) {
tree.push(category);
category.children = Categories.getTree(categories, category.cid);
}
}
2015-05-30 18:44:31 +03:00
return tree;
2015-05-30 18:44:31 +03:00
};
Categories.buildForSelect = function (uid, callback) {
2016-09-16 14:20:07 +03:00
function recursive(category, categoriesData, level) {
if (category.link) {
return;
}
var bullet = level ? '&bull; ' : '';
category.value = category.cid;
category.text = level + bullet + category.name;
2016-09-16 14:20:07 +03:00
categoriesData.push(category);
category.children.forEach(function (child) {
2016-09-16 14:20:07 +03:00
recursive(child, categoriesData, '&nbsp;&nbsp;&nbsp;&nbsp;' + level);
});
}
Categories.getCategoriesByPrivilege('cid:0:children', uid, 'read', function (err, categories) {
2016-09-16 14:20:07 +03:00
if (err) {
return callback(err);
}
var categoriesData = [];
categories = categories.filter(function (category) {
2016-09-16 14:20:07 +03:00
return category && !category.link && !parseInt(category.parentCid, 10);
});
categories.forEach(function (category) {
2016-09-16 14:20:07 +03:00
recursive(category, categoriesData, '');
});
callback(null, categoriesData);
});
};
Categories.getIgnorers = function (cid, start, stop, callback) {
2016-05-18 19:02:43 +03:00
db.getSortedSetRevRange('cid:' + cid + ':ignorers', start, stop, callback);
};
Categories.filterIgnoringUids = function (cid, uids, callback) {
2016-05-18 19:02:43 +03:00
async.waterfall([
function (next) {
2017-02-09 13:15:37 +03:00
db.isSortedSetMembers('cid:' + cid + ':ignorers', uids, next);
2016-05-18 19:02:43 +03:00
},
2017-02-09 13:15:37 +03:00
function (isIgnoring, next) {
var readingUids = uids.filter(function (uid, index) {
2017-02-09 13:15:37 +03:00
return uid && !isIgnoring[index];
2016-05-18 19:02:43 +03:00
});
next(null, readingUids);
2017-02-17 19:31:21 -07:00
},
2016-05-18 19:02:43 +03:00
], callback);
};
2014-04-10 20:31:57 +01:00
}(exports));