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/topics/delete.js

128 lines
3.2 KiB
JavaScript

11 years ago
'use strict';
var async = require('async'),
db = require('../database'),
10 years ago
user = require('../user'),
11 years ago
plugins = require('../plugins');
module.exports = function(Topics) {
11 years ago
11 years ago
Topics.delete = function(tid, callback) {
11 years ago
async.parallel([
function(next) {
Topics.setTopicField(tid, 'deleted', 1, next);
},
function(next) {
10 years ago
db.sortedSetsRemove(['topics:recent', 'topics:posts', 'topics:views'], tid, next);
11 years ago
}
], callback);
11 years ago
};
Topics.restore = function(tid, callback) {
Topics.getTopicFields(tid, ['lastposttime', 'postcount', 'viewcount'], function(err, topicData) {
if (err) {
11 years ago
return callback(err);
}
async.parallel([
function(next) {
Topics.setTopicField(tid, 'deleted', 0, next);
},
function(next) {
11 years ago
Topics.updateRecent(tid, topicData.lastposttime, next);
11 years ago
},
function(next) {
db.sortedSetAdd('topics:posts', topicData.postcount, tid, next);
},
function(next) {
db.sortedSetAdd('topics:views', topicData.viewcount, tid, next);
}
], callback);
11 years ago
});
};
Topics.purge = function(tid, callback) {
11 years ago
async.parallel([
function(next) {
db.deleteAll([
'tid:' + tid + ':followers',
'tid:' + tid + ':posts',
'tid:' + tid + ':posts:votes'
], next);
11 years ago
},
function(next) {
11 years ago
db.sortedSetsRemove(['topics:tid', 'topics:recent', 'topics:posts', 'topics:views'], tid, next);
11 years ago
},
function(next) {
deleteTopicFromCategoryAndUser(tid, next);
},
function(next) {
Topics.deleteTopicTags(tid, next);
},
function(next) {
reduceCounters(tid, next);
11 years ago
}
], function(err) {
if (err) {
return callback(err);
}
plugins.fireHook('action:topic.purge', tid);
11 years ago
db.delete('topic:' + tid, callback);
});
};
function deleteTopicFromCategoryAndUser(tid, callback) {
Topics.getTopicFields(tid, ['cid', 'uid'], function(err, topicData) {
11 years ago
if (err) {
return callback(err);
}
10 years ago
async.parallel([
function(next) {
db.sortedSetsRemove([
'cid:' + topicData.cid + ':tids',
'cid:' + topicData.cid + ':tids:posts',
'cid:' + topicData.cid + ':uid:' + topicData.uid + ':tids',
'uid:' + topicData.uid + ':topics'
], tid, next);
},
function(next) {
user.decrementUserFieldBy(topicData.uid, 'topiccount', 1, next);
}
], callback);
});
}
function reduceCounters(tid, callback) {
var incr = -1;
async.parallel([
function(next) {
db.incrObjectFieldBy('global', 'topicCount', incr, next);
},
function(next) {
Topics.getTopicFields(tid, ['cid', 'postcount'], function(err, topicData) {
if (err) {
return next(err);
}
topicData.postcount = parseInt(topicData.postcount, 10);
topicData.postcount = topicData.postcount || 0;
var postCountChange = incr * topicData.postcount;
11 years ago
11 years ago
async.parallel([
function(next) {
db.incrObjectFieldBy('global', 'postCount', postCountChange, next);
11 years ago
},
function(next) {
db.incrObjectFieldBy('category:' + topicData.cid, 'post_count', postCountChange, next);
},
function(next) {
db.incrObjectFieldBy('category:' + topicData.cid, 'topic_count', incr, next);
11 years ago
}
], next);
});
}
], callback);
11 years ago
}
};