feat: #7743 categories

This commit is contained in:
Barış Soner Uşaklı
2019-07-16 00:41:42 -04:00
parent 45223cded6
commit fcf3e0770b
13 changed files with 714 additions and 1230 deletions

View File

@@ -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);
}; };
}; };

View File

@@ -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;
var slug = cid + '/' + utils.slugify(data.name);
var order = data.order || cid; // If no order provided, place it at the end
var 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 : '',
descriptionParsed: data.descriptionParsed ? data.descriptionParsed : '', descriptionParsed: data.descriptionParsed ? data.descriptionParsed : '',
icon: data.icon ? data.icon : '', icon: data.icon ? data.icon : '',
bgColor: data.bgColor || colours[0], bgColor: data.bgColor || colours[0],
color: data.color || colours[1], color: data.color || colours[1],
slug: slug, slug: slug,
parentCid: parentCid, parentCid: parentCid,
topic_count: 0, topic_count: 0,
post_count: 0, post_count: 0,
disabled: data.disabled ? 1 : 0, disabled: data.disabled ? 1 : 0,
order: order, order: order,
link: data.link || '', link: data.link || '',
numRecentReplies: 1, numRecentReplies: 1,
class: (data.class ? data.class : 'col-md-3 col-xs-6'), class: (data.class ? data.class : 'col-md-3 col-xs-6'),
imageClass: 'cover', imageClass: 'cover',
isSection: 0, isSection: 0,
}; };
if (data.backgroundImage) { if (data.backgroundImage) {
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',
'topics:create', 'topics:create',
'topics:reply', 'topics:reply',
'topics:tag', 'topics:tag',
'posts:edit', 'posts:edit',
'posts:history', 'posts:history',
'posts:delete', 'posts:delete',
'posts:upvote', 'posts:upvote',
'posts:downvote', 'posts:downvote',
'topics:delete', 'topics:delete',
]; ];
const modPrivileges = defaultPrivileges.concat([ const modPrivileges = defaultPrivileges.concat([
'posts:view_deleted', 'posts:view_deleted',
'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(); await db.sortedSetsAdd(['categories:cid', 'cid:' + parentCid + ':children'], category.order, category.cid);
} await privileges.categories.give(defaultPrivileges, category.cid, 'registered-users');
Categories.parseDescription(category.cid, category.description, next); await privileges.categories.give(modPrivileges, category.cid, ['administrators', 'Global Moderators']);
}, await privileges.categories.give(['find', 'read', 'topics:read'], category.cid, ['guests', 'spiders']);
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); cache.del(['categories:cid', 'cid:' + parentCid + ':children']);
}, if (data.cloneFromCid && parseInt(data.cloneFromCid, 10)) {
function (_category, next) { category = await Categories.copySettingsFrom(data.cloneFromCid, category.cid, !data.parentCid);
category = _category; }
if (data.cloneChildren) {
return duplicateCategoriesChildren(category.cid, data.cloneFromCid, data.uid, next);
}
next(); if (data.cloneChildren) {
}, await duplicateCategoriesChildren(category.cid, data.cloneFromCid, data.uid);
function (next) { }
plugins.fireHook('action:category.create', { category: category });
next(null, category); plugins.fireHook('action:category.create', { category: 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];
children.forEach(function (child) { children.forEach(function (child) {
child.parentCid = parentCid; child.parentCid = parentCid;
child.cloneFromCid = child.cid; child.cloneFromCid = child.cid;
child.cloneChildren = true; child.cloneChildren = true;
child.name = utils.decodeHTMLEntities(child.name); child.name = utils.decodeHTMLEntities(child.name);
child.description = utils.decodeHTMLEntities(child.description); child.description = utils.decodeHTMLEntities(child.description);
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 newParent = parseInt(results.source.parentCid, 10) || 0;
if (copyParent && newParent !== parseInt(toCid, 10)) {
tasks.push(async.apply(db.sortedSetRemove, 'cid:' + oldParent + ':children', toCid));
tasks.push(async.apply(db.sortedSetAdd, 'cid:' + newParent + ':children', results.source.order, toCid));
tasks.push(function (next) {
cache.del(['cid:' + oldParent + ':children', 'cid:' + newParent + ':children']);
setImmediate(next);
});
}
destination.description = results.source.description;
destination.descriptionParsed = results.source.descriptionParsed;
destination.icon = results.source.icon;
destination.bgColor = results.source.bgColor;
destination.color = results.source.color;
destination.link = results.source.link;
destination.numRecentReplies = results.source.numRecentReplies;
destination.class = results.source.class;
destination.image = results.source.image;
destination.imageClass = results.source.imageClass;
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) {
var data;
async.waterfall([
function (next) {
db.getSortedSetRangeWithScores('cid:' + fromCid + ':tag:whitelist', 0, -1, next);
},
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) {
if (typeof group === 'function') {
callback = group;
group = '';
} }
async.waterfall([ const oldParent = parseInt(destination.parentCid, 10) || 0;
function (next) { const newParent = parseInt(source.parentCid, 10) || 0;
plugins.fireHook('filter:categories.copyPrivilegesFrom', { if (copyParent && newParent !== parseInt(toCid, 10)) {
privileges: group ? privileges.groupPrivilegeList.slice() : privileges.privilegeList.slice(), await db.sortedSetRemove('cid:' + oldParent + ':children', toCid);
fromCid: fromCid, await db.sortedSetAdd('cid:' + newParent + ':children', source.order, toCid);
toCid: toCid, cache.del(['cid:' + oldParent + ':children', 'cid:' + newParent + ':children']);
group: group, }
}, next);
}, destination.description = source.description;
function (data, next) { destination.descriptionParsed = source.descriptionParsed;
if (group) { destination.icon = source.icon;
copyPrivilegesByGroup(data.privileges, data.fromCid, data.toCid, group, next); destination.bgColor = source.bgColor;
} else { destination.color = source.color;
copyPrivileges(data.privileges, data.fromCid, data.toCid, next); destination.link = source.link;
} destination.numRecentReplies = source.numRecentReplies;
}, destination.class = source.class;
], callback); destination.image = source.image;
destination.imageClass = source.imageClass;
if (copyParent) {
destination.parentCid = source.parentCid || 0;
}
await db.setObject('category:' + toCid, destination);
await copyTagWhitelist(fromCid, toCid);
await Categories.copyPrivilegesFrom(fromCid, toCid);
return destination;
}; };
function copyPrivileges(privileges, fromCid, toCid, callback) { async function copyTagWhitelist(fromCid, toCid) {
const toGroups = privileges.map(privilege => 'group:cid:' + toCid + ':privileges:' + privilege + ':members'); const data = await db.getSortedSetRangeWithScores('cid:' + fromCid + ':tag:whitelist', 0, -1);
const fromGroups = privileges.map(privilege => 'group:cid:' + fromCid + ':privileges:' + privilege + ':members'); await db.delete('cid:' + toCid + ':tag:whitelist');
async.waterfall([ await db.sortedSetAdd('cid:' + toCid + ':tag:whitelist', data.map(item => item.score), data.map(item => item.value));
function (next) { cache.del('cid:' + toCid + ':tag:whitelist');
db.getSortedSetsMembers(toGroups.concat(fromGroups), next);
},
function (currentMembers, next) {
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) { Categories.copyPrivilegesFrom = async function (fromCid, toCid, group) {
async.waterfall([ group = group || '';
function (next) {
const leaveGroups = privileges.map(privilege => 'cid:' + toCid + ':privileges:' + privilege); const data = await plugins.fireHook('filter:categories.copyPrivilegesFrom', {
groups.leave(leaveGroups, group, next); privileges: group ? privileges.groupPrivilegeList.slice() : privileges.privilegeList.slice(),
}, fromCid: fromCid,
function (next) { toCid: toCid,
const checkGroups = privileges.map(privilege => 'group:cid:' + fromCid + ':privileges:' + privilege + ':members'); group: group,
db.isMemberOfSortedSets(checkGroups, group, next); });
}, if (group) {
function (isMembers, next) { await copyPrivilegesByGroup(data.privileges, data.fromCid, data.toCid, group);
privileges = privileges.filter((priv, index) => isMembers[index]); } else {
const joinGroups = privileges.map(privilege => 'cid:' + toCid + ':privileges:' + privilege); await copyPrivileges(data.privileges, data.fromCid, data.toCid);
groups.join(joinGroups, group, next); }
}, };
], callback);
async function copyPrivileges(privileges, fromCid, toCid) {
const toGroups = privileges.map(privilege => 'group:cid:' + toCid + ':privileges:' + privilege + ':members');
const fromGroups = privileges.map(privilege => 'group:cid:' + fromCid + ':privileges:' + privilege + ':members');
const currentMembers = await db.getSortedSetsMembers(toGroups.concat(fromGroups));
const copyGroups = _.uniq(_.flatten(currentMembers));
await async.each(copyGroups, async function (group) {
await copyPrivilegesByGroup(privileges, fromCid, toCid, group);
});
}
async function copyPrivilegesByGroup(privileges, fromCid, toCid, group) {
const leaveGroups = privileges.map(privilege => 'cid:' + toCid + ':privileges:' + privilege);
await groups.leave(leaveGroups, group);
const checkGroups = privileges.map(privilege => 'group:cid:' + fromCid + ':privileges:' + privilege + ':members');
const isMembers = await db.isMemberOfSortedSets(checkGroups, group);
privileges = privileges.filter((priv, index) => isMembers[index]);
const joinGroups = privileges.map(privilege => 'cid:' + toCid + ':privileges:' + privilege);
await groups.join(joinGroups, group);
} }
}; };

View File

@@ -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);
}; };
}; };

View File

@@ -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); 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) { plugins.fireHook('action:category.delete', { cid: cid, uid: uid });
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) { 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([
}, 'cid:' + cid + ':tids',
function (next) { 'cid:' + cid + ':tids:pinned',
removeFromParent(cid, next); 'cid:' + cid + ':tids:posts',
}, 'cid:' + cid + ':pids',
function (next) { 'cid:' + cid + ':read_by_uid',
db.deleteAll([ 'cid:' + cid + ':uid:watch:state',
'cid:' + cid + ':tids', 'cid:' + cid + ':children',
'cid:' + cid + ':tids:pinned', 'cid:' + cid + ':tag:whitelist',
'cid:' + cid + ':tids:posts', 'category:' + cid,
'cid:' + cid + ':pids', ]);
'cid:' + cid + ':read_by_uid', await groups.destroy(privileges.privilegeList.map(privilege => 'cid:' + cid + ':privileges:' + privilege));
'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);
});
} }
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',
]);
} }
}; };

