"use strict"; var async = require('async'), _ = require('underscore'), db = require('./database'), posts = require('./posts'), utils = require('../public/src/utils'), plugins = require('./plugins'), user = require('./user'), categories = require('./categories'), privileges = require('./privileges'); (function(Topics) { require('./topics/data')(Topics); require('./topics/create')(Topics); require('./topics/delete')(Topics); require('./topics/unread')(Topics); require('./topics/recent')(Topics); require('./topics/popular')(Topics); require('./topics/user')(Topics); require('./topics/fork')(Topics); require('./topics/posts')(Topics); require('./topics/follow')(Topics); require('./topics/tags')(Topics); require('./topics/teaser')(Topics); require('./topics/suggested')(Topics); require('./topics/tools')(Topics); Topics.exists = function(tid, callback) { db.isSortedSetMember('topics:tid', tid, callback); }; Topics.getPageCount = function(tid, uid, callback) { Topics.getTopicField(tid, 'postcount', function(err, postCount) { if (err) { return callback(err); } if (!parseInt(postCount, 10)) { return callback(null, 1); } user.getSettings(uid, function(err, settings) { if (err) { return callback(err); } callback(null, Math.ceil((parseInt(postCount, 10) - 1) / settings.postsPerPage)); }); }); }; Topics.getTidPage = function(tid, uid, callback) { if(!tid) { return callback(new Error('[[error:invalid-tid]]')); } async.parallel({ index: function(next) { categories.getTopicIndex(tid, next); }, settings: function(next) { user.getSettings(uid, next); } }, function(err, results) { if (err) { return callback(err); } callback(null, Math.ceil((results.index + 1) / results.settings.topicsPerPage)); }); }; Topics.getTopicsFromSet = function(set, uid, start, stop, callback) { async.waterfall([ function(next) { db.getSortedSetRevRange(set, start, stop, next); }, function(tids, next) { Topics.getTopics(tids, uid, next); }, function(topics, next) { next(null, {topics: topics, nextStart: stop + 1}); } ], callback); }; Topics.getTopics = function(tids, uid, callback) { async.waterfall([ function(next) { privileges.topics.filterTids('read', tids, uid, next); }, function(tids, next) { Topics.getTopicsByTids(tids, uid, next); } ], callback); }; Topics.getTopicsByTids = function(tids, uid, callback) { if (!Array.isArray(tids) || !tids.length) { return callback(null, []); } var uids, cids, topics; async.waterfall([ function(next) { Topics.getTopicsData(tids, next); }, function(_topics, next) { function mapFilter(array, field) { return array.map(function(topic) { return topic && topic[field] && topic[field].toString(); }).filter(function(value, index, array) { return utils.isNumber(value) && array.indexOf(value) === index; }); } topics = _topics; uids = mapFilter(topics, 'uid'); cids = mapFilter(topics, 'cid'); async.parallel({ users: function(next) { user.getUsersFields(uids, ['uid', 'username', 'fullname', 'userslug', 'reputation', 'postcount', 'picture', 'signature', 'banned', 'status'], next); }, categories: function(next) { categories.getCategoriesFields(cids, ['cid', 'name', 'slug', 'icon', 'bgColor', 'color', 'disabled'], next); }, hasRead: function(next) { Topics.hasReadTopics(tids, uid, next); }, teasers: function(next) { Topics.getTeasers(topics, next); }, tags: function(next) { Topics.getTopicsTagsObjects(tids, next); } }, next); }, function(results, next) { var users = _.object(uids, results.users); var categories = _.object(cids, results.categories); for (var i=0; i