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

192 lines
4.8 KiB
JavaScript

11 years ago
'use strict';
var async = require('async'),
db = require('../database'),
10 years ago
user = require('../user'),
10 years ago
posts = require('../posts'),
10 years ago
plugins = require('../plugins'),
batch = require('../batch');
11 years ago
module.exports = function(Topics) {
11 years ago
11 years ago
Topics.delete = function(tid, callback) {
10 years ago
Topics.getTopicFields(tid, ['cid'], function(err, topicData) {
if (err) {
return callback(err);
11 years ago
}
10 years ago
async.parallel([
function(next) {
Topics.setTopicField(tid, 'deleted', 1, next);
},
function(next) {
db.sortedSetsRemove(['topics:recent', 'topics:posts', 'topics:views'], tid, next);
},
function(next) {
Topics.getPids(tid, function(err, pids) {
if (err) {
return next(err);
}
db.sortedSetRemove('cid:' + topicData.cid + ':pids', pids, next);
});
}
10 years ago
], function(err, results) {
callback(err);
});
10 years ago
});
11 years ago
};
Topics.restore = function(tid, callback) {
10 years ago
Topics.getTopicFields(tid, ['cid', '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);
10 years ago
},
function(next) {
Topics.getPids(tid, function(err, pids) {
if (err) {
return callback(err);
}
posts.getPostsFields(pids, ['pid', 'timestamp', 'deleted'], function(err, postData) {
if (err) {
return next(err);
}
postData = postData.filter(function(post) {
10 years ago
return post && parseInt(post.deleted, 10) !== 1;
10 years ago
});
var pidsToAdd = [], scores = [];
postData.forEach(function(post) {
pidsToAdd.push(post.pid);
scores.push(post.timestamp);
});
db.sortedSetAdd('cid:' + topicData.cid + ':pids', scores, pidsToAdd, next);
});
});
11 years ago
}
10 years ago
], function(err, results) {
callback(err);
});
11 years ago
});
};
10 years ago
Topics.purgePostsAndTopic = function(tid, callback) {
var mainPid;
async.waterfall([
function (next) {
Topics.getTopicField(tid, 'mainPid', next);
},
function (_mainPid, next) {
mainPid = _mainPid;
batch.processSortedSet('tid:' + tid + ':posts', function(pids, next) {
async.eachLimit(pids, 10, posts.purge, next);
}, {alwaysStartAt: 0}, next);
},
function (next) {
posts.purge(mainPid, next);
},
function (next) {
Topics.purge(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',
'tid:' + tid + ':bookmarks'
], 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
}
};