better topic delete restore

v1.18.x
Baris Soner Usakli 11 years ago
parent 9b88dcdc14
commit 9d1a295b85

@ -67,27 +67,31 @@ var winston = require('winston'),
return callback(new Error('topic-already-deleted')); return callback(new Error('topic-already-deleted'));
} }
topics.delete(tid); topics.delete(tid, function(err) {
if(err) {
return callback(err);
}
db.decrObjectField('global', 'topicCount'); db.decrObjectField('global', 'topicCount');
ThreadTools.lock(tid); ThreadTools.lock(tid);
db.searchRemove('topic', tid); db.searchRemove('topic', tid);
events.logTopicDelete(uid, tid); events.logTopicDelete(uid, tid);
websockets.emitTopicPostStats(); websockets.emitTopicPostStats();
websockets.in('topic_' + tid).emit('event:topic_deleted', { websockets.in('topic_' + tid).emit('event:topic_deleted', {
tid: tid tid: tid
}); });
callback(null, { callback(null, {
tid: tid tid: tid
});
}); });
}); });
} };
ThreadTools.restore = function(tid, uid, callback) { ThreadTools.restore = function(tid, uid, callback) {
topics.getTopicField(tid, 'deleted', function(err, deleted) { topics.getTopicField(tid, 'deleted', function(err, deleted) {
@ -99,29 +103,33 @@ var winston = require('winston'),
return callback(new Error('topic-already-restored')); return callback(new Error('topic-already-restored'));
} }
topics.restore(tid); topics.restore(tid, function(err) {
if(err) {
return callback(err);
}
db.incrObjectField('global', 'topicCount'); db.incrObjectField('global', 'topicCount');
ThreadTools.unlock(tid); ThreadTools.unlock(tid);
events.logTopicRestore(uid, tid); events.logTopicRestore(uid, tid);
websockets.emitTopicPostStats(); websockets.emitTopicPostStats();
websockets.in('topic_' + tid).emit('event:topic_restored', { websockets.in('topic_' + tid).emit('event:topic_restored', {
tid: tid tid: tid
}); });
topics.getTopicField(tid, 'title', function(err, title) { topics.getTopicField(tid, 'title', function(err, title) {
db.searchIndex('topic', title, tid); db.searchIndex('topic', title, tid);
}); });
callback(null, { callback(null, {
tid:tid tid:tid
});
}); });
}); });
} };
ThreadTools.lock = function(tid, uid, callback) { ThreadTools.lock = function(tid, uid, callback) {
topics.setTopicField(tid, 'locked', 1); topics.setTopicField(tid, 'locked', 1);

@ -1154,29 +1154,61 @@ var async = require('async'),
}); });
} }
Topics.delete = function(tid) { Topics.delete = function(tid, callback) {
Topics.setTopicField(tid, 'deleted', 1); async.parallel([
db.sortedSetRemove('topics:recent', tid); function(next) {
db.sortedSetRemove('topics:posts', tid); Topics.setTopicField(tid, 'deleted', 1, next);
db.sortedSetRemove('topics:views', tid); },
function(next) {
Topics.getTopicField(tid, 'cid', function(err, cid) { db.sortedSetRemove('topics:recent', tid, next);
db.incrObjectFieldBy('category:' + cid, 'topic_count', -1); },
}); function(next) {
} db.sortedSetRemove('topics:posts', tid, next);
},
function(next) {
db.sortedSetRemove('topics:views', tid, next);
},
function(next) {
Topics.getTopicField(tid, 'cid', function(err, cid) {
if(err) {
return next(err);
}
db.incrObjectFieldBy('category:' + cid, 'topic_count', -1, next);
});
}
], callback);
};
Topics.restore = function(tid) { Topics.restore = function(tid, callback) {
Topics.setTopicField(tid, 'deleted', 0);
Topics.getTopicFields(tid, ['lastposttime', 'postcount', 'viewcount'], function(err, topicData) { Topics.getTopicFields(tid, ['lastposttime', 'postcount', 'viewcount'], function(err, topicData) {
db.sortedSetAdd('topics:recent', topicData.lastposttime, tid); if(err) {
db.sortedSetAdd('topics:posts', topicData.postcount, tid); return callback(err);
db.sortedSetAdd('topics:views', topicData.viewcount, tid); }
});
Topics.getTopicField(tid, 'cid', function(err, cid) { async.parallel([
db.incrObjectFieldBy('category:' + cid, 'topic_count', 1); function(next) {
Topics.setTopicField(tid, 'deleted', 0, next);
},
function(next) {
db.sortedSetAdd('topics:recent', topicData.lastposttime, tid, next);
},
function(next) {
db.sortedSetAdd('topics:posts', topicData.postcount, tid, next);
},
function(next) {
db.sortedSetAdd('topics:views', topicData.viewcount, tid, next);
},
function(next) {
Topics.getTopicField(tid, 'cid', function(err, cid) {
if(err) {
return next(err);
}
db.incrObjectFieldBy('category:' + cid, 'topic_count', 1, next);
});
}
], callback);
}); });
} };
Topics.reIndexTopic = function(tid, callback) { Topics.reIndexTopic = function(tid, callback) {
Topics.getPids(tid, function(err, pids) { Topics.getPids(tid, function(err, pids) {

Loading…
Cancel
Save