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'), async = require('async'), feed = require('./feed.js'); marked.setOptions({ breaks: true }); (function(Topics) { Topics.getTopicById = function(tid, current_user, callback) { function getTopicData(next) { RDB.multi() .get(schema.topics(tid).title) .get(schema.topics(tid).locked) .get(schema.topics(tid).category_name) .get(schema.topics(tid).category_slug) .get(schema.topics(tid).deleted) .get(schema.topics(tid).pinned) .get(schema.topics(tid).slug) .exec(function(err, replies) { next(null, { topic_name: replies[0], locked: replies[1] || 0, category_name: replies[2], category_slug: replies[3], deleted: replies[4] || 0, pinned: replies[5] || 0, slug: replies[6] }); }); } function getTopicPosts(next) { posts.getPostsByTid(tid, current_user, 0, 10, function(postData) { next(null, postData); }); } function getPrivileges(next) { threadTools.privileges(tid, current_user, function(privData) { next(null, privData); }); } async.parallel([getTopicData, getTopicPosts, getPrivileges], function(err, results) { var retrieved_posts = [], main_posts = []; var topicData = results[0], postData = results[1].postData, userData = results[1].userData, voteData = results[1].voteData, privileges = results[2]; if (!postData) { callback(false); return; } for (var i=0, ii= postData.pid.length; i 0) { RDB.sismember(schema.topics(tid).read_by_uid, uid, function(err, read) { topicData.badgeclass = read ? '' : 'badge-important'; next(); }); } else { next(); } } function get_teaser(next) { Topics.get_teaser(tid, function(teaser) { topicData.teaser_text = teaser.text; topicData.teaser_username = teaser.username; next(); }); } async.parallel([get_topic_data, get_read_status, get_teaser], function(err) { if (err) { throw new Error(err); } topicData.tid = tid; callback(topicData); }); } Topics.getAllTopics = function(callback) { RDB.smembers('topics:tid', function(err, tids) { var topics = []; async.each(tids, function(tid, next) { Topics.get_topic(tid, 0, function(topicData) { topics.push(topicData); next(); }); }, function(err) { callback(topics); }); }); } Topics.get_cid_by_tid = function(tid, callback) { RDB.get(schema.topics(tid).cid, function(err, cid) { if (cid && parseInt(cid) > 0) { callback(cid); } else { callback(false); } }); } Topics.getTitle = function(tid, callback) { RDB.get('tid:' + tid + ':title', function(err, title) { callback(title); }); } Topics.getSlug = function(tid, callback) { RDB.get('tid:' + tid + ':slug', function(err, slug) { callback(slug); }); } Topics.getTitleByPid = function(pid, callback) { RDB.get('pid:' + pid + ':tid', function(err, tid) { if (!err) { Topics.getTitle(tid, function(title) { callback(title); }); } else callback('Could not grab title'); }); } Topics.markAsRead = function(tid, uid) { // there is an issue with this fn. if you read a topic that is previously read you will mark the category as read anyways - there is no check RDB.sadd(schema.topics(tid).read_by_uid, uid); Topics.get_cid_by_tid(tid, function(cid) { RDB.sadd('cid:' + cid + ':read_by_uid', uid); }); } Topics.hasReadTopics = function(tids, uid, callback) { var batch = RDB.multi(); for (var i=0, ii=tids.length; i 0) { RDB.lpush(schema.topics(tid).posts, pid); // Auto-subscribe the post creator to the newly created topic threadTools.toggleFollow(tid, uid); // Notify any users looking at the category that a new topic has arrived Topics.get_topic(tid, uid, function(topicData) { io.sockets.in('category_' + category_id).emit('event:new_topic', topicData); }); } }); Topics.markAsRead(tid, uid); // User Details - move this out later RDB.lpush('uid:' + uid + ':topics', tid); socket.emit('event:alert', { title: 'Thank you for posting', message: 'You have successfully posted. Click here to view your post.', type: 'notify', timeout: 2000 }); // let everyone know that there is an unread topic in this category RDB.del('cid:' + category_id + ':read_by_uid'); RDB.zadd(schema.topics().recent, Date.now(), tid); //RDB.zadd('topics:active', tid); // in future it may be possible to add topics to several categories, so leaving the door open here. RDB.sadd('categories:' + category_id + ':tid', tid); RDB.set(schema.topics(tid).cid, category_id); categories.getCategories([category_id], function(data) { RDB.set(schema.topics(tid).category_name, data.categories[0].name); RDB.set(schema.topics(tid).category_slug, data.categories[0].slug); }); RDB.incr('cid:' + category_id + ':topiccount'); feed.updateCategory(category_id); }); }); }; }(exports));