var RDB = require('./redis.js'), posts = require('./posts.js'), utils = require('./utils.js'), user = require('./user.js'), configs = require('../config.js'), categories = require('./categories.js'), marked = require('marked'), async = require('async'); marked.setOptions({ breaks: true }); (function(Topics) { Topics.get_by_category = function(callback, category, start, end) { } Topics.get = function(callback, category_id, current_user, start, end) { if (start == null) start = 0; if (end == null) end = start + 10; //build a proper wrapper for this and move it into above function later var range_var = (category_id) ? 'categories:' + category_id + ':tid' : 'topics:tid'; RDB.smembers(range_var, function(err, tids) { var title = [], uid = [], timestamp = [], slug = [], postcount = [], locked = [], deleted = [], pinned = []; for (var i=0, ii=tids.length; i 0) { multi .mget(title) .mget(uid) .mget(timestamp) .mget(slug) .mget(postcount) .mget(locked) .mget(deleted) .mget(pinned) } multi.exec(function(err, replies) { category_name = replies[0]; if(category_id && category_name === null) { callback(false); return; } active_usernames = replies[1]; var topics = []; title = replies[2]; uid = replies[3]; timestamp = replies[4]; slug = replies[5]; postcount = replies[6]; locked = replies[7]; deleted = replies[8]; pinned = replies[9]; var usernames, has_read, moderators, teaser_info; function generate_topic() { if (!usernames || !has_read || !moderators || !teaser_info) return; if (tids.length > 0) { for (var i=0, ii=title.length; i 0 ? '' : 'none', 'moderators': moderators }); } user.get_usernames_by_uids(uid, function(userNames) { usernames = userNames; generate_topic(); }); Topics.hasReadTopics(tids, current_user, function(hasRead) { has_read = hasRead; generate_topic(); }); categories.getModerators(category_id, function(mods) { moderators = mods; generate_topic(); }); Topics.get_teasers(tids, function(teasers) { teaser_info = teasers; generate_topic(); }); // else { // callback({ // 'category_name' : category_id ? category_name : 'Recent', // 'show_topic_button' : category_id ? 'show' : 'hidden', // 'category_id': category_id || 0, // 'topics': [] // }); // } }); }); } Topics.get_topic = function(tid, uid, callback) { var topicData = {}; async.parallel([ function(next) { RDB.mget([ 'tid:' + tid + ':title', 'tid:' + tid + ':uid', 'tid:' + tid + ':timestamp', 'tid:' + tid + ':slug', 'tid:' + tid + ':postcount', 'tid:' + tid + ':locked', 'tid:' + tid + ':pinned', 'tid:' + tid + ':deleted' ], function(err, topic) { topicData.title = topic[0]; topicData.uid = topic[1]; topicData.timestamp = topic[2]; topicData.relativeTime = utils.relativeTime(topic[2]), topicData.slug = topic[3]; topicData.post_count = topic[4]; topicData.locked = topic[5]; topicData.pinned = topic[6]; topicData.deleted = topic[7]; user.getUserField(topic[1], 'username', function(username) { topicData.username = username; next(null); }) }); }, function(next) { if (uid && parseInt(uid) > 0) { RDB.sismember('tid:' + tid + ':read_by_uid', uid, function(err, read) { topicData.badgeclass = read ? '' : 'badge-important'; next(null); }); } else next(null); }, function(next) { Topics.get_teaser(tid, function(teaser) { topicData.teaser_text = teaser.text; topicData.teaser_username = teaser.username; next(null); }); } ], function(err) { if (!err) { callback(topicData); } }); } Topics.get_cid_by_tid = function(tid, callback) { RDB.get('tid:' + pid + ':cid', function(err, cid) { if (cid && parseInt(cid) > 0) callback(cid); else callback(false); }); } Topics.markAsRead = function(tid, uid) { RDB.sadd('tid:' + tid + ':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('tid:' + tid + ':posts', pid); // Notify any users looking at the category that a new post 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 }); // 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('tid:' + tid + ':cid', category_id); categories.get_category([category_id], function(data) { RDB.set('tid:' + tid + ':category_name', data.categories[0].name); RDB.set('tid:' + tid + ':category_slug', data.categories[0].slug); }); }); }; Topics.lock = function(tid, uid, socket) { user.getUserField(uid, 'reputation', function(rep) { if (rep >= configs.privilege_thresholds.manage_thread) { // Mark thread as locked RDB.set('tid:' + tid + ':locked', 1); if (socket) { io.sockets.in('topic_' + tid).emit('event:topic_locked', { tid: tid, status: 'ok' }); } } }); } Topics.unlock = function(tid, uid, socket) { user.getUserField(uid, 'reputation', function(rep) { if (rep >= configs.privilege_thresholds.manage_thread) { // Mark thread as locked RDB.del('tid:' + tid + ':locked'); if (socket) { io.sockets.in('topic_' + tid).emit('event:topic_unlocked', { tid: tid, status: 'ok' }); } } }); } Topics.delete = function(tid, uid, socket) { user.getUserField(uid, 'reputation', function(rep) { if (rep >= configs.privilege_thresholds.manage_thread) { // Mark thread as deleted RDB.set('tid:' + tid + ':deleted', 1); Topics.lock(tid, uid); if (socket) { io.sockets.in('topic_' + tid).emit('event:topic_deleted', { tid: tid, status: 'ok' }); } } }); } Topics.restore = function(tid, uid, socket) { user.getUserField(uid, 'reputation', function(rep) { if (rep >= configs.privilege_thresholds.manage_thread) { // Mark thread as deleted RDB.del('tid:' + tid + ':deleted'); Topics.unlock(tid, uid); if (socket) { io.sockets.in('topic_' + tid).emit('event:topic_restored', { tid: tid, status: 'ok' }); } } }); } Topics.pin = function(tid, uid, socket) { user.getUserField(uid, 'reputation', function(rep) { if (rep >= configs.privilege_thresholds.manage_thread) { // Mark thread as deleted RDB.set('tid:' + tid + ':pinned', 1); if (socket) { io.sockets.in('topic_' + tid).emit('event:topic_pinned', { tid: tid, status: 'ok' }); } } }); } Topics.unpin = function(tid, uid, socket) { user.getUserField(uid, 'reputation', function(rep) { if (rep >= configs.privilege_thresholds.manage_thread) { // Mark thread as deleted RDB.del('tid:' + tid + ':pinned'); if (socket) { io.sockets.in('topic_' + tid).emit('event:topic_unpinned', { tid: tid, status: 'ok' }); } } }); } Topics.move = function(tid, cid, socket) { RDB.get('tid:' + tid + ':cid', function(err, oldCid) { RDB.handle(err); RDB.smove('categories:' + oldCid + ':tid', 'categories:' + cid + ':tid', tid, function(err, result) { if (!err && result === 1) { RDB.set('tid:' + tid + ':cid', cid); categories.get_category([cid], function(data) { RDB.set('tid:' + tid + ':category_name', data.categories[0].name); RDB.set('tid:' + tid + ':category_slug', data.categories[0].slug); }); socket.emit('api:topic.move', { status: 'ok' }); io.sockets.in('topic_' + tid).emit('event:topic_moved', { tid: tid }); } else { socket.emit('api:topic.move', { status: 'error' }); } }); }); } }(exports));