var RDB = require('./redis.js'), posts = require('./posts.js'), utils = require('./utils.js'), user = require('./user.js'), config = require('../config.js'), categories = require('./categories.js'), posts = require('./posts.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, tid, current_user, start, end) { if (start == null) start = 0; if (end == null) end = -1;//start + 10; var post_data, user_data, thread_data, vote_data, privileges; getTopicPosts(); getPrivileges(); //compile thread after all data is asynchronously called function generateThread() { if (!post_data || !user_data || !thread_data || !vote_data || !privileges) return; var retrieved_posts = [], main_posts = []; for (var i=0, ii= post_data.pid.length; i= config.privilege_thresholds.manage_thread); }); } ], function(err, results) { callback({ editable: results[0].editable || (results.slice(1).indexOf(true) !== -1 ? true : false), view_deleted: results[0].view_deleted || (results.slice(1).indexOf(true) !== -1 ? true : false) }); }); } 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:' + tid + ':cid', function(err, cid) { if (cid && parseInt(cid) > 0) callback(cid); else callback(false); }); } 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('tid:' + 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('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 }); // let everyone know that there is an unread topic in this category RDB.del('cid:' + category_id + ':read_by_uid'); // 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); }); RDB.incr('cid:' + category_id + ':topiccount'); }); }; Topics.lock = function(tid, uid, socket) { Topics.privileges(tid, uid, function(privileges) { if (privileges.editable) { // 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) { Topics.privileges(tid, uid, function(privileges) { if (privileges.editable) { // Mark thread as unlocked 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) { Topics.privileges(tid, uid, function(privileges) { if (privileges.editable) { // 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) { Topics.privileges(tid, uid, function(privileges) { if (privileges.editable) { // Mark thread as restored 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) { Topics.privileges(tid, uid, function(privileges) { if (privileges.editable) { // Mark thread as pinned 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) { Topics.privileges(tid, uid, function(privileges) { if (privileges.editable) { // Mark thread as unpinned 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));