This commit is contained in:
Barış Soner Uşaklı
2017-11-30 12:39:03 -05:00
parent 17c52a515d
commit 4f2f84e47c
6 changed files with 149 additions and 2 deletions

View File

@@ -16,6 +16,7 @@ require('./topics/move')(SocketTopics);
require('./topics/tools')(SocketTopics);
require('./topics/infinitescroll')(SocketTopics);
require('./topics/tags')(SocketTopics);
require('./topics/merge')(SocketTopics);
SocketTopics.post = function (socket, data, callback) {
if (!data) {

View File

@@ -0,0 +1,27 @@
'use strict';
var async = require('async');
var topics = require('../../topics');
var privileges = require('../../privileges');
module.exports = function (SocketTopics) {
SocketTopics.merge = function (socket, tids, callback) {
if (!Array.isArray(tids)) {
return callback(new Error('[[error:invalid-data]]'));
}
async.waterfall([
function (next) {
async.map(tids, function (tid, next) {
privileges.topics.isAdminOrMod(tid, socket.uid, next);
}, next);
},
function (allowed, next) {
if (allowed.includes(false)) {
return next(new Error('[[error:no-privileges]]'));
}
topics.merge(tids, next);
},
], callback);
};
};

View File

@@ -31,6 +31,7 @@ require('./topics/suggested')(Topics);
require('./topics/tools')(Topics);
require('./topics/thumb')(Topics);
require('./topics/bookmarks')(Topics);
require('./topics/merge')(Topics);
Topics.exists = function (tid, callback) {
db.isSortedSetMember('topics:tid', tid, callback);
@@ -252,7 +253,7 @@ function getMainPostAndReplies(topic, set, uid, start, stop, reverse, callback)
return callback(null, []);
}
if (topic.mainPid && start === 0) {
if (parseInt(topic.mainPid, 10) && start === 0) {
pids.unshift(topic.mainPid);
}
posts.getPostsByPids(pids, uid, next);

33
src/topics/merge.js Normal file
View File

@@ -0,0 +1,33 @@
'use strict';
var async = require('async');
module.exports = function (Topics) {
Topics.merge = function (tids, callback) {
var mergeIntoTid = findOldestTopic(tids);
var otherTids = tids.filter(function (tid) {
return tid && parseInt(tid, 10) !== parseInt(mergeIntoTid, 10);
});
async.eachSeries(otherTids, function (tid, next) {
async.waterfall([
function (next) {
Topics.getPids(tid, next);
},
function (pids, next) {
async.eachSeries(pids, function (pid, next) {
Topics.movePostToTopic(pid, mergeIntoTid, next);
}, next);
},
function (next) {
Topics.setTopicField(tid, 'mainPid', 0, next);
},
], next);
}, callback);
};
function findOldestTopic(tids) {
return Math.min.apply(null, tids);
}
};

View File

@@ -326,7 +326,7 @@ module.exports = function (Topics) {
}, next);
},
function (results, next) {
if (results.mainPid) {
if (parseInt(results.mainPid, 10)) {
results.pids = [results.mainPid].concat(results.pids);
}
next(null, results.pids);