feat: #7743 categories

v1.18.x
Barış Soner Uşaklı 6 years ago
parent 45223cded6
commit fcf3e0770b

@ -1,28 +1,17 @@
'use strict'; 'use strict';
var async = require('async'); const _ = require('lodash');
var _ = require('lodash');
var posts = require('../posts'); const posts = require('../posts');
var db = require('../database'); const db = require('../database');
module.exports = function (Categories) { module.exports = function (Categories) {
Categories.getActiveUsers = function (cids, callback) { Categories.getActiveUsers = async function (cids) {
if (!Array.isArray(cids)) { if (!Array.isArray(cids)) {
cids = [cids]; cids = [cids];
} }
async.waterfall([ const pids = await db.getSortedSetRevRange(cids.map(cid => 'cid:' + cid + ':pids'), 0, 24);
function (next) { const postData = await posts.getPostsFields(pids, ['uid']);
db.getSortedSetRevRange(cids.map(cid => 'cid:' + cid + ':pids'), 0, 24, next); return _.uniq(postData.map(post => post.uid).filter(uid => uid));
},
function (pids, next) {
posts.getPostsFields(pids, ['uid'], next);
},
function (posts, next) {
var uids = _.uniq(posts.map(post => post.uid).filter(uid => uid));
next(null, uids);
},
], callback);
}; };
}; };

@ -11,21 +11,16 @@ var utils = require('../utils');
var cache = require('../cache'); var cache = require('../cache');
module.exports = function (Categories) { module.exports = function (Categories) {
Categories.create = function (data, callback) { Categories.create = async function (data) {
var category; const parentCid = data.parentCid ? data.parentCid : 0;
var parentCid = data.parentCid ? data.parentCid : 0; const cid = await db.incrObjectField('global', 'nextCid');
async.waterfall([
function (next) {
db.incrObjectField('global', 'nextCid', next);
},
function (cid, next) {
data.name = data.name || 'Category ' + cid; data.name = data.name || 'Category ' + cid;
var slug = cid + '/' + utils.slugify(data.name); const slug = cid + '/' + utils.slugify(data.name);
var order = data.order || cid; // If no order provided, place it at the end const order = data.order || cid; // If no order provided, place it at the end
var colours = Categories.assignColours(); const colours = Categories.assignColours();
category = { let category = {
cid: cid, cid: cid,
name: data.name, name: data.name,
description: data.description ? data.description : '', description: data.description ? data.description : '',
@ -50,12 +45,10 @@ module.exports = function (Categories) {
category.backgroundImage = data.backgroundImage; category.backgroundImage = data.backgroundImage;
} }
plugins.fireHook('filter:category.create', { category: category, data: data }, next); const result = await plugins.fireHook('filter:category.create', { category: category, data: data });
}, category = result.category;
function (data, next) {
category = data.category;
var defaultPrivileges = [ const defaultPrivileges = [
'find', 'find',
'read', 'read',
'topics:read', 'topics:read',
@ -74,47 +67,32 @@ module.exports = function (Categories) {
'purge', 'purge',
]); ]);
async.series([ await db.setObject('category:' + category.cid, category);
async.apply(db.setObject, 'category:' + category.cid, category), if (!category.descriptionParsed) {
function (next) { await Categories.parseDescription(category.cid, category.description);
if (category.descriptionParsed) {
return next();
} }
Categories.parseDescription(category.cid, category.description, next); await db.sortedSetsAdd(['categories:cid', 'cid:' + parentCid + ':children'], category.order, category.cid);
}, await privileges.categories.give(defaultPrivileges, category.cid, 'registered-users');
async.apply(db.sortedSetsAdd, ['categories:cid', 'cid:' + parentCid + ':children'], category.order, category.cid), await privileges.categories.give(modPrivileges, category.cid, ['administrators', 'Global Moderators']);
async.apply(privileges.categories.give, defaultPrivileges, category.cid, 'registered-users'), await privileges.categories.give(['find', 'read', 'topics:read'], category.cid, ['guests', 'spiders']);
async.apply(privileges.categories.give, modPrivileges, category.cid, ['administrators', 'Global Moderators']),
async.apply(privileges.categories.give, ['find', 'read', 'topics:read'], category.cid, ['guests', 'spiders']),
], next);
},
function (results, next) {
cache.del(['categories:cid', 'cid:' + parentCid + ':children']); cache.del(['categories:cid', 'cid:' + parentCid + ':children']);
if (data.cloneFromCid && parseInt(data.cloneFromCid, 10)) { if (data.cloneFromCid && parseInt(data.cloneFromCid, 10)) {
return Categories.copySettingsFrom(data.cloneFromCid, category.cid, !data.parentCid, next); category = await Categories.copySettingsFrom(data.cloneFromCid, category.cid, !data.parentCid);
} }
next(null, category);
},
function (_category, next) {
category = _category;
if (data.cloneChildren) { if (data.cloneChildren) {
return duplicateCategoriesChildren(category.cid, data.cloneFromCid, data.uid, next); await duplicateCategoriesChildren(category.cid, data.cloneFromCid, data.uid);
} }
next();
},
function (next) {
plugins.fireHook('action:category.create', { category: category }); plugins.fireHook('action:category.create', { category: category });
next(null, category); return category;
},
], callback);
}; };
function duplicateCategoriesChildren(parentCid, cid, uid, callback) { async function duplicateCategoriesChildren(parentCid, cid, uid) {
Categories.getChildren([cid], uid, function (err, children) { let children = await Categories.getChildren([cid], uid);
if (err || !children.length) { if (!children.length) {
return callback(err); return;
} }
children = children[0]; children = children[0];
@ -128,8 +106,7 @@ module.exports = function (Categories) {
child.uid = uid; child.uid = uid;
}); });
async.each(children, Categories.create, callback); await async.each(children, Categories.create);
});
} }
Categories.assignColours = function () { Categories.assignColours = function () {
@ -140,136 +117,89 @@ module.exports = function (Categories) {
return [backgrounds[index], text[index]]; return [backgrounds[index], text[index]];
}; };
Categories.copySettingsFrom = function (fromCid, toCid, copyParent, callback) { Categories.copySettingsFrom = async function (fromCid, toCid, copyParent) {
var destination; const [source, destination] = await Promise.all([
async.waterfall([ db.getObject('category:' + fromCid),
function (next) { db.getObject('category:' + toCid),
async.parallel({ ]);
source: async.apply(db.getObject, 'category:' + fromCid), if (!source) {
destination: async.apply(db.getObject, 'category:' + toCid), throw new Error('[[error:invalid-cid]]');
}, next);
},
function (results, next) {
if (!results.source) {
return next(new Error('[[error:invalid-cid]]'));
} }
destination = results.destination;
var tasks = [];
const oldParent = parseInt(destination.parentCid, 10) || 0; const oldParent = parseInt(destination.parentCid, 10) || 0;
const newParent = parseInt(results.source.parentCid, 10) || 0; const newParent = parseInt(source.parentCid, 10) || 0;
if (copyParent && newParent !== parseInt(toCid, 10)) { if (copyParent && newParent !== parseInt(toCid, 10)) {
tasks.push(async.apply(db.sortedSetRemove, 'cid:' + oldParent + ':children', toCid)); await db.sortedSetRemove('cid:' + oldParent + ':children', toCid);
tasks.push(async.apply(db.sortedSetAdd, 'cid:' + newParent + ':children', results.source.order, toCid)); await db.sortedSetAdd('cid:' + newParent + ':children', source.order, toCid);
tasks.push(function (next) {
cache.del(['cid:' + oldParent + ':children', 'cid:' + newParent + ':children']); cache.del(['cid:' + oldParent + ':children', 'cid:' + newParent + ':children']);
setImmediate(next);
});
} }
destination.description = results.source.description; destination.description = source.description;
destination.descriptionParsed = results.source.descriptionParsed; destination.descriptionParsed = source.descriptionParsed;
destination.icon = results.source.icon; destination.icon = source.icon;
destination.bgColor = results.source.bgColor; destination.bgColor = source.bgColor;
destination.color = results.source.color; destination.color = source.color;
destination.link = results.source.link; destination.link = source.link;
destination.numRecentReplies = results.source.numRecentReplies; destination.numRecentReplies = source.numRecentReplies;
destination.class = results.source.class; destination.class = source.class;
destination.image = results.source.image; destination.image = source.image;
destination.imageClass = results.source.imageClass; destination.imageClass = source.imageClass;
if (copyParent) { if (copyParent) {
destination.parentCid = results.source.parentCid || 0; destination.parentCid = source.parentCid || 0;
} }
tasks.push(async.apply(db.setObject, 'category:' + toCid, destination)); await db.setObject('category:' + toCid, destination);
async.series(tasks, next); await copyTagWhitelist(fromCid, toCid);
},
function (results, next) { await Categories.copyPrivilegesFrom(fromCid, toCid);
copyTagWhitelist(fromCid, toCid, next);
}, return destination;
function (next) {
Categories.copyPrivilegesFrom(fromCid, toCid, next);
},
], function (err) {
callback(err, destination);
});
}; };
function copyTagWhitelist(fromCid, toCid, callback) { async function copyTagWhitelist(fromCid, toCid) {
var data; const data = await db.getSortedSetRangeWithScores('cid:' + fromCid + ':tag:whitelist', 0, -1);
async.waterfall([ await db.delete('cid:' + toCid + ':tag:whitelist');
function (next) { await db.sortedSetAdd('cid:' + toCid + ':tag:whitelist', data.map(item => item.score), data.map(item => item.value));
db.getSortedSetRangeWithScores('cid:' + fromCid + ':tag:whitelist', 0, -1, next); cache.del('cid:' + toCid + ':tag:whitelist');
},
function (_data, next) {
data = _data;
db.delete('cid:' + toCid + ':tag:whitelist', next);
},
function (next) {
db.sortedSetAdd('cid:' + toCid + ':tag:whitelist', data.map(item => item.score), data.map(item => item.value), next);
},
], callback);
} }
Categories.copyPrivilegesFrom = function (fromCid, toCid, group, callback) { Categories.copyPrivilegesFrom = async function (fromCid, toCid, group) {
if (typeof group === 'function') { group = group || '';
callback = group;
group = '';
}
async.waterfall([ const data = await plugins.fireHook('filter:categories.copyPrivilegesFrom', {
function (next) {
plugins.fireHook('filter:categories.copyPrivilegesFrom', {
privileges: group ? privileges.groupPrivilegeList.slice() : privileges.privilegeList.slice(), privileges: group ? privileges.groupPrivilegeList.slice() : privileges.privilegeList.slice(),
fromCid: fromCid, fromCid: fromCid,
toCid: toCid, toCid: toCid,
group: group, group: group,
}, next); });
},
function (data, next) {
if (group) { if (group) {
copyPrivilegesByGroup(data.privileges, data.fromCid, data.toCid, group, next); await copyPrivilegesByGroup(data.privileges, data.fromCid, data.toCid, group);
} else { } else {
copyPrivileges(data.privileges, data.fromCid, data.toCid, next); await copyPrivileges(data.privileges, data.fromCid, data.toCid);
} }
},
], callback);
}; };
function copyPrivileges(privileges, fromCid, toCid, callback) { async function copyPrivileges(privileges, fromCid, toCid) {
const toGroups = privileges.map(privilege => 'group:cid:' + toCid + ':privileges:' + privilege + ':members'); const toGroups = privileges.map(privilege => 'group:cid:' + toCid + ':privileges:' + privilege + ':members');
const fromGroups = privileges.map(privilege => 'group:cid:' + fromCid + ':privileges:' + privilege + ':members'); const fromGroups = privileges.map(privilege => 'group:cid:' + fromCid + ':privileges:' + privilege + ':members');
async.waterfall([
function (next) { const currentMembers = await db.getSortedSetsMembers(toGroups.concat(fromGroups));
db.getSortedSetsMembers(toGroups.concat(fromGroups), next);
},
function (currentMembers, next) {
const copyGroups = _.uniq(_.flatten(currentMembers)); const copyGroups = _.uniq(_.flatten(currentMembers));
async.each(copyGroups, function (group, next) { await async.each(copyGroups, async function (group) {
copyPrivilegesByGroup(privileges, fromCid, toCid, group, next); await copyPrivilegesByGroup(privileges, fromCid, toCid, group);
}, next); });
},
], callback);
} }
function copyPrivilegesByGroup(privileges, fromCid, toCid, group, callback) { async function copyPrivilegesByGroup(privileges, fromCid, toCid, group) {
async.waterfall([
function (next) {
const leaveGroups = privileges.map(privilege => 'cid:' + toCid + ':privileges:' + privilege); const leaveGroups = privileges.map(privilege => 'cid:' + toCid + ':privileges:' + privilege);
groups.leave(leaveGroups, group, next); await groups.leave(leaveGroups, group);
},
function (next) {
const checkGroups = privileges.map(privilege => 'group:cid:' + fromCid + ':privileges:' + privilege + ':members'); const checkGroups = privileges.map(privilege => 'group:cid:' + fromCid + ':privileges:' + privilege + ':members');
db.isMemberOfSortedSets(checkGroups, group, next); const isMembers = await db.isMemberOfSortedSets(checkGroups, group);
},
function (isMembers, next) {
privileges = privileges.filter((priv, index) => isMembers[index]); privileges = privileges.filter((priv, index) => isMembers[index]);
const joinGroups = privileges.map(privilege => 'cid:' + toCid + ':privileges:' + privilege); const joinGroups = privileges.map(privilege => 'cid:' + toCid + ':privileges:' + privilege);
groups.join(joinGroups, group, next); await groups.join(joinGroups, group);
},
], callback);
} }
}; };

@ -1,6 +1,5 @@
'use strict'; 'use strict';
var async = require('async');
var validator = require('validator'); var validator = require('validator');
var db = require('../database'); var db = require('../database');
@ -11,64 +10,51 @@ const intFields = [
]; ];
module.exports = function (Categories) { module.exports = function (Categories) {
Categories.getCategoriesFields = function (cids, fields, callback) { Categories.getCategoriesFields = async function (cids, fields) {
if (!Array.isArray(cids) || !cids.length) { if (!Array.isArray(cids) || !cids.length) {
return setImmediate(callback, null, []); return [];
} }
let categories;
async.waterfall([
function (next) {
const keys = cids.map(cid => 'category:' + cid); const keys = cids.map(cid => 'category:' + cid);
if (fields.length) { if (fields.length) {
db.getObjectsFields(keys, fields, next); categories = await db.getObjectsFields(keys, fields);
} else { } else {
db.getObjects(keys, next); categories = await db.getObjects(keys);
} }
},
function (categories, next) {
categories.forEach(category => modifyCategory(category, fields)); categories.forEach(category => modifyCategory(category, fields));
next(null, categories); return categories;
},
], callback);
}; };
Categories.getCategoryData = function (cid, callback) { Categories.getCategoryData = async function (cid) {
Categories.getCategoriesFields([cid], [], function (err, categories) { const categories = await Categories.getCategoriesFields([cid], []);
callback(err, categories && categories.length ? categories[0] : null); return categories && categories.length ? categories[0] : null;
});
}; };
Categories.getCategoriesData = function (cids, callback) { Categories.getCategoriesData = async function (cids) {
Categories.getCategoriesFields(cids, [], callback); return await Categories.getCategoriesFields(cids, []);
}; };
Categories.getCategoryField = function (cid, field, callback) { Categories.getCategoryField = async function (cid, field) {
Categories.getCategoryFields(cid, [field], function (err, category) { const category = await Categories.getCategoryFields(cid, [field]);
callback(err, category ? category[field] : null); return category ? category[field] : null;
});
}; };
Categories.getCategoryFields = function (cid, fields, callback) { Categories.getCategoryFields = async function (cid, fields) {
Categories.getCategoriesFields([cid], fields, function (err, categories) { const categories = await Categories.getCategoriesFields([cid], fields);
callback(err, categories ? categories[0] : null); return categories ? categories[0] : null;
});
}; };
Categories.getAllCategoryFields = function (fields, callback) { Categories.getAllCategoryFields = async function (fields) {
async.waterfall([ const cids = await Categories.getAllCidsFromSet('categories:cid');
async.apply(Categories.getAllCidsFromSet, 'categories:cid'), return await Categories.getCategoriesFields(cids, fields);
function (cids, next) {
Categories.getCategoriesFields(cids, fields, next);
},
], callback);
}; };
Categories.setCategoryField = function (cid, field, value, callback) { Categories.setCategoryField = async function (cid, field, value) {
db.setObjectField('category:' + cid, field, value, callback); await db.setObjectField('category:' + cid, field, value);
}; };
Categories.incrementCategoryFieldBy = function (cid, field, value, callback) { Categories.incrementCategoryFieldBy = async function (cid, field, value) {
db.incrObjectFieldBy('category:' + cid, field, value, callback); await db.incrObjectFieldBy('category:' + cid, field, value);
}; };
}; };

@ -10,43 +10,25 @@ var privileges = require('../privileges');
var cache = require('../cache'); var cache = require('../cache');
module.exports = function (Categories) { module.exports = function (Categories) {
Categories.purge = function (cid, uid, callback) { Categories.purge = async function (cid, uid) {
async.waterfall([ await batch.processSortedSet('cid:' + cid + ':tids', async function (tids) {
function (next) { await async.eachLimit(tids, 10, async function (tid) {
batch.processSortedSet('cid:' + cid + ':tids', function (tids, next) { await topics.purgePostsAndTopic(tid, uid);
async.eachLimit(tids, 10, function (tid, next) { });
topics.purgePostsAndTopic(tid, uid, next); }, { alwaysStartAt: 0 });
}, next);
}, { alwaysStartAt: 0 }, next); const pinnedTids = await db.getSortedSetRevRange('cid:' + cid + ':tids:pinned', 0, -1);
}, await async.eachLimit(pinnedTids, 10, async function (tid) {
function (next) { await topics.purgePostsAndTopic(tid, uid);
db.getSortedSetRevRange('cid:' + cid + ':tids:pinned', 0, -1, next); });
}, await purgeCategory(cid);
function (pinnedTids, next) {
async.eachLimit(pinnedTids, 10, function (tid, next) {
topics.purgePostsAndTopic(tid, uid, next);
}, next);
},
function (next) {
purgeCategory(cid, next);
},
function (next) {
plugins.fireHook('action:category.delete', { cid: cid, uid: uid }); plugins.fireHook('action:category.delete', { cid: cid, uid: uid });
next();
},
], callback);
}; };
function purgeCategory(cid, callback) { async function purgeCategory(cid) {
async.series([ await db.sortedSetRemove('categories:cid', cid);
function (next) { await removeFromParent(cid);
db.sortedSetRemove('categories:cid', cid, next); await db.deleteAll([
},
function (next) {
removeFromParent(cid, next);
},
function (next) {
db.deleteAll([
'cid:' + cid + ':tids', 'cid:' + cid + ':tids',
'cid:' + cid + ':tids:pinned', 'cid:' + cid + ':tids:pinned',
'cid:' + cid + ':tids:posts', 'cid:' + cid + ':tids:posts',
@ -56,61 +38,34 @@ module.exports = function (Categories) {
'cid:' + cid + ':children', 'cid:' + cid + ':children',
'cid:' + cid + ':tag:whitelist', 'cid:' + cid + ':tag:whitelist',
'category:' + cid, 'category:' + cid,
], next); ]);
}, await groups.destroy(privileges.privilegeList.map(privilege => 'cid:' + cid + ':privileges:' + privilege));
function (next) {
groups.destroy(privileges.privilegeList.map(privilege => 'cid:' + cid + ':privileges:' + privilege), next);
},
], function (err) {
callback(err);
});
} }
function removeFromParent(cid, callback) { async function removeFromParent(cid) {
async.waterfall([ const [parentCid, children] = await Promise.all([
function (next) { Categories.getCategoryField(cid, 'parentCid'),
async.parallel({ db.getSortedSetRange('cid:' + cid + ':children', 0, -1),
parentCid: function (next) { ]);
Categories.getCategoryField(cid, 'parentCid', next);
}, const bulkAdd = [];
children: function (next) { const childrenKeys = children.map(function (cid) {
db.getSortedSetRange('cid:' + cid + ':children', 0, -1, next); bulkAdd.push(['cid:0:children', cid, cid]);
}, return 'category:' + cid;
}, next); });
},
function (results, next) { await Promise.all([
async.parallel([ db.sortedSetRemove('cid:' + parentCid + ':children', cid),
function (next) { db.setObjectField(childrenKeys, 'parentCid', 0),
db.sortedSetRemove('cid:' + results.parentCid + ':children', cid, next); db.sortedSetAddBulk(bulkAdd),
}, ]);
function (next) {
async.each(results.children, function (cid, next) {
async.parallel([
function (next) {
db.setObjectField('category:' + cid, 'parentCid', 0, next);
},
function (next) {
db.sortedSetAdd('cid:0:children', cid, cid, next);
},
], next);
}, next);
},
], function (err) {
if (err) {
return next(err);
}
cache.del([ cache.del([
'categories:cid', 'categories:cid',
'cid:0:children', 'cid:0:children',
'cid:' + results.parentCid + ':children', 'cid:' + parentCid + ':children',
'cid:' + cid + ':children', 'cid:' + cid + ':children',
'cid:' + cid + ':tag:whitelist', 'cid:' + cid + ':tag:whitelist',
]); ]);
next();
});
},
], function (err) {
callback(err);
});
} }
}; };

@ -1,7 +1,6 @@
'use strict'; 'use strict';
var async = require('async');
var _ = require('lodash'); var _ = require('lodash');
var db = require('../database'); var db = require('../database');
@ -23,137 +22,85 @@ require('./recentreplies')(Categories);
require('./update')(Categories); require('./update')(Categories);
require('./watch')(Categories); require('./watch')(Categories);
Categories.exists = function (cid, callback) { Categories.exists = async function (cid) {
db.exists('category:' + cid, callback); return await db.exists('category:' + cid);
}; };
Categories.getCategoryById = function (data, callback) { Categories.getCategoryById = async function (data) {
var category; const categories = await Categories.getCategories([data.cid], data.uid);
async.waterfall([
function (next) {
Categories.getCategories([data.cid], data.uid, next);
},
function (categories, next) {
if (!categories[0]) { if (!categories[0]) {
return callback(null, null); return null;
} }
category = categories[0]; const category = categories[0];
data.category = category; data.category = category;
async.parallel({
topics: function (next) { const promises = [
Categories.getCategoryTopics(data, next); Categories.getCategoryTopics(data),
}, Categories.getTopicCount(data),
topicCount: function (next) { Categories.getWatchState([data.cid], data.uid),
Categories.getTopicCount(data, next); getChildrenTree(category, data.uid),
}, ];
watchState: function (next) {
Categories.getWatchState([data.cid], data.uid, next);
},
parent: function (next) {
if (category.parentCid) { if (category.parentCid) {
Categories.getCategoryData(category.parentCid, next); promises.push(Categories.getCategoryData(category.parentCid));
} else {
next();
} }
}, const [topics, topicCount, watchState, , parent] = await Promise.all(promises);
children: function (next) {
getChildrenTree(category, data.uid, next); category.topics = topics.topics;
}, category.nextStart = topics.nextStart;
}, next); category.topic_count = topicCount;
}, category.isWatched = watchState[0] === Categories.watchStates.watching;
function (results, next) { category.isNotWatched = watchState[0] === Categories.watchStates.notwatching;
category.topics = results.topics.topics; category.isIgnored = watchState[0] === Categories.watchStates.ignoring;
category.nextStart = results.topics.nextStart; category.parent = parent;
category.topic_count = results.topicCount;
category.isWatched = results.watchState[0] === Categories.watchStates.watching;
category.isNotWatched = results.watchState[0] === Categories.watchStates.notwatching;
category.isIgnored = results.watchState[0] === Categories.watchStates.ignoring;
category.parent = results.parent;
calculateTopicPostCount(category); calculateTopicPostCount(category);
plugins.fireHook('filter:category.get', { category: category, uid: data.uid }, next); const result = await plugins.fireHook('filter:category.get', { category: category, uid: data.uid });
}, return result.category;
function (data, next) {
next(null, data.category);
},
], callback);
}; };
Categories.getAllCidsFromSet = function (key, callback) { Categories.getAllCidsFromSet = async function (key) {
const cids = cache.get(key); let cids = cache.get(key);
if (cids) { if (cids) {
return setImmediate(callback, null, cids.slice()); return cids.slice();
} }
db.getSortedSetRange(key, 0, -1, function (err, cids) { cids = await db.getSortedSetRange(key, 0, -1);
if (err) {
return callback(err);
}
cache.set(key, cids); cache.set(key, cids);
callback(null, cids.slice()); return cids.slice();
});
}; };
Categories.getAllCategories = function (uid, callback) { Categories.getAllCategories = async function (uid) {
async.waterfall([ const cids = await Categories.getAllCidsFromSet('categories:cid');
function (next) { return await Categories.getCategories(cids, uid);
Categories.getAllCidsFromSet('categories:cid', next);
},
function (cids, next) {
Categories.getCategories(cids, uid, next);
},
], callback);
}; };
Categories.getCidsByPrivilege = function (set, uid, privilege, callback) { Categories.getCidsByPrivilege = async function (set, uid, privilege) {
async.waterfall([ const cids = await Categories.getAllCidsFromSet('categories:cid');
function (next) { return await privileges.categories.filterCids(privilege, cids, uid);
Categories.getAllCidsFromSet(set, next);
},
function (cids, next) {
privileges.categories.filterCids(privilege, cids, uid, next);
},
], callback);
}; };
Categories.getCategoriesByPrivilege = function (set, uid, privilege, callback) { Categories.getCategoriesByPrivilege = async function (set, uid, privilege) {
async.waterfall([ const cids = await Categories.getCidsByPrivilege(set, uid, privilege);
function (next) { return await Categories.getCategories(cids, uid);
Categories.getCidsByPrivilege(set, uid, privilege, next);
},
function (cids, next) {
Categories.getCategories(cids, uid, next);
},
], callback);
}; };
Categories.getModerators = function (cid, callback) { Categories.getModerators = async function (cid) {
async.waterfall([ const uids = await Categories.getModeratorUids([cid]);
function (next) { return await user.getUsersFields(uids[0], ['uid', 'username', 'userslug', 'picture']);
Categories.getModeratorUids([cid], next);
},
function (uids, next) {
user.getUsersFields(uids[0], ['uid', 'username', 'userslug', 'picture'], next);
},
], callback);
}; };
Categories.getModeratorUids = function (cids, callback) { Categories.getModeratorUids = async function (cids) {
var sets; const groupNames = cids.reduce(function (memo, cid) {
var uniqGroups;
async.waterfall([
function (next) {
var groupNames = cids.reduce(function (memo, cid) {
memo.push('cid:' + cid + ':privileges:moderate'); memo.push('cid:' + cid + ':privileges:moderate');
memo.push('cid:' + cid + ':privileges:groups:moderate'); memo.push('cid:' + cid + ':privileges:groups:moderate');
return memo; return memo;
}, []); }, []);
groups.getMembersOfGroups(groupNames, next); const memberSets = await groups.getMembersOfGroups(groupNames);
},
function (memberSets, next) {
// Every other set is actually a list of user groups, not uids, so convert those to members // Every other set is actually a list of user groups, not uids, so convert those to members
sets = memberSets.reduce(function (memo, set, idx) { const sets = memberSets.reduce(function (memo, set, idx) {
if (idx % 2) { if (idx % 2) {
memo.groupNames.push(set); memo.groupNames.push(set);
} else { } else {
@ -163,55 +110,40 @@ Categories.getModeratorUids = function (cids, callback) {
return memo; return memo;
}, { groupNames: [], uids: [] }); }, { groupNames: [], uids: [] });
uniqGroups = _.uniq(_.flatten(sets.groupNames)); const uniqGroups = _.uniq(_.flatten(sets.groupNames));
groups.getMembersOfGroups(uniqGroups, next); const groupUids = await groups.getMembersOfGroups(uniqGroups);
}, const map = _.zipObject(uniqGroups, groupUids);
function (groupUids, next) {
var map = _.zipObject(uniqGroups, groupUids);
const moderatorUids = cids.map(function (cid, index) { const moderatorUids = cids.map(function (cid, index) {
return _.uniq(sets.uids[index].concat(_.flatten(sets.groupNames[index].map(g => map[g])))); return _.uniq(sets.uids[index].concat(_.flatten(sets.groupNames[index].map(g => map[g]))));
}); });
next(null, moderatorUids); return moderatorUids;
},
], callback);
}; };
Categories.getCategories = function (cids, uid, callback) { Categories.getCategories = async function (cids, uid) {
if (!Array.isArray(cids)) { if (!Array.isArray(cids)) {
return callback(new Error('[[error:invalid-cid]]')); throw new Error('[[error:invalid-cid]]');
} }
if (!cids.length) { if (!cids.length) {
return callback(null, []); return [];
} }
uid = parseInt(uid, 10); uid = parseInt(uid, 10);
async.waterfall([
function (next) { const [categories, tagWhitelist, hasRead] = await Promise.all([
async.parallel({ Categories.getCategoriesData(cids),
categories: function (next) { Categories.getTagWhitelist(cids),
Categories.getCategoriesData(cids, next); Categories.hasReadCategories(cids, uid),
}, ]);
tagWhitelist: function (next) { categories.forEach(function (category, i) {
Categories.getTagWhitelist(cids, next);
},
hasRead: function (next) {
Categories.hasReadCategories(cids, uid, next);
},
}, next);
},
function (results, next) {
results.categories.forEach(function (category, i) {
if (category) { if (category) {
category.tagWhitelist = results.tagWhitelist[i]; category.tagWhitelist = tagWhitelist[i];
category['unread-class'] = (category.topic_count === 0 || (results.hasRead[i] && uid !== 0)) ? '' : 'unread'; category['unread-class'] = (category.topic_count === 0 || (hasRead[i] && uid !== 0)) ? '' : 'unread';
} }
}); });
next(null, results.categories); return categories;
},
], callback);
}; };
Categories.getTagWhitelist = function (cids, callback) { Categories.getTagWhitelist = async function (cids) {
const cachedData = {}; const cachedData = {};
const nonCachedCids = cids.filter((cid) => { const nonCachedCids = cids.filter((cid) => {
@ -224,20 +156,17 @@ Categories.getTagWhitelist = function (cids, callback) {
}); });
if (!nonCachedCids.length) { if (!nonCachedCids.length) {
return setImmediate(callback, null, _.clone(cids.map(cid => cachedData[cid]))); return _.clone(cids.map(cid => cachedData[cid]));
} }
const keys = nonCachedCids.map(cid => 'cid:' + cid + ':tag:whitelist'); const keys = nonCachedCids.map(cid => 'cid:' + cid + ':tag:whitelist');
db.getSortedSetsMembers(keys, function (err, data) { const data = await db.getSortedSetsMembers(keys);
if (err) {
return callback(err);
}
nonCachedCids.forEach((cid, index) => { nonCachedCids.forEach((cid, index) => {
cachedData[cid] = data[index]; cachedData[cid] = data[index];
cache.set('cid:' + cid + ':tag:whitelist', data[index]); cache.set('cid:' + cid + ':tag:whitelist', data[index]);
}); });
callback(null, _.clone(cids.map(cid => cachedData[cid]))); return _.clone(cids.map(cid => cachedData[cid]));
});
}; };
function calculateTopicPostCount(category) { function calculateTopicPostCount(category) {
@ -263,114 +192,65 @@ function calculateTopicPostCount(category) {
category.totalTopicCount = topicCount; category.totalTopicCount = topicCount;
} }
Categories.getParents = function (cids, callback) { Categories.getParents = async function (cids) {
var categoriesData; const categoriesData = await Categories.getCategoriesFields(cids, ['parentCid']);
var parentCids; const parentCids = categoriesData.filter(c => c && c.parentCid).map(c => c.parentCid);
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);
if (!parentCids.length) { if (!parentCids.length) {
return callback(null, cids.map(() => null)); return cids.map(() => null);
} }
const parentData = await Categories.getCategoriesData(parentCids);
Categories.getCategoriesData(parentCids, next);
},
function (parentData, next) {
const cidToParent = _.zipObject(parentCids, parentData); const cidToParent = _.zipObject(parentCids, parentData);
parentData = categoriesData.map(category => cidToParent[category.parentCid]); return categoriesData.map(category => cidToParent[category.parentCid]);
next(null, parentData);
},
], callback);
}; };
Categories.getChildren = function (cids, uid, callback) { Categories.getChildren = async function (cids, uid) {
var categories; const categoryData = await Categories.getCategoriesFields(cids, ['parentCid']);
async.waterfall([ const categories = categoryData.map((category, index) => ({ cid: cids[index], parentCid: category.parentCid }));
function (next) { await Promise.all(categories.map(c => getChildrenTree(c, uid)));
Categories.getCategoriesFields(cids, ['parentCid'], next); return categories.map(c => c && c.children);
},
function (categoryData, next) {
categories = categoryData.map((category, index) => ({ cid: cids[index], parentCid: category.parentCid }));
async.each(categories, function (category, next) {
getChildrenTree(category, uid, next);
}, next);
},
function (next) {
next(null, categories.map(c => c && c.children));
},
], callback);
}; };
function getChildrenTree(category, uid, callback) { async function getChildrenTree(category, uid) {
let children; let childrenCids = await Categories.getChildrenCids(category.cid);
async.waterfall([ childrenCids = await privileges.categories.filterCids('find', childrenCids, uid);
function (next) { childrenCids = childrenCids.filter(cid => parseInt(category.cid, 10) !== parseInt(cid, 10));
Categories.getChildrenCids(category.cid, next); if (!childrenCids.length) {
},
function (children, next) {
privileges.categories.filterCids('find', children, uid, next);
},
function (children, next) {
children = children.filter(cid => parseInt(category.cid, 10) !== parseInt(cid, 10));
if (!children.length) {
category.children = []; category.children = [];
return callback(); return;
} }
Categories.getCategoriesData(children, next); let childrenData = await Categories.getCategoriesData(childrenCids);
}, childrenData = childrenData.filter(Boolean);
function (_children, next) { childrenCids = childrenData.map(child => child.cid);
children = _children.filter(Boolean); const hasRead = await Categories.hasReadCategories(childrenCids, uid);
childrenData.forEach(function (child, i) {
const cids = children.map(child => child.cid); child['unread-class'] = (child.topic_count === 0 || (hasRead[i] && uid !== 0)) ? '' : 'unread';
Categories.hasReadCategories(cids, uid, next);
},
function (hasRead, next) {
hasRead.forEach(function (read, i) {
const child = children[i];
child['unread-class'] = (child.topic_count === 0 || (read && uid !== 0)) ? '' : 'unread';
}); });
Categories.getTree([category].concat(children), category.parentCid); Categories.getTree([category].concat(childrenData), category.parentCid);
next();
},
], callback);
} }
Categories.getChildrenCids = function (rootCid, callback) { Categories.getChildrenCids = async function (rootCid) {
let allCids = []; let allCids = [];
function recursive(keys, callback) { async function recursive(keys) {
db.getSortedSetRange(keys, 0, -1, function (err, childrenCids) { let childrenCids = await db.getSortedSetRange(keys, 0, -1);
if (err) {
return callback(err);
}
childrenCids = childrenCids.filter(cid => !allCids.includes(parseInt(cid, 10))); childrenCids = childrenCids.filter(cid => !allCids.includes(parseInt(cid, 10)));
if (!childrenCids.length) { if (!childrenCids.length) {
return callback(); return;
} }
const keys = childrenCids.map(cid => 'cid:' + cid + ':children'); keys = childrenCids.map(cid => 'cid:' + cid + ':children');
childrenCids.forEach(cid => allCids.push(parseInt(cid, 10))); childrenCids.forEach(cid => allCids.push(parseInt(cid, 10)));
recursive(keys, callback); recursive(keys);
});
} }
const key = 'cid:' + rootCid + ':children'; const key = 'cid:' + rootCid + ':children';
const childrenCids = cache.get(key); const childrenCids = cache.get(key);
if (childrenCids) { if (childrenCids) {
return setImmediate(callback, null, childrenCids.slice()); return childrenCids.slice();
} }
recursive(key, function (err) { await recursive(key);
if (err) {
return callback(err);
}
allCids = _.uniq(allCids); allCids = _.uniq(allCids);
cache.set(key, allCids); cache.set(key, allCids);
callback(null, allCids.slice()); return allCids.slice();
});
}; };
Categories.flattenCategories = function (allCategories, categoryData) { Categories.flattenCategories = function (allCategories, categoryData) {
@ -440,19 +320,13 @@ Categories.getTree = function (categories, parentCid) {
return tree; return tree;
}; };
Categories.buildForSelect = function (uid, privilege, callback) { Categories.buildForSelect = async function (uid, privilege) {
async.waterfall([ let categories = await Categories.getCategoriesByPrivilege('categories:cid', uid, privilege);
function (next) {
Categories.getCategoriesByPrivilege('categories:cid', uid, privilege, next);
},
function (categories, next) {
categories = Categories.getTree(categories); categories = Categories.getTree(categories);
Categories.buildForSelectCategories(categories, next); return await Categories.buildForSelectCategories(categories);
},
], callback);
}; };
Categories.buildForSelectCategories = function (categories, callback) { Categories.buildForSelectCategories = async function (categories) {
function recursive(category, categoriesData, level, depth) { function recursive(category, categoriesData, level, depth) {
var bullet = level ? '• ' : ''; var bullet = level ? '• ' : '';
category.value = category.cid; category.value = category.cid;
@ -474,7 +348,7 @@ Categories.buildForSelectCategories = function (categories, callback) {
categories.forEach(function (category) { categories.forEach(function (category) {
recursive(category, categoriesData, '', 0); recursive(category, categoriesData, '', 0);
}); });
callback(null, categoriesData); return categoriesData;
}; };
Categories.async = require('../promisify')(Categories); Categories.async = require('../promisify')(Categories);

@ -1,7 +1,6 @@
'use strict'; 'use strict';
var async = require('async');
var _ = require('lodash'); var _ = require('lodash');
var db = require('../database'); var db = require('../database');
@ -11,128 +10,76 @@ var privileges = require('../privileges');
var batch = require('../batch'); var batch = require('../batch');
module.exports = function (Categories) { module.exports = function (Categories) {
Categories.getRecentReplies = function (cid, uid, count, callback) { Categories.getRecentReplies = async function (cid, uid, count) {
if (!parseInt(count, 10)) { if (!parseInt(count, 10)) {
return callback(null, []); return [];
} }
let pids = await db.getSortedSetRevRange('cid:' + cid + ':pids', 0, count - 1);
async.waterfall([ pids = await privileges.posts.filter('topics:read', pids, uid);
function (next) { return await posts.getPostSummaryByPids(pids, uid, { stripTags: true });
db.getSortedSetRevRange('cid:' + cid + ':pids', 0, count - 1, next);
},
function (pids, next) {
privileges.posts.filter('topics:read', pids, uid, next);
},
function (pids, next) {
posts.getPostSummaryByPids(pids, uid, { stripTags: true }, next);
},
], callback);
}; };
Categories.updateRecentTid = function (cid, tid, callback) { Categories.updateRecentTid = async function (cid, tid) {
async.waterfall([ const [count, numRecentReplies] = await Promise.all([
function (next) { db.sortedSetCard('cid:' + cid + ':recent_tids'),
async.parallel({ db.getObjectField('category:' + cid, 'numRecentReplies'),
count: function (next) { ]);
db.sortedSetCard('cid:' + cid + ':recent_tids', next);
}, if (count < numRecentReplies) {
numRecentReplies: function (next) { return await db.sortedSetAdd('cid:' + cid + ':recent_tids', Date.now(), tid);
db.getObjectField('category:' + cid, 'numRecentReplies', next);
},
}, next);
},
function (results, next) {
if (results.count < results.numRecentReplies) {
return db.sortedSetAdd('cid:' + cid + ':recent_tids', Date.now(), tid, callback);
} }
db.getSortedSetRangeWithScores('cid:' + cid + ':recent_tids', 0, results.count - results.numRecentReplies, next); const data = await db.getSortedSetRangeWithScores('cid:' + cid + ':recent_tids', 0, count - numRecentReplies);
}, if (data.length) {
function (data, next) { await db.sortedSetsRemoveRangeByScore(['cid:' + cid + ':recent_tids'], '-inf', data[data.length - 1].score);
if (!data.length) {
return next();
} }
db.sortedSetsRemoveRangeByScore(['cid:' + cid + ':recent_tids'], '-inf', data[data.length - 1].score, next); await db.sortedSetAdd('cid:' + cid + ':recent_tids', Date.now(), tid);
},
function (next) {
db.sortedSetAdd('cid:' + cid + ':recent_tids', Date.now(), tid, next);
},
], callback);
}; };
Categories.updateRecentTidForCid = function (cid, callback) { Categories.updateRecentTidForCid = async function (cid) {
async.waterfall([ const pids = await db.getSortedSetRevRange('cid:' + cid + ':pids', 0, 0);
function (next) { if (!pids.length) {
db.getSortedSetRevRange('cid:' + cid + ':pids', 0, 0, next); return;
}, }
function (pid, next) { const tid = await posts.getPostField(pids[0], 'tid');
pid = pid[0];
posts.getPostField(pid, 'tid', next);
},
function (tid, next) {
if (!tid) { if (!tid) {
return next(); return;
} }
await Categories.updateRecentTid(cid, tid);
Categories.updateRecentTid(cid, tid, next);
},
], callback);
}; };
Categories.getRecentTopicReplies = function (categoryData, uid, callback) { Categories.getRecentTopicReplies = async function (categoryData, uid) {
if (!Array.isArray(categoryData) || !categoryData.length) { if (!Array.isArray(categoryData) || !categoryData.length) {
return callback(); return;
} }
async.waterfall([
function (next) {
const categoriesToLoad = categoryData.filter(category => category && category.numRecentReplies && parseInt(category.numRecentReplies, 10) > 0); const categoriesToLoad = categoryData.filter(category => category && category.numRecentReplies && parseInt(category.numRecentReplies, 10) > 0);
const keys = categoriesToLoad.map(category => 'cid:' + category.cid + ':recent_tids'); const keys = categoriesToLoad.map(category => 'cid:' + category.cid + ':recent_tids');
db.getSortedSetsMembers(keys, next); const results = await db.getSortedSetsMembers(keys);
}, let tids = _.uniq(_.flatten(results).filter(Boolean));
function (results, next) {
var tids = _.uniq(_.flatten(results).filter(Boolean));
privileges.topics.filterTids('topics:read', tids, uid, next); tids = await privileges.topics.filterTids('topics:read', tids, uid);
}, const topics = await getTopics(tids, uid);
function (tids, next) {
getTopics(tids, uid, next);
},
function (topics, next) {
assignTopicsToCategories(categoryData, topics); assignTopicsToCategories(categoryData, topics);
bubbleUpChildrenPosts(categoryData); bubbleUpChildrenPosts(categoryData);
next();
},
], callback);
}; };
function getTopics(tids, uid, callback) { async function getTopics(tids, uid) {
var topicData; const topicData = await topics.getTopicsFields(tids, ['tid', 'mainPid', 'slug', 'title', 'teaserPid', 'cid', 'postcount']);
async.waterfall([
function (next) {
topics.getTopicsFields(tids, ['tid', 'mainPid', 'slug', 'title', 'teaserPid', 'cid', 'postcount'], next);
},
function (_topicData, next) {
topicData = _topicData;
topicData.forEach(function (topic) { topicData.forEach(function (topic) {
if (topic) { if (topic) {
topic.teaserPid = topic.teaserPid || topic.mainPid; topic.teaserPid = topic.teaserPid || topic.mainPid;
} }
}); });
var cids = _.uniq(topicData.map(topic => topic && topic.cid).filter(cid => parseInt(cid, 10))); var cids = _.uniq(topicData.map(topic => topic && topic.cid).filter(cid => parseInt(cid, 10)));
const [categoryData, teasers] = await Promise.all([
async.parallel({ Categories.getCategoriesFields(cids, ['cid', 'parentCid']),
categoryData: async.apply(Categories.getCategoriesFields, cids, ['cid', 'parentCid']), topics.getTeasers(topicData, uid),
teasers: async.apply(topics.getTeasers, _topicData, uid), ]);
}, next);
},
function (results, next) {
var parentCids = {}; var parentCids = {};
results.categoryData.forEach(function (category) { categoryData.forEach(function (category) {
parentCids[category.cid] = category.parentCid; parentCids[category.cid] = category.parentCid;
}); });
results.teasers.forEach(function (teaser, index) { teasers.forEach(function (teaser, index) {
if (teaser) { if (teaser) {
teaser.cid = topicData[index].cid; teaser.cid = topicData[index].cid;
teaser.parentCid = parseInt(parentCids[teaser.cid], 10) || 0; teaser.parentCid = parseInt(parentCids[teaser.cid], 10) || 0;
@ -144,10 +91,7 @@ module.exports = function (Categories) {
}; };
} }
}); });
results.teasers = results.teasers.filter(Boolean); return teasers.filter(Boolean);
next(null, results.teasers);
},
], callback);
} }
function assignTopicsToCategories(categories, topics) { function assignTopicsToCategories(categories, topics) {
@ -188,80 +132,43 @@ module.exports = function (Categories) {
getPostsRecursive(child, posts); getPostsRecursive(child, posts);
}); });
} }
// terrible name, should be topics.moveTopicPosts
Categories.moveRecentReplies = function (tid, oldCid, cid, callback) { Categories.moveRecentReplies = async function (tid, oldCid, cid) {
callback = callback || function () {}; await updatePostCount(tid, oldCid, cid);
const pids = await topics.getPids(tid);
async.waterfall([
function (next) { await batch.processArray(pids, async function (pids) {
updatePostCount(tid, oldCid, cid, next); const postData = await posts.getPostsFields(pids, ['pid', 'uid', 'timestamp', 'upvotes', 'downvotes']);
}, const timestamps = postData.map(p => p && p.timestamp);
function (next) { const bulkRemove = [];
topics.getPids(tid, next); const bulkAdd = [];
}, postData.forEach((post) => {
function (pids, next) { bulkRemove.push(['cid:' + oldCid + ':uid:' + post.uid + ':pids', post.pid]);
batch.processArray(pids, function (pids, next) { bulkRemove.push(['cid:' + oldCid + ':uid:' + post.uid + ':pids:votes', post.pid]);
async.waterfall([ bulkAdd.push(['cid:' + cid + ':uid:' + post.uid + ':pids', post.timestamp, post.pid]);
function (next) {
posts.getPostsFields(pids, ['pid', 'uid', 'timestamp', 'upvotes', 'downvotes'], next);
},
function (postData, next) {
var timestamps = postData.map(p => p && p.timestamp);
async.parallel([
function (next) {
db.sortedSetRemove('cid:' + oldCid + ':pids', pids, next);
},
function (next) {
db.sortedSetAdd('cid:' + cid + ':pids', timestamps, pids, next);
},
function (next) {
async.each(postData, function (post, next) {
db.sortedSetRemove([
'cid:' + oldCid + ':uid:' + post.uid + ':pids',
'cid:' + oldCid + ':uid:' + post.uid + ':pids:votes',
], post.pid, next);
}, next);
},
function (next) {
async.each(postData, function (post, next) {
const keys = ['cid:' + cid + ':uid:' + post.uid + ':pids'];
const scores = [post.timestamp];
if (post.votes > 0) { if (post.votes > 0) {
keys.push('cid:' + cid + ':uid:' + post.uid + ':pids:votes'); bulkAdd.push(['cid:' + cid + ':uid:' + post.uid + ':pids:votes', post.votes, post.pid]);
scores.push(post.votes);
} }
db.sortedSetsAdd(keys, scores, post.pid, next); });
}, next);
}, await Promise.all([
], next); db.sortedSetRemove('cid:' + oldCid + ':pids', pids),
}, db.sortedSetAdd('cid:' + cid + ':pids', timestamps, pids),
], next); db.sortedSetRemoveBulk(bulkRemove),
}, next); db.sortedSetAddBulk(bulkAdd),
}, ]);
], callback); }, { batch: 500 });
}; };
function updatePostCount(tid, oldCid, newCid, callback) { async function updatePostCount(tid, oldCid, newCid) {
async.waterfall([ const postCount = await topics.getTopicField(tid, 'postcount');
function (next) {
topics.getTopicField(tid, 'postcount', next);
},
function (postCount, next) {
if (!postCount) { if (!postCount) {
return callback(); return;
} }
async.parallel([
function (next) { await Promise.all([
db.incrObjectFieldBy('category:' + oldCid, 'post_count', -postCount, next); db.incrObjectFieldBy('category:' + oldCid, 'post_count', -postCount),
}, db.incrObjectFieldBy('category:' + newCid, 'post_count', postCount),
function (next) { ]);
db.incrObjectFieldBy('category:' + newCid, 'post_count', postCount, next);
},
], function (err) {
next(err);
});
},
], callback);
} }
}; };

@ -1,6 +1,5 @@
'use strict'; 'use strict';
var async = require('async');
var _ = require('lodash'); var _ = require('lodash');
var db = require('../database'); var db = require('../database');
@ -10,123 +9,89 @@ var meta = require('../meta');
var user = require('../user'); var user = require('../user');
module.exports = function (Categories) { module.exports = function (Categories) {
Categories.getCategoryTopics = function (data, callback) { Categories.getCategoryTopics = async function (data) {
async.waterfall([ let results = await plugins.fireHook('filter:category.topics.prepare', data);
function (next) { const tids = await Categories.getTopicIds(results);
plugins.fireHook('filter:category.topics.prepare', data, next); let topicsData = await topics.getTopicsByTids(tids, data.uid);
}, topicsData = await user.blocks.filter(data.uid, topicsData);
function (data, next) {
Categories.getTopicIds(data, next);
},
function (tids, next) {
topics.getTopicsByTids(tids, data.uid, next);
},
async.apply(user.blocks.filter, data.uid),
function (topicsData, next) {
if (!topicsData.length) { if (!topicsData.length) {
return next(null, { topics: [], uid: data.uid }); return { topics: [], uid: data.uid };
} }
topics.calculateTopicIndices(topicsData, data.start); topics.calculateTopicIndices(topicsData, data.start);
plugins.fireHook('filter:category.topics.get', { cid: data.cid, topics: topicsData, uid: data.uid }, next); results = await plugins.fireHook('filter:category.topics.get', { cid: data.cid, topics: topicsData, uid: data.uid });
}, return { topics: results.topics, nextStart: data.stop + 1 };
function (results, next) {
next(null, { topics: results.topics, nextStart: data.stop + 1 });
},
], callback);
}; };
Categories.getTopicIds = function (data, callback) { Categories.getTopicIds = async function (data) {
var pinnedTids; const dataForPinned = _.cloneDeep(data);
async.waterfall([
function (next) {
var dataForPinned = _.cloneDeep(data);
dataForPinned.start = 0; dataForPinned.start = 0;
dataForPinned.stop = -1; dataForPinned.stop = -1;
async.parallel({ const [pinnedTids, set, direction] = await Promise.all([
pinnedTids: async.apply(Categories.getPinnedTids, dataForPinned), Categories.getPinnedTids(dataForPinned),
set: async.apply(Categories.buildTopicsSortedSet, data), Categories.buildTopicsSortedSet(data),
direction: async.apply(Categories.getSortedSetRangeDirection, data.sort), Categories.getSortedSetRangeDirection(data.sort),
}, next); ]);
},
function (results, next) {
var totalPinnedCount = results.pinnedTids.length;
pinnedTids = results.pinnedTids.slice(data.start, data.stop !== -1 ? data.stop + 1 : undefined);
var pinnedCount = pinnedTids.length;
var topicsPerPage = data.stop - data.start + 1;
var normalTidsToGet = Math.max(0, topicsPerPage - pinnedCount); const totalPinnedCount = pinnedTids.length;
const pinnedTidsOnPage = pinnedTids.slice(data.start, data.stop !== -1 ? data.stop + 1 : undefined);
const pinnedCountOnPage = pinnedTidsOnPage.length;
const topicsPerPage = data.stop - data.start + 1;
const normalTidsToGet = Math.max(0, topicsPerPage - pinnedCountOnPage);
if (!normalTidsToGet && data.stop !== -1) { if (!normalTidsToGet && data.stop !== -1) {
return next(null, []); return pinnedTidsOnPage;
} }
if (plugins.hasListeners('filter:categories.getTopicIds')) { if (plugins.hasListeners('filter:categories.getTopicIds')) {
return plugins.fireHook('filter:categories.getTopicIds', { const result = await plugins.fireHook('filter:categories.getTopicIds', {
tids: [], tids: [],
data: data, data: data,
pinnedTids: pinnedTids, pinnedTids: pinnedTidsOnPage,
allPinnedTids: results.pinnedTids, allPinnedTids: pinnedTids,
totalPinnedCount: totalPinnedCount, totalPinnedCount: totalPinnedCount,
normalTidsToGet: normalTidsToGet, normalTidsToGet: normalTidsToGet,
}, function (err, data) {
callback(err, data && data.tids);
}); });
return result && result.tids;
} }
var set = results.set; let start = data.start;
var direction = results.direction;
var start = data.start;
if (start > 0 && totalPinnedCount) { if (start > 0 && totalPinnedCount) {
start -= totalPinnedCount - pinnedCount; start -= totalPinnedCount - pinnedCountOnPage;
} }
var stop = data.stop === -1 ? data.stop : start + normalTidsToGet - 1; const stop = data.stop === -1 ? data.stop : start + normalTidsToGet - 1;
let normalTids;
const reverse = direction === 'highest-to-lowest';
if (Array.isArray(set)) { if (Array.isArray(set)) {
const weights = set.map((s, index) => (index ? 0 : 1)); const weights = set.map((s, index) => (index ? 0 : 1));
db[direction === 'highest-to-lowest' ? 'getSortedSetRevIntersect' : 'getSortedSetIntersect']({ sets: set, start: start, stop: stop, weights: weights }, next); normalTids = await db[reverse ? 'getSortedSetRevIntersect' : 'getSortedSetIntersect']({ sets: set, start: start, stop: stop, weights: weights });
} else { } else {
db[direction === 'highest-to-lowest' ? 'getSortedSetRevRange' : 'getSortedSetRange'](set, start, stop, next); normalTids = await db[reverse ? 'getSortedSetRevRange' : 'getSortedSetRange'](set, start, stop);
} }
},
function (normalTids, next) {
normalTids = normalTids.filter(tid => !pinnedTids.includes(tid)); normalTids = normalTids.filter(tid => !pinnedTids.includes(tid));
next(null, pinnedTids.concat(normalTids)); return pinnedTids.concat(normalTids);
},
], callback);
}; };
Categories.getTopicCount = function (data, callback) { Categories.getTopicCount = async function (data) {
if (plugins.hasListeners('filter:categories.getTopicCount')) { if (plugins.hasListeners('filter:categories.getTopicCount')) {
return plugins.fireHook('filter:categories.getTopicCount', { const result = await plugins.fireHook('filter:categories.getTopicCount', {
topicCount: data.category.topic_count, topicCount: data.category.topic_count,
data: data, data: data,
}, function (err, data) {
callback(err, data && data.topicCount);
}); });
return result && result.topicCount;
} }
async.waterfall([ const set = await Categories.buildTopicsSortedSet(data);
function (next) {
Categories.buildTopicsSortedSet(data, next);
},
function (set, next) {
if (Array.isArray(set)) { if (Array.isArray(set)) {
db.sortedSetIntersectCard(set, next); return await db.sortedSetIntersectCard(set);
} else {
next(null, data.category.topic_count);
} }
}, return data.category.topic_count;
], callback);
}; };
Categories.buildTopicsSortedSet = function (data, callback) { Categories.buildTopicsSortedSet = async function (data) {
var cid = data.cid; var cid = data.cid;
var set = 'cid:' + cid + ':tids'; var set = 'cid:' + cid + ':tids';
var sort = data.sort || (data.settings && data.settings.categoryTopicSort) || meta.config.categoryTopicSort || 'newest_to_oldest'; var sort = data.sort || (data.settings && data.settings.categoryTopicSort) || meta.config.categoryTopicSort || 'newest_to_oldest';
@ -148,40 +113,37 @@ module.exports = function (Categories) {
set = [set, 'tag:' + data.tag + ':topics']; set = [set, 'tag:' + data.tag + ':topics'];
} }
} }
plugins.fireHook('filter:categories.buildTopicsSortedSet', { const result = await plugins.fireHook('filter:categories.buildTopicsSortedSet', {
set: set, set: set,
data: data, data: data,
}, function (err, data) {
callback(err, data && data.set);
}); });
return result && result.set;
}; };
Categories.getSortedSetRangeDirection = function (sort, callback) { Categories.getSortedSetRangeDirection = async function (sort) {
sort = sort || 'newest_to_oldest'; sort = sort || 'newest_to_oldest';
var direction = sort === 'newest_to_oldest' || sort === 'most_posts' || sort === 'most_votes' ? 'highest-to-lowest' : 'lowest-to-highest'; var direction = sort === 'newest_to_oldest' || sort === 'most_posts' || sort === 'most_votes' ? 'highest-to-lowest' : 'lowest-to-highest';
plugins.fireHook('filter:categories.getSortedSetRangeDirection', { const result = await plugins.fireHook('filter:categories.getSortedSetRangeDirection', {
sort: sort, sort: sort,
direction: direction, direction: direction,
}, function (err, data) {
callback(err, data && data.direction);
}); });
return result && result.direction;
}; };
Categories.getAllTopicIds = function (cid, start, stop, callback) { Categories.getAllTopicIds = async function (cid, start, stop) {
db.getSortedSetRange(['cid:' + cid + ':tids:pinned', 'cid:' + cid + ':tids'], start, stop, callback); return await db.getSortedSetRange(['cid:' + cid + ':tids:pinned', 'cid:' + cid + ':tids'], start, stop);
}; };
Categories.getPinnedTids = function (data, callback) { Categories.getPinnedTids = async function (data) {
if (plugins.hasListeners('filter:categories.getPinnedTids')) { if (plugins.hasListeners('filter:categories.getPinnedTids')) {
return plugins.fireHook('filter:categories.getPinnedTids', { const result = await plugins.fireHook('filter:categories.getPinnedTids', {
pinnedTids: [], pinnedTids: [],
data: data, data: data,
}, function (err, data) {
callback(err, data && data.pinnedTids);
}); });
return result && result.pinnedTids;
} }
db.getSortedSetRevRange('cid:' + data.cid + ':tids:pinned', data.start, data.stop, callback); return await db.getSortedSetRevRange('cid:' + data.cid + ':tids:pinned', data.start, data.stop);
}; };
Categories.modifyTopicsByPrivilege = function (topics, privileges) { Categories.modifyTopicsByPrivilege = function (topics, privileges) {
@ -200,27 +162,18 @@ module.exports = function (Categories) {
}); });
}; };
Categories.onNewPostMade = function (cid, pinned, postData, callback) { Categories.onNewPostMade = async function (cid, pinned, postData) {
if (!cid || !postData) { if (!cid || !postData) {
return setImmediate(callback); return;
} }
const promises = [
async.parallel([ db.sortedSetAdd('cid:' + cid + ':pids', postData.timestamp, postData.pid),
function (next) { db.incrObjectField('category:' + cid, 'post_count'),
db.sortedSetAdd('cid:' + cid + ':pids', postData.timestamp, postData.pid, next); Categories.updateRecentTid(cid, postData.tid),
}, ];
function (next) { if (!pinned) {
db.incrObjectField('category:' + cid, 'post_count', next); promises.push(db.sortedSetIncrBy('cid:' + cid + ':tids:posts', 1, postData.tid));
}, }
function (next) { await Promise.all(promises);
if (pinned) {
return setImmediate(next);
}
db.sortedSetIncrBy('cid:' + cid + ':tids:posts', 1, postData.tid, err => next(err));
},
function (next) {
Categories.updateRecentTid(cid, postData.tid, next);
},
], callback);
}; };
}; };

@ -1,50 +1,38 @@
'use strict'; 'use strict';
var async = require('async'); const db = require('../database');
var db = require('../database');
module.exports = function (Categories) { module.exports = function (Categories) {
Categories.markAsRead = function (cids, uid, callback) { Categories.markAsRead = async function (cids, uid) {
callback = callback || function () {};
if (!Array.isArray(cids) || !cids.length || parseInt(uid, 10) <= 0) { if (!Array.isArray(cids) || !cids.length || parseInt(uid, 10) <= 0) {
return setImmediate(callback); return;
} }
var keys = cids.map(cid => 'cid:' + cid + ':read_by_uid'); let keys = cids.map(cid => 'cid:' + cid + ':read_by_uid');
const hasRead = await db.isMemberOfSets(keys, uid);
async.waterfall([
function (next) {
db.isMemberOfSets(keys, uid, next);
},
function (hasRead, next) {
keys = keys.filter((key, index) => !hasRead[index]); keys = keys.filter((key, index) => !hasRead[index]);
await db.setsAdd(keys, uid);
db.setsAdd(keys, uid, next);
},
], callback);
}; };
Categories.markAsUnreadForAll = function (cid, callback) { Categories.markAsUnreadForAll = async function (cid) {
if (!parseInt(cid, 10)) { if (!parseInt(cid, 10)) {
return callback(); return;
} }
callback = callback || function () {}; await db.delete('cid:' + cid + ':read_by_uid');
db.delete('cid:' + cid + ':read_by_uid', callback);
}; };
Categories.hasReadCategories = function (cids, uid, callback) { Categories.hasReadCategories = async function (cids, uid) {
if (parseInt(uid, 10) <= 0) { if (parseInt(uid, 10) <= 0) {
return setImmediate(callback, null, cids.map(() => false)); return cids.map(() => false);
} }
const sets = cids.map(cid => 'cid:' + cid + ':read_by_uid'); const sets = cids.map(cid => 'cid:' + cid + ':read_by_uid');
db.isMemberOfSets(sets, uid, callback); return await db.isMemberOfSets(sets, uid);
}; };
Categories.hasReadCategory = function (cid, uid, callback) { Categories.hasReadCategory = async function (cid, uid) {
if (parseInt(uid, 10) <= 0) { if (parseInt(uid, 10) <= 0) {
return setImmediate(callback, null, false); return false;
} }
db.isSetMember('cid:' + cid + ':read_by_uid', uid, callback); return await db.isSetMember('cid:' + cid + ':read_by_uid', uid);
}; };
}; };

@ -10,41 +10,25 @@ var plugins = require('../plugins');
var cache = require('../cache'); var cache = require('../cache');
module.exports = function (Categories) { module.exports = function (Categories) {
Categories.update = function (modified, callback) { Categories.update = async function (modified) {
var cids = Object.keys(modified); var cids = Object.keys(modified);
await Promise.all(cids.map(cid => updateCategory(cid, modified[cid])));
async.each(cids, function (cid, next) { return cids;
updateCategory(cid, modified[cid], next);
}, function (err) {
callback(err, cids);
});
}; };
function updateCategory(cid, modifiedFields, callback) { async function updateCategory(cid, modifiedFields) {
var category; const exists = await Categories.exists(cid);
async.waterfall([
function (next) {
Categories.exists(cid, next);
},
function (exists, next) {
if (!exists) { if (!exists) {
return callback(); return;
} }
if (modifiedFields.hasOwnProperty('name')) { if (modifiedFields.hasOwnProperty('name')) {
translator.translate(modifiedFields.name, function (translated) { const translated = await translator.translate(modifiedFields.name);
modifiedFields.slug = cid + '/' + utils.slugify(translated); modifiedFields.slug = cid + '/' + utils.slugify(translated);
next();
});
} else {
next();
} }
}, const result = await plugins.fireHook('filter:category.update', { cid: cid, category: modifiedFields });
function (next) {
plugins.fireHook('filter:category.update', { cid: cid, category: modifiedFields }, next); const category = result.category;
},
function (categoryData, next) {
category = categoryData.category;
var fields = Object.keys(category); var fields = Object.keys(category);
// move parent to front, so its updated first // move parent to front, so its updated first
var parentCidIndex = fields.indexOf('parentCid'); var parentCidIndex = fields.indexOf('parentCid');
@ -52,120 +36,62 @@ module.exports = function (Categories) {
fields.splice(0, 0, fields.splice(parentCidIndex, 1)[0]); fields.splice(0, 0, fields.splice(parentCidIndex, 1)[0]);
} }
async.eachSeries(fields, function (key, next) { await async.eachSeries(fields, async function (key) {
updateCategoryField(cid, key, category[key], next); await updateCategoryField(cid, key, category[key]);
}, next); });
},
function (next) {
plugins.fireHook('action:category.update', { cid: cid, modified: category }); plugins.fireHook('action:category.update', { cid: cid, modified: category });
next();
},
], callback);
} }
function updateCategoryField(cid, key, value, callback) { async function updateCategoryField(cid, key, value) {
if (key === 'parentCid') { if (key === 'parentCid') {
return updateParent(cid, value, callback); return await updateParent(cid, value);
} else if (key === 'tagWhitelist') { } else if (key === 'tagWhitelist') {
return updateTagWhitelist(cid, value, callback); return await updateTagWhitelist(cid, value);
} }
await db.setObjectField('category:' + cid, key, value);
async.waterfall([
function (next) {
db.setObjectField('category:' + cid, key, value, next);
},
function (next) {
if (key === 'order') { if (key === 'order') {
updateOrder(cid, value, next); await updateOrder(cid, value);
} else if (key === 'description') { } else if (key === 'description') {
Categories.parseDescription(cid, value, next); await Categories.parseDescription(cid, value);
} else {
next();
} }
},
], callback);
} }
function updateParent(cid, newParent, callback) { async function updateParent(cid, newParent) {
if (parseInt(cid, 10) === parseInt(newParent, 10)) { newParent = parseInt(newParent, 10) || 0;
return callback(new Error('[[error:cant-set-self-as-parent]]')); if (parseInt(cid, 10) === newParent) {
throw new Error('[[error:cant-set-self-as-parent]]');
} }
async.waterfall([ const childrenCids = await Categories.getChildrenCids(cid);
function (next) { if (childrenCids.includes(newParent)) {
Categories.getChildrenCids(cid, next); throw new Error('[[error:cant-set-child-as-parent]]');
},
function (childrenCids, next) {
if (childrenCids.includes(parseInt(newParent, 10))) {
return next(new Error('[[error:cant-set-child-as-parent]]'));
} }
Categories.getCategoryField(cid, 'parentCid', next); const oldParent = await Categories.getCategoryField(cid, 'parentCid');
}, await Promise.all([
function (oldParent, next) { db.sortedSetRemove('cid:' + oldParent + ':children', cid),
async.series([ db.sortedSetAdd('cid:' + newParent + ':children', cid, cid),
function (next) { db.setObjectField('category:' + cid, 'parentCid', newParent),
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 (next) {
cache.del(['cid:' + oldParent + ':children', 'cid:' + newParent + ':children']); cache.del(['cid:' + oldParent + ':children', 'cid:' + newParent + ':children']);
next();
},
], next);
},
], function (err) {
callback(err);
});
} }
function updateTagWhitelist(cid, tags, callback) { async function updateTagWhitelist(cid, tags) {
tags = tags.split(','); tags = tags.split(',').map(tag => utils.cleanUpTag(tag, meta.config.maximumTagLength))
tags = tags.map(function (tag) { .filter(Boolean);
return utils.cleanUpTag(tag, meta.config.maximumTagLength); await db.delete('cid:' + cid + ':tag:whitelist');
}).filter(Boolean); const scores = tags.map((tag, index) => index);
await db.sortedSetAdd('cid:' + cid + ':tag:whitelist', scores, tags);
async.waterfall([
function (next) {
db.delete('cid:' + cid + ':tag:whitelist', next);
},
function (next) {
var scores = tags.map((tag, index) => index);
db.sortedSetAdd('cid:' + cid + ':tag:whitelist', scores, tags, next);
},
function (next) {
cache.del('cid:' + cid + ':tag:whitelist'); cache.del('cid:' + cid + ':tag:whitelist');
next();
},
], callback);
} }
function updateOrder(cid, order, callback) { async function updateOrder(cid, order) {
async.waterfall([ const parentCid = await Categories.getCategoryField(cid, 'parentCid');
function (next) { await db.sortedSetsAdd(['categories:cid', 'cid:' + parentCid + ':children'], order, cid);
Categories.getCategoryField(cid, 'parentCid', next);
},
function (parentCid, next) {
db.sortedSetsAdd(['categories:cid', 'cid:' + parentCid + ':children'], order, cid, function (err) {
cache.del(['categories:cid', 'cid:' + parentCid + ':children']); cache.del(['categories:cid', 'cid:' + parentCid + ':children']);
next(err);
});
},
], err => callback(err));
} }
Categories.parseDescription = function (cid, description, callback) { Categories.parseDescription = async function (cid, description) {
async.waterfall([ const parsedDescription = await plugins.fireHook('filter:parse.raw', description);
function (next) { await Categories.setCategoryField(cid, 'descriptionParsed', parsedDescription);
plugins.fireHook('filter:parse.raw', description, next);
},
function (parsedDescription, next) {
Categories.setCategoryField(cid, 'descriptionParsed', parsedDescription, next);
},
], callback);
}; };
}; };

@ -1,7 +1,5 @@
'use strict'; 'use strict';
const async = require('async');
const db = require('../database'); const db = require('../database');
const user = require('../user'); const user = require('../user');
@ -12,69 +10,45 @@ module.exports = function (Categories) {
watching: 3, watching: 3,
}; };
Categories.isIgnored = function (cids, uid, callback) { Categories.isIgnored = async function (cids, uid) {
if (!(parseInt(uid, 10) > 0)) { if (!(parseInt(uid, 10) > 0)) {
return setImmediate(callback, null, cids.map(() => false)); return cids.map(() => false);
} }
async.waterfall([ const states = await Categories.getWatchState(cids, uid);
function (next) { return states.map(state => state === Categories.watchStates.ignoring);
Categories.getWatchState(cids, uid, next);
},
function (states, next) {
next(null, states.map(state => state === Categories.watchStates.ignoring));
},
], callback);
}; };
Categories.getWatchState = function (cids, uid, callback) { Categories.getWatchState = async function (cids, uid) {
if (!(parseInt(uid, 10) > 0)) { if (!(parseInt(uid, 10) > 0)) {
return setImmediate(callback, null, cids.map(() => Categories.watchStates.notwatching)); return cids.map(() => Categories.watchStates.notwatching);
} }
if (!Array.isArray(cids) || !cids.length) { if (!Array.isArray(cids) || !cids.length) {
return setImmediate(callback, null, []); return [];
} }
async.waterfall([
function (next) {
const keys = cids.map(cid => 'cid:' + cid + ':uid:watch:state'); const keys = cids.map(cid => 'cid:' + cid + ':uid:watch:state');
async.parallel({ const [userSettings, states] = await Promise.all([
userSettings: async.apply(user.getSettings, uid), user.getSettings(uid),
states: async.apply(db.sortedSetsScore, keys, uid), db.sortedSetsScore(keys, uid),
}, next); ]);
}, return states.map(state => state || Categories.watchStates[userSettings.categoryWatchState]);
function (results, next) {
next(null, results.states.map(state => state || Categories.watchStates[results.userSettings.categoryWatchState]));
},
], callback);
}; };
Categories.getIgnorers = function (cid, start, stop, callback) { Categories.getIgnorers = async function (cid, start, stop) {
const count = (stop === -1) ? -1 : (stop - start + 1); const count = (stop === -1) ? -1 : (stop - start + 1);
db.getSortedSetRevRangeByScore('cid:' + cid + ':uid:watch:state', start, count, Categories.watchStates.ignoring, Categories.watchStates.ignoring, callback); return await db.getSortedSetRevRangeByScore('cid:' + cid + ':uid:watch:state', start, count, Categories.watchStates.ignoring, Categories.watchStates.ignoring);
}; };
Categories.filterIgnoringUids = function (cid, uids, callback) { Categories.filterIgnoringUids = async function (cid, uids) {
async.waterfall([ const states = await Categories.getUidsWatchStates(cid, uids);
function (next) {
Categories.getUidsWatchStates(cid, uids, next);
},
function (states, next) {
const readingUids = uids.filter((uid, index) => uid && states[index] !== Categories.watchStates.ignoring); const readingUids = uids.filter((uid, index) => uid && states[index] !== Categories.watchStates.ignoring);
next(null, readingUids); return readingUids;
},
], callback);
}; };
Categories.getUidsWatchStates = function (cid, uids, callback) { Categories.getUidsWatchStates = async function (cid, uids) {
async.waterfall([ const [userSettings, states] = await Promise.all([
function (next) { user.getMultipleUserSettings(uids),
async.parallel({ db.sortedSetScores('cid:' + cid + ':uid:watch:state', uids),
userSettings: async.apply(user.getMultipleUserSettings, uids), ]);
states: async.apply(db.sortedSetScores, 'cid:' + cid + ':uid:watch:state', uids), return states.map((state, index) => state || Categories.watchStates[userSettings[index].categoryWatchState]);
}, next);
},
function (results, next) {
next(null, results.states.map((state, index) => state || Categories.watchStates[results.userSettings[index].categoryWatchState]));
},
], callback);
}; };
}; };

@ -38,7 +38,9 @@ module.exports = function (db, module) {
return; return;
} }
var query = { _key: { $in: keys } }; var query = { _key: { $in: keys } };
if (keys.length === 1) {
query._key = keys[0];
}
if (min !== '-inf') { if (min !== '-inf') {
query.score = { $gte: parseFloat(min) }; query.score = { $gte: parseFloat(min) };
} }

@ -13,7 +13,7 @@ var PubSub = function () {
var subClient = db.connect(); var subClient = db.connect();
this.pubClient = db.connect(); this.pubClient = db.connect();
channelName = 'db:' + nconf.get('redis:database') + 'pubsub_channel'; channelName = 'db:' + nconf.get('redis:database') + ':pubsub_channel';
subClient.subscribe(channelName); subClient.subscribe(channelName);
subClient.on('message', function (channel, message) { subClient.on('message', function (channel, message) {

@ -123,7 +123,7 @@ function copyPrivilegesToChildrenRecursive(parentCid, category, group, callback)
}, },
function (next) { function (next) {
async.eachSeries(category.children, function (child, next) { async.eachSeries(category.children, function (child, next) {
copyPrivilegesToChildrenRecursive(parentCid, child, next); copyPrivilegesToChildrenRecursive(parentCid, child, group, next);
}, next); }, next);
}, },
], callback); ], callback);

Loading…
Cancel
Save