Files
NodeBB/src/threadTools.js

216 lines
5.1 KiB
JavaScript
Raw Normal View History

2014-03-18 20:03:56 -04:00
'use strict';
2013-12-21 19:42:07 -05:00
var winston = require('winston'),
nconf = require('nconf'),
async = require('async'),
db = require('./database'),
topics = require('./topics'),
categories = require('./categories'),
user = require('./user'),
notifications = require('./notifications'),
2013-08-08 11:40:31 -04:00
posts = require('./posts'),
meta = require('./meta'),
websockets = require('./socket.io'),
2014-02-19 21:06:30 -05:00
events = require('./events'),
Plugins = require('./plugins');
2013-12-21 19:42:07 -05:00
(function(ThreadTools) {
ThreadTools.exists = function(tid, callback) {
db.isSortedSetMember('topics:tid', tid, callback);
2014-03-18 20:03:56 -04:00
};
2013-08-23 13:14:36 -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
};
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, ['cid', 'deleted'], function(err, topicData) {
2014-01-22 23:58:21 -05:00
if(err) {
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-04-24 20:05:05 -04:00
function emitTo(room) {
websockets.in(room).emit(isDelete ? 'event:topic_deleted' : 'event:topic_restored', {
tid: tid,
isDelete: isDelete
});
}
2014-02-17 22:53:01 -05:00
if(err) {
return callback(err);
}
2014-01-16 19:58:57 -05:00
2014-03-18 20:03:56 -04:00
ThreadTools[isDelete ? 'lock' : 'unlock'](tid);
2014-01-22 23:58:21 -05:00
2014-03-18 20:03:56 -04:00
Plugins.fireHook(isDelete ? 'action:topic.delete' : 'action:topic.restore', tid);
2014-02-24 16:23:11 -05:00
2014-03-18 20:03:56 -04:00
events[isDelete ? 'logTopicDelete' : 'logTopicRestore'](uid, tid);
2014-01-22 23:58:21 -05:00
2014-02-17 22:53:01 -05:00
websockets.emitTopicPostStats();
2014-01-22 23:58:21 -05:00
2014-04-24 20:05:05 -04:00
emitTo('topic_' + tid);
emitTo('category_' + topicData.cid);
2014-01-22 23:58:21 -05:00
2014-02-17 22:53:01 -05:00
callback(null, {
2014-03-18 20:03:56 -04:00
tid: tid
2014-02-17 22:53:01 -05:00
});
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) {
async.parallel({
topic: function(next) {
topics.getTopicFields(tid, ['cid'], next);
2014-06-10 16:56:55 -04:00
},
pids: function(next) {
topics.getPids(tid, next);
}
}, function(err, results) {
if (err) {
return callback(err);
}
async.parallel([
function(next) {
async.eachLimit(results.pids, 10, posts.purge, next);
2014-06-10 16:56:55 -04:00
},
function(next) {
topics.purge(tid, next);
}
], 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) {
topics.getTopicField(tid, 'cid', function(err, cid) {
2014-04-24 20:05:05 -04:00
function emitTo(room) {
websockets.in(room).emit(lock ? 'event:topic_locked' : 'event:topic_unlocked', {
tid: tid,
isLocked: lock
});
}
if (err) {
return callback(err);
}
topics.setTopicField(tid, 'locked', lock ? 1 : 0);
2014-04-24 20:05:05 -04:00
emitTo('topic_' + tid);
emitTo('category_' + cid);
if (typeof callback === 'function') {
callback(null, {
tid: tid,
isLocked: lock
});
}
});
}
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) {
topics.getTopicField(tid, 'cid', function(err, cid) {
2014-04-24 20:05:05 -04:00
function emitTo(room) {
websockets.in(room).emit(pin ? 'event:topic_pinned' : 'event:topic_unpinned', {
tid: tid,
isPinned: pin
});
}
if (err) {
return callback(err);
}
topics.setTopicField(tid, 'pinned', pin ? 1 : 0);
topics.getTopicFields(tid, ['cid', 'lastposttime'], function(err, topicData) {
db.sortedSetAdd('categories:' + topicData.cid + ':tid', pin ? Math.pow(2, 53) : topicData.lastposttime, tid);
});
2014-04-24 20:05:05 -04:00
emitTo('topic_' + tid);
emitTo('category_' + cid);
if (typeof callback === 'function') {
callback(null, {
tid: tid,
isPinned: pin
});
}
});
}
2014-01-16 16:10:23 -05:00
ThreadTools.move = function(tid, cid, callback) {
var topic;
async.waterfall([
function(next) {
2014-02-07 12:27:55 -05:00
topics.getTopicFields(tid, ['cid', 'lastposttime', 'pinned', 'deleted'], next);
},
function(topicData, next) {
topic = topicData;
db.sortedSetRemove('categories:' + topicData.cid + ':tid', tid, next);
},
function(result, next) {
2014-02-07 12:27:55 -05:00
var timestamp = parseInt(topic.pinned, 10) ? Math.pow(2, 53) : topic.lastposttime;
db.sortedSetAdd('categories:' + cid + ':tid', timestamp, tid, next);
}
], function(err, result) {
2014-01-25 22:43:49 -05:00
if(err) {
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, callback);
});
2014-03-18 20:03:56 -04:00
};
2014-01-16 20:53:32 -05:00
ThreadTools.toggleFollow = function(tid, uid, callback) {
2014-04-24 20:05:05 -04:00
topics.isFollowing(tid, uid, function(err, following) {
2014-05-05 09:33:47 -04:00
if (err) {
2014-01-16 20:53:32 -05:00
return callback(err);
}
2014-01-16 20:53:32 -05:00
2014-05-05 09:33:47 -04:00
db[following ? 'setRemove' : 'setAdd']('tid:' + tid + ':followers', uid, function(err) {
if (typeof callback === 'function') {
callback(err, !following);
2014-01-16 20:53:32 -05:00
}
});
});
2014-03-18 20:03:56 -04:00
};
2014-04-10 20:31:57 +01:00
}(exports));