Files
NodeBB/src/topics/delete.js

265 lines
6.7 KiB
JavaScript
Raw Normal View History

2014-06-10 14:24:50 -04:00
'use strict';
2016-04-19 20:04:32 +03:00
var async = require('async');
var db = require('../database');
2014-06-10 14:24:50 -04:00
2016-04-19 20:04:32 +03:00
var user = require('../user');
var posts = require('../posts');
var plugins = require('../plugins');
var batch = require('../batch');
2014-06-10 14:24:50 -04:00
module.exports = function (Topics) {
Topics.delete = function (tid, uid, callback) {
2017-03-12 21:38:47 +03:00
async.parallel([
function (next) {
Topics.setTopicFields(tid, {
deleted: 1,
deleterUid: uid,
deletedTimestamp: Date.now(),
}, next);
},
function (next) {
db.sortedSetsRemove([
'topics:recent',
'topics:posts',
'topics:views',
'topics:votes',
], tid, next);
2017-03-12 21:38:47 +03:00
},
function (next) {
async.waterfall([
function (next) {
async.parallel({
cid: function (next) {
Topics.getTopicField(tid, 'cid', next);
},
pids: function (next) {
Topics.getPids(tid, next);
},
}, next);
},
function (results, next) {
db.sortedSetRemove('cid:' + results.cid + ':pids', results.pids, next);
},
], next);
},
], function (err) {
callback(err);
2015-04-16 12:57:55 -04:00
});
2014-06-10 16:56:55 -04:00
};
Topics.restore = function (tid, uid, callback) {
2017-03-12 21:38:47 +03:00
var topicData;
async.waterfall([
function (next) {
Topics.getTopicData(tid, next);
2017-03-12 21:38:47 +03:00
},
function (_topicData, next) {
topicData = _topicData;
async.parallel([
function (next) {
Topics.setTopicField(tid, 'deleted', 0, next);
},
function (next) {
Topics.deleteTopicFields(tid, ['deleterUid', 'deletedTimestamp'], next);
},
function (next) {
Topics.updateRecent(tid, topicData.lastposttime, next);
},
function (next) {
2019-06-24 21:36:20 -04:00
db.sortedSetAddBulk([
['topics:posts', topicData.postcount, tid],
['topics:views', topicData.viewcount, tid],
['topics:votes', parseInt(topicData.votes, 10) || 0, tid],
], next);
},
2017-03-12 21:38:47 +03:00
function (next) {
async.waterfall([
function (next) {
Topics.getPids(tid, next);
},
function (pids, next) {
posts.getPostsFields(pids, ['pid', 'timestamp', 'deleted'], next);
},
function (postData, next) {
2018-10-25 17:02:59 -04:00
postData = postData.filter(post => post && !post.deleted);
2017-03-12 21:38:47 +03:00
var pidsToAdd = [];
var scores = [];
postData.forEach(function (post) {
pidsToAdd.push(post.pid);
scores.push(post.timestamp);
});
db.sortedSetAdd('cid:' + topicData.cid + ':pids', scores, pidsToAdd, next);
},
], next);
},
], function (err) {
next(err);
});
},
], callback);
2014-06-10 16:56:55 -04:00
};
Topics.purgePostsAndTopic = function (tid, uid, callback) {
2015-09-17 13:37:04 -04:00
var mainPid;
async.waterfall([
function (next) {
Topics.getTopicField(tid, 'mainPid', next);
},
function (_mainPid, next) {
mainPid = _mainPid;
batch.processSortedSet('tid:' + tid + ':posts', function (pids, next) {
2018-03-22 17:02:39 -04:00
async.eachSeries(pids, function (pid, next) {
2016-02-25 18:01:59 +02:00
posts.purge(pid, uid, next);
}, next);
2017-02-18 12:30:49 -07:00
}, { alwaysStartAt: 0 }, next);
2015-09-17 13:37:04 -04:00
},
function (next) {
2016-02-25 18:01:59 +02:00
posts.purge(mainPid, uid, next);
2015-09-17 13:37:04 -04:00
},
function (next) {
2016-03-12 17:29:33 +02:00
Topics.purge(tid, uid, next);
2017-02-17 19:31:21 -07:00
},
2015-09-17 13:37:04 -04:00
], callback);
};
Topics.purge = function (tid, uid, callback) {
2018-06-21 11:27:37 -04:00
var deletedTopic;
2016-08-17 12:12:07 +03:00
async.waterfall([
function (next) {
2018-06-21 11:27:37 -04:00
async.parallel({
topic: async.apply(Topics.getTopicData, tid),
tags: async.apply(Topics.getTopicTags, tid),
}, next);
},
function (results, next) {
if (!results.topic) {
return callback();
}
deletedTopic = results.topic;
deletedTopic.tags = results.tags;
2016-08-17 12:12:07 +03:00
deleteFromFollowersIgnorers(tid, next);
2014-10-14 22:18:24 -04:00
},
function (next) {
2016-08-17 12:12:07 +03:00
async.parallel([
function (next) {
2016-08-17 12:12:07 +03:00
db.deleteAll([
'tid:' + tid + ':followers',
'tid:' + tid + ':ignorers',
'tid:' + tid + ':posts',
'tid:' + tid + ':posts:votes',
'tid:' + tid + ':bookmarks',
2017-02-17 19:31:21 -07:00
'tid:' + tid + ':posters',
2016-08-17 12:12:07 +03:00
], next);
},
function (next) {
db.sortedSetsRemove([
'topics:tid',
'topics:recent',
'topics:posts',
'topics:views',
'topics:votes',
], tid, next);
2016-08-17 12:12:07 +03:00
},
function (next) {
2016-08-17 12:12:07 +03:00
deleteTopicFromCategoryAndUser(tid, next);
},
function (next) {
2016-08-17 12:12:07 +03:00
Topics.deleteTopicTags(tid, next);
},
function (next) {
2016-08-17 12:12:07 +03:00
reduceCounters(tid, next);
2017-02-17 19:31:21 -07:00
},
2017-01-12 14:31:28 +03:00
], function (err) {
next(err);
});
},
function (next) {
2018-06-21 11:27:37 -04:00
plugins.fireHook('action:topic.purge', { topic: deletedTopic, uid: uid });
2017-01-12 14:31:28 +03:00
db.delete('topic:' + tid, next);
2017-02-24 12:46:40 -05:00
},
2017-01-12 14:31:28 +03:00
], callback);
2014-06-10 14:24:50 -04:00
};
2016-08-17 12:12:07 +03:00
function deleteFromFollowersIgnorers(tid, callback) {
async.waterfall([
function (next) {
2016-08-17 12:12:07 +03:00
async.parallel({
followers: async.apply(db.getSetMembers, 'tid:' + tid + ':followers'),
2017-02-17 19:31:21 -07:00
ignorers: async.apply(db.getSetMembers, 'tid:' + tid + ':ignorers'),
2016-08-17 12:12:07 +03:00
}, next);
},
function (results, next) {
var followerKeys = results.followers.map(function (uid) {
2016-08-17 12:12:07 +03:00
return 'uid:' + uid + ':followed_tids';
});
var ignorerKeys = results.ignorers.map(function (uid) {
2016-08-17 12:12:07 +03:00
return 'uid:' + uid + 'ignored_tids';
});
db.sortedSetsRemove(followerKeys.concat(ignorerKeys), tid, next);
2017-02-17 19:31:21 -07:00
},
2016-08-17 12:12:07 +03:00
], callback);
}
2014-06-10 14:24:50 -04:00
function deleteTopicFromCategoryAndUser(tid, callback) {
2017-03-12 21:38:47 +03:00
async.waterfall([
function (next) {
Topics.getTopicFields(tid, ['cid', 'uid'], next);
},
function (topicData, next) {
async.parallel([
function (next) {
db.sortedSetsRemove([
'cid:' + topicData.cid + ':tids',
'cid:' + topicData.cid + ':tids:pinned',
'cid:' + topicData.cid + ':tids:posts',
2017-10-30 17:07:51 -04:00
'cid:' + topicData.cid + ':tids:lastposttime',
'cid:' + topicData.cid + ':tids:votes',
2017-07-14 16:23:16 -04:00
'cid:' + topicData.cid + ':recent_tids',
2017-03-12 21:38:47 +03:00
'cid:' + topicData.cid + ':uid:' + topicData.uid + ':tids',
'uid:' + topicData.uid + ':topics',
], tid, next);
},
function (next) {
user.decrementUserFieldBy(topicData.uid, 'topiccount', 1, next);
},
], next);
},
], function (err) {
callback(err);
2014-10-14 22:18:24 -04:00
});
}
2014-10-14 22:51:48 -04:00
function reduceCounters(tid, callback) {
var incr = -1;
2014-10-14 22:18:24 -04:00
async.parallel([
function (next) {
2014-10-14 22:18:24 -04:00
db.incrObjectFieldBy('global', 'topicCount', incr, next);
},
function (next) {
2017-03-12 21:38:47 +03:00
async.waterfall([
function (next) {
Topics.getTopicFields(tid, ['cid', 'postcount'], next);
},
function (topicData, next) {
var postCountChange = incr * topicData.postcount;
2014-06-10 14:24:50 -04:00
2017-03-12 21:38:47 +03:00
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);
},
], next);
2017-02-17 19:31:21 -07:00
},
2014-10-14 22:18:24 -04:00
], callback);
2014-06-10 14:24:50 -04:00
}
};