var RDB = require('./redis.js'), utils = require('./utils.js'), marked = require('marked'), user = require('./user.js'), config = require('../config.js'); (function(Posts) { Posts.get = function(callback, tid, current_user, start, end) { if (start == null) start = 0; if (end == null) end = start + 10; var post_data, user_data, thread_data, vote_data, viewer_data; //compile thread after all data is asynchronously called function generateThread() { if (!post_data ||! user_data || !thread_data || !vote_data || !viewer_data) return; var posts = []; for (var i=0, ii= post_data.pid.length; i= config.privilege_thresholds.manage_thread ? 1 : 0, 'posts': posts }); } // get all data for thread in asynchronous fashion RDB.lrange('tid:' + tid + ':posts', start, end, function(err, pids) { RDB.handle(err); var content = [], uid = [], timestamp = [], pid = [], post_rep = []; for (var i=0, ii=pids.length; i 0) { RDB.rpush('tid:' + tid + ':posts', pid); socket.emit('event:alert', { title: 'Reply Successful', message: 'You have successfully replied. Click here to view your reply.', type: 'notify', timeout: 2000 }); user.getUserFields(uid, ['username','reputation','picture'], function(data){ var timestamp = new Date().getTime(); io.sockets.in('topic_' + tid).emit('event:new_post', { 'posts' : [ { 'pid' : pid, 'content' : marked(content || ''), 'uid' : uid, 'username' : data.username || 'anonymous', 'user_rep' : data.reputation || 0, 'post_rep' : 0, 'gravatar' : data.picture, 'timestamp' : timestamp, 'relativeTime': utils.relativeTime(timestamp), 'fav_star_class' :'icon-star-empty' } ] }); }); } else { socket.emit('event:alert', { title: 'Reply Unsuccessful', message: 'Your reply could not be posted at this time. Please try again later.', type: 'notify', timeout: 2000 }); } }); }; Posts.create = function(uid, tid, content, callback) { if (uid === null) return; RDB.get('tid:' + tid + ':locked', function(err, locked) { RDB.handle(err); if (!locked || locked === '0') { RDB.incr('global:next_post_id', function(err, pid) { RDB.handle(err); // Posts Info RDB.set('pid:' + pid + ':content', content); RDB.set('pid:' + pid + ':uid', uid); RDB.set('pid:' + pid + ':timestamp', new Date().getTime()); RDB.set('pid:' + pid + ':rep', 0); RDB.incr('tid:' + tid + ':postcount'); // User Details - move this out later RDB.lpush('uid:' + uid + ':posts', pid); user.incrementUserFieldBy(uid, 'postcount', 1); if (callback) callback(pid); }); } else { callback(-1); } }); } Posts.favourite = function(io, pid, room_id, uid) { RDB.get('pid:' + pid + ':uid', function(err, uid_of_poster) { RDB.handle(err); Posts.hasFavourited(pid, uid, function(hasFavourited) { if (hasFavourited == false) { RDB.sadd('pid:' + pid + ':users_favourited', uid); user.incrementUserFieldBy(uid_of_poster, 'reputation', 1); RDB.incr('pid:' + pid + ':rep'); if (room_id) { io.sockets.in(room_id).emit('event:rep_up', {uid: uid_of_poster, pid: pid}); } } }); }); } Posts.unfavourite = function(io, pid, room_id, uid) { RDB.get('pid:' + pid + ':uid', function(err, uid_of_poster) { RDB.handle(err); Posts.hasFavourited(pid, uid, function(hasFavourited) { if (hasFavourited == true) { RDB.srem('pid:' + pid + ':users_favourited', uid); user.incrementUserFieldBy(uid_of_poster, 'reputation', -1); RDB.decr('pid:' + pid + ':rep'); if (room_id) { io.sockets.in(room_id).emit('event:rep_down', {uid: uid_of_poster, pid: pid}); } } }); }); } Posts.hasFavourited = function(pid, uid, callback) { RDB.sismember('pid:' + pid + ':users_favourited', uid, function(err, hasFavourited) { RDB.handle(err); callback(hasFavourited); }); } Posts.getFavouritesByPostIDs = function(pids, uid, callback) { var loaded = 0; var data = {}; for (var i=0, ii=pids.length; i