Files
NodeBB/src/threadTools.js

202 lines
4.6 KiB
JavaScript
Raw Normal View History

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'),
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'),
batch = require('./batch');
2013-12-21 19:42:07 -05:00
(function(ThreadTools) {
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
};
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) {
topics.getTopicFields(tid, ['tid', 'cid', 'deleted', 'title', 'mainPid'], function(err, topicData) {
2014-11-07 17:46:03 -05:00
if (err) {
2014-01-22 23:58:21 -05:00
return callback(err);
}
2013-10-16 13:04:28 -04:00
var alreadyDeletedOrRestored = (parseInt(topicData.deleted, 10) && isDelete) || (!parseInt(topicData.deleted, 10) && !isDelete);
if (alreadyDeletedOrRestored) {
return callback(null, {tid: tid});
2014-01-22 23:58:21 -05:00
}
2013-12-21 19:42:07 -05:00
2014-03-18 20:03:56 -04:00
topics[isDelete ? 'delete' : 'restore'](tid, function(err) {
2014-11-07 17:46:03 -05:00
if (err) {
2014-02-17 22:53:01 -05:00
return callback(err);
}
2014-01-16 19:58:57 -05:00
2014-11-10 16:52:38 -05:00
ThreadTools[isDelete ? 'lock' : 'unlock'](tid, uid);
2014-11-07 17:46:03 -05:00
if (isDelete) {
plugins.fireHook('action:topic.delete', tid);
} else {
plugins.fireHook('action:topic.restore', topicData);
}
2014-02-24 16:23:11 -05:00
var data = {
tid: tid,
cid: topicData.cid,
isDelete: isDelete,
uid: uid
};
2014-01-22 23:58:21 -05:00
callback(null, data);
2014-01-16 19:58:57 -05:00
});
2014-01-22 23:58:21 -05:00
});
2014-03-18 20:03:56 -04:00
}
2014-06-10 16:56:55 -04:00
ThreadTools.purge = function(tid, uid, callback) {
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-01-10 17:12:32 -05:00
batch.processSortedSet('tid:' + tid + ':posts', function(pids, next) {
async.eachLimit(pids, 10, posts.purge, next);
}, {alwaysStartAt: 0}, next);
},
function(next) {
topics.getTopicFields(tid, ['mainPid', 'cid'], next);
2015-01-10 17:12:32 -05:00
},
function(_topic, next) {
topic = _topic;
posts.purge(topic.mainPid, next);
2015-01-10 17:12:32 -05:00
},
function(next) {
topics.purge(tid, next);
},
function(next) {
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-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) {
callback = callback || function() {};
topics.getTopicField(tid, 'cid', function(err, cid) {
if (err) {
return callback(err);
}
topics.setTopicField(tid, 'locked', lock ? 1 : 0);
var data = {
tid: tid,
isLocked: lock,
uid: uid,
cid: cid
};
plugins.fireHook('action:topic.lock', data);
callback(null, data);
});
}
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);
};
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) {
2014-11-07 19:15:26 -05:00
topics.getTopicFields(tid, ['cid', 'lastposttime'], function(err, topicData) {
if (err) {
return callback(err);
}
topics.setTopicField(tid, 'pinned', pin ? 1 : 0);
2014-11-07 19:15:26 -05:00
db.sortedSetAdd('cid:' + topicData.cid + ':tids', pin ? Math.pow(2, 53) : topicData.lastposttime, tid);
var data = {
tid: tid,
isPinned: pin,
uid: uid,
cid: topicData.cid
};
plugins.fireHook('action:topic.pin', data);
callback(null, data);
});
}
ThreadTools.move = function(tid, cid, uid, callback) {
var topic;
async.waterfall([
function(next) {
2015-01-08 13:47:15 -05:00
topics.getTopicFields(tid, ['cid', 'lastposttime', 'pinned', 'deleted', 'postcount'], next);
},
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-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-09-03 20:19:51 -04:00
], function(err) {
if (err) {
2014-01-25 22:43:49 -05:00
return callback(err);
}
var oldCid = topic.cid;
2014-01-25 22:43:49 -05:00
if(!parseInt(topic.deleted, 10)) {
categories.incrementCategoryFieldBy(oldCid, 'topic_count', -1);
categories.incrementCategoryFieldBy(cid, 'topic_count', 1);
}
categories.moveRecentReplies(tid, oldCid, cid);
topics.setTopicField(tid, 'cid', cid, function(err) {
if (err) {
return callback(err);
}
plugins.fireHook('action:topic.move', {
tid: tid,
fromCid: oldCid,
toCid: cid,
uid: uid
});
});
});
2014-03-18 20:03:56 -04:00
};
2014-08-13 21:42:04 -04:00
2014-04-10 20:31:57 +01:00
}(exports));