2014-03-18 20:03:56 -04:00
|
|
|
'use strict';
|
|
|
|
|
|
2015-02-02 15:56:29 -05:00
|
|
|
var async = require('async'),
|
2013-12-21 19:42:07 -05:00
|
|
|
|
|
|
|
|
db = require('./database'),
|
2013-11-27 11:27:20 -05:00
|
|
|
topics = require('./topics'),
|
|
|
|
|
categories = require('./categories'),
|
2013-08-08 11:40:31 -04:00
|
|
|
posts = require('./posts'),
|
2014-09-16 21:43:11 -04:00
|
|
|
plugins = require('./plugins'),
|
2015-09-15 18:21:17 -04:00
|
|
|
batch = require('./batch'),
|
|
|
|
|
privileges = require('./privileges');
|
2013-12-21 19:42:07 -05:00
|
|
|
|
2013-05-23 12:52:16 -04:00
|
|
|
|
|
|
|
|
(function(ThreadTools) {
|
2013-07-05 16:44:11 -04:00
|
|
|
|
2014-01-01 14:15:53 -05:00
|
|
|
ThreadTools.delete = function(tid, uid, callback) {
|
2014-03-18 20:03:56 -04:00
|
|
|
toggleDelete(tid, uid, true, callback);
|
2014-02-17 22:53:01 -05:00
|
|
|
};
|
2013-06-20 16:19:17 -04:00
|
|
|
|
2014-01-01 14:15:53 -05:00
|
|
|
ThreadTools.restore = function(tid, uid, callback) {
|
2014-03-18 20:03:56 -04:00
|
|
|
toggleDelete(tid, uid, false, callback);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function toggleDelete(tid, uid, isDelete, callback) {
|
2015-09-15 18:21:17 -04:00
|
|
|
var topicData;
|
|
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
|
|
|
|
privileges.topics.isOwnerOrAdminOrMod(tid, uid, next);
|
|
|
|
|
},
|
|
|
|
|
function (isOwnerOrAdminOrMod, next) {
|
|
|
|
|
if (!isOwnerOrAdminOrMod) {
|
|
|
|
|
return next(new Error('[[error:no-privileges]]'));
|
|
|
|
|
}
|
|
|
|
|
topics.getTopicFields(tid, ['tid', 'cid', 'uid', 'deleted', 'title', 'mainPid'], next);
|
|
|
|
|
},
|
|
|
|
|
function (_topicData, next) {
|
|
|
|
|
topicData = _topicData;
|
2013-12-21 19:42:07 -05:00
|
|
|
|
2015-09-15 18:21:17 -04:00
|
|
|
if (parseInt(topicData.deleted, 10) === 1 && isDelete) {
|
|
|
|
|
return callback(new Error('[[error:topic-already-deleted]]'));
|
|
|
|
|
} else if(parseInt(topicData.deleted, 10) !== 1 && !isDelete) {
|
|
|
|
|
return callback(new Error('[[error:topic-already-restored]]'));
|
2014-02-17 22:53:01 -05:00
|
|
|
}
|
2015-09-15 18:21:17 -04:00
|
|
|
|
|
|
|
|
topics[isDelete ? 'delete' : 'restore'](tid, next);
|
|
|
|
|
},
|
|
|
|
|
function (next) {
|
2015-04-14 13:05:58 -04:00
|
|
|
topicData.deleted = isDelete ? 1 : 0;
|
2015-04-21 16:26:24 -04:00
|
|
|
|
2014-11-07 17:46:03 -05:00
|
|
|
if (isDelete) {
|
2015-06-06 13:29:23 +02:00
|
|
|
plugins.fireHook('action:topic.delete', topicData);
|
2014-11-07 17:46:03 -05:00
|
|
|
} else {
|
|
|
|
|
plugins.fireHook('action:topic.restore', topicData);
|
|
|
|
|
}
|
2014-02-24 16:23:11 -05:00
|
|
|
|
2015-02-01 21:38:10 -05:00
|
|
|
var data = {
|
|
|
|
|
tid: tid,
|
|
|
|
|
cid: topicData.cid,
|
|
|
|
|
isDelete: isDelete,
|
|
|
|
|
uid: uid
|
|
|
|
|
};
|
2014-01-22 23:58:21 -05:00
|
|
|
|
2015-02-01 21:38:10 -05:00
|
|
|
callback(null, data);
|
2015-09-15 18:21:17 -04:00
|
|
|
}
|
|
|
|
|
], callback);
|
2014-03-18 20:03:56 -04:00
|
|
|
}
|
2013-06-20 16:19:17 -04:00
|
|
|
|
2014-06-10 16:56:55 -04:00
|
|
|
ThreadTools.purge = function(tid, uid, callback) {
|
2015-02-01 21:38:10 -05:00
|
|
|
var topic;
|
2015-01-10 17:12:32 -05:00
|
|
|
async.waterfall([
|
|
|
|
|
function(next) {
|
2015-01-13 14:54:13 -05:00
|
|
|
topics.exists(tid, next);
|
2015-01-10 17:12:32 -05:00
|
|
|
},
|
|
|
|
|
function(exists, next) {
|
|
|
|
|
if (!exists) {
|
|
|
|
|
return callback();
|
2014-06-10 16:56:55 -04:00
|
|
|
}
|
2015-09-15 18:21:17 -04:00
|
|
|
privileges.topics.isOwnerOrAdminOrMod(tid, uid, next);
|
2015-01-10 17:12:32 -05:00
|
|
|
},
|
2015-09-15 18:21:17 -04:00
|
|
|
function (isOwnerOrAdminOrMod, next) {
|
|
|
|
|
if (!isOwnerOrAdminOrMod) {
|
|
|
|
|
return next(new Error('[[error:no-privileges]]'));
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-01 21:38:10 -05:00
|
|
|
topics.getTopicFields(tid, ['mainPid', 'cid'], next);
|
2015-01-10 17:12:32 -05:00
|
|
|
},
|
2015-09-15 18:21:17 -04:00
|
|
|
function (_topic, next) {
|
2015-02-01 21:38:10 -05:00
|
|
|
topic = _topic;
|
2015-09-15 18:21:17 -04:00
|
|
|
|
|
|
|
|
batch.processSortedSet('tid:' + tid + ':posts', function(pids, next) {
|
|
|
|
|
async.eachLimit(pids, 10, posts.purge, next);
|
|
|
|
|
}, {alwaysStartAt: 0}, next);
|
|
|
|
|
},
|
|
|
|
|
function (next) {
|
2015-02-01 21:38:10 -05:00
|
|
|
posts.purge(topic.mainPid, next);
|
2015-01-10 17:12:32 -05:00
|
|
|
},
|
2015-09-15 18:21:17 -04:00
|
|
|
function (next) {
|
2015-01-10 17:12:32 -05:00
|
|
|
topics.purge(tid, next);
|
2015-02-01 21:38:10 -05:00
|
|
|
},
|
2015-09-15 18:21:17 -04:00
|
|
|
function (next) {
|
2015-02-01 21:38:10 -05:00
|
|
|
next(null, {tid: tid, cid: topic.cid, uid: uid});
|
2015-01-10 17:12:32 -05:00
|
|
|
}
|
|
|
|
|
], callback);
|
2014-06-10 16:56:55 -04:00
|
|
|
};
|
|
|
|
|
|
2014-01-16 19:58:57 -05:00
|
|
|
ThreadTools.lock = function(tid, uid, callback) {
|
2014-03-18 20:03:56 -04:00
|
|
|
toggleLock(tid, uid, true, callback);
|
2014-02-24 16:23:11 -05:00
|
|
|
};
|
2014-01-10 10:46:26 -05:00
|
|
|
|
2014-01-16 19:58:57 -05:00
|
|
|
ThreadTools.unlock = function(tid, uid, callback) {
|
2014-03-18 20:03:56 -04:00
|
|
|
toggleLock(tid, uid, false, callback);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function toggleLock(tid, uid, lock, callback) {
|
2015-02-01 21:38:10 -05:00
|
|
|
callback = callback || function() {};
|
2014-01-10 10:46:26 -05:00
|
|
|
|
2015-09-15 18:21:17 -04:00
|
|
|
var cid;
|
|
|
|
|
|
|
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
|
|
|
|
topics.getTopicField(tid, 'cid', next);
|
|
|
|
|
},
|
|
|
|
|
function (_cid, next) {
|
|
|
|
|
cid = _cid;
|
|
|
|
|
if (!cid) {
|
|
|
|
|
return next(new Error('[[error:no-topic]]'));
|
|
|
|
|
}
|
|
|
|
|
privileges.categories.isAdminOrMod(cid, uid, next);
|
|
|
|
|
},
|
|
|
|
|
function (isAdminOrMod, next) {
|
|
|
|
|
if (!isAdminOrMod) {
|
|
|
|
|
return next(new Error('[[error:no-privileges]]'));
|
|
|
|
|
}
|
2014-04-17 20:07:23 -04:00
|
|
|
|
2015-09-15 18:21:17 -04:00
|
|
|
topics.setTopicField(tid, 'locked', lock ? 1 : 0, next);
|
|
|
|
|
},
|
|
|
|
|
function (next) {
|
|
|
|
|
var data = {
|
|
|
|
|
tid: tid,
|
|
|
|
|
isLocked: lock,
|
|
|
|
|
uid: uid,
|
|
|
|
|
cid: cid
|
|
|
|
|
};
|
2014-07-24 14:22:48 -04:00
|
|
|
|
2015-09-15 18:21:17 -04:00
|
|
|
plugins.fireHook('action:topic.lock', data);
|
2014-04-17 20:07:23 -04:00
|
|
|
|
2015-09-15 18:21:17 -04:00
|
|
|
next(null, data);
|
|
|
|
|
}
|
|
|
|
|
], callback);
|
2014-01-10 10:46:26 -05:00
|
|
|
}
|
|
|
|
|
|
2014-01-16 19:58:57 -05:00
|
|
|
ThreadTools.pin = function(tid, uid, callback) {
|
2014-03-18 20:03:56 -04:00
|
|
|
togglePin(tid, uid, true, callback);
|
|
|
|
|
};
|
2013-06-20 16:19:17 -04:00
|
|
|
|
2014-01-16 19:58:57 -05:00
|
|
|
ThreadTools.unpin = function(tid, uid, callback) {
|
2014-03-18 20:03:56 -04:00
|
|
|
togglePin(tid, uid, false, callback);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function togglePin(tid, uid, pin, callback) {
|
2015-07-10 16:43:25 -04:00
|
|
|
var topicData;
|
|
|
|
|
async.waterfall([
|
2015-09-15 18:21:17 -04:00
|
|
|
function (next) {
|
|
|
|
|
topics.exists(tid, next);
|
|
|
|
|
},
|
|
|
|
|
function (exists, next) {
|
|
|
|
|
if (!exists) {
|
|
|
|
|
return callback();
|
|
|
|
|
}
|
2015-07-10 16:43:25 -04:00
|
|
|
topics.getTopicFields(tid, ['cid', 'lastposttime'], next);
|
|
|
|
|
},
|
2015-09-15 18:21:17 -04:00
|
|
|
function (_topicData, next) {
|
2015-07-10 16:43:25 -04:00
|
|
|
topicData = _topicData;
|
2015-09-15 18:21:17 -04:00
|
|
|
privileges.categories.isAdminOrMod(_topicData.cid, uid, next);
|
|
|
|
|
},
|
|
|
|
|
function(isAdminOrMod, next) {
|
|
|
|
|
if (!isAdminOrMod) {
|
|
|
|
|
return next(new Error('[[error:no-privileges]]'));
|
|
|
|
|
}
|
2015-07-10 16:43:25 -04:00
|
|
|
async.parallel([
|
|
|
|
|
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);
|
|
|
|
|
},
|
|
|
|
|
function(results, next) {
|
|
|
|
|
var data = {
|
|
|
|
|
tid: tid,
|
|
|
|
|
isPinned: pin,
|
|
|
|
|
uid: uid,
|
|
|
|
|
cid: topicData.cid
|
|
|
|
|
};
|
2014-07-24 14:22:48 -04:00
|
|
|
|
2015-07-10 16:43:25 -04:00
|
|
|
plugins.fireHook('action:topic.pin', data);
|
2014-04-17 20:07:23 -04:00
|
|
|
|
2015-07-10 16:43:25 -04:00
|
|
|
next(null, data);
|
|
|
|
|
}
|
|
|
|
|
], callback);
|
2013-05-23 12:52:16 -04:00
|
|
|
}
|
|
|
|
|
|
2014-07-24 14:32:17 -04:00
|
|
|
ThreadTools.move = function(tid, cid, uid, callback) {
|
2014-02-02 15:43:33 -05:00
|
|
|
var topic;
|
|
|
|
|
async.waterfall([
|
|
|
|
|
function(next) {
|
2015-01-08 13:47:15 -05:00
|
|
|
topics.getTopicFields(tid, ['cid', 'lastposttime', 'pinned', 'deleted', 'postcount'], next);
|
2014-02-02 15:43:33 -05:00
|
|
|
},
|
|
|
|
|
function(topicData, next) {
|
|
|
|
|
topic = topicData;
|
2015-01-08 13:47:15 -05:00
|
|
|
db.sortedSetsRemove([
|
|
|
|
|
'cid:' + topicData.cid + ':tids',
|
|
|
|
|
'cid:' + topicData.cid + ':tids:posts'
|
|
|
|
|
], tid, next);
|
2014-02-02 15:43:33 -05:00
|
|
|
},
|
2014-09-03 20:19:51 -04:00
|
|
|
function(next) {
|
2014-02-07 12:27:55 -05:00
|
|
|
var timestamp = parseInt(topic.pinned, 10) ? Math.pow(2, 53) : topic.lastposttime;
|
2015-01-08 13:47:15 -05:00
|
|
|
async.parallel([
|
|
|
|
|
function(next) {
|
|
|
|
|
db.sortedSetAdd('cid:' + cid + ':tids', timestamp, tid, next);
|
|
|
|
|
},
|
|
|
|
|
function(next) {
|
|
|
|
|
topic.postcount = topic.postcount || 0;
|
|
|
|
|
db.sortedSetAdd('cid:' + cid + ':tids:posts', topic.postcount, tid, next);
|
|
|
|
|
}
|
|
|
|
|
], next);
|
2014-02-02 15:43:33 -05:00
|
|
|
}
|
2014-09-03 20:19:51 -04:00
|
|
|
], function(err) {
|
|
|
|
|
if (err) {
|
2014-01-25 22:43:49 -05:00
|
|
|
return callback(err);
|
|
|
|
|
}
|
2014-02-02 15:43:33 -05:00
|
|
|
var oldCid = topic.cid;
|
2014-04-17 20:07:23 -04:00
|
|
|
categories.moveRecentReplies(tid, oldCid, cid);
|
|
|
|
|
|
2015-08-30 15:16:47 -04:00
|
|
|
async.parallel([
|
|
|
|
|
function (next) {
|
|
|
|
|
categories.incrementCategoryFieldBy(oldCid, 'topic_count', -1, next);
|
|
|
|
|
},
|
|
|
|
|
function (next) {
|
|
|
|
|
categories.incrementCategoryFieldBy(cid, 'topic_count', 1, next);
|
|
|
|
|
},
|
|
|
|
|
function (next) {
|
|
|
|
|
topics.setTopicField(tid, 'cid', cid, next);
|
|
|
|
|
}
|
|
|
|
|
], function(err) {
|
2015-03-15 00:24:08 -04:00
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
plugins.fireHook('action:topic.move', {
|
|
|
|
|
tid: tid,
|
|
|
|
|
fromCid: oldCid,
|
|
|
|
|
toCid: cid,
|
|
|
|
|
uid: uid
|
|
|
|
|
});
|
2015-04-02 14:09:19 -04:00
|
|
|
callback();
|
2014-07-24 14:22:48 -04:00
|
|
|
});
|
2013-05-23 12:52:16 -04:00
|
|
|
});
|
2014-03-18 20:03:56 -04:00
|
|
|
};
|
2013-05-23 12:52:16 -04:00
|
|
|
|
2014-08-13 21:42:04 -04:00
|
|
|
|
2014-04-10 20:31:57 +01:00
|
|
|
}(exports));
|