Files
NodeBB/src/topics/tools.js

252 lines
5.8 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'),
categories = require('../categories'),
plugins = require('../plugins'),
privileges = require('../privileges');
2013-12-21 19:42:07 -05:00
module.exports = function(Topics) {
var topicTools = {};
Topics.tools = topicTools;
topicTools.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
};
topicTools.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);
2015-09-15 18:21:17 -04:00
},
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);
2015-09-15 18:21:17 -04:00
},
function (next) {
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
var data = {
tid: tid,
cid: topicData.cid,
isDelete: isDelete,
uid: uid
};
2014-01-22 23:58:21 -05:00
callback(null, data);
2015-09-15 18:21:17 -04:00
}
], callback);
2014-03-18 20:03:56 -04:00
}
topicTools.purge = function(tid, uid, callback) {
2015-09-17 13:37:04 -04:00
var cid;
2015-01-10 17:12:32 -05:00
async.waterfall([
function(next) {
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-16 08:35:40 -04:00
privileges.topics.canPurge(tid, uid, next);
2015-01-10 17:12:32 -05:00
},
2015-09-16 08:35:40 -04:00
function (canPurge, next) {
if (!canPurge) {
2015-09-15 18:21:17 -04:00
return next(new Error('[[error:no-privileges]]'));
}
Topics.getTopicField(tid, 'cid', next);
2015-01-10 17:12:32 -05:00
},
2015-09-17 13:37:04 -04:00
function (_cid, next) {
cid = _cid;
2015-09-15 18:21:17 -04:00
Topics.purgePostsAndTopic(tid, next);
},
2015-09-15 18:21:17 -04:00
function (next) {
2015-09-17 13:37:04 -04:00
next(null, {tid: tid, cid: cid, uid: uid});
2015-01-10 17:12:32 -05:00
}
], callback);
2014-06-10 16:56:55 -04:00
};
topicTools.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
};
topicTools.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() {};
2015-09-15 18:21:17 -04:00
var cid;
async.waterfall([
function (next) {
Topics.getTopicField(tid, 'cid', next);
2015-09-15 18:21:17 -04:00
},
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]]'));
}
Topics.setTopicField(tid, 'locked', lock ? 1 : 0, next);
2015-09-15 18:21:17 -04:00
},
function (next) {
var data = {
tid: tid,
isLocked: lock,
uid: uid,
cid: cid
};
2015-09-15 18:21:17 -04:00
plugins.fireHook('action:topic.lock', data);
2015-09-15 18:21:17 -04:00
next(null, data);
}
], callback);
}
topicTools.pin = function(tid, uid, callback) {
2014-03-18 20:03:56 -04:00
togglePin(tid, uid, true, callback);
};
topicTools.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);
2015-09-15 18:21:17 -04:00
},
function (exists, next) {
if (!exists) {
return callback();
}
Topics.getTopicFields(tid, ['cid', 'lastposttime'], next);
2015-07-10 16:43:25 -04:00
},
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),
2015-07-10 16:43:25 -04:00
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
};
2015-07-10 16:43:25 -04:00
plugins.fireHook('action:topic.pin', data);
2015-07-10 16:43:25 -04:00
next(null, data);
}
], callback);
}
topicTools.move = function(tid, cid, uid, callback) {
var topic;
async.waterfall([
function(next) {
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;
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);
2015-08-30 15:16:47 -04:00
}
], function(err) {
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-03-18 20:03:56 -04:00
};
2014-08-13 21:42:04 -04:00
};