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

140 lines
3.4 KiB
JavaScript

11 years ago
'use strict';
var async = require('async'),
db = require('../database'),
plugins = require('../plugins');
module.exports = function(Topics) {
11 years ago
11 years ago
function updateCounters(tid, incr, callback) {
async.parallel([
function(next) {
db.incrObjectFieldBy('global', 'topicCount', incr, next);
},
function(next) {
11 years ago
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
async.parallel([
function(next) {
db.incrObjectFieldBy('global', 'postCount', postCountChange, next);
},
function(next) {
db.incrObjectFieldBy('category:' + topicData.cid, 'post_count', postCountChange, next);
},
function(next) {
db.incrObjectFieldBy('category:' + topicData.cid, 'topic_count', incr, next);
}
], next);
});
}
], callback);
}
11 years ago
Topics.delete = function(tid, callback) {
11 years ago
async.parallel([
function(next) {
Topics.setTopicField(tid, 'deleted', 1, next);
},
function(next) {
11 years ago
Topics.removeRecent(tid, next);
11 years ago
},
function(next) {
11 years ago
db.sortedSetsRemove(['topics:posts', 'topics:views'], tid, next);
11 years ago
}
], function(err) {
if (err) {
return callback(err);
}
11 years ago
updateCounters(tid, -1, callback);
11 years ago
});
};
Topics.restore = function(tid, callback) {
Topics.getTopicFields(tid, ['lastposttime', 'postcount', 'viewcount'], function(err, topicData) {
if(err) {
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);
}
], function(err) {
if (err) {
return callback(err);
}
11 years ago
updateCounters(tid, 1, callback);
11 years ago
});
});
};
Topics.purge = function(tid, callback) {
11 years ago
async.parallel([
function(next) {
11 years ago
db.deleteAll(['tid:' + tid + ':followers', 'tid:' + tid + ':read_by_uid'], 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(err) {
if (err) {
return callback(err);
}
plugins.fireHook('action:topic.delete', tid);
db.delete('topic:' + tid, callback);
});
};
function deleteTopicFromCategoryAndUser(tid, callback) {
Topics.getTopicFields(tid, ['cid', 'uid', 'deleted'], function(err, topicData) {
if (err) {
return callback(err);
}
11 years ago
db.sortedSetsRemove(['categories:' + topicData.cid + ':tid', 'uid:' + topicData.uid + ':topics'], tid, function(err) {
11 years ago
if (err) {
return callback(err);
}
11 years ago
if (parseInt(topicData.deleted, 10) === 0) {
async.parallel([
function(next) {
db.decrObjectField('category:' + topicData.cid, 'topic_count', next);
},
function(next) {
db.decrObjectField('global', 'topicCount', next);
11 years ago
}
], callback);
} else {
callback();
}
11 years ago
});
});
}
};