var RDB = require('./redis.js'), posts = require('./posts.js'), utils = require('./../public/src/utils.js'), user = require('./user.js'), async = require('async'), topics = require('./topics.js'); (function(Categories) { // START Move into possibly admin/categories.js // An admin-only function. Seeing how we have no control panel yet ima leave this right here. sit pretty, you Categories.create = function(data, callback) { RDB.incr('global:next_category_id', function(err, cid) { RDB.handle(err); var slug = cid + '/' + utils.slugify(data.name); RDB.rpush('categories:cid', cid); // Topic Info RDB.set('cid:' + cid + ':name', data.name); RDB.set('cid:' + cid + ':description', data.description); RDB.set('cid:' + cid + ':icon', data.icon); RDB.set('cid:' + cid + ':blockclass', data.blockclass); RDB.set('cid:' + cid + ':slug', slug); RDB.set('category:slug:' + slug + ':cid', cid); if (callback) callback({'status': 1}); }); }; Categories.edit = function(data, callback) { // just a reminder to self that name + slugs are stored into topics data as well. }; // END Move into possibly admin/categories.js Categories.privileges = function(cid, uid, callback) { function isModerator(next) { user.isModerator(uid, cid, function(isMod) { next(null, isMod); }); } function isAdministrator(next) { user.isAdministrator(uid, function(isAdmin) { next(null, isAdmin); }); } async.parallel([isModerator, isAdministrator], function(err, results) { callback({ editable: results.indexOf(true) !== -1 ? true : false, view_deleted: results.indexOf(true) !== -1 ? true : false }); }); } Categories.get = function(callback, category_id, current_user) { RDB.smembers('categories:' + category_id + ':tid', function(err, tids) { RDB.multi() .get('cid:' + category_id + ':name') .smembers('cid:' + category_id + ':active_users') .exec(function(err, replies) { category_name = replies[0]; active_usernames = replies[1]; if (category_name === null) { callback(false); } var active_users = []; for (var username in active_usernames) { active_users.push({'username': active_usernames[username]}); } var categoryData = { 'category_name' : category_name, 'show_topic_button' : 'hidden', 'category_id': category_id, 'active_users': active_users, 'topics' : [] }; if (tids.length === 0) { getModerators(function(err, moderators) { categoryData.moderator_block_class = moderators.length > 0 ? '' : 'none'; categoryData.moderators = moderators; callback(categoryData); }); } function getTopics(next) { Categories.getTopicsByTids(tids, current_user, function(topics) { next(null, topics); }, category_id); } function getModerators(next) { Categories.getModerators(category_id, function(moderators) { next(null, moderators); }); } async.parallel([getTopics, getModerators], function(err, results) { categoryData.topics = results[0]; categoryData.moderator_block_class = results[1].length > 0 ? '' : 'none'; categoryData.moderators = results[1]; callback(categoryData); }); }); }); } Categories.getTopicsByTids = function(tids, current_user, callback, category_id /*temporary*/) { var title = [], uid = [], timestamp = [], slug = [], postcount = [], locked = [], deleted = [], pinned = []; for (var i=0, ii=tids.length; i 0) { RDB.multi() .mget(name) .mget(description) .mget(icon) .mget(blockclass) .mget(slug) .mget(topic_count) .exec(function(err, replies) { name = replies[0]; description = replies[1]; icon = replies[2]; blockclass = replies[3]; slug = replies[4]; topic_count = replies[5]; function generateCategories() { var categories = []; for (var i=0, ii=cids.length; i