more tests

v1.18.x
Baris Usakli 8 years ago
parent 11d9410229
commit 61eb7aa68b

@ -9,148 +9,160 @@ var Groups = require('./groups');
var plugins = require('./plugins'); var plugins = require('./plugins');
var privileges = require('./privileges'); var privileges = require('./privileges');
(function (Categories) { var Categories = module.exports;
require('./categories/data')(Categories);
require('./categories/create')(Categories); require('./categories/data')(Categories);
require('./categories/delete')(Categories); require('./categories/create')(Categories);
require('./categories/topics')(Categories); require('./categories/delete')(Categories);
require('./categories/unread')(Categories); require('./categories/topics')(Categories);
require('./categories/activeusers')(Categories); require('./categories/unread')(Categories);
require('./categories/recentreplies')(Categories); require('./categories/activeusers')(Categories);
require('./categories/update')(Categories); require('./categories/recentreplies')(Categories);
require('./categories/update')(Categories);
Categories.exists = function (cid, callback) {
db.isSortedSetMember('categories:cid', cid, callback); Categories.exists = function (cid, callback) {
}; db.isSortedSetMember('categories:cid', cid, callback);
};
Categories.getCategoryById = function (data, callback) {
var category; Categories.getCategoryById = function (data, callback) {
async.waterfall([ var category;
function (next) { async.waterfall([
Categories.getCategories([data.cid], data.uid, next); function (next) {
}, Categories.getCategories([data.cid], data.uid, next);
function (categories, next) { },
if (!Array.isArray(categories) || !categories[0]) { function (categories, next) {
return next(new Error('[[error:invalid-cid]]')); if (!Array.isArray(categories) || !categories[0]) {
} return next(new Error('[[error:invalid-cid]]'));
category = categories[0];
async.parallel({
topics: function (next) {
Categories.getCategoryTopics(data, next);
},
topicCount: function (next) {
if (Array.isArray(data.set)) {
db.sortedSetIntersectCard(data.set, next);
} else {
next(null, category.topic_count);
}
},
isIgnored: function (next) {
Categories.isIgnored([data.cid], data.uid, next);
},
}, next);
},
function (results, next) {
category.topics = results.topics.topics;
category.nextStart = results.topics.nextStart;
category.isIgnored = results.isIgnored[0];
category.topic_count = results.topicCount;
plugins.fireHook('filter:category.get', { category: category, uid: data.uid }, next);
},
function (data, next) {
next(null, data.category);
},
], callback);
};
Categories.isIgnored = function (cids, uid, callback) {
db.isSortedSetMembers('uid:' + uid + ':ignored:cids', cids, callback);
};
Categories.getPageCount = function (cid, uid, callback) {
async.parallel({
topicCount: async.apply(Categories.getCategoryField, cid, 'topic_count'),
settings: async.apply(user.getSettings, uid),
}, function (err, results) {
if (err) {
return callback(err);
} }
category = categories[0];
async.parallel({
topics: function (next) {
Categories.getCategoryTopics(data, next);
},
topicCount: function (next) {
if (Array.isArray(data.set)) {
db.sortedSetIntersectCard(data.set, next);
} else {
next(null, category.topic_count);
}
},
isIgnored: function (next) {
Categories.isIgnored([data.cid], data.uid, next);
},
}, next);
},
function (results, next) {
category.topics = results.topics.topics;
category.nextStart = results.topics.nextStart;
category.isIgnored = results.isIgnored[0];
category.topic_count = results.topicCount;
plugins.fireHook('filter:category.get', { category: category, uid: data.uid }, next);
},
function (data, next) {
next(null, data.category);
},
], callback);
};
Categories.isIgnored = function (cids, uid, callback) {
db.isSortedSetMembers('uid:' + uid + ':ignored:cids', cids, callback);
};
Categories.getPageCount = function (cid, uid, callback) {
async.waterfall([
function (next) {
async.parallel({
topicCount: async.apply(Categories.getCategoryField, cid, 'topic_count'),
settings: async.apply(user.getSettings, uid),
}, next);
},
function (results, next) {
if (!parseInt(results.topicCount, 10)) { if (!parseInt(results.topicCount, 10)) {
return callback(null, 1); return next(null, 1);
} }
callback(null, Math.ceil(parseInt(results.topicCount, 10) / results.settings.topicsPerPage)); next(null, Math.ceil(parseInt(results.topicCount, 10) / results.settings.topicsPerPage));
}); },
}; ], callback);
};
Categories.getAllCategories = function (uid, callback) {
db.getSortedSetRange('categories:cid', 0, -1, function (err, cids) { Categories.getAllCategories = function (uid, callback) {
if (err || !Array.isArray(cids) || !cids.length) { async.waterfall([
return callback(err, []); function (next) {
db.getSortedSetRange('categories:cid', 0, -1, next);
},
function (cids, next) {
if (!Array.isArray(cids) || !cids.length) {
return next(null, []);
} }
Categories.getCategories(cids, uid, callback); Categories.getCategories(cids, uid, next);
}); },
}; ], callback);
};
Categories.getCategoriesByPrivilege = function (set, uid, privilege, callback) {
async.waterfall([ Categories.getCategoriesByPrivilege = function (set, uid, privilege, callback) {
function (next) { async.waterfall([
db.getSortedSetRange(set, 0, -1, next); function (next) {
}, db.getSortedSetRange(set, 0, -1, next);
function (cids, next) { },
privileges.categories.filterCids(privilege, cids, uid, next); function (cids, next) {
}, privileges.categories.filterCids(privilege, cids, uid, next);
function (cids, next) { },
Categories.getCategories(cids, uid, next); function (cids, next) {
}, Categories.getCategories(cids, uid, next);
], callback); },
}; ], callback);
};
Categories.getModerators = function (cid, callback) {
Groups.getMembers('cid:' + cid + ':privileges:mods', 0, -1, function (err, uids) { Categories.getModerators = function (cid, callback) {
if (err || !Array.isArray(uids) || !uids.length) { async.waterfall([
return callback(err, []); function (next) {
Groups.getMembers('cid:' + cid + ':privileges:mods', 0, -1, next);
},
function (uids, next) {
if (!Array.isArray(uids) || !uids.length) {
return next(null, []);
} }
user.getUsersFields(uids, ['uid', 'username', 'userslug', 'picture'], callback); user.getUsersFields(uids, ['uid', 'username', 'userslug', 'picture'], next);
}); },
}; ], callback);
};
Categories.getCategories = function (cids, uid, callback) {
if (!Array.isArray(cids)) {
return callback(new Error('[[error:invalid-cid]]'));
}
if (!cids.length) { Categories.getCategories = function (cids, uid, callback) {
return callback(null, []); if (!Array.isArray(cids)) {
} return callback(new Error('[[error:invalid-cid]]'));
}
async.parallel({ if (!cids.length) {
categories: function (next) { return callback(null, []);
Categories.getCategoriesData(cids, next); }
},
children: function (next) {
Categories.getChildren(cids, uid, next);
},
parents: function (next) {
Categories.getParents(cids, next);
},
tagWhitelist: function (next) {
Categories.getTagWhitelist(cids, next);
},
hasRead: function (next) {
Categories.hasReadCategories(cids, uid, next);
},
}, function (err, results) {
if (err) {
return callback(err);
}
async.waterfall([
function (next) {
async.parallel({
categories: function (next) {
Categories.getCategoriesData(cids, next);
},
children: function (next) {
Categories.getChildren(cids, uid, next);
},
parents: function (next) {
Categories.getParents(cids, next);
},
tagWhitelist: function (next) {
Categories.getTagWhitelist(cids, next);
},
hasRead: function (next) {
Categories.hasReadCategories(cids, uid, next);
},
}, next);
},
function (results, next) {
uid = parseInt(uid, 10); uid = parseInt(uid, 10);
results.categories.forEach(function (category, i) { results.categories.forEach(function (category, i) {
if (category) { if (category) {
@ -162,202 +174,202 @@ var privileges = require('./privileges');
} }
}); });
callback(null, results.categories); next(null, results.categories);
}); },
}; ], callback);
};
Categories.getTagWhitelist = function (cids, callback) {
var keys = cids.map(function (cid) { Categories.getTagWhitelist = function (cids, callback) {
return 'cid:' + cid + ':tag:whitelist'; var keys = cids.map(function (cid) {
}); return 'cid:' + cid + ':tag:whitelist';
db.getSortedSetsMembers(keys, callback); });
}; db.getSortedSetsMembers(keys, callback);
};
function calculateTopicPostCount(category) {
if (!category) { function calculateTopicPostCount(category) {
return; if (!category) {
} return;
}
var postCount = parseInt(category.post_count, 10) || 0;
var topicCount = parseInt(category.topic_count, 10) || 0;
if (!Array.isArray(category.children) || !category.children.length) {
category.totalPostCount = postCount;
category.totalTopicCount = topicCount;
return;
}
category.children.forEach(function (child) {
calculateTopicPostCount(child);
postCount += parseInt(child.totalPostCount, 10) || 0;
topicCount += parseInt(child.totalTopicCount, 10) || 0;
});
var postCount = parseInt(category.post_count, 10) || 0;
var topicCount = parseInt(category.topic_count, 10) || 0;
if (!Array.isArray(category.children) || !category.children.length) {
category.totalPostCount = postCount; category.totalPostCount = postCount;
category.totalTopicCount = topicCount; category.totalTopicCount = topicCount;
return;
} }
Categories.getParents = function (cids, callback) { category.children.forEach(function (child) {
var categoriesData; calculateTopicPostCount(child);
var parentCids; postCount += parseInt(child.totalPostCount, 10) || 0;
async.waterfall([ topicCount += parseInt(child.totalTopicCount, 10) || 0;
function (next) { });
Categories.getCategoriesFields(cids, ['parentCid'], next);
}, category.totalPostCount = postCount;
function (_categoriesData, next) { category.totalTopicCount = topicCount;
categoriesData = _categoriesData; }
parentCids = categoriesData.filter(function (category) { Categories.getParents = function (cids, callback) {
return category && category.hasOwnProperty('parentCid') && parseInt(category.parentCid, 10); var categoriesData;
}).map(function (category) { var parentCids;
return parseInt(category.parentCid, 10); async.waterfall([
}); function (next) {
Categories.getCategoriesFields(cids, ['parentCid'], next);
if (!parentCids.length) { },
return callback(null, cids.map(function () { return null; })); function (_categoriesData, next) {
} categoriesData = _categoriesData;
Categories.getCategoriesData(parentCids, next); parentCids = categoriesData.filter(function (category) {
}, return category && category.hasOwnProperty('parentCid') && parseInt(category.parentCid, 10);
function (parentData, next) { }).map(function (category) {
parentData = categoriesData.map(function (category) { return parseInt(category.parentCid, 10);
return parentData[parentCids.indexOf(parseInt(category.parentCid, 10))]; });
});
next(null, parentData);
},
], callback);
};
Categories.getChildren = function (cids, uid, callback) {
var categories = cids.map(function (cid) {
return { cid: cid };
});
async.each(categories, function (category, next) {
getChildrenRecursive(category, uid, next);
}, function (err) {
callback(err, categories.map(function (c) {
return c && c.children;
}));
});
};
function getChildrenRecursive(category, uid, callback) {
async.waterfall([
function (next) {
db.getSortedSetRange('cid:' + category.cid + ':children', 0, -1, next);
},
function (children, next) {
privileges.categories.filterCids('find', children, uid, next);
},
function (children, next) {
children = children.filter(function (cid) {
return parseInt(category.cid, 10) !== parseInt(cid, 10);
});
if (!children.length) {
category.children = [];
return callback();
}
Categories.getCategoriesData(children, next);
},
function (childrenData, next) {
childrenData = childrenData.filter(Boolean);
category.children = childrenData;
async.each(category.children, function (child, next) {
getChildrenRecursive(child, uid, next);
}, next);
},
], callback);
}
Categories.flattenCategories = function (allCategories, categoryData) { if (!parentCids.length) {
categoryData.forEach(function (category) { return callback(null, cids.map(function () { return null; }));
if (category) { }
if (!category.parent) {
allCategories.push(category);
}
if (Array.isArray(category.children) && category.children.length) { Categories.getCategoriesData(parentCids, next);
Categories.flattenCategories(allCategories, category.children); },
} function (parentData, next) {
parentData = categoriesData.map(function (category) {
return parentData[parentCids.indexOf(parseInt(category.parentCid, 10))];
});
next(null, parentData);
},
], callback);
};
Categories.getChildren = function (cids, uid, callback) {
var categories = cids.map(function (cid) {
return { cid: cid };
});
async.each(categories, function (category, next) {
getChildrenRecursive(category, uid, next);
}, function (err) {
callback(err, categories.map(function (c) {
return c && c.children;
}));
});
};
function getChildrenRecursive(category, uid, callback) {
async.waterfall([
function (next) {
db.getSortedSetRange('cid:' + category.cid + ':children', 0, -1, next);
},
function (children, next) {
privileges.categories.filterCids('find', children, uid, next);
},
function (children, next) {
children = children.filter(function (cid) {
return parseInt(category.cid, 10) !== parseInt(cid, 10);
});
if (!children.length) {
category.children = [];
return callback();
} }
}); Categories.getCategoriesData(children, next);
}; },
function (childrenData, next) {
/** childrenData = childrenData.filter(Boolean);
* Recursively build tree category.children = childrenData;
* async.each(category.children, function (child, next) {
* @param categories {array} flat list of categories getChildrenRecursive(child, uid, next);
* @param parentCid {number} start from 0 to build full tree }, next);
*/ },
Categories.getTree = function (categories, parentCid) { ], callback);
var tree = []; }
var i = 0;
var len = categories.length; Categories.flattenCategories = function (allCategories, categoryData) {
var category; categoryData.forEach(function (category) {
if (category) {
for (i; i < len; i += 1) { if (!category.parent) {
category = categories[i]; allCategories.push(category);
if (!category.hasOwnProperty('parentCid') || category.parentCid === null) {
category.parentCid = 0;
} }
if (parseInt(category.parentCid, 10) === parseInt(parentCid, 10)) { if (Array.isArray(category.children) && category.children.length) {
tree.push(category); Categories.flattenCategories(allCategories, category.children);
category.children = Categories.getTree(categories, category.cid);
} }
} }
});
};
/**
* Recursively build tree
*
* @param categories {array} flat list of categories
* @param parentCid {number} start from 0 to build full tree
*/
Categories.getTree = function (categories, parentCid) {
var tree = [];
var i = 0;
var len = categories.length;
var category;
for (i; i < len; i += 1) {
category = categories[i];
if (!category.hasOwnProperty('parentCid') || category.parentCid === null) {
category.parentCid = 0;
}
return tree; if (parseInt(category.parentCid, 10) === parseInt(parentCid, 10)) {
}; tree.push(category);
category.children = Categories.getTree(categories, category.cid);
}
}
Categories.buildForSelect = function (uid, callback) { return tree;
function recursive(category, categoriesData, level) { };
if (category.link) {
return;
}
var bullet = level ? '&bull; ' : ''; Categories.buildForSelect = function (uid, callback) {
category.value = category.cid; function recursive(category, categoriesData, level) {
category.text = level + bullet + category.name; if (category.link) {
categoriesData.push(category); return;
}
category.children.forEach(function (child) { var bullet = level ? '&bull; ' : '';
recursive(child, categoriesData, '&nbsp;&nbsp;&nbsp;&nbsp;' + level); category.value = category.cid;
}); category.text = level + bullet + category.name;
categoriesData.push(category);
category.children.forEach(function (child) {
recursive(child, categoriesData, '&nbsp;&nbsp;&nbsp;&nbsp;' + level);
});
}
Categories.getCategoriesByPrivilege('cid:0:children', uid, 'read', function (err, categories) {
if (err) {
return callback(err);
} }
Categories.getCategoriesByPrivilege('cid:0:children', uid, 'read', function (err, categories) {
if (err) {
return callback(err);
}
var categoriesData = []; var categoriesData = [];
categories = categories.filter(function (category) { categories = categories.filter(function (category) {
return category && !category.link && !parseInt(category.parentCid, 10); return category && !category.link && !parseInt(category.parentCid, 10);
}); });
categories.forEach(function (category) { categories.forEach(function (category) {
recursive(category, categoriesData, ''); recursive(category, categoriesData, '');
});
callback(null, categoriesData);
}); });
}; callback(null, categoriesData);
});
Categories.getIgnorers = function (cid, start, stop, callback) { };
db.getSortedSetRevRange('cid:' + cid + ':ignorers', start, stop, callback);
}; Categories.getIgnorers = function (cid, start, stop, callback) {
db.getSortedSetRevRange('cid:' + cid + ':ignorers', start, stop, callback);
Categories.filterIgnoringUids = function (cid, uids, callback) { };
async.waterfall([
function (next) { Categories.filterIgnoringUids = function (cid, uids, callback) {
db.isSortedSetMembers('cid:' + cid + ':ignorers', uids, next); async.waterfall([
}, function (next) {
function (isIgnoring, next) { db.isSortedSetMembers('cid:' + cid + ':ignorers', uids, next);
var readingUids = uids.filter(function (uid, index) { },
return uid && !isIgnoring[index]; function (isIgnoring, next) {
}); var readingUids = uids.filter(function (uid, index) {
next(null, readingUids); return uid && !isIgnoring[index];
}, });
], callback); next(null, readingUids);
}; },
}(exports)); ], callback);
};

