Files
NodeBB/src/categories.js

289 lines
7.7 KiB
JavaScript
Raw Normal View History

2014-03-01 16:59:04 -05:00
'use strict';
2014-11-08 23:54:21 -05:00
var async = require('async'),
nconf = require('nconf'),
db = require('./database'),
2014-01-26 17:17:34 -05:00
user = require('./user'),
2014-02-07 11:21:23 -05:00
Groups = require('./groups'),
plugins = require('./plugins'),
2014-03-25 12:23:55 -04:00
validator = require('validator'),
2014-11-08 23:54:21 -05:00
privileges = require('./privileges');
(function(Categories) {
2014-11-08 23:54:21 -05:00
require('./categories/create')(Categories);
require('./categories/delete')(Categories);
2014-11-08 23:54:21 -05:00
require('./categories/topics')(Categories);
require('./categories/unread')(Categories);
2014-03-12 21:41:53 -04:00
require('./categories/activeusers')(Categories);
require('./categories/recentreplies')(Categories);
require('./categories/update')(Categories);
2014-03-12 21:41:53 -04:00
2014-05-15 20:49:47 -04:00
Categories.exists = function(cid, callback) {
db.isSortedSetMember('categories:cid', cid, callback);
};
Categories.getCategoryById = function(data, callback) {
Categories.getCategories([data.cid], data.uid, function(err, categories) {
2014-08-22 19:10:26 -04:00
if (err || !Array.isArray(categories) || !categories[0]) {
2014-04-09 21:56:30 -04:00
return callback(err || new Error('[[error:invalid-cid]]'));
2014-02-26 16:43:21 -05:00
}
2014-08-22 19:10:26 -04:00
var category = categories[0];
if (parseInt(data.uid, 10)) {
Categories.markAsRead([data.cid], data.uid);
2014-02-07 20:30:10 -05:00
}
2013-07-19 16:13:00 -04:00
async.parallel({
topics: function(next) {
Categories.getCategoryTopics(data, next);
},
pageCount: function(next) {
Categories.getPageCount(data.cid, data.uid, next);
2014-08-29 15:57:20 -04:00
},
isIgnored: function(next) {
Categories.isIgnored([data.cid], data.uid, next);
}
}, function(err, results) {
if(err) {
return callback(err);
}
category.topics = results.topics.topics;
category.nextStart = results.topics.nextStart;
category.pageCount = results.pageCount;
2014-08-29 15:57:20 -04:00
category.isIgnored = results.isIgnored[0];
2013-05-24 07:52:15 -04:00
2015-02-12 13:23:12 -05:00
plugins.fireHook('filter:category.get', {category: category, uid: data.uid}, function(err, data) {
callback(err, data ? data.category : null);
});
});
});
2013-09-12 14:07:24 -04:00
};
2014-08-29 15:57:20 -04:00
Categories.isIgnored = function(cids, uid, callback) {
user.getIgnoredCategories(uid, function(err, ignoredCids) {
if (err) {
return callback(err);
}
cids = cids.map(function(cid) {
return ignoredCids.indexOf(cid.toString()) !== -1;
});
callback(null, cids);
});
};
2014-02-10 14:15:54 -05:00
Categories.getPageCount = function(cid, uid, callback) {
2014-11-09 00:33:26 -05:00
async.parallel({
topicCount: async.apply(Categories.getCategoryField, cid, 'topic_count'),
settings: async.apply(user.getSettings, uid)
}, function(err, results) {
2014-10-14 23:12:47 -04:00
if (err) {
return callback(err);
}
2014-11-09 00:33:26 -05:00
if (!parseInt(results.topicCount, 10)) {
return callback(null, 1);
}
2014-11-09 00:33:26 -05:00
callback(null, Math.ceil(parseInt(results.topicCount, 10) / results.settings.topicsPerPage));
});
};
2014-08-22 19:10:26 -04:00
Categories.getAllCategories = function(uid, callback) {
db.getSortedSetRange('categories:cid', 0, -1, function(err, cids) {
2014-11-09 00:45:18 -05:00
if (err || !Array.isArray(cids) || !cids.length) {
return callback(err, []);
2013-11-26 19:09:32 -05:00
}
2013-11-27 15:02:09 -05:00
2014-08-22 19:10:26 -04:00
Categories.getCategories(cids, uid, callback);
2014-05-27 12:44:28 -04:00
});
};
2014-08-15 15:45:01 -04:00
Categories.getCategoriesByPrivilege = function(uid, privilege, callback) {
2014-11-09 00:33:26 -05:00
async.waterfall([
function(next) {
db.getSortedSetRange('categories:cid', 0, -1, next);
},
function(cids, next) {
privileges.categories.filterCids(privilege, cids, uid, next);
},
function(cids, next) {
Categories.getCategories(cids, uid, next);
}
2014-11-09 00:33:26 -05:00
], callback);
2013-09-12 14:07:24 -04:00
};
2013-05-16 12:49:39 -04:00
Categories.getModerators = function(cid, callback) {
Groups.getMembers('cid:' + cid + ':privileges:mods', 0, -1, function(err, uids) {
2014-11-09 00:33:26 -05:00
if (err || !Array.isArray(uids) || !uids.length) {
2014-11-09 00:45:18 -05:00
return callback(err, []);
2013-07-19 16:13:00 -04:00
}
2014-11-09 00:33:26 -05:00
user.getMultipleUserFields(uids, ['uid', 'username', 'userslug', 'picture'], callback);
2013-05-16 12:49:39 -04:00
});
2013-09-12 14:07:24 -04:00
};
2013-06-28 12:40:15 -04:00
2014-08-22 19:10:26 -04:00
Categories.getCategoryData = function(cid, callback) {
Categories.getCategoriesData([cid], function(err, categories) {
2014-03-09 23:09:08 -04:00
callback(err, categories ? categories[0] : null);
});
2013-09-12 14:07:24 -04:00
};
2013-08-20 12:11:17 -04:00
2014-08-22 19:10:26 -04:00
Categories.getCategoriesData = function(cids, callback) {
2014-11-14 20:43:52 -05:00
if (!Array.isArray(cids) || !cids.length) {
return callback(null, []);
}
2014-03-09 23:09:08 -04:00
var keys = cids.map(function(cid) {
2014-05-19 19:24:06 -04:00
return 'category:' + cid;
2014-03-09 23:09:08 -04:00
});
db.getObjects(keys, function(err, categories) {
2014-11-09 00:45:18 -05:00
if (err || !Array.isArray(categories) || !categories.length) {
return callback(err, []);
2014-03-09 23:09:08 -04:00
}
async.map(categories, modifyCategory, callback);
2014-03-09 23:09:08 -04:00
});
2014-03-10 00:17:06 -04:00
};
2014-03-09 23:09:08 -04:00
function modifyCategory(category, callback) {
2014-11-14 22:07:19 -05:00
if (!category) {
return callback(null, null);
}
category.name = validator.escape(category.name);
category.disabled = parseInt(category.disabled, 10) === 1;
category.icon = category.icon || 'hidden';
2014-11-24 21:45:13 -05:00
if (category.hasOwnProperty('post_count')) {
category.post_count = category.post_count || 0;
}
if (category.description) {
category.description = validator.escape(category.description);
}
if (category.image) {
2014-11-13 13:00:36 -05:00
category.backgroundImage = category.image;
}
callback(null, category);
}
Categories.getCategoryField = function(cid, field, callback) {
2013-12-02 21:20:55 -05:00
db.getObjectField('category:' + cid, field, callback);
2013-09-12 14:07:24 -04:00
};
Categories.getMultipleCategoryFields = function(cids, fields, callback) {
2014-11-14 20:43:52 -05:00
if (!Array.isArray(cids) || !cids.length) {
return callback(null, []);
}
var keys = cids.map(function(cid) {
return 'category:' + cid;
});
2014-11-14 20:43:52 -05:00
db.getObjectsFields(keys, fields, function(err, categories) {
if (err) {
return callback(err);
}
async.map(categories, modifyCategory, callback);
});
};
Categories.getCategoryFields = function(cid, fields, callback) {
2013-12-02 21:20:55 -05:00
db.getObjectFields('category:' + cid, fields, callback);
2013-09-12 14:07:24 -04:00
};
2013-08-20 12:11:17 -04:00
2013-11-27 15:02:09 -05:00
Categories.setCategoryField = function(cid, field, value, callback) {
2013-12-02 21:20:55 -05:00
db.setObjectField('category:' + cid, field, value, callback);
2013-09-12 14:07:24 -04:00
};
2013-11-27 15:02:09 -05:00
Categories.incrementCategoryFieldBy = function(cid, field, value, callback) {
2013-12-02 21:20:55 -05:00
db.incrObjectFieldBy('category:' + cid, field, value, callback);
2013-09-12 14:07:24 -04:00
};
2013-07-03 12:59:45 -04:00
2013-11-26 14:25:46 -05:00
Categories.getCategories = function(cids, uid, callback) {
if (!Array.isArray(cids)) {
2014-04-09 21:56:30 -04:00
return callback(new Error('[[error:invalid-cid]]'));
}
2013-08-20 12:11:17 -04:00
if (!cids.length) {
2014-11-09 00:01:46 -05:00
return callback(null, []);
}
2014-03-09 23:09:08 -04:00
async.parallel({
categories: function(next) {
Categories.getCategoriesData(cids, next);
},
2014-08-22 19:10:26 -04:00
children: function(next) {
Categories.getChildren(cids, uid, next);
},
parents: function(next) {
Categories.getParents(cids, next);
},
2014-03-09 23:09:08 -04:00
hasRead: function(next) {
Categories.hasReadCategories(cids, uid, next);
}
}, function(err, results) {
2013-09-17 13:09:37 -04:00
if (err) {
return callback(err);
}
2013-08-20 12:11:17 -04:00
2014-03-09 23:09:08 -04:00
var categories = results.categories;
var hasRead = results.hasRead;
uid = parseInt(uid, 10);
for(var i=0; i<results.categories.length; ++i) {
2014-09-01 18:46:42 -04:00
if (categories[i]) {
categories[i]['unread-class'] = (parseInt(categories[i].topic_count, 10) === 0 || (hasRead[i] && uid !== 0)) ? '' : 'unread';
categories[i].children = results.children[i];
categories[i].parent = results.parents[i] && !results.parents[i].disabled ? results.parents[i] : null;
2014-09-01 18:46:42 -04:00
}
2014-03-09 23:09:08 -04:00
}
2013-10-07 17:48:11 -04:00
2014-05-27 12:44:28 -04:00
callback(null, categories);
});
2013-08-20 12:11:17 -04:00
};
2014-08-22 19:10:26 -04:00
Categories.getParents = function(cids, callback) {
2014-11-14 20:43:52 -05:00
Categories.getMultipleCategoryFields(cids, ['parentCid'], function(err, data) {
2014-08-22 19:10:26 -04:00
if (err) {
return callback(err);
}
var parentCids = data.map(function(category) {
if (category && category.hasOwnProperty('parentCid') && category.parentCid) {
return category.parentCid;
} else {
return 0;
}
2014-08-22 19:10:26 -04:00
});
Categories.getCategoriesData(parentCids, callback);
});
};
Categories.getChildren = function(cids, uid, callback) {
async.waterfall([
async.apply(db.getSortedSetRange, 'categories:cid', 0, -1),
2014-08-22 19:10:26 -04:00
function(cids, next) {
privileges.categories.filterCids('find', cids, uid, next);
},
function (cids, next) {
2014-08-22 19:10:26 -04:00
Categories.getCategoriesData(cids, next);
},
function (categories, next) {
async.map(cids, function(cid, next) {
next(null, categories.filter(function(category) {
2015-02-25 14:17:30 -05:00
return category && parseInt(category.parentCid, 10) === parseInt(cid, 10);
2014-08-22 19:10:26 -04:00
}));
}, next);
}
], callback);
};
2014-04-10 20:31:57 +01:00
}(exports));