recursively get all children
calculate topic/post count from children
new sorted set `cid:<id>:children`
fix search query params
v1.18.x
barisusakli 10 years ago
parent a990e9c3bf
commit 5b87af4389

@ -503,7 +503,7 @@ app.cacheBuster = null;
app.load = function() { app.load = function() {
$('document').ready(function () { $('document').ready(function () {
var url = ajaxify.start(window.location.pathname.slice(1), true, window.location.search); var url = ajaxify.start(window.location.pathname.slice(1) + window.location.search, true);
ajaxify.end(url, app.template); ajaxify.end(url, app.template);
handleStatusChange(); handleStatusChange();

@ -103,10 +103,10 @@ var async = require('async'),
}); });
}; };
Categories.getCategoriesByPrivilege = function(uid, privilege, callback) { Categories.getCategoriesByPrivilege = function(set, uid, privilege, callback) {
async.waterfall([ async.waterfall([
function(next) { function(next) {
db.getSortedSetRange('categories:cid', 0, -1, next); db.getSortedSetRange(set, 0, -1, next);
}, },
function(cids, next) { function(cids, next) {
privileges.categories.filterCids(privilege, cids, uid, next); privileges.categories.filterCids(privilege, cids, uid, next);
@ -273,6 +273,7 @@ var async = require('async'),
if (!category) { if (!category) {
return; return;
} }
var postCount = parseInt(category.post_count, 10) || 0; var postCount = parseInt(category.post_count, 10) || 0;
var topicCount = parseInt(category.topic_count, 10) || 0; var topicCount = parseInt(category.topic_count, 10) || 0;
if (!Array.isArray(category.children) || !category.children.length) { if (!Array.isArray(category.children) || !category.children.length) {
@ -282,9 +283,11 @@ var async = require('async'),
} }
category.children.forEach(function(child) { category.children.forEach(function(child) {
postCount += parseInt(child.post_count, 10) || 0; calculateTopicPostCount(child);
topicCount += parseInt(child.topic_count, 10) || 0; postCount += parseInt(child.totalPostCount, 10) || 0;
topicCount += parseInt(child.totalTopicCount, 10) || 0;
}); });
category.totalPostCount = postCount; category.totalPostCount = postCount;
category.totalTopicCount = topicCount; category.totalTopicCount = topicCount;
} }
@ -308,23 +311,58 @@ var async = require('async'),
}; };
Categories.getChildren = function(cids, uid, callback) { Categories.getChildren = function(cids, uid, callback) {
var categories = cids.map(function(cid) {
return {cid: cid};
});
async.each(categories, function(category, next) {
getChildrenRecursive(category, category.cid, uid, next);
}, function (err) {
callback(err, categories.map(function(c) {
return c && c.children;
}));
});
};
function getChildrenRecursive(category, parentCid, uid, callback) {
async.waterfall([ async.waterfall([
async.apply(db.getSortedSetRange, 'categories:cid', 0, -1), function (next) {
function(cids, next) { db.getSortedSetRange('cid:' + parentCid + ':children', 0, -1, next);
privileges.categories.filterCids('find', cids, uid, next);
}, },
function (cids, next) { function (children, next) {
Categories.getCategoriesData(cids, next); privileges.categories.filterCids('find', children, uid, next);
},
function (children, next) {
if (!children.length) {
category.children = [];
return callback();
}
Categories.getCategoriesData(children, next);
}, },
function (categories, next) { function (childrenData, next) {
async.map(cids, function(cid, next) { category.children = childrenData;
next(null, categories.filter(function(category) { async.each(category.children, function(child, next) {
return category && parseInt(category.parentCid, 10) === parseInt(cid, 10); getChildrenRecursive(child, child.cid, uid, next);
}));
}, next); }, next);
} }
], callback); ], 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);
}
});
}
/** /**
* Recursively build tree * Recursively build tree
@ -335,13 +373,13 @@ var async = require('async'),
Categories.getTree = function(categories, parentCid) { Categories.getTree = function(categories, parentCid) {
var tree = [], i = 0, len = categories.length, category; var tree = [], i = 0, len = categories.length, category;
for(i; i < len; ++i) { for (i; i < len; ++i) {
category = categories[i]; category = categories[i];
if (!category.hasOwnProperty('parentCid')) { if (!category.hasOwnProperty('parentCid')) {
category.parentCid = 0; category.parentCid = 0;
} }
if(category.parentCid == parentCid){ if (category.parentCid == parentCid){
tree.push(category); tree.push(category);
category.children = Categories.getTree(categories, category.cid); category.children = Categories.getTree(categories, category.cid);
} }

@ -10,6 +10,8 @@ module.exports = function(Categories) {
Categories.create = function(data, callback) { Categories.create = function(data, callback) {
var category; var category;
var parentCid = data.parentCid ? data.parentCid : 0;
async.waterfall([ async.waterfall([
function(next) { function(next) {
db.incrObjectField('global', 'nextCid', next); db.incrObjectField('global', 'nextCid', next);
@ -27,7 +29,7 @@ module.exports = function(Categories) {
bgColor: data.bgColor || colours[0], bgColor: data.bgColor || colours[0],
color: data.color || colours[1], color: data.color || colours[1],
slug: slug, slug: slug,
parentCid: ( data.parentCid ? data.parentCid : 0 ), parentCid: parentCid,
topic_count: 0, topic_count: 0,
post_count: 0, post_count: 0,
disabled: 0, disabled: 0,
@ -48,6 +50,7 @@ module.exports = function(Categories) {
async.series([ async.series([
async.apply(db.setObject, 'category:' + category.cid, category), async.apply(db.setObject, 'category:' + category.cid, category),
async.apply(db.sortedSetAdd, 'categories:cid', category.order, category.cid), async.apply(db.sortedSetAdd, 'categories:cid', category.order, category.cid),
async.apply(db.sortedSetAdd, 'cid:' + parentCid + ':children', category.order, category.cid),
async.apply(privileges.categories.give, defaultPrivileges, category.cid, 'administrators'), async.apply(privileges.categories.give, defaultPrivileges, category.cid, 'administrators'),
async.apply(privileges.categories.give, defaultPrivileges, category.cid, 'registered-users'), async.apply(privileges.categories.give, defaultPrivileges, category.cid, 'registered-users'),
async.apply(privileges.categories.give, ['find', 'read'], category.cid, 'guests') async.apply(privileges.categories.give, ['find', 'read'], category.cid, 'guests')

@ -30,15 +30,32 @@ module.exports = function(Categories) {
function(next) { function(next) {
db.sortedSetRemove('categories:cid', cid, next); db.sortedSetRemove('categories:cid', cid, next);
}, },
function(next) {
removeFromParent(cid, next);
},
function(next) { function(next) {
db.deleteAll([ db.deleteAll([
'cid:' + cid + ':tids', 'cid:' + cid + ':tids',
'cid:' + cid + ':tids:posts', 'cid:' + cid + ':tids:posts',
'cid:' + cid + ':pids', 'cid:' + cid + ':pids',
'cid:' + cid + ':read_by_uid', 'cid:' + cid + ':read_by_uid',
'cid:' + cid + ':children',
'category:' + cid 'category:' + cid
], next); ], next);
} }
], callback); ], callback);
} }
function removeFromParent(cid, callback) {
async.waterfall([
function(next) {
Categories.getCategoryField(cid, 'parentCid', next);
},
function(parentCid, next) {
parentCid = parseInt(parentCid, 10) || 0;
db.sortedSetRemove('cid:' + parentCid + ':children', cid, next);
}
], callback);
}
}; };

@ -50,17 +50,63 @@ module.exports = function(Categories) {
}; };
function updateCategoryField(cid, key, value, callback) { function updateCategoryField(cid, key, value, callback) {
if (key === 'parentCid') {
return updateParent(cid, value, callback);
}
db.setObjectField('category:' + cid, key, value, function(err) { db.setObjectField('category:' + cid, key, value, function(err) {
if (err) { if (err) {
return callback(err); return callback(err);
} }
if (key === 'order') { if (key === 'order') {
db.sortedSetAdd('categories:cid', value, cid, callback); updateOrder(cid, value, callback);
} else { } else {
callback(); callback();
} }
}); });
} }
function updateParent(cid, newParent, callback) {
Categories.getCategoryField(cid, 'parentCid', function(err, oldParent) {
if (err) {
return callback(err);
}
async.series([
function (next) {
oldParent = parseInt(oldParent, 10) || 0;
db.sortedSetRemove('cid:' + oldParent + ':children', cid, next);
},
function (next) {
newParent = parseInt(newParent, 10) || 0;
db.sortedSetAdd('cid:' + newParent + ':children', cid, cid, next);
},
function (next) {
db.setObjectField('category:' + cid, 'parentCid', newParent, next);
}
], function(err, results) {
callback(err);
});
});
}
function updateOrder(cid, order, callback) {
Categories.getCategoryField(cid, 'parentCid', function(err, parentCid) {
if (err) {
return callback(err);
}
async.parallel([
function (next) {
db.sortedSetAdd('categories:cid', order, cid, next);
},
function (next) {
parentCid = parseInt(parentCid, 10) || 0;
db.sortedSetAdd('cid:' + parentCid + ':children', order, cid, next);
}
], callback);
});
}
}; };

@ -33,7 +33,7 @@ categoriesController.list = function(req, res, next) {
content: 'website' content: 'website'
}]; }];
if(meta.config['brand:logo']) { if (meta.config['brand:logo']) {
res.locals.metaTags.push({ res.locals.metaTags.push({
property: 'og:image', property: 'og:image',
content: meta.config['brand:logo'] content: meta.config['brand:logo']
@ -46,22 +46,13 @@ categoriesController.list = function(req, res, next) {
var categoryData; var categoryData;
async.waterfall([ async.waterfall([
function(next) { function(next) {
categories.getCategoriesByPrivilege(req.uid, 'find', next); categories.getCategoriesByPrivilege('cid:0:children', req.uid, 'find', next);
}, },
function(_categoryData, next) { function(_categoryData, next) {
categoryData = _categoryData; categoryData = _categoryData;
var allCategories = [];
categoryData = categoryData.filter(function(category) {
if (!category.parent) {
allCategories.push(category);
}
if (Array.isArray(category.children) && category.children.length) { var allCategories = [];
allCategories.push.apply(allCategories, category.children); categories.flattenCategories(allCategories, categoryData);
}
return category && !category.parent;
});
categories.getRecentTopicReplies(allCategories, req.uid, next); categories.getRecentTopicReplies(allCategories, req.uid, next);
} }

@ -17,7 +17,7 @@ searchController.search = function(req, res, next) {
var breadcrumbs = helpers.buildBreadcrumbs([{text: '[[global:search]]'}]); var breadcrumbs = helpers.buildBreadcrumbs([{text: '[[global:search]]'}]);
categories.getCategoriesByPrivilege(req.uid, 'read', function(err, categories) { categories.getCategoriesByPrivilege('categories:cid', req.uid, 'read', function(err, categories) {
if (err) { if (err) {
return next(err); return next(err);
} }

@ -417,11 +417,14 @@ function getChildrenCids(cids, uid, callback) {
} }
var childrenCids = []; var childrenCids = [];
var allCategories = [];
childrenCategories.forEach(function(childrens) { childrenCategories.forEach(function(childrens) {
childrenCids = childrenCids.concat(childrens.map(function(category) { categories.flattenCategories(allCategories, childrens);
return category && category.cid; childrenCids = childrenCids.concat(allCategories.map(function(category) {
})); return category && category.cid;
}); }));
});
callback(null, childrenCids); callback(null, childrenCids);
}); });

@ -61,7 +61,7 @@ sitemap.getDynamicUrls = function(callback) {
async.parallel({ async.parallel({
categoryUrls: function(next) { categoryUrls: function(next) {
var categoryUrls = []; var categoryUrls = [];
categories.getCategoriesByPrivilege(0, 'find', function(err, categoriesData) { categories.getCategoriesByPrivilege('categories:cid', 0, 'find', function(err, categoriesData) {
if (err) { if (err) {
return next(err); return next(err);
} }

@ -20,7 +20,7 @@ Categories.create = function(socket, data, callback) {
Categories.getAll = function(socket, data, callback) { Categories.getAll = function(socket, data, callback) {
async.waterfall([ async.waterfall([
async.apply(db.getSortedSetRangeByScore, 'categories:cid', 0, -1, 0, Date.now()), async.apply(db.getSortedSetRange, 'categories:cid', 0, -1),
async.apply(categories.getCategoriesData), async.apply(categories.getCategoriesData),
function(categories, next) { function(categories, next) {
//Hook changes, there is no req, and res //Hook changes, there is no req, and res

@ -15,7 +15,7 @@ SocketCategories.getRecentReplies = function(socket, cid, callback) {
}; };
SocketCategories.get = function(socket, data, callback) { SocketCategories.get = function(socket, data, callback) {
categories.getCategoriesByPrivilege(socket.uid, 'find', callback); categories.getCategoriesByPrivilege('categories:cid', socket.uid, 'find', callback);
}; };
SocketCategories.getWatchedCategories = function(socket, data, callback) { SocketCategories.getWatchedCategories = function(socket, data, callback) {
@ -117,7 +117,7 @@ SocketCategories.getUsersInCategory = function(socket, cid, callback) {
}; };
SocketCategories.getCategoriesByPrivilege = function(socket, privilege, callback) { SocketCategories.getCategoriesByPrivilege = function(socket, privilege, callback) {
categories.getCategoriesByPrivilege(socket.uid, privilege, callback); categories.getCategoriesByPrivilege('categories:cid', socket.uid, privilege, callback);
}; };
SocketCategories.watch = function(socket, cid, callback) { SocketCategories.watch = function(socket, cid, callback) {

@ -21,7 +21,7 @@ var db = require('./database'),
schemaDate, thisSchemaDate, schemaDate, thisSchemaDate,
// IMPORTANT: REMEMBER TO UPDATE VALUE OF latestSchema // IMPORTANT: REMEMBER TO UPDATE VALUE OF latestSchema
latestSchema = Date.UTC(2015, 6, 3); latestSchema = Date.UTC(2015, 7, 18);
Upgrade.check = function(callback) { Upgrade.check = function(callback) {
db.get('schemaDate', function(err, value) { db.get('schemaDate', function(err, value) {
@ -446,6 +446,46 @@ Upgrade.upgrade = function(callback) {
winston.info('[2015/07/03] Enabling default composer plugin skipped'); winston.info('[2015/07/03] Enabling default composer plugin skipped');
next(); next();
} }
},
function(next) {
thisSchemaDate = Date.UTC(2015, 7, 18);
if (schemaDate < thisSchemaDate) {
updatesMade = true;
winston.info('[2015/08/18] Creating children category sorted sets');
db.getSortedSetRange('categories:cid', 0, -1, function(err, cids) {
if (err) {
return next(err);
}
async.each(cids, function(cid, next) {
db.getObjectFields('category:' + cid, ['parentCid', 'order'], function(err, category) {
if (err) {
return next(err);
}
if (!category) {
return next();
}
if (parseInt(category.parentCid, 10)) {
db.sortedSetAdd('cid:' + category.parentCid + ':children', parseInt(category.order, 10), cid, next);
} else {
db.sortedSetAdd('cid:0:children', parseInt(category.order, 10), cid, next);
}
});
}, function(err) {
if (err) {
return next(err);
}
winston.info('[2015/08/18] Creating children category sorted sets done');
Upgrade.update(thisSchemaDate, next);
});
});
} else {
winston.info('[2015/08/18] Creating children category sorted sets skipped');
next();
}
} }

Loading…
Cancel
Save