You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nodebb/src/threadTools.js

230 lines
5.7 KiB
JavaScript

'use strict';
11 years ago
var winston = require('winston'),
nconf = require('nconf'),
async = require('async'),
db = require('./database'),
topics = require('./topics'),
categories = require('./categories'),
CategoryTools = require('./categoryTools'),
user = require('./user'),
notifications = require('./notifications'),
posts = require('./posts'),
meta = require('./meta'),
websockets = require('./socket.io'),
11 years ago
events = require('./events'),
Plugins = require('./plugins');
11 years ago
(function(ThreadTools) {
ThreadTools.exists = function(tid, callback) {
db.isSortedSetMember('topics:tid', tid, callback);
};
ThreadTools.privileges = function(tid, uid, callback) {
async.parallel({
categoryPrivs: function(next) {
topics.getTopicField(tid, 'cid', function(err, cid) {
CategoryTools.privileges(cid, uid, next);
});
},
hasEnoughRep: function(next) {
if (parseInt(meta.config['privileges:disabled'], 10)) {
return next(null, false);
} else {
user.getUserField(uid, 'reputation', function(err, reputation) {
if (err) {
return next(null, false);
}
next(null, parseInt(reputation, 10) >= parseInt(meta.config['privileges:manage_topic'], 10));
});
}
}
}, function(err, results) {
callback(err, !results ? undefined : {
read: results.categoryPrivs.read,
write: results.categoryPrivs.write,
editable: results.categoryPrivs.editable || results.hasEnoughRep,
view_deleted: results.categoryPrivs.view_deleted || results.hasEnoughRep,
moderator: results.categoryPrivs.moderator,
admin: results.categoryPrivs.admin
});
});
};
ThreadTools.delete = function(tid, uid, callback) {
toggleDelete(tid, uid, true, callback);
};
ThreadTools.restore = function(tid, uid, callback) {
toggleDelete(tid, uid, false, callback);
};
function toggleDelete(tid, uid, isDelete, callback) {
topics.getTopicFields(tid, ['cid', 'deleted'], function(err, topicData) {
11 years ago
if(err) {
return callback(err);
}
11 years ago
var alreadyDeletedOrRestored = (parseInt(topicData.deleted, 10) && isDelete) || (!parseInt(topicData.deleted, 10) && !isDelete);
if (alreadyDeletedOrRestored) {
return callback(null, {tid: tid});
11 years ago
}
11 years ago
topics[isDelete ? 'delete' : 'restore'](tid, function(err) {
function emitTo(room) {
websockets.in(room).emit(isDelete ? 'event:topic_deleted' : 'event:topic_restored', {
tid: tid,
isDelete: isDelete
});
}
if(err) {
return callback(err);
}
11 years ago
ThreadTools[isDelete ? 'lock' : 'unlock'](tid);
11 years ago
Plugins.fireHook(isDelete ? 'action:topic.delete' : 'action:topic.restore', tid);
11 years ago
events[isDelete ? 'logTopicDelete' : 'logTopicRestore'](uid, tid);
11 years ago
websockets.emitTopicPostStats();
11 years ago
emitTo('topic_' + tid);
emitTo('category_' + topicData.cid);
11 years ago
callback(null, {
tid: tid
});
11 years ago
});
11 years ago
});
}
11 years ago
ThreadTools.lock = function(tid, uid, callback) {
toggleLock(tid, uid, true, callback);
11 years ago
};
11 years ago
ThreadTools.unlock = function(tid, uid, callback) {
toggleLock(tid, uid, false, callback);
};
function toggleLock(tid, uid, lock, callback) {
topics.getTopicField(tid, 'cid', function(err, cid) {
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);
emitTo('topic_' + tid);
emitTo('category_' + cid);
if (typeof callback === 'function') {
callback(null, {
tid: tid,
isLocked: lock
});
}
});
}
11 years ago
ThreadTools.pin = function(tid, uid, callback) {
togglePin(tid, uid, true, callback);
};
11 years ago
ThreadTools.unpin = function(tid, uid, callback) {
togglePin(tid, uid, false, callback);
};
function togglePin(tid, uid, pin, callback) {
topics.getTopicField(tid, 'cid', function(err, cid) {
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);
});
emitTo('topic_' + tid);
emitTo('category_' + cid);
if (typeof callback === 'function') {
callback(null, {
tid: tid,
isPinned: pin
});
}
});
}
11 years ago
ThreadTools.move = function(tid, cid, callback) {
var topic;
async.waterfall([
function(next) {
11 years ago
topics.getTopicFields(tid, ['cid', 'lastposttime', 'pinned', 'deleted'], next);
},
function(topicData, next) {
topic = topicData;
db.sortedSetRemove('categories:' + topicData.cid + ':tid', tid, next);
},
function(result, next) {
11 years ago
var timestamp = parseInt(topic.pinned, 10) ? Math.pow(2, 53) : topic.lastposttime;
db.sortedSetAdd('categories:' + cid + ':tid', timestamp, tid, next);
}
], function(err, result) {
if(err) {
return callback(err);
}
var oldCid = topic.cid;
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);
});
};
ThreadTools.toggleFollow = function(tid, uid, callback) {
topics.isFollowing(tid, uid, function(err, following) {
if(err) {
return callback(err);
}
db[following ? 'setRemove' : 'setAdd']('tid:' + tid + ':followers', uid, function(err, success) {
if (callback) {
if(err) {
return callback(err);
}
callback(null, !following);
}
});
});
};
}(exports));