diff --git a/src/posts.js b/src/posts.js index 24d3371c23..bb6ff499b7 100644 --- a/src/posts.js +++ b/src/posts.js @@ -22,6 +22,7 @@ var async = require('async'), require('./posts/summary')(Posts); require('./posts/recent')(Posts); require('./posts/flags')(Posts); + require('./posts/tools')(Posts); Posts.exists = function(pid, callback) { db.isSortedSetMember('posts:pid', pid, callback); diff --git a/src/posts/delete.js b/src/posts/delete.js index dd8abe21ac..4f3612ad1f 100644 --- a/src/posts/delete.js +++ b/src/posts/delete.js @@ -38,7 +38,7 @@ module.exports = function(Posts) { topics.updateTeaser(postData.tid, next); } ], function(err) { - callback(err, postData); + next(err, postData); }); } ], callback); diff --git a/src/postTools.js b/src/posts/tools.js similarity index 67% rename from src/postTools.js rename to src/posts/tools.js index 9458152635..fbf043b32a 100644 --- a/src/postTools.js +++ b/src/posts/tools.js @@ -2,27 +2,27 @@ var async = require('async'), - posts = require('./posts'), - privileges = require('./privileges'), - cache = require('./posts/cache'); + privileges = require('../privileges'), + cache = require('./cache'); -(function(PostTools) { +module.exports = function(Posts) { + Posts.tools = {}; - PostTools.delete = function(uid, pid, callback) { + Posts.tools.delete = function(uid, pid, callback) { togglePostDelete(uid, pid, true, callback); }; - PostTools.restore = function(uid, pid, callback) { + Posts.tools.restore = function(uid, pid, callback) { togglePostDelete(uid, pid, false, callback); }; function togglePostDelete(uid, pid, isDelete, callback) { async.waterfall([ function(next) { - posts.getPostField(pid, 'deleted', next); + Posts.getPostField(pid, 'deleted', next); }, function(deleted, next) { - if(parseInt(deleted, 10) === 1 && isDelete) { + if (parseInt(deleted, 10) === 1 && isDelete) { return next(new Error('[[error:post-already-deleted]]')); } else if(parseInt(deleted, 10) !== 1 && !isDelete) { return next(new Error('[[error:post-already-restored]]')); @@ -43,19 +43,19 @@ var async = require('async'), if (isDelete) { cache.del(pid); - posts.delete(pid, callback); + Posts.delete(pid, callback); } else { - posts.restore(pid, function(err, postData) { + Posts.restore(pid, function(err, postData) { if (err) { return callback(err); } - posts.parsePost(postData, callback); + Posts.parsePost(postData, callback); }); } }); } - PostTools.purge = function(uid, pid, callback) { + Posts.tools.purge = function(uid, pid, callback) { async.waterfall([ function (next) { privileges.posts.canPurge(pid, uid, next); @@ -65,10 +65,10 @@ var async = require('async'), return next(new Error('[[error:no-privileges]]')); } cache.del(pid); - posts.purge(pid, next); + Posts.purge(pid, next); } ], callback); }; +}; -}(exports)); diff --git a/src/socket.io/posts.js b/src/socket.io/posts.js index d6af25a71a..64b39632c9 100644 --- a/src/socket.io/posts.js +++ b/src/socket.io/posts.js @@ -11,7 +11,6 @@ var async = require('async'), meta = require('../meta'), topics = require('../topics'), favourites = require('../favourites'), - postTools = require('../postTools'), notifications = require('../notifications'), groups = require('../groups'), user = require('../user'), @@ -346,28 +345,27 @@ SocketPosts.edit = function(socket, data, callback) { }; SocketPosts.delete = function(socket, data, callback) { - deleteOrRestore('delete', socket, data, callback); + doPostAction('delete', 'event:post_deleted', socket, data, callback); }; SocketPosts.restore = function(socket, data, callback) { - deleteOrRestore('restore', socket, data, callback); + doPostAction('restore', 'event:post_restored', socket, data, callback); }; -function deleteOrRestore(command, socket, data, callback) { +function doPostAction(command, eventName, socket, data, callback) { if (!data) { return callback(new Error('[[error:invalid-data]]')); } - postTools[command](socket.uid, data.pid, function(err, postData) { + posts.tools[command](socket.uid, data.pid, function(err, postData) { if (err) { return callback(err); } - var eventName = command === 'delete' ? 'event:post_deleted' : 'event:post_restored'; websockets.in('topic_' + data.tid).emit(eventName, postData); events.log({ - type: command === 'delete' ? 'post-delete' : 'post-restore', + type: 'post-' + command, uid: socket.uid, pid: data.pid, ip: socket.ip @@ -379,7 +377,7 @@ function deleteOrRestore(command, socket, data, callback) { SocketPosts.purge = function(socket, data, callback) { function purgePost() { - postTools.purge(socket.uid, data.pid, function(err) { + posts.tools.purge(socket.uid, data.pid, function(err) { if (err) { return callback(err); } diff --git a/src/socket.io/topics.js b/src/socket.io/topics.js index 5d9fbed84f..958d9f6ddd 100644 --- a/src/socket.io/topics.js +++ b/src/socket.io/topics.js @@ -10,7 +10,6 @@ var nconf = require('nconf'), privileges = require('../privileges'), plugins = require('../plugins'), notifications = require('../notifications'), - threadTools = require('../threadTools'), websockets = require('./index'), user = require('../user'), db = require('../database'), @@ -239,12 +238,12 @@ SocketTopics.doTopicAction = function(action, event, socket, data, callback) { return callback(new Error('[[error:invalid-tid]]')); } - if (typeof threadTools[action] !== 'function') { + if (typeof topics.tools[action] !== 'function') { return callback(); } async.each(data.tids, function(tid, next) { - threadTools[action](tid, socket.uid, function(err, data) { + topics.tools[action](tid, socket.uid, function(err, data) { if (err) { return next(err); } @@ -330,7 +329,7 @@ SocketTopics.move = function(socket, data, callback) { function(_topicData, next) { topicData = _topicData; topicData.tid = tid; - threadTools.move(tid, data.cid, socket.uid, next); + topics.tools.move(tid, data.cid, socket.uid, next); } ], function(err) { if(err) { @@ -392,7 +391,7 @@ SocketTopics.moveAll = function(socket, data, callback) { } async.eachLimit(tids, 10, function(tid, next) { - threadTools.move(tid, data.cid, socket.uid, next); + topics.tools.move(tid, data.cid, socket.uid, next); }, callback); }); }); diff --git a/src/threadTools.js b/src/topics/tools.js similarity index 78% rename from src/threadTools.js rename to src/topics/tools.js index 37a2b1b721..16ab1c5ecc 100644 --- a/src/threadTools.js +++ b/src/topics/tools.js @@ -2,22 +2,23 @@ var async = require('async'), - db = require('./database'), - topics = require('./topics'), - categories = require('./categories'), - posts = require('./posts'), - plugins = require('./plugins'), - batch = require('./batch'), - privileges = require('./privileges'); + db = require('../database'), + categories = require('../categories'), + plugins = require('../plugins'), + privileges = require('../privileges'); -(function(ThreadTools) { +module.exports = function(Topics) { - ThreadTools.delete = function(tid, uid, callback) { + var topicTools = {}; + Topics.tools = topicTools; + + + topicTools.delete = function(tid, uid, callback) { toggleDelete(tid, uid, true, callback); }; - ThreadTools.restore = function(tid, uid, callback) { + topicTools.restore = function(tid, uid, callback) { toggleDelete(tid, uid, false, callback); }; @@ -31,7 +32,7 @@ var async = require('async'), if (!isOwnerOrAdminOrMod) { return next(new Error('[[error:no-privileges]]')); } - topics.getTopicFields(tid, ['tid', 'cid', 'uid', 'deleted', 'title', 'mainPid'], next); + Topics.getTopicFields(tid, ['tid', 'cid', 'uid', 'deleted', 'title', 'mainPid'], next); }, function (_topicData, next) { topicData = _topicData; @@ -42,7 +43,7 @@ var async = require('async'), return callback(new Error('[[error:topic-already-restored]]')); } - topics[isDelete ? 'delete' : 'restore'](tid, next); + Topics[isDelete ? 'delete' : 'restore'](tid, next); }, function (next) { topicData.deleted = isDelete ? 1 : 0; @@ -65,11 +66,11 @@ var async = require('async'), ], callback); } - ThreadTools.purge = function(tid, uid, callback) { + topicTools.purge = function(tid, uid, callback) { var cid; async.waterfall([ function(next) { - topics.exists(tid, next); + Topics.exists(tid, next); }, function(exists, next) { if (!exists) { @@ -82,12 +83,12 @@ var async = require('async'), return next(new Error('[[error:no-privileges]]')); } - topics.getTopicField(tid, 'cid', next); + Topics.getTopicField(tid, 'cid', next); }, function (_cid, next) { cid = _cid; - topics.purgePostsAndTopic(tid, next); + Topics.purgePostsAndTopic(tid, next); }, function (next) { next(null, {tid: tid, cid: cid, uid: uid}); @@ -95,11 +96,11 @@ var async = require('async'), ], callback); }; - ThreadTools.lock = function(tid, uid, callback) { + topicTools.lock = function(tid, uid, callback) { toggleLock(tid, uid, true, callback); }; - ThreadTools.unlock = function(tid, uid, callback) { + topicTools.unlock = function(tid, uid, callback) { toggleLock(tid, uid, false, callback); }; @@ -110,7 +111,7 @@ var async = require('async'), async.waterfall([ function (next) { - topics.getTopicField(tid, 'cid', next); + Topics.getTopicField(tid, 'cid', next); }, function (_cid, next) { cid = _cid; @@ -124,7 +125,7 @@ var async = require('async'), return next(new Error('[[error:no-privileges]]')); } - topics.setTopicField(tid, 'locked', lock ? 1 : 0, next); + Topics.setTopicField(tid, 'locked', lock ? 1 : 0, next); }, function (next) { var data = { @@ -141,11 +142,11 @@ var async = require('async'), ], callback); } - ThreadTools.pin = function(tid, uid, callback) { + topicTools.pin = function(tid, uid, callback) { togglePin(tid, uid, true, callback); }; - ThreadTools.unpin = function(tid, uid, callback) { + topicTools.unpin = function(tid, uid, callback) { togglePin(tid, uid, false, callback); }; @@ -153,13 +154,13 @@ var async = require('async'), var topicData; async.waterfall([ function (next) { - topics.exists(tid, next); + Topics.exists(tid, next); }, function (exists, next) { if (!exists) { return callback(); } - topics.getTopicFields(tid, ['cid', 'lastposttime'], next); + Topics.getTopicFields(tid, ['cid', 'lastposttime'], next); }, function (_topicData, next) { topicData = _topicData; @@ -170,7 +171,7 @@ var async = require('async'), return next(new Error('[[error:no-privileges]]')); } async.parallel([ - async.apply(topics.setTopicField, tid, 'pinned', pin ? 1 : 0), + async.apply(Topics.setTopicField, tid, 'pinned', pin ? 1 : 0), async.apply(db.sortedSetAdd, 'cid:' + topicData.cid + ':tids', pin ? Math.pow(2, 53) : topicData.lastposttime, tid) ], next); }, @@ -189,11 +190,11 @@ var async = require('async'), ], callback); } - ThreadTools.move = function(tid, cid, uid, callback) { + topicTools.move = function(tid, cid, uid, callback) { var topic; async.waterfall([ function(next) { - topics.getTopicFields(tid, ['cid', 'lastposttime', 'pinned', 'deleted', 'postcount'], next); + Topics.getTopicFields(tid, ['cid', 'lastposttime', 'pinned', 'deleted', 'postcount'], next); }, function(topicData, next) { topic = topicData; @@ -229,7 +230,7 @@ var async = require('async'), categories.incrementCategoryFieldBy(cid, 'topic_count', 1, next); }, function (next) { - topics.setTopicField(tid, 'cid', cid, next); + Topics.setTopicField(tid, 'cid', cid, next); } ], function(err) { if (err) { @@ -247,4 +248,4 @@ var async = require('async'), }; -}(exports)); +};