View File

@@ -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);
}
category = categories[0];
data.category = category;
async.parallel({
topics: function (next) {
Categories.getCategoryTopics(data, next);
},
topicCount: function (next) {
Categories.getTopicCount(data, next);
},
watchState: function (next) {
Categories.getWatchState([data.cid], data.uid, next);
},
parent: function (next) {
if (category.parentCid) {
Categories.getCategoryData(category.parentCid, next);
} else {
next();
}
},
children: function (next) {
getChildrenTree(category, data.uid, next);
},
}, 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); const promises = [
plugins.fireHook('filter:category.get', { category: category, uid: data.uid }, next); Categories.getCategoryTopics(data),
}, Categories.getTopicCount(data),
function (data, next) { Categories.getWatchState([data.cid], data.uid),
next(null, data.category); getChildrenTree(category, data.uid),
}, ];
], callback);
if (category.parentCid) {
promises.push(Categories.getCategoryData(category.parentCid));
}
const [topics, topicCount, watchState, , parent] = await Promise.all(promises);
category.topics = topics.topics;
category.nextStart = topics.nextStart;
category.topic_count = topicCount;
category.isWatched = watchState[0] === Categories.watchStates.watching;
category.isNotWatched = watchState[0] === Categories.watchStates.notwatching;
category.isIgnored = watchState[0] === Categories.watchStates.ignoring;
category.parent = parent;
calculateTopicPostCount(category);
const result = await plugins.fireHook('filter:category.get', { category: category, uid: data.uid });
return result.category;
}; };
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();
};
Categories.getAllCategories = async function (uid) {
const cids = await Categories.getAllCidsFromSet('categories:cid');
return await Categories.getCategories(cids, uid);
};
Categories.getCidsByPrivilege = async function (set, uid, privilege) {
const cids = await Categories.getAllCidsFromSet('categories:cid');
return await privileges.categories.filterCids(privilege, cids, uid);
};
Categories.getCategoriesByPrivilege = async function (set, uid, privilege) {
const cids = await Categories.getCidsByPrivilege(set, uid, privilege);
return await Categories.getCategories(cids, uid);
};
Categories.getModerators = async function (cid) {
const uids = await Categories.getModeratorUids([cid]);
return await user.getUsersFields(uids[0], ['uid', 'username', 'userslug', 'picture']);
};
Categories.getModeratorUids = async function (cids) {
const groupNames = cids.reduce(function (memo, cid) {
memo.push('cid:' + cid + ':privileges:moderate');
memo.push('cid:' + cid + ':privileges:groups:moderate');
return memo;
}, []);
const memberSets = await groups.getMembersOfGroups(groupNames);
// 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) {
memo.groupNames.push(set);
} else {
memo.uids.push(set);
} }
cache.set(key, cids);
callback(null, cids.slice()); return memo;
}, { groupNames: [], uids: [] });
const uniqGroups = _.uniq(_.flatten(sets.groupNames));
const groupUids = await groups.getMembersOfGroups(uniqGroups);
const map = _.zipObject(uniqGroups, groupUids);
const moderatorUids = cids.map(function (cid, index) {
return _.uniq(sets.uids[index].concat(_.flatten(sets.groupNames[index].map(g => map[g]))));
}); });
return moderatorUids;
}; };
Categories.getAllCategories = function (uid, callback) { Categories.getCategories = async function (cids, uid) {
async.waterfall([
function (next) {
Categories.getAllCidsFromSet('categories:cid', next);
},
function (cids, next) {
Categories.getCategories(cids, uid, next);
},
], callback);
};
Categories.getCidsByPrivilege = function (set, uid, privilege, callback) {
async.waterfall([
function (next) {
Categories.getAllCidsFromSet(set, next);
},
function (cids, next) {
privileges.categories.filterCids(privilege, cids, uid, next);
},
], callback);
};
Categories.getCategoriesByPrivilege = function (set, uid, privilege, callback) {
async.waterfall([
function (next) {
Categories.getCidsByPrivilege(set, uid, privilege, next);
},
function (cids, next) {
Categories.getCategories(cids, uid, next);
},
], callback);
};
Categories.getModerators = function (cid, callback) {
async.waterfall([
function (next) {
Categories.getModeratorUids([cid], next);
},
function (uids, next) {
user.getUsersFields(uids[0], ['uid', 'username', 'userslug', 'picture'], next);
},
], callback);
};
Categories.getModeratorUids = function (cids, callback) {
var sets;
var uniqGroups;
async.waterfall([
function (next) {
var groupNames = cids.reduce(function (memo, cid) {
memo.push('cid:' + cid + ':privileges:moderate');
memo.push('cid:' + cid + ':privileges:groups:moderate');
return memo;
}, []);
groups.getMembersOfGroups(groupNames, next);
},
function (memberSets, next) {
// 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;
}, { groupNames: [], uids: [] });
uniqGroups = _.uniq(_.flatten(sets.groupNames));
groups.getMembersOfGroups(uniqGroups, next);
},
function (groupUids, next) {
var map = _.zipObject(uniqGroups, groupUids);
const moderatorUids = cids.map(function (cid, index) {
return _.uniq(sets.uids[index].concat(_.flatten(sets.groupNames[index].map(g => map[g]))));
});
next(null, moderatorUids);
},
], callback);
};
Categories.getCategories = function (cids, uid, callback) {
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);

View File

@@ -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);
}, if (count < numRecentReplies) {
numRecentReplies: function (next) { return await db.sortedSetAdd('cid:' + cid + ':recent_tids', Date.now(), tid);
db.getObjectField('category:' + cid, 'numRecentReplies', next); }
}, const data = await db.getSortedSetRangeWithScores('cid:' + cid + ':recent_tids', 0, count - numRecentReplies);
}, next); if (data.length) {
}, await db.sortedSetsRemoveRangeByScore(['cid:' + cid + ':recent_tids'], '-inf', data[data.length - 1].score);
function (results, next) { }
if (results.count < results.numRecentReplies) { await db.sortedSetAdd('cid:' + cid + ':recent_tids', Date.now(), tid);
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) { 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]; if (!tid) {
posts.getPostField(pid, 'tid', next); return;
}, }
function (tid, next) { await Categories.updateRecentTid(cid, tid);
if (!tid) {
return next();
}
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;
} }
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);
Categories.moveRecentReplies = function (tid, oldCid, cid, callback) { await batch.processArray(pids, async function (pids) {
callback = callback || function () {}; 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]);
}
});
async.waterfall([ await Promise.all([
function (next) { db.sortedSetRemove('cid:' + oldCid + ':pids', pids),
updatePostCount(tid, oldCid, cid, next); db.sortedSetAdd('cid:' + cid + ':pids', timestamps, pids),
}, db.sortedSetRemoveBulk(bulkRemove),
function (next) { db.sortedSetAddBulk(bulkAdd),
topics.getPids(tid, next); ]);
}, }, { batch: 500 });
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);
} }
}; };

View File

@@ -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) {
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); if (!topicsData.length) {
}, return { topics: [], uid: data.uid };
function (results, next) { }
next(null, { topics: results.topics, nextStart: data.stop + 1 }); topics.calculateTopicIndices(topicsData, data.start);
},
], callback); results = await plugins.fireHook('filter:category.topics.get', { cid: data.cid, topics: topicsData, uid: data.uid });
return { topics: results.topics, nextStart: data.stop + 1 };
}; };
Categories.getTopicIds = function (data, callback) { Categories.getTopicIds = async function (data) {
var pinnedTids; const dataForPinned = _.cloneDeep(data);
dataForPinned.start = 0;
dataForPinned.stop = -1;
async.waterfall([ const [pinnedTids, set, direction] = await Promise.all([
function (next) { Categories.getPinnedTids(dataForPinned),
var dataForPinned = _.cloneDeep(data); Categories.buildTopicsSortedSet(data),
dataForPinned.start = 0; Categories.getSortedSetRangeDirection(data.sort),
dataForPinned.stop = -1; ]);
async.parallel({ const totalPinnedCount = pinnedTids.length;
pinnedTids: async.apply(Categories.getPinnedTids, dataForPinned), const pinnedTidsOnPage = pinnedTids.slice(data.start, data.stop !== -1 ? data.stop + 1 : undefined);
set: async.apply(Categories.buildTopicsSortedSet, data), const pinnedCountOnPage = pinnedTidsOnPage.length;
direction: async.apply(Categories.getSortedSetRangeDirection, data.sort), const topicsPerPage = data.stop - data.start + 1;
}, next); const normalTidsToGet = Math.max(0, topicsPerPage - pinnedCountOnPage);
},
function (results, next) {
var totalPinnedCount = results.pinnedTids.length;
pinnedTids = results.pinnedTids.slice(data.start, data.stop !== -1 ? data.stop + 1 : undefined); if (!normalTidsToGet && data.stop !== -1) {
return pinnedTidsOnPage;
}
var pinnedCount = pinnedTids.length; if (plugins.hasListeners('filter:categories.getTopicIds')) {
const result = await plugins.fireHook('filter:categories.getTopicIds', {
tids: [],
data: data,
pinnedTids: pinnedTidsOnPage,
allPinnedTids: pinnedTids,
totalPinnedCount: totalPinnedCount,
normalTidsToGet: normalTidsToGet,
});
return result && result.tids;
}
var topicsPerPage = data.stop - data.start + 1; let start = data.start;
if (start > 0 && totalPinnedCount) {
start -= totalPinnedCount - pinnedCountOnPage;
}
var normalTidsToGet = Math.max(0, topicsPerPage - pinnedCount); const stop = data.stop === -1 ? data.stop : start + normalTidsToGet - 1;
let normalTids;
const reverse = direction === 'highest-to-lowest';
if (Array.isArray(set)) {
const weights = set.map((s, index) => (index ? 0 : 1));
normalTids = await db[reverse ? 'getSortedSetRevIntersect' : 'getSortedSetIntersect']({ sets: set, start: start, stop: stop, weights: weights });
} else {
normalTids = await db[reverse ? 'getSortedSetRevRange' : 'getSortedSetRange'](set, start, stop);
}
normalTids = normalTids.filter(tid => !pinnedTids.includes(tid));
if (!normalTidsToGet && data.stop !== -1) { return pinnedTids.concat(normalTids);
return next(null, []);
}
if (plugins.hasListeners('filter:categories.getTopicIds')) {
return plugins.fireHook('filter:categories.getTopicIds', {
tids: [],
data: data,
pinnedTids: pinnedTids,
allPinnedTids: results.pinnedTids,
totalPinnedCount: totalPinnedCount,
normalTidsToGet: normalTidsToGet,
}, function (err, data) {
callback(err, data && data.tids);
});
}
var set = results.set;
var direction = results.direction;
var start = data.start;
if (start > 0 && totalPinnedCount) {
start -= totalPinnedCount - pinnedCount;
}
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);
}; };
}; };

View File

@@ -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);
}; };
}; };

View File

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

View File

@@ -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);
}; };
}; };

View File

@@ -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) };
} }

View File

@@ -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) {

View File

@@ -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);