@ -141,19 +141,22 @@ module.exports = function (Topics) {
Topics.updateRecent = function (tid, timestamp, callback) { Topics.updateRecent = function (tid, timestamp, callback) {
callback = callback || function () {}; callback = callback || function () {};
if (plugins.hasListeners('filter:topics.updateRecent')) {
plugins.fireHook('filter:topics.updateRecent', { tid: tid, timestamp: timestamp }, function (err, data) { async.waterfall([
if (err) { function (next) {
return callback(err); if (plugins.hasListeners('filter:topics.updateRecent')) {
plugins.fireHook('filter:topics.updateRecent', { tid: tid, timestamp: timestamp }, next);
} else {
next(null, { tid: tid, timestamp: timestamp });
} }
},
function (data, next) {
if (data && data.tid && data.timestamp) { if (data && data.tid && data.timestamp) {
db.sortedSetAdd('topics:recent', data.timestamp, data.tid, callback); db.sortedSetAdd('topics:recent', data.timestamp, data.tid, next);
} else { } else {
callback(); next();
} }
}); },
} else { ], callback);
db.sortedSetAdd('topics:recent', timestamp, tid, callback);
}
}; };
}; };

@ -8,7 +8,6 @@ var image = require('../src/image');
var file = require('../src/file'); var file = require('../src/file');
describe('image', function () { describe('image', function () {
it('should normalise image', function (done) { it('should normalise image', function (done) {
image.normalise(path.join(__dirname, 'files/normalise.jpg'), '.jpg', function (err) { image.normalise(path.join(__dirname, 'files/normalise.jpg'), '.jpg', function (err) {
assert.ifError(err); assert.ifError(err);
@ -19,5 +18,4 @@ describe('image', function () {
}); });
}); });
}); });
}); });

@ -110,6 +110,14 @@ describe('Topic\'s', function () {
done(); done();
}); });
}); });
it('should return false for falsy uid', function (done) {
topics.isOwner(topic.tid, 0, function (err, isOwner) {
assert.ifError(err);
assert(!isOwner);
done();
});
});
}); });
describe('.reply', function () { describe('.reply', function () {

Loading…
Cancel
Save