Node.js based forum software built for the modern web
begin refactor of posts/topics + pagination + cleanup + async
psychobunnya9672ab9d837
diff --git a/src/categories.js b/src/categories.jsindex ec1e2a0ea1..7ec41b2292 100644--- a/src/categories.js+++ b/src/categories.js@@ -61,7 +61,6 @@ var RDB = require('./redis.js'),if (start == null) start = 0;if (end == null) end = start + 10;- //build a proper wrapper for this and move it into above function latervar range_var = (category_id) ? 'categories:' + category_id + ':tid' : 'topics:tid';RDB.smembers(range_var, function(err, tids) {diff --git a/src/postTools.js b/src/postTools.jsnew file mode 100644index 0000000000..cc8d703bdf--- /dev/null+++ b/src/postTools.js@@ -0,0 +1,92 @@+var RDB = require('./redis.js'),+ posts = require('./posts.js'),+ threadTools = require('./threadTools.js'),+ user = require('./user.js'),+ config = require('../config.js'),+ async = require('async'),+ marked = require('marked');++marked.setOptions({+ breaks: true+});++(function(PostTools) {++ PostTools.privileges = function(pid, uid, callback) {+ //todo: break early if one condition is true++ function getThreadPrivileges(next) {+ posts.get_tid_by_pid(pid, function(tid) {+ threadTools.privileges(tid, uid, function(privileges) {+ next(null, privileges);+ });+ });+ }++ function isOwnPost(next) {+ RDB.get('pid:' + pid + ':uid', function(err, author) {+ if (author && parseInt(author) > 0) next(null, author === uid);+ });+ }++ function hasEnoughRep(next) {+ // DRY fail in threadTools.++ user.getUserField(uid, 'reputation', function(reputation) {+ next(null, reputation >= config.privilege_thresholds.manage_content);+ });+ }++ async.parallel([getThreadPrivileges, isOwnPost, hasEnoughRep], 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)+ });+ });+ }++ PostTools.edit = function(uid, pid, content) {+ var success = function() {+ RDB.set('pid:' + pid + ':content', content);+ RDB.set('pid:' + pid + ':edited', new Date().getTime());+ RDB.set('pid:' + pid + ':editor', uid);++ posts.get_tid_by_pid(pid, function(tid) {+ io.sockets.in('topic_' + tid).emit('event:post_edited', { pid: pid, content: marked(content || '') });+ });+ };++ PostTools.privileges(pid, uid, function(privileges) {+ if (privileges.editable) success();+ });+ }++ PostTools.delete = function(uid, pid) {+ var success = function() {+ RDB.set('pid:' + pid + ':deleted', 1);++ posts.get_tid_by_pid(pid, function(tid) {+ io.sockets.in('topic_' + tid).emit('event:post_deleted', { pid: pid });+ });+ };++ PostTools.privileges(pid, uid, function(privileges) {+ if (privileges.editable) success();+ });+ }++ PostTools.restore = function(uid, pid) {+ var success = function() {+ RDB.del('pid:' + pid + ':deleted');++ posts.get_tid_by_pid(pid, function(tid) {+ io.sockets.in('topic_' + tid).emit('event:post_restored', { pid: pid });+ });+ };++ PostTools.privileges(pid, uid, function(privileges) {+ if (privileges.editable) success();+ });+ }++}(exports));\ No newline at end of filediff --git a/src/posts.js b/src/posts.jsindex 9f2a9e7694..38d269ccc5 100644--- a/src/posts.js+++ b/src/posts.js@@ -4,6 +4,7 @@ var RDB = require('./redis.js'),user = require('./user.js'),topics = require('./topics.js'),config = require('../config.js'),+ threadTools = require('./threadTools.js'),async = require('async');marked.setOptions({@@ -12,49 +13,108 @@ marked.setOptions({(function(Posts) {- Posts.get = function(callback, pid, current_user) {- // Not used... although Topics.get could be refactored to call Posts.get for every post- }+ Posts.get = function(callback, tid, current_user, start, end) {+ if (start == null) start = 0;+ if (end == null) end = start + 10;++ RDB.lrange('tid:' + tid + ':posts', start, end, function(err, pids) {+ RDB.handle(err);++ if (pids.length === 0 ) {+ callback(false);+ return;+ }++ topics.markAsRead(tid, current_user);++ var content = [], uid = [], timestamp = [], pid = [], post_rep = [], editor = [], editTime = [], deleted = [];++ for (var i=0, ii=pids.length; i<ii; i++) {+ content.push('pid:' + pids[i] + ':content');+ uid.push('pid:' + pids[i] + ':uid');+ timestamp.push('pid:' + pids[i] + ':timestamp');+ post_rep.push('pid:' + pids[i] + ':rep');+ editor.push('pid:' + pids[i] + ':editor');+ editTime.push('pid:' + pids[i] + ':edited');+ deleted.push('pid:' + pids[i] + ':deleted');+ pid.push(pids[i]);+ }+++ function getFavouritesData(next) {+ Posts.getFavouritesByPostIDs(pids, current_user, function(fav_data) {+ next(null, fav_data);+ }); // to be moved+ }- Posts.privileges = function(pid, uid, callback) {- async.parallel([- function(next) {- Posts.get_tid_by_pid(pid, function(tid) {- topics.privileges(tid, uid, function(privileges) {- next(null, privileges);+ function getPostData(next) {+ RDB.multi()+ .mget(content)+ .mget(uid)+ .mget(timestamp)+ .mget(post_rep)+ .mget(editor)+ .mget(editTime)+ .mget(deleted)+ .exec(function(err, replies) {+ post_data = {+ pid: pids,+ content: replies[0],+ uid: replies[1],+ timestamp: replies[2],+ reputation: replies[3],+ editor: replies[4],+ editTime: replies[5],+ deleted: replies[6]+ };++ // below, to be deprecated+ // Add any editors to the user_data object+ for(var x = 0, numPosts = post_data.editor.length; x < numPosts; x++) {+ if (post_data.editor[x] !== null && post_data.uid.indexOf(post_data.editor[x]) === -1) {+ post_data.uid.push(post_data.editor[x]);+ }+ }++ user.getMultipleUserFields(post_data.uid, ['username','reputation','picture', 'signature'], function(user_details) {+ next(null, {+ users: user_details,+ posts: post_data+ });+ });+ // above, to be deprecated});- });- },- function(next) {- RDB.get('pid:' + pid + ':uid', function(err, author) {- if (author && parseInt(author) > 0) next(null, author === uid);- });- },- function(next) {- user.getUserField(uid, 'reputation', function(reputation) {- next(null, reputation >= config.privilege_thresholds.manage_content);- });}- ], 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)++ async.parallel([getFavouritesData, getPostData], function(err, results) {+ callback({+ 'voteData' : results[0], // to be moved+ 'userData' : results[1].users, // to be moved+ 'postData' : results[1].posts+ });});+});}Posts.get_tid_by_pid = function(pid, callback) {RDB.get('pid:' + pid + ':tid', function(err, tid) {- if (tid && parseInt(tid) > 0) callback(tid);- else callback(false);+ if (tid && parseInt(tid) > 0) {+ callback(tid);+ } else {+ callback(false);+ }});}Posts.get_cid_by_pid = function(pid, callback) {Posts.get_tid_by_pid(pid, function(tid) {if (tid) topics.get_cid_by_tid(tid, function(cid) {- if (cid) callback(cid);- else callback(false);+ if (cid) {+ callback(cid);+ } else {+ callback(false);+ }});})}@@ -81,6 +141,7 @@ marked.setOptions({// Re-add the poster, so he/she does not get an "unread" flag on this topictopics.markAsRead(tid, uid);+ // this will duplicate once we enter the thread, which is where we should be goingsocket.emit('event:alert', {title: 'Reply Successful',@@ -89,7 +150,7 @@ marked.setOptions({timeout: 2000});- user.getUserFields(uid, ['username','reputation','picture','signature'], function(data){+ user.getUserFields(uid, ['username','reputation','picture','signature'], function(data) {var timestamp = new Date().getTime();@@ -144,7 +205,7 @@ marked.setOptions({RDB.incr('tid:' + tid + ':postcount');- user.getUserFields(uid, ['username'], function(data){+ user.getUserFields(uid, ['username'], function(data) {//add active users to this categoryRDB.get('tid:' + tid + ':cid', function(err, cid) {RDB.handle(err);@@ -275,48 +336,4 @@ marked.setOptions({socket.emit('api:posts.getRawPost', { post: raw });});}-- Posts.edit = function(uid, pid, content) {- var success = function() {- RDB.set('pid:' + pid + ':content', content);- RDB.set('pid:' + pid + ':edited', new Date().getTime());- RDB.set('pid:' + pid + ':editor', uid);-- Posts.get_tid_by_pid(pid, function(tid) {- io.sockets.in('topic_' + tid).emit('event:post_edited', { pid: pid, content: marked(content || '') });- });- };-- Posts.privileges(pid, uid, function(privileges) {- if (privileges.editable) success();- });- }-- Posts.delete = function(uid, pid) {- var success = function() {- RDB.set('pid:' + pid + ':deleted', 1);-- Posts.get_tid_by_pid(pid, function(tid) {- io.sockets.in('topic_' + tid).emit('event:post_deleted', { pid: pid });- });- };-- Posts.privileges(pid, uid, function(privileges) {- if (privileges.editable) success();- });- }-- Posts.restore = function(uid, pid) {- var success = function() {- RDB.del('pid:' + pid + ':deleted');-- Posts.get_tid_by_pid(pid, function(tid) {- io.sockets.in('topic_' + tid).emit('event:post_restored', { pid: pid });- });- };-- Posts.privileges(pid, uid, function(privileges) {- if (privileges.editable) success();- });- }}(exports));\ No newline at end of filediff --git a/src/threadTools.js b/src/threadTools.jsnew file mode 100644index 0000000000..06850a2b05--- /dev/null+++ b/src/threadTools.js@@ -0,0 +1,157 @@+var RDB = require('./redis.js'),+ topics = require('./topics.js'),+ categories = require('./categories.js'),+ user = require('./user.js'),+ config = require('../config.js'),+ async = require('async');+++(function(ThreadTools) {++ ThreadTools.privileges = function(tid, uid, callback) {+ //todo: break early if one condition is true++ function getCategoryPrivileges(next) {+ topics.get_cid_by_tid(tid, function(cid) {+ categories.privileges(cid, uid, function(privileges) {+ next(null, privileges);+ });+ });+ }++ function hasEnoughRep(next) {+ // DRY fail in postTools++ user.getUserField(uid, 'reputation', function(reputation) {+ next(null, reputation >= config.privilege_thresholds.manage_thread);+ });+ }+++ async.parallel([getCategoryPrivileges, hasEnoughRep], 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)+ });+ });+ }++ ThreadTools.lock = function(tid, uid, socket) {+ ThreadTools.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'+ });+ }+ }+ });+ }++ ThreadTools.unlock = function(tid, uid, socket) {+ ThreadTools.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'+ });+ }+ }+ });+ }++ ThreadTools.delete = function(tid, uid, socket) {+ ThreadTools.privileges(tid, uid, function(privileges) {+ if (privileges.editable) {+ // Mark thread as deleted+ RDB.set('tid:' + tid + ':deleted', 1);+ ThreadTools.lock(tid, uid);++ if (socket) {+ io.sockets.in('topic_' + tid).emit('event:topic_deleted', {+ tid: tid,+ status: 'ok'+ });+ }+ }+ });+ }++ ThreadTools.restore = function(tid, uid, socket) {+ ThreadTools.privileges(tid, uid, function(privileges) {+ if (privileges.editable) {+ // Mark thread as restored+ RDB.del('tid:' + tid + ':deleted');+ ThreadTools.unlock(tid, uid);++ if (socket) {+ io.sockets.in('topic_' + tid).emit('event:topic_restored', {+ tid: tid,+ status: 'ok'+ });+ }+ }+ });+ }++ ThreadTools.pin = function(tid, uid, socket) {+ ThreadTools.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'+ });+ }+ }+ });+ }++ ThreadTools.unpin = function(tid, uid, socket) {+ ThreadTools.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'+ });+ }+ }+ });+ }++ ThreadTools.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));\ No newline at end of filediff --git a/src/topics.js b/src/topics.jsindex a5b8389ba8..a139797869 100644--- a/src/topics.js+++ b/src/topics.js@@ -6,6 +6,7 @@ var RDB = require('./redis.js'),categories = require('./categories.js'),posts = require('./posts.js'),marked = require('marked'),+ threadTools = require('./threadTools.js'),async = require('async');marked.setOptions({@@ -13,240 +14,175 @@ marked.setOptions({});(function(Topics) {+ Topics.get = function(callback, tid, current_user) {++ function getTopicData(next) {+ RDB.multi()+ .get('tid:' + tid + ':title')+ .get('tid:' + tid + ':locked')+ .get('tid:' + tid + ':category_name')+ .get('tid:' + tid + ':category_slug')+ .get('tid:' + tid + ':deleted')+ .get('tid:' + tid + ':pinned')+ .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+ });+ });+ }- 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;+ function getTopicPosts(next) {+ posts.get(function(postData) {+ next(null, postData);+ }, tid, current_user, 0, 10);+ }- getTopicPosts();+ function getPrivileges(next) {+ threadTools.privileges(tid, current_user, function(privData) {+ next(null, privData);+ });+ }- getPrivileges();-+ async.parallel([getTopicData, getTopicPosts, getPrivileges], function(err, results) {+ var retrieved_posts = [],+ main_posts = [];- //compile thread after all data is asynchronously called- function generateThread() {- if (!post_data || !user_data || !thread_data || !vote_data || !privileges) return;+ var topicData = results[0],+ postData = results[1].postData,+ userData = results[1].userData,+ voteData = results[1].voteData,+ privileges = results[2];- var retrieved_posts = [],- main_posts = [];+ for (var i=0, ii= postData.pid.length; i<ii; i++) {+ var uid = postData.uid[i],+ pid = postData.pid[i];+- for (var i=0, ii= post_data.pid.length; i<ii; i++) {- var uid = post_data.uid[i],- pid = post_data.pid[i];-- if (post_data.deleted[i] === null || (post_data.deleted[i] === '1' && privileges.view_deleted) || current_user === uid) {+ // ############ to be moved into posts.get ############+ if (postData.deleted[i] === null || (postData.deleted[i] === '1' && privileges.view_deleted) || current_user === uid) {var post_obj = {'pid' : pid,'uid' : uid,- 'content' : marked(post_data.content[i] || ''),- 'post_rep' : post_data.reputation[i] || 0,- 'timestamp' : post_data.timestamp[i],- 'relativeTime': utils.relativeTime(post_data.timestamp[i]),- 'username' : user_data[uid].username || 'anonymous',- 'user_rep' : user_data[uid].reputation || 0,- 'gravatar' : user_data[uid].picture || 'http://www.gravatar.com/avatar/d41d8cd98f00b204e9800998ecf8427e',- 'signature' : marked(user_data[uid].signature || ''),- 'fav_star_class' : vote_data[pid] ? 'icon-star' : 'icon-star-empty',+ 'content' : marked(postData.content[i] || ''),+ 'post_rep' : postData.reputation[i] || 0,+ 'timestamp' : postData.timestamp[i],+ 'relativeTime': utils.relativeTime(postData.timestamp[i]),+ 'username' : userData[uid].username || 'anonymous',+ 'user_rep' : userData[uid].reputation || 0,+ 'gravatar' : userData[uid].picture || 'http://www.gravatar.com/avatar/d41d8cd98f00b204e9800998ecf8427e',+ 'signature' : marked(userData[uid].signature || ''),+ 'fav_star_class' : voteData[pid] ? 'icon-star' : 'icon-star-empty','display_moderator_tools': (uid == current_user || privileges.editable) ? 'show' : 'none',- 'edited-class': post_data.editor[i] !== null ? '' : 'none',- 'editor': post_data.editor[i] !== null ? user_data[post_data.editor[i]].username : '',- 'relativeEditTime': post_data.editTime !== null ? utils.relativeTime(post_data.editTime[i]) : '',- 'deleted': post_data.deleted[i] || '0'+ 'edited-class': postData.editor[i] !== null ? '' : 'none',+ 'editor': postData.editor[i] !== null ? userData[postData.editor[i]].username : '',+ 'relativeEditTime': postData.editTime !== null ? utils.relativeTime(postData.editTime[i]) : '',+ 'deleted': postData.deleted[i] || '0'};- if (i == 0) main_posts.push(post_obj);- else retrieved_posts.push(post_obj);+ if (i == 0) {+ main_posts.push(post_obj);+ } else {+ retrieved_posts.push(post_obj);+ }}+ // ########## end to be moved into posts.get ############}callback({- 'topic_name':thread_data.topic_name,- 'category_name':thread_data.category_name,- 'category_slug':thread_data.category_slug,- 'locked': parseInt(thread_data.locked) || 0,- 'deleted': parseInt(thread_data.deleted) || 0,- 'pinned': parseInt(thread_data.pinned) || 0,+ 'topic_name':topicData.topic_name,+ 'category_name':topicData.category_name,+ 'category_slug':topicData.category_slug,+ 'locked': parseInt(topicData.locked) || 0,+ 'deleted': parseInt(topicData.deleted) || 0,+ 'pinned': parseInt(topicData.pinned) || 0,'topic_id': tid,'expose_tools': privileges.editable ? 1 : 0,'posts': retrieved_posts,'main_posts': main_posts});- }- function getTopicPosts() {- // get all data for thread in asynchronous fashion- RDB.lrange('tid:' + tid + ':posts', start, end, function(err, pids) {- RDB.handle(err);-- if(pids.length === 0 ){- callback(false);- return;+ });+ }++ Topics.get_topic = function(tid, uid, callback) {+ var topicData = {};++ function get_topic_data(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) {+ if (err) {+ throw new Error(err);}- Topics.markAsRead(tid, current_user);-- var content = [], uid = [], timestamp = [], pid = [], post_rep = [], editor = [], editTime = [], deleted = [];-- for (var i=0, ii=pids.length; i<ii; i++) {- content.push('pid:' + pids[i] + ':content');- uid.push('pid:' + pids[i] + ':uid');- timestamp.push('pid:' + pids[i] + ':timestamp');- post_rep.push('pid:' + pids[i] + ':rep');- editor.push('pid:' + pids[i] + ':editor');- editTime.push('pid:' + pids[i] + ':edited');- deleted.push('pid:' + pids[i] + ':deleted');- pid.push(pids[i]);- }-- posts.getFavouritesByPostIDs(pids, current_user, function(fav_data) {- vote_data = fav_data;- generateThread();+ 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];+ topiscData.deleted = topic[7];++ user.getUserField(topic[1], 'username', function(username) {+ topicData.username = username;++ next();});-- RDB.multi()- .mget(content)- .mget(uid)- .mget(timestamp)- .mget(post_rep)- .get('tid:' + tid + ':title')- .get('tid:' + tid + ':locked')- .get('tid:' + tid + ':category_name')- .get('tid:' + tid + ':category_slug')- .get('tid:' + tid + ':deleted')- .get('tid:' + tid + ':pinned')- .mget(editor)- .mget(editTime)- .mget(deleted)- .exec(function(err, replies) {- post_data = {- pid: pids,- content: replies[0],- uid: replies[1],- timestamp: replies[2],- reputation: replies[3],- editor: replies[10],- editTime: replies[11],- deleted: replies[12]- };-- thread_data = {- topic_name: replies[4],- locked: replies[5] || 0,- category_name: replies[6],- category_slug: replies[7],- deleted: replies[8] || 0,- pinned: replies[9] || 0- };-- // Add any editors to the user_data object- for(var x=0,numPosts=replies[10].length;x<numPosts;x++) {- if (replies[10][x] !== null && post_data.uid.indexOf(replies[10][x]) === -1) {- post_data.uid.push(replies[10][x]);- }- }-- user.getMultipleUserFields(post_data.uid, ['username','reputation','picture', 'signature'], function(user_details){- user_data = user_details;- generateThread();- });- });});}- function getPrivileges() {- Topics.privileges(tid, current_user, function(user_privs) {- privileges = user_privs;- generateThread();- });- }- }+ function get_read_status(next) {+ // posts.create calls this function - should be an option to skip this because its always true+ if (uid && parseInt(uid) > 0) {+ RDB.sismember('tid:' + tid + ':read_by_uid', uid, function(err, read) {+ topicData.badgeclass = read ? '' : 'badge-important';- Topics.privileges = function(tid, uid, callback) {- async.parallel([- function(next) {- Topics.get_cid_by_tid(tid, function(cid) {- categories.privileges(cid, uid, function(privileges) {- next(null, privileges);- });- });- },- function(next) {- user.getUserField(uid, 'reputation', function(reputation) {- next(null, reputation >= config.privilege_thresholds.manage_thread);+ next();});+ } else {+ next();}- ], 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 = {};+ function get_teaser(next) {+ Topics.get_teaser(tid, function(teaser) {+ topicData.teaser_text = teaser.text;+ topicData.teaser_username = teaser.username;- 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);+ next();+ });+ }++ async.parallel([get_topic_data, get_read_status, get_teaser], function(err) {+ if (err) {+ throw new Error(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);+ if (cid && parseInt(cid) > 0) {+ callback(cid);+ } else {+ callback(false);+ }});}@@ -274,18 +210,20 @@ marked.setOptions({var requests = [];if (Array.isArray(tids)) {for(x=0,numTids=tids.length;x<numTids;x++) {- (function(x) {+ (function(tid) {requests.push(function(next) {- Topics.get_teaser(tids[x], function(teaser_info) {+ Topics.get_teaser(tid, function(teaser_info) {next(null, teaser_info);});});- })(x);+ })(tids[x]);}async.parallel(requests, function(err, teasers) {callback(teasers);});- } else callback([]);+ } else {+ callback([]);+ }}Topics.get_latest_undeleted_pid = function(tid, callback) {@@ -409,121 +347,4 @@ marked.setOptions({});};- 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));\ No newline at end of filediff --git a/src/websockets.js b/src/websockets.jsindex 502bea880a..f84ce5207d 100644--- a/src/websockets.js+++ b/src/websockets.js@@ -6,6 +6,8 @@ var SocketIO = require('socket.io').listen(global.server,{log:false}),user = require('./user.js'),posts = require('./posts.js'),topics = require('./topics.js'),+ threadTools = require('./threadTools.js'),+ postTools = require('./postTools.js'),categories = require('./categories.js');(function(io) {@@ -185,27 +187,31 @@ var SocketIO = require('socket.io').listen(global.server,{log:false}),});socket.on('api:topic.delete', function(data) {- topics.delete(data.tid, uid, socket);+ threadTools.delete(data.tid, uid, socket);});socket.on('api:topic.restore', function(data) {- topics.restore(data.tid, uid, socket);+ threadTools.restore(data.tid, uid, socket);});socket.on('api:topic.lock', function(data) {- topics.lock(data.tid, uid, socket);+ threadTools.lock(data.tid, uid, socket);});socket.on('api:topic.unlock', function(data) {- topics.unlock(data.tid, uid, socket);+ threadTools.unlock(data.tid, uid, socket);});socket.on('api:topic.pin', function(data) {- topics.pin(data.tid, uid, socket);+ threadTools.pin(data.tid, uid, socket);});socket.on('api:topic.unpin', function(data) {- topics.unpin(data.tid, uid, socket);+ threadTools.unpin(data.tid, uid, socket);+ });++ socket.on('api:topic.move', function(data) {+ threadTools.move(data.tid, data.cid, socket);});socket.on('api:categories.get', function() {@@ -214,24 +220,20 @@ var SocketIO = require('socket.io').listen(global.server,{log:false}),});});- socket.on('api:topic.move', function(data) {- topics.move(data.tid, data.cid, socket);- });-socket.on('api:posts.getRawPost', function(data) {posts.getRawContent(data.pid, socket);});socket.on('api:posts.edit', function(data) {- posts.edit(uid, data.pid, data.content);+ postTools.edit(uid, data.pid, data.content);});socket.on('api:posts.delete', function(data) {- posts.delete(uid, data.pid);+ postTools.delete(uid, data.pid);});socket.on('api:posts.restore', function(data) {- posts.restore(uid, data.pid);+ postTools.restore(uid, data.pid);});});