Files
NodeBB/src/topics/fork.js

130 lines
2.8 KiB
JavaScript
Raw Normal View History

2014-03-21 15:47:46 -04:00
'use strict';
var async = require('async'),
winston = require('winston'),
2014-12-12 00:45:43 -05:00
db = require('../database'),
2014-03-21 15:47:46 -04:00
2014-12-12 00:45:43 -05:00
posts = require('../posts'),
privileges = require('../privileges'),
2014-12-12 00:45:43 -05:00
postTools = require('../postTools'),
plugins = require('../plugins'),
threadTools = require('../threadTools');
2014-03-21 15:47:46 -04:00
module.exports = function(Topics) {
Topics.createTopicFromPosts = function(uid, title, pids, callback) {
if (title) {
2014-03-21 15:47:46 -04:00
title = title.trim();
}
if (!title) {
2014-04-09 22:26:23 -04:00
return callback(new Error('[[error:invalid-title]]'));
2014-03-21 15:47:46 -04:00
}
if (!pids || !pids.length) {
2014-04-09 22:26:23 -04:00
return callback(new Error('[[error:invalid-pid]]'));
2014-03-21 15:47:46 -04:00
}
2015-01-10 17:41:03 -05:00
pids.sort(function(a, b) {
return a - b;
});
2014-03-21 15:47:46 -04:00
var mainPid = pids[0];
async.parallel({
postData: function(callback) {
posts.getPostData(mainPid, callback);
},
cid: function(callback) {
posts.getCidByPid(mainPid, callback);
}
}, function(err, results) {
2015-02-24 13:02:58 -05:00
if (err) {
return callback(err);
}
2014-03-21 15:47:46 -04:00
Topics.create({uid: results.postData.uid, title: title, cid: results.cid}, function(err, tid) {
if (err) {
2014-03-21 15:47:46 -04:00
return callback(err);
}
async.eachSeries(pids, move, function(err) {
if (err) {
2014-03-21 15:47:46 -04:00
return callback(err);
}
2014-09-19 15:54:13 -04:00
Topics.updateTimestamp(tid, Date.now(), function(err) {
if (err) {
return callback(err);
}
Topics.getTopicData(tid, callback);
});
2014-03-21 15:47:46 -04:00
});
function move(pid, next) {
privileges.posts.canEdit(pid, uid, function(err, canEdit) {
if(err || !canEdit) {
2014-03-21 15:47:46 -04:00
return next(err);
}
Topics.movePostToTopic(pid, tid, next);
2014-03-21 15:47:46 -04:00
});
}
});
});
};
Topics.movePostToTopic = function(pid, tid, callback) {
2014-12-12 00:45:43 -05:00
var postData;
async.waterfall([
function(next) {
2015-01-13 14:54:13 -05:00
Topics.exists(tid, next);
2014-12-12 00:45:43 -05:00
},
function(exists, next) {
if (!exists) {
return next(new Error('[[error:no-topic]]'));
2014-03-21 15:47:46 -04:00
}
2014-12-12 00:45:43 -05:00
posts.getPostFields(pid, ['tid', 'timestamp', 'votes'], next);
},
function(post, next) {
if (!post || !post.tid) {
return next(new Error('[[error:no-post]]'));
2014-03-21 15:47:46 -04:00
}
2014-12-12 00:52:59 -05:00
if (parseInt(post.tid, 10) === parseInt(tid, 10)) {
return next(new Error('[[error:cant-move-to-same-topic]]'))
}
2014-12-12 00:45:43 -05:00
postData = post;
postData.pid = pid;
2014-03-21 15:47:46 -04:00
2014-12-12 00:45:43 -05:00
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);
2014-03-21 15:47:46 -04:00
}
2014-12-12 00:45:43 -05:00
], next);
}
], function(err) {
if (err) {
return callback(err);
}
plugins.fireHook('action:post.move', {post: postData, tid: tid});
2014-12-23 15:06:05 -05:00
callback();
2014-03-21 15:47:46 -04:00
});
};
2014-04-10 20:31:57 +01:00
};