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,125 +11,102 @@ 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([ data.name = data.name || 'Category ' + cid;
function (next) { const slug = cid + '/' + utils.slugify(data.name);
db.incrObjectField('global', 'nextCid', next); const order = data.order || cid; // If no order provided, place it at the end
}, const colours = Categories.assignColours();
function (cid, next) {
data.name = data.name || 'Category ' + cid; let category = {
var slug = cid + '/' + utils.slugify(data.name); cid: cid,
var order = data.order || cid; // If no order provided, place it at the end name: data.name,
var colours = Categories.assignColours(); description: data.description ? data.description : '',
descriptionParsed: data.descriptionParsed ? data.descriptionParsed : '',
category = { icon: data.icon ? data.icon : '',
cid: cid, bgColor: data.bgColor || colours[0],
name: data.name, color: data.color || colours[1],
description: data.description ? data.description : '', slug: slug,
descriptionParsed: data.descriptionParsed ? data.descriptionParsed : '', parentCid: parentCid,
icon: data.icon ? data.icon : '', topic_count: 0,
bgColor: data.bgColor || colours[0], post_count: 0,
color: data.color || colours[1], disabled: data.disabled ? 1 : 0,
slug: slug, order: order,
parentCid: parentCid, link: data.link || '',
topic_count: 0, numRecentReplies: 1,
post_count: 0, class: (data.class ? data.class : 'col-md-3 col-xs-6'),
disabled: data.disabled ? 1 : 0, imageClass: 'cover',
order: order, isSection: 0,
link: data.link || '', };
numRecentReplies: 1,
class: (data.class ? data.class : 'col-md-3 col-xs-6'), if (data.backgroundImage) {
imageClass: 'cover', category.backgroundImage = data.backgroundImage;
isSection: 0, }
};
if (data.backgroundImage) {
category.backgroundImage = data.backgroundImage;
}
plugins.fireHook('filter:category.create', { category: category, data: data }, next);
},
function (data, next) {
category = data.category;
var defaultPrivileges = [
'find',
'read',
'topics:read',
'topics:create',
'topics:reply',
'topics:tag',
'posts:edit',
'posts:history',
'posts:delete',
'posts:upvote',
'posts:downvote',
'topics:delete',
];
const modPrivileges = defaultPrivileges.concat([
'posts:view_deleted',
'purge',
]);
async.series([
async.apply(db.setObject, 'category:' + category.cid, category),
function (next) {
if (category.descriptionParsed) {
return next();
}
Categories.parseDescription(category.cid, category.description, next);
},
async.apply(db.sortedSetsAdd, ['categories:cid', 'cid:' + parentCid + ':children'], category.order, category.cid),
async.apply(privileges.categories.give, defaultPrivileges, category.cid, 'registered-users'),
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']);
if (data.cloneFromCid && parseInt(data.cloneFromCid, 10)) {
return Categories.copySettingsFrom(data.cloneFromCid, category.cid, !data.parentCid, next);
}
next(null, category);
},
function (_category, next) {
category = _category;
if (data.cloneChildren) {
return duplicateCategoriesChildren(category.cid, data.cloneFromCid, data.uid, next);
}
next();
},
function (next) {
plugins.fireHook('action:category.create', { category: category });
next(null, category);
},
], callback);
};
function duplicateCategoriesChildren(parentCid, cid, uid, callback) { const result = await plugins.fireHook('filter:category.create', { category: category, data: data });
Categories.getChildren([cid], uid, function (err, children) { category = result.category;
if (err || !children.length) {
return callback(err); const defaultPrivileges = [
} 'find',
'read',
'topics:read',
'topics:create',
'topics:reply',
'topics:tag',
'posts:edit',
'posts:history',
'posts:delete',
'posts:upvote',
'posts:downvote',
'topics:delete',
];
const modPrivileges = defaultPrivileges.concat([
'posts:view_deleted',
'purge',
]);
await db.setObject('category:' + category.cid, category);
if (!category.descriptionParsed) {
await Categories.parseDescription(category.cid, category.description);
}
await db.sortedSetsAdd(['categories:cid', 'cid:' + parentCid + ':children'], category.order, category.cid);
await privileges.categories.give(defaultPrivileges, category.cid, 'registered-users');
await privileges.categories.give(modPrivileges, category.cid, ['administrators', 'Global Moderators']);
await privileges.categories.give(['find', 'read', 'topics:read'], category.cid, ['guests', 'spiders']);
cache.del(['categories:cid', 'cid:' + parentCid + ':children']);
if (data.cloneFromCid && parseInt(data.cloneFromCid, 10)) {
category = await Categories.copySettingsFrom(data.cloneFromCid, category.cid, !data.parentCid);
}
children = children[0]; if (data.cloneChildren) {
await duplicateCategoriesChildren(category.cid, data.cloneFromCid, data.uid);
}
children.forEach(function (child) { plugins.fireHook('action:category.create', { category: category });
child.parentCid = parentCid; return category;
child.cloneFromCid = child.cid; };
child.cloneChildren = true;
child.name = utils.decodeHTMLEntities(child.name);
child.description = utils.decodeHTMLEntities(child.description);
child.uid = uid;
});
async.each(children, Categories.create, callback); async function duplicateCategoriesChildren(parentCid, cid, uid) {
let children = await Categories.getChildren([cid], uid);
if (!children.length) {
return;
}
children = children[0];
children.forEach(function (child) {
child.parentCid = parentCid;
child.cloneFromCid = child.cid;
child.cloneChildren = true;
child.name = utils.decodeHTMLEntities(child.name);
child.description = utils.decodeHTMLEntities(child.description);
child.uid = uid;
}); });
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) { const oldParent = parseInt(destination.parentCid, 10) || 0;
if (!results.source) { const newParent = parseInt(source.parentCid, 10) || 0;
return next(new Error('[[error:invalid-cid]]')); if (copyParent && newParent !== parseInt(toCid, 10)) {
} await db.sortedSetRemove('cid:' + oldParent + ':children', toCid);
destination = results.destination; await db.sortedSetAdd('cid:' + newParent + ':children', source.order, toCid);
cache.del(['cid:' + oldParent + ':children', 'cid:' + newParent + ':children']);
var tasks = []; }
const oldParent = parseInt(destination.parentCid, 10) || 0; destination.description = source.description;
const newParent = parseInt(results.source.parentCid, 10) || 0; destination.descriptionParsed = source.descriptionParsed;
if (copyParent && newParent !== parseInt(toCid, 10)) { destination.icon = source.icon;
tasks.push(async.apply(db.sortedSetRemove, 'cid:' + oldParent + ':children', toCid)); destination.bgColor = source.bgColor;
tasks.push(async.apply(db.sortedSetAdd, 'cid:' + newParent + ':children', results.source.order, toCid)); destination.color = source.color;
tasks.push(function (next) { destination.link = source.link;
cache.del(['cid:' + oldParent + ':children', 'cid:' + newParent + ':children']); destination.numRecentReplies = source.numRecentReplies;
setImmediate(next); destination.class = source.class;
}); destination.image = source.image;
} destination.imageClass = source.imageClass;
destination.description = results.source.description; if (copyParent) {
destination.descriptionParsed = results.source.descriptionParsed; destination.parentCid = source.parentCid || 0;
destination.icon = results.source.icon; }
destination.bgColor = results.source.bgColor;
destination.color = results.source.color; await db.setObject('category:' + toCid, destination);
destination.link = results.source.link;
destination.numRecentReplies = results.source.numRecentReplies; await copyTagWhitelist(fromCid, toCid);
destination.class = results.source.class;
destination.image = results.source.image; await Categories.copyPrivilegesFrom(fromCid, toCid);
destination.imageClass = results.source.imageClass;
return destination;
if (copyParent) {
destination.parentCid = results.source.parentCid || 0;
}
tasks.push(async.apply(db.setObject, 'category:' + toCid, destination));
async.series(tasks, next);
},
function (results, next) {
copyTagWhitelist(fromCid, toCid, next);
},
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) { privileges: group ? privileges.groupPrivilegeList.slice() : privileges.privilegeList.slice(),
plugins.fireHook('filter:categories.copyPrivilegesFrom', { fromCid: fromCid,
privileges: group ? privileges.groupPrivilegeList.slice() : privileges.privilegeList.slice(), toCid: toCid,
fromCid: fromCid, group: group,
toCid: toCid, });
group: group, if (group) {
}, next); await copyPrivilegesByGroup(data.privileges, data.fromCid, data.toCid, group);
}, } else {
function (data, next) { await copyPrivileges(data.privileges, data.fromCid, data.toCid);
if (group) { }
copyPrivilegesByGroup(data.privileges, data.fromCid, data.toCid, group, next);
} else {
copyPrivileges(data.privileges, data.fromCid, data.toCid, next);
}
},
], 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); const copyGroups = _.uniq(_.flatten(currentMembers));
}, await async.each(copyGroups, async function (group) {
function (currentMembers, next) { await copyPrivilegesByGroup(privileges, fromCid, toCid, group);
const copyGroups = _.uniq(_.flatten(currentMembers)); });
async.each(copyGroups, function (group, next) {
copyPrivilegesByGroup(privileges, fromCid, toCid, group, next);
}, next);
},
], callback);
} }
function copyPrivilegesByGroup(privileges, fromCid, toCid, group, callback) { async function copyPrivilegesByGroup(privileges, fromCid, toCid, group) {
async.waterfall([ const leaveGroups = privileges.map(privilege => 'cid:' + toCid + ':privileges:' + privilege);
function (next) { await groups.leave(leaveGroups, group);
const leaveGroups = privileges.map(privilege => 'cid:' + toCid + ':privileges:' + privilege);
groups.leave(leaveGroups, group, next); const checkGroups = privileges.map(privilege => 'group:cid:' + fromCid + ':privileges:' + privilege + ':members');
}, const isMembers = await db.isMemberOfSortedSets(checkGroups, group);
function (next) { privileges = privileges.filter((priv, index) => isMembers[index]);
const checkGroups = privileges.map(privilege => 'group:cid:' + fromCid + ':privileges:' + privilege + ':members'); const joinGroups = privileges.map(privilege => 'cid:' + toCid + ':privileges:' + privilege);
db.isMemberOfSortedSets(checkGroups, group, next); await groups.join(joinGroups, group);
},
function (isMembers, next) {
privileges = privileges.filter((priv, index) => isMembers[index]);
const joinGroups = privileges.map(privilege => 'cid:' + toCid + ':privileges:' + privilege);
groups.join(joinGroups, group, next);
},
], 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([ const keys = cids.map(cid => 'category:' + cid);
function (next) { if (fields.length) {
const keys = cids.map(cid => 'category:' + cid); categories = await db.getObjectsFields(keys, fields);
if (fields.length) { } else {
db.getObjectsFields(keys, fields, next); categories = await db.getObjects(keys);
} else { }
db.getObjects(keys, next); categories.forEach(category => modifyCategory(category, fields));
} return categories;
},
function (categories, next) {
categories.forEach(category => modifyCategory(category, fields));
next(null, 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,107 +10,62 @@ 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);
},
function (next) {
db.getSortedSetRevRange('cid:' + cid + ':tids:pinned', 0, -1, next);
},
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 });
next();
},
], callback);
};
function purgeCategory(cid, callback) { const pinnedTids = await db.getSortedSetRevRange('cid:' + cid + ':tids:pinned', 0, -1);
async.series([ await async.eachLimit(pinnedTids, 10, async function (tid) {
function (next) { await topics.purgePostsAndTopic(tid, uid);
db.sortedSetRemove('categories:cid', cid, next);
},
function (next) {
removeFromParent(cid, next);
},
function (next) {
db.deleteAll([
'cid:' + cid + ':tids',
'cid:' + cid + ':tids:pinned',
'cid:' + cid + ':tids:posts',
'cid:' + cid + ':pids',
'cid:' + cid + ':read_by_uid',
'cid:' + cid + ':uid:watch:state',
'cid:' + cid + ':children',
'cid:' + cid + ':tag:whitelist',
'category:' + cid,
], next);
},
function (next) {
groups.destroy(privileges.privilegeList.map(privilege => 'cid:' + cid + ':privileges:' + privilege), next);
},
], function (err) {
callback(err);
}); });
await purgeCategory(cid);
plugins.fireHook('action:category.delete', { cid: cid, uid: uid });
};
async function purgeCategory(cid) {
await db.sortedSetRemove('categories:cid', cid);
await removeFromParent(cid);
await db.deleteAll([
'cid:' + cid + ':tids',
'cid:' + cid + ':tids:pinned',
'cid:' + cid + ':tids:posts',
'cid:' + cid + ':pids',
'cid:' + cid + ':read_by_uid',
'cid:' + cid + ':uid:watch:state',
'cid:' + cid + ':children',
'cid:' + cid + ':tag:whitelist',
'category:' + cid,
]);
await groups.destroy(privileges.privilegeList.map(privilege => 'cid:' + cid + ':privileges:' + privilege));
} }
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) {
async.parallel([
function (next) {
db.sortedSetRemove('cid:' + results.parentCid + ':children', cid, next);
},
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([
'categories:cid',
'cid:0:children',
'cid:' + results.parentCid + ':children',
'cid:' + cid + ':children',
'cid:' + cid + ':tag:whitelist',
]);
next();
});
},
], function (err) {
callback(err);
}); });
await Promise.all([
db.sortedSetRemove('cid:' + parentCid + ':children', cid),
db.setObjectField(childrenKeys, 'parentCid', 0),
db.sortedSetAddBulk(bulkAdd),
]);
cache.del([
'categories:cid',
'cid:0:children',
'cid:' + parentCid + ':children',
'cid:' + cid + ':children',
'cid:' + cid + ':tag:whitelist',
]);
} }
}; };

@ -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,195 +22,128 @@ 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([ if (!categories[0]) {
function (next) { return null;
Categories.getCategories([data.cid], data.uid, next); }
}, const category = categories[0];
function (categories, next) { data.category = category;
if (!categories[0]) {
return callback(null, null); const promises = [
} Categories.getCategoryTopics(data),
category = categories[0]; Categories.getTopicCount(data),
data.category = category; Categories.getWatchState([data.cid], data.uid),
async.parallel({ getChildrenTree(category, data.uid),
topics: function (next) { ];
Categories.getCategoryTopics(data, next);
}, if (category.parentCid) {
topicCount: function (next) { promises.push(Categories.getCategoryData(category.parentCid));
Categories.getTopicCount(data, next); }
}, const [topics, topicCount, watchState, , parent] = await Promise.all(promises);
watchState: function (next) {
Categories.getWatchState([data.cid], data.uid, next); category.topics = topics.topics;
}, category.nextStart = topics.nextStart;
parent: function (next) { category.topic_count = topicCount;
if (category.parentCid) { category.isWatched = watchState[0] === Categories.watchStates.watching;
Categories.getCategoryData(category.parentCid, next); category.isNotWatched = watchState[0] === Categories.watchStates.notwatching;
} else { category.isIgnored = watchState[0] === Categories.watchStates.ignoring;
next(); category.parent = parent;
}
},
children: function (next) { calculateTopicPostCount(category);
getChildrenTree(category, data.uid, next); const result = await plugins.fireHook('filter:category.get', { category: category, uid: data.uid });
}, return result.category;
}, next);
},
function (results, next) {
category.topics = results.topics.topics;
category.nextStart = results.topics.nextStart;
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);
plugins.fireHook('filter:category.get', { category: category, uid: data.uid }, next);
},
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) { cache.set(key, cids);
return callback(err); return cids.slice();
}
cache.set(key, cids);
callback(null, 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; memo.push('cid:' + cid + ':privileges:moderate');
async.waterfall([ memo.push('cid:' + cid + ':privileges:groups:moderate');
function (next) { return memo;
var groupNames = cids.reduce(function (memo, cid) { }, []);
memo.push('cid:' + cid + ':privileges:moderate');
memo.push('cid:' + cid + ':privileges:groups:moderate'); const memberSets = await groups.getMembersOfGroups(groupNames);
return memo; // Every other set is actually a list of user groups, not uids, so convert those to members
}, []); const sets = memberSets.reduce(function (memo, set, idx) {
if (idx % 2) {
groups.getMembersOfGroups(groupNames, next); memo.groupNames.push(set);
}, } else {
function (memberSets, next) { memo.uids.push(set);
// Every other set is actually a list of user groups, not uids, so convert those to members }
sets = memberSets.reduce(function (memo, set, idx) {
if (idx % 2) {
memo.groupNames.push(set);
} else {
memo.uids.push(set);
}
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) { const moderatorUids = cids.map(function (cid, index) {
var map = _.zipObject(uniqGroups, groupUids); return _.uniq(sets.uids[index].concat(_.flatten(sets.groupNames[index].map(g => map[g]))));
const moderatorUids = cids.map(function (cid, index) { });
return _.uniq(sets.uids[index].concat(_.flatten(sets.groupNames[index].map(g => map[g])))); return moderatorUids;
});
next(null, 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); if (category) {
}, category.tagWhitelist = tagWhitelist[i];
hasRead: function (next) { category['unread-class'] = (category.topic_count === 0 || (hasRead[i] && uid !== 0)) ? '' : 'unread';
Categories.hasReadCategories(cids, uid, next); }
}, });
}, next); return categories;
},
function (results, next) {
results.categories.forEach(function (category, i) {
if (category) {
category.tagWhitelist = results.tagWhitelist[i];
category['unread-class'] = (category.topic_count === 0 || (results.hasRead[i] && uid !== 0)) ? '' : 'unread';
}
});
next(null, results.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) => {
} cachedData[cid] = data[index];
nonCachedCids.forEach((cid, index) => { cache.set('cid:' + cid + ':tag:whitelist', data[index]);
cachedData[cid] = 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([ if (!parentCids.length) {
function (next) { return cids.map(() => null);
Categories.getCategoriesFields(cids, ['parentCid'], next); }
}, const parentData = await Categories.getCategoriesData(parentCids);
function (_categoriesData, next) { const cidToParent = _.zipObject(parentCids, parentData);
categoriesData = _categoriesData; return categoriesData.map(category => cidToParent[category.parentCid]);
parentCids = categoriesData.filter(c => c && c.parentCid).map(c => c.parentCid);
if (!parentCids.length) {
return callback(null, cids.map(() => null));
}
Categories.getCategoriesData(parentCids, next);
},
function (parentData, next) {
const cidToParent = _.zipObject(parentCids, parentData);
parentData = 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) {
}, category.children = [];
function (children, next) { return;
privileges.categories.filterCids('find', children, uid, next); }
}, let childrenData = await Categories.getCategoriesData(childrenCids);
function (children, next) { childrenData = childrenData.filter(Boolean);
children = children.filter(cid => parseInt(category.cid, 10) !== parseInt(cid, 10)); childrenCids = childrenData.map(child => child.cid);
if (!children.length) { const hasRead = await Categories.hasReadCategories(childrenCids, uid);
category.children = []; childrenData.forEach(function (child, i) {
return callback(); child['unread-class'] = (child.topic_count === 0 || (hasRead[i] && uid !== 0)) ? '' : 'unread';
} });
Categories.getCategoriesData(children, next); Categories.getTree([category].concat(childrenData), category.parentCid);
},
function (_children, next) {
children = _children.filter(Boolean);
const cids = children.map(child => child.cid);
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);
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)));
} if (!childrenCids.length) {
childrenCids = childrenCids.filter(cid => !allCids.includes(parseInt(cid, 10))); return;
if (!childrenCids.length) { }
return callback(); keys = childrenCids.map(cid => 'cid:' + cid + ':children');
} childrenCids.forEach(cid => allCids.push(parseInt(cid, 10)));
const keys = childrenCids.map(cid => 'cid:' + cid + ':children'); recursive(keys);
childrenCids.forEach(cid => allCids.push(parseInt(cid, 10)));
recursive(keys, callback);
});
} }
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) { allCids = _.uniq(allCids);
return callback(err); cache.set(key, allCids);
} return allCids.slice();
allCids = _.uniq(allCids);
cache.set(key, allCids);
callback(null, 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 = Categories.getTree(categories);
Categories.getCategoriesByPrivilege('categories:cid', uid, privilege, next); return await Categories.buildForSelectCategories(categories);
},
function (categories, next) {
categories = Categories.getTree(categories);
Categories.buildForSelectCategories(categories, next);
},
], 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,143 +10,88 @@ 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);
},
numRecentReplies: function (next) {
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);
},
function (data, next) {
if (!data.length) {
return next();
}
db.sortedSetsRemoveRangeByScore(['cid:' + cid + ':recent_tids'], '-inf', data[data.length - 1].score, next);
},
function (next) {
db.sortedSetAdd('cid:' + cid + ':recent_tids', Date.now(), tid, next);
},
], callback);
};
Categories.updateRecentTidForCid = function (cid, callback) { if (count < numRecentReplies) {
async.waterfall([ return await db.sortedSetAdd('cid:' + cid + ':recent_tids', Date.now(), tid);
function (next) { }
db.getSortedSetRevRange('cid:' + cid + ':pids', 0, 0, next); const data = await db.getSortedSetRangeWithScores('cid:' + cid + ':recent_tids', 0, count - numRecentReplies);
}, if (data.length) {
function (pid, next) { await db.sortedSetsRemoveRangeByScore(['cid:' + cid + ':recent_tids'], '-inf', data[data.length - 1].score);
pid = pid[0]; }
posts.getPostField(pid, 'tid', next); await db.sortedSetAdd('cid:' + cid + ':recent_tids', Date.now(), tid);
}, };
function (tid, next) {
if (!tid) {
return next();
}
Categories.updateRecentTid(cid, tid, next); Categories.updateRecentTidForCid = async function (cid) {
}, const pids = await db.getSortedSetRevRange('cid:' + cid + ':pids', 0, 0);
], callback); if (!pids.length) {
return;
}
const tid = await posts.getPostField(pids[0], 'tid');
if (!tid) {
return;
}
await Categories.updateRecentTid(cid, tid);
}; };
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;
} }
const categoriesToLoad = categoryData.filter(category => category && category.numRecentReplies && parseInt(category.numRecentReplies, 10) > 0);
const keys = categoriesToLoad.map(category => 'cid:' + category.cid + ':recent_tids');
const results = await db.getSortedSetsMembers(keys);
let tids = _.uniq(_.flatten(results).filter(Boolean));
async.waterfall([ tids = await privileges.topics.filterTids('topics:read', tids, uid);
function (next) { const topics = await getTopics(tids, uid);
const categoriesToLoad = categoryData.filter(category => category && category.numRecentReplies && parseInt(category.numRecentReplies, 10) > 0); assignTopicsToCategories(categoryData, topics);
const keys = categoriesToLoad.map(category => 'cid:' + category.cid + ':recent_tids');
db.getSortedSetsMembers(keys, next);
},
function (results, next) {
var tids = _.uniq(_.flatten(results).filter(Boolean));
privileges.topics.filterTids('topics:read', tids, uid, next); bubbleUpChildrenPosts(categoryData);
},
function (tids, next) {
getTopics(tids, uid, next);
},
function (topics, next) {
assignTopicsToCategories(categoryData, topics);
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([ topicData.forEach(function (topic) {
function (next) { if (topic) {
topics.getTopicsFields(tids, ['tid', 'mainPid', 'slug', 'title', 'teaserPid', 'cid', 'postcount'], next); topic.teaserPid = topic.teaserPid || topic.mainPid;
}, }
function (_topicData, next) { });
topicData = _topicData; var cids = _.uniq(topicData.map(topic => topic && topic.cid).filter(cid => parseInt(cid, 10)));
topicData.forEach(function (topic) { const [categoryData, teasers] = await Promise.all([
if (topic) { Categories.getCategoriesFields(cids, ['cid', 'parentCid']),
topic.teaserPid = topic.teaserPid || topic.mainPid; topics.getTeasers(topicData, uid),
} ]);
}); var parentCids = {};
var cids = _.uniq(topicData.map(topic => topic && topic.cid).filter(cid => parseInt(cid, 10))); categoryData.forEach(function (category) {
parentCids[category.cid] = category.parentCid;
async.parallel({ });
categoryData: async.apply(Categories.getCategoriesFields, cids, ['cid', 'parentCid']), teasers.forEach(function (teaser, index) {
teasers: async.apply(topics.getTeasers, _topicData, uid), if (teaser) {
}, next); teaser.cid = topicData[index].cid;
}, teaser.parentCid = parseInt(parentCids[teaser.cid], 10) || 0;
function (results, next) { teaser.tid = undefined;
var parentCids = {}; teaser.uid = undefined;
results.categoryData.forEach(function (category) { teaser.topic = {
parentCids[category.cid] = category.parentCid; slug: topicData[index].slug,
}); title: topicData[index].title,
results.teasers.forEach(function (teaser, index) { };
if (teaser) { }
teaser.cid = topicData[index].cid; });
teaser.parentCid = parseInt(parentCids[teaser.cid], 10) || 0; return teasers.filter(Boolean);
teaser.tid = undefined;
teaser.uid = undefined;
teaser.topic = {
slug: topicData[index].slug,
title: topicData[index].title,
};
}
});
results.teasers = results.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 = async function (tid, oldCid, cid) {
await updatePostCount(tid, oldCid, cid);
const pids = await topics.getPids(tid);
await batch.processArray(pids, async function (pids) {
const postData = await posts.getPostsFields(pids, ['pid', 'uid', 'timestamp', 'upvotes', 'downvotes']);
const timestamps = postData.map(p => p && p.timestamp);
const bulkRemove = [];
const bulkAdd = [];
postData.forEach((post) => {
bulkRemove.push(['cid:' + oldCid + ':uid:' + post.uid + ':pids', post.pid]);
bulkRemove.push(['cid:' + oldCid + ':uid:' + post.uid + ':pids:votes', post.pid]);
bulkAdd.push(['cid:' + cid + ':uid:' + post.uid + ':pids', post.timestamp, post.pid]);
if (post.votes > 0) {
bulkAdd.push(['cid:' + cid + ':uid:' + post.uid + ':pids:votes', post.votes, post.pid]);
}
});
Categories.moveRecentReplies = function (tid, oldCid, cid, callback) { await Promise.all([
callback = callback || function () {}; db.sortedSetRemove('cid:' + oldCid + ':pids', pids),
db.sortedSetAdd('cid:' + cid + ':pids', timestamps, pids),
async.waterfall([ db.sortedSetRemoveBulk(bulkRemove),
function (next) { db.sortedSetAddBulk(bulkAdd),
updatePostCount(tid, oldCid, cid, next); ]);
}, }, { batch: 500 });
function (next) {
topics.getPids(tid, next);
},
function (pids, next) {
batch.processArray(pids, function (pids, next) {
async.waterfall([
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) {
keys.push('cid:' + cid + ':uid:' + post.uid + ':pids:votes');
scores.push(post.votes);
}
db.sortedSetsAdd(keys, scores, post.pid, next);
}, next);
},
], next);
},
], next);
}, next);
},
], callback);
}; };
function updatePostCount(tid, oldCid, newCid, callback) { async function updatePostCount(tid, oldCid, newCid) {
async.waterfall([ const postCount = await topics.getTopicField(tid, 'postcount');
function (next) { if (!postCount) {
topics.getTopicField(tid, 'postcount', next); return;
}, }
function (postCount, next) {
if (!postCount) { await Promise.all([
return callback(); db.incrObjectFieldBy('category:' + oldCid, 'post_count', -postCount),
} db.incrObjectFieldBy('category:' + newCid, 'post_count', postCount),
async.parallel([ ]);
function (next) {
db.incrObjectFieldBy('category:' + oldCid, 'post_count', -postCount, next);
},
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); if (!topicsData.length) {
}, return { topics: [], uid: data.uid };
function (tids, next) { }
topics.getTopicsByTids(tids, data.uid, next); topics.calculateTopicIndices(topicsData, data.start);
},
async.apply(user.blocks.filter, data.uid), results = await plugins.fireHook('filter:category.topics.get', { cid: data.cid, topics: topicsData, uid: data.uid });
function (topicsData, next) { return { topics: results.topics, nextStart: data.stop + 1 };
if (!topicsData.length) {
return next(null, { topics: [], uid: data.uid });
}
topics.calculateTopicIndices(topicsData, data.start);
plugins.fireHook('filter:category.topics.get', { cid: data.cid, topics: topicsData, uid: data.uid }, next);
},
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);
dataForPinned.start = 0;
async.waterfall([ dataForPinned.stop = -1;
function (next) {
var dataForPinned = _.cloneDeep(data); const [pinnedTids, set, direction] = await Promise.all([
dataForPinned.start = 0; Categories.getPinnedTids(dataForPinned),
dataForPinned.stop = -1; Categories.buildTopicsSortedSet(data),
Categories.getSortedSetRangeDirection(data.sort),
async.parallel({ ]);
pinnedTids: async.apply(Categories.getPinnedTids, dataForPinned),
set: async.apply(Categories.buildTopicsSortedSet, data), const totalPinnedCount = pinnedTids.length;
direction: async.apply(Categories.getSortedSetRangeDirection, data.sort), const pinnedTidsOnPage = pinnedTids.slice(data.start, data.stop !== -1 ? data.stop + 1 : undefined);
}, next); const pinnedCountOnPage = pinnedTidsOnPage.length;
}, const topicsPerPage = data.stop - data.start + 1;
function (results, next) { const normalTidsToGet = Math.max(0, topicsPerPage - pinnedCountOnPage);
var totalPinnedCount = results.pinnedTids.length;
if (!normalTidsToGet && data.stop !== -1) {
pinnedTids = results.pinnedTids.slice(data.start, data.stop !== -1 ? data.stop + 1 : undefined); return pinnedTidsOnPage;
}
var pinnedCount = pinnedTids.length;
if (plugins.hasListeners('filter:categories.getTopicIds')) {
var topicsPerPage = data.stop - data.start + 1; const result = await plugins.fireHook('filter:categories.getTopicIds', {
tids: [],
var normalTidsToGet = Math.max(0, topicsPerPage - pinnedCount); data: data,
pinnedTids: pinnedTidsOnPage,
if (!normalTidsToGet && data.stop !== -1) { allPinnedTids: pinnedTids,
return next(null, []); totalPinnedCount: totalPinnedCount,
} normalTidsToGet: normalTidsToGet,
});
if (plugins.hasListeners('filter:categories.getTopicIds')) { return result && result.tids;
return plugins.fireHook('filter:categories.getTopicIds', { }
tids: [],
data: data, let start = data.start;
pinnedTids: pinnedTids, if (start > 0 && totalPinnedCount) {
allPinnedTids: results.pinnedTids, start -= totalPinnedCount - pinnedCountOnPage;
totalPinnedCount: totalPinnedCount, }
normalTidsToGet: normalTidsToGet,
}, function (err, data) { const stop = data.stop === -1 ? data.stop : start + normalTidsToGet - 1;
callback(err, data && data.tids); let normalTids;
}); const reverse = direction === 'highest-to-lowest';
} if (Array.isArray(set)) {
const weights = set.map((s, index) => (index ? 0 : 1));
var set = results.set; normalTids = await db[reverse ? 'getSortedSetRevIntersect' : 'getSortedSetIntersect']({ sets: set, start: start, stop: stop, weights: weights });
var direction = results.direction; } else {
var start = data.start; normalTids = await db[reverse ? 'getSortedSetRevRange' : 'getSortedSetRange'](set, start, stop);
if (start > 0 && totalPinnedCount) { }
start -= totalPinnedCount - pinnedCount; normalTids = normalTids.filter(tid => !pinnedTids.includes(tid));
}
return pinnedTids.concat(normalTids);
var stop = data.stop === -1 ? data.stop : start + normalTidsToGet - 1;
if (Array.isArray(set)) {
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);
} else {
db[direction === 'highest-to-lowest' ? 'getSortedSetRevRange' : 'getSortedSetRange'](set, start, stop, next);
}
},
function (normalTids, next) {
normalTids = normalTids.filter(tid => !pinnedTids.includes(tid));
next(null, 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) { if (Array.isArray(set)) {
Categories.buildTopicsSortedSet(data, next); return await db.sortedSetIntersectCard(set);
}, }
function (set, next) { return data.category.topic_count;
if (Array.isArray(set)) {
db.sortedSetIntersectCard(set, next);
} else {
next(null, 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([ keys = keys.filter((key, index) => !hasRead[index]);
function (next) { await db.setsAdd(keys, uid);
db.isMemberOfSets(keys, uid, next);
},
function (hasRead, next) {
keys = keys.filter((key, index) => !hasRead[index]);
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,162 +10,88 @@ 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([ if (!exists) {
function (next) { return;
Categories.exists(cid, next); }
},
function (exists, next) {
if (!exists) {
return callback();
}
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(); }
}); const result = await plugins.fireHook('filter:category.update', { cid: cid, category: modifiedFields });
} else {
next();
}
},
function (next) {
plugins.fireHook('filter:category.update', { cid: cid, category: modifiedFields }, next);
},
function (categoryData, next) {
category = categoryData.category;
var fields = Object.keys(category);
// move parent to front, so its updated first
var parentCidIndex = fields.indexOf('parentCid');
if (parentCidIndex !== -1 && fields.length > 1) {
fields.splice(0, 0, fields.splice(parentCidIndex, 1)[0]);
}
async.eachSeries(fields, function (key, next) { const category = result.category;
updateCategoryField(cid, key, category[key], next); var fields = Object.keys(category);
}, next); // move parent to front, so its updated first
}, var parentCidIndex = fields.indexOf('parentCid');
function (next) { if (parentCidIndex !== -1 && fields.length > 1) {
plugins.fireHook('action:category.update', { cid: cid, modified: category }); fields.splice(0, 0, fields.splice(parentCidIndex, 1)[0]);
next(); }
},
], callback); await async.eachSeries(fields, async function (key) {
await updateCategoryField(cid, key, category[key]);
});
plugins.fireHook('action:category.update', { cid: cid, modified: category });
} }
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);
if (key === 'order') {
await updateOrder(cid, value);
} else if (key === 'description') {
await Categories.parseDescription(cid, value);
} }
async.waterfall([
function (next) {
db.setObjectField('category:' + cid, key, value, next);
},
function (next) {
if (key === 'order') {
updateOrder(cid, value, next);
} else if (key === 'description') {
Categories.parseDescription(cid, value, next);
} 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) { const oldParent = await Categories.getCategoryField(cid, 'parentCid');
if (childrenCids.includes(parseInt(newParent, 10))) { await Promise.all([
return next(new Error('[[error:cant-set-child-as-parent]]')); db.sortedSetRemove('cid:' + oldParent + ':children', cid),
} db.sortedSetAdd('cid:' + newParent + ':children', cid, cid),
Categories.getCategoryField(cid, 'parentCid', next); db.setObjectField('category:' + cid, 'parentCid', newParent),
}, ]);
function (oldParent, next) {
async.series([
function (next) {
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']);
next();
},
], next);
},
], function (err) {
callback(err);
});
}
function updateTagWhitelist(cid, tags, callback) { cache.del(['cid:' + oldParent + ':children', 'cid:' + newParent + ':children']);
tags = tags.split(','); }
tags = tags.map(function (tag) {
return utils.cleanUpTag(tag, meta.config.maximumTagLength);
}).filter(Boolean);
async.waterfall([ async function updateTagWhitelist(cid, tags) {
function (next) { tags = tags.split(',').map(tag => utils.cleanUpTag(tag, meta.config.maximumTagLength))
db.delete('cid:' + cid + ':tag:whitelist', next); .filter(Boolean);
}, await db.delete('cid:' + cid + ':tag:whitelist');
function (next) { const scores = tags.map((tag, index) => index);
var scores = tags.map((tag, index) => index); await db.sortedSetAdd('cid:' + cid + ':tag:whitelist', scores, tags);
db.sortedSetAdd('cid:' + cid + ':tag:whitelist', scores, tags, next); cache.del('cid:' + cid + ':tag:whitelist');
},
function (next) {
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); cache.del(['categories:cid', 'cid:' + parentCid + ':children']);
},
function (parentCid, next) {
db.sortedSetsAdd(['categories:cid', 'cid:' + parentCid + ':children'], order, cid, function (err) {
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([ const keys = cids.map(cid => 'cid:' + cid + ':uid:watch:state');
function (next) { const [userSettings, states] = await Promise.all([
const keys = cids.map(cid => 'cid:' + cid + ':uid:watch:state'); user.getSettings(uid),
async.parallel({ db.sortedSetsScore(keys, uid),
userSettings: async.apply(user.getSettings, uid), ]);
states: async.apply(db.sortedSetsScore, keys, uid), return states.map(state => state || Categories.watchStates[userSettings.categoryWatchState]);
}, next);
},
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) { const readingUids = uids.filter((uid, index) => uid && states[index] !== Categories.watchStates.ignoring);
Categories.getUidsWatchStates(cid, uids, next); return readingUids;
},
function (states, next) {
const readingUids = uids.filter((uid, index) => uid && states[index] !== Categories.watchStates.ignoring);
next(null, 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