From 8bba811aaf3d360fd8ef44793dcfd40869e83157 Mon Sep 17 00:00:00 2001 From: barisusakli Date: Fri, 12 Dec 2014 00:45:43 -0500 Subject: [PATCH] closes #2522 --- src/topics/fork.js | 78 +++++++++++++++++++++++++-------------------- src/topics/posts.js | 4 ++- 2 files changed, 46 insertions(+), 36 deletions(-) diff --git a/src/topics/fork.js b/src/topics/fork.js index 422b53a8f1..20f0d8abce 100644 --- a/src/topics/fork.js +++ b/src/topics/fork.js @@ -4,12 +4,13 @@ var async = require('async'), winston = require('winston'), - db = require('./../database'), + db = require('../database'), - posts = require('./../posts'), + posts = require('../posts'), privileges = require('../privileges'), - postTools = require('./../postTools'), - threadTools = require('./../threadTools'); + postTools = require('../postTools'), + plugins = require('../plugins'), + threadTools = require('../threadTools'); module.exports = function(Topics) { @@ -70,41 +71,48 @@ module.exports = function(Topics) { }; Topics.movePostToTopic = function(pid, tid, callback) { - threadTools.exists(tid, function(err, exists) { - if (err || !exists) { - return callback(err || new Error('[[error:no-topic]]')); - } - - posts.getPostFields(pid, ['tid', 'timestamp', 'votes'], function(err, postData) { - if (err) { - return callback(err); + var postData; + async.waterfall([ + function(next) { + threadTools.exists(tid, next); + }, + function(exists, next) { + if (!exists) { + return next(new Error('[[error:no-topic]]')); } - - if (!postData || !postData.tid) { - return callback(new Error('[[error:no-post]]')); + posts.getPostFields(pid, ['tid', 'timestamp', 'votes'], next); + }, + function(post, next) { + if (!post || !post.tid) { + return next(new Error('[[error:no-post]]')); } + postData = post; + postData.pid = pid; - Topics.removePostFromTopic(postData.tid, pid, function(err) { - if (err) { - return callback(err); + Topics.removePostFromTopic(postData.tid, pid, next); + }, + function(next) { + async.parallel([ + function(next) { + Topics.decreasePostCount(postData.tid, next); + }, + function(next) { + Topics.increasePostCount(tid, next); + }, + function(next) { + posts.setPostField(pid, 'tid', tid, next); + }, + function(next) { + Topics.addPostToTopic(tid, pid, postData.timestamp, postData.votes, next); } - - async.parallel([ - function(next) { - Topics.decreasePostCount(postData.tid, next); - }, - function(next) { - Topics.increasePostCount(tid, next); - }, - function(next) { - posts.setPostField(pid, 'tid', tid, next); - }, - function(next) { - Topics.addPostToTopic(tid, pid, postData.timestamp, postData.votes, next); - } - ], callback); - }); - }); + ], next); + } + ], function(err) { + if (err) { + return callback(err); + } + plugins.fireHook('action:post.move', {post: postData, tid: tid}); + callback(null); }); }; }; diff --git a/src/topics/posts.js b/src/topics/posts.js index a89f368782..e119b4d14f 100644 --- a/src/topics/posts.js +++ b/src/topics/posts.js @@ -201,7 +201,9 @@ module.exports = function(Topics) { function (next) { db.sortedSetRemove('tid:' + tid + ':posts:votes', pid, next); } - ], callback); + ], function(err, results) { + callback(err); + }); }; Topics.getPids = function(tid, callback) {