var RDB = require('./redis.js') schema = require('./schema.js'), posts = require('./posts.js'), utils = require('./../public/src/utils.js'), user = require('./user.js'), categories = require('./categories.js'), posts = require('./posts.js'), marked = require('marked'), threadTools = require('./threadTools.js'), postTools = require('./postTools'), async = require('async'), feed = require('./feed.js'), favourites = require('./favourites.js'); marked.setOptions({ breaks: true }); (function(Topics) { Topics.minimumTitleLength = 3; Topics.getTopicData = function(tid, callback) { RDB.hgetall('topic:' + tid, function(err, data) { if(err === null) callback(data); else console.log(err); }); } Topics.getTopicDataWithUsername = function(tid, callback) { Topics.getTopicData(tid, function(topic) { user.getUserField(topic.uid, 'username', function(username) { topic.username = username; callback(topic); }); }); } Topics.getTopicPosts = function(tid, start, end, current_user, callback) { posts.getPostsByTid(tid, start, end, function(postData) { function getFavouritesData(next) { var pids = []; for(var i=0; i 0) { Topics.hasReadTopic(tid, uid, function(read) { next(null, read); }); } else { next(null, null); } } function getTeaser(next) { Topics.getTeaser(tid, function(err, teaser) { if (err) teaser = {}; next(null, teaser); }); } async.parallel([getTopicData, getReadStatus, getTeaser], function(err, results) { if (err) { throw new Error(err); } var topicData = results[0], hasRead = results[1], teaser = results[2]; topicData.relativeTime = utils.relativeTime(topicData.timestamp); topicData.badgeclass = hasRead ? '' : 'badge-important'; topicData.teaser_text = teaser.text || ''; topicData.teaser_username = teaser.username || ''; topicData.teaser_timestamp = teaser.timestamp ? utils.relativeTime(teaser.timestamp) : ''; topicData.teaser_userpicture = teaser.picture; callback(topicData); }); } Topics.getAllTopics = function(limit, after, callback) { RDB.smembers('topics:tid', function(err, tids) { var topics = [], numTids, x; // Sort into ascending order tids.sort(function(a, b) { return a - b; }); // Eliminate everything after the "after" tid if (after) { for(x=0,numTids=tids.length;x= after) { tids = tids.slice(0, x); break; } } } if (limit) { if (limit > 0 && limit < tids.length) { tids = tids.slice(tids.length - limit); } } // Sort into descending order tids.sort(function(a, b) { return b - a; }); async.each(tids, function(tid, next) { Topics.getTopicDataWithUsername(tid, function(topicData) { topics.push(topicData); next(); }); }, function(err) { callback(topics); }); }); } Topics.markAllRead = function(uid) { RDB.smembers('topics:tid', function(err, tids) { if(err) { console.log(err); return; } if(tids && tids.length) { for(var i=0; i