From 46c8101c5c03cc7fe8359aaee00a3534aa8527ae Mon Sep 17 00:00:00 2001 From: barisusakli Date: Tue, 23 Feb 2016 13:08:38 +0200 Subject: [PATCH] closes #4152 --- src/posts/delete.js | 116 ++++++++++++++++++++++++-------------------- src/posts/parse.js | 4 +- src/posts/tools.js | 37 ++++++-------- 3 files changed, 81 insertions(+), 76 deletions(-) diff --git a/src/posts/delete.js b/src/posts/delete.js index 44d969b8ab..a77c153cb2 100644 --- a/src/posts/delete.js +++ b/src/posts/delete.js @@ -1,31 +1,32 @@ 'use strict'; -var async = require('async'), - _ = require('underscore'), +var async = require('async'); +var _ = require('underscore'); - db = require('../database'), - topics = require('../topics'), - user = require('../user'), - plugins = require('../plugins'); +var db = require('../database'); +var topics = require('../topics'); +var user = require('../user'); +var plugins = require('../plugins'); module.exports = function(Posts) { - Posts.delete = function(pid, callback) { + Posts.delete = function(pid, uid, callback) { var postData; async.waterfall([ - function(next) { + function (next) { + plugins.fireHook('filter:post.delete', {pid: pid, uid: uid}, next); + }, + function (data, next) { Posts.setPostField(pid, 'deleted', 1, next); }, - function(next) { + function (next) { Posts.getPostFields(pid, ['pid', 'tid', 'uid', 'timestamp'], next); }, - function(_post, next) { + function (_post, next) { postData = _post; topics.getTopicField(_post.tid, 'cid', next); }, - function(cid, next) { - plugins.fireHook('action:post.delete', pid); - + function (cid, next) { async.parallel([ function(next) { updateTopicTimestamp(postData.tid, next); @@ -40,29 +41,31 @@ module.exports = function(Posts) { topics.updateTeaser(postData.tid, next); } ], function(err) { + plugins.fireHook('action:post.delete', pid); next(err, postData); }); } ], callback); }; - Posts.restore = function(pid, callback) { + Posts.restore = function(pid, uid, callback) { var postData; async.waterfall([ - function(next) { + function (next) { + plugins.fireHook('filter:post.restore', {pid: pid, uid: uid}, next); + }, + function (data, next) { Posts.setPostField(pid, 'deleted', 0, next); }, - function(next) { + function (next) { Posts.getPostFields(pid, ['pid', 'tid', 'uid', 'content', 'timestamp'], next); }, - function(_post, next) { + function (_post, next) { postData = _post; topics.getTopicField(_post.tid, 'cid', next); }, - function(cid, next) { + function (cid, next) { postData.cid = cid; - plugins.fireHook('action:post.restore', _.clone(postData)); - async.parallel([ function(next) { updateTopicTimestamp(postData.tid, next); @@ -74,6 +77,7 @@ module.exports = function(Posts) { topics.updateTeaser(postData.tid, next); } ], function(err) { + plugins.fireHook('action:post.restore', _.clone(postData)); next(err, postData); }); } @@ -99,40 +103,46 @@ module.exports = function(Posts) { }); } - Posts.purge = function(pid, callback) { - Posts.exists(pid, function(err, exists) { - if (err || !exists) { - return callback(err); - } - - async.parallel([ - function(next) { - deletePostFromTopicAndUser(pid, next); - }, - function(next) { - deletePostFromCategoryRecentPosts(pid, next); - }, - function(next) { - deletePostFromUsersFavourites(pid, next); - }, - function(next) { - deletePostFromUsersVotes(pid, next); - }, - function(next) { - db.sortedSetsRemove(['posts:pid', 'posts:flagged'], pid, next); - }, - function(next) { - Posts.dismissFlag(pid, next); - } - ], function(err) { - if (err) { - return callback(err); + Posts.purge = function(pid, uid, callback) { + async.waterfall([ + function (next) { + Posts.exists(pid, next); + }, + function (exists, next) { + if (!exists) { + return callback(); } - - plugins.fireHook('action:post.purge', pid); - db.delete('post:' + pid, callback); - }); - }); + plugins.fireHook('filter:post.purge', {pid: pid, uid: uid}, next); + }, + function (data, next) { + async.parallel([ + function (next) { + deletePostFromTopicAndUser(pid, next); + }, + function (next) { + deletePostFromCategoryRecentPosts(pid, next); + }, + function (next) { + deletePostFromUsersFavourites(pid, next); + }, + function (next) { + deletePostFromUsersVotes(pid, next); + }, + function (next) { + db.sortedSetsRemove(['posts:pid', 'posts:flagged'], pid, next); + }, + function (next) { + Posts.dismissFlag(pid, next); + } + ], function(err) { + if (err) { + return next(err); + } + plugins.fireHook('action:post.purge', pid); + db.delete('post:' + pid, next); + }); + } + ], callback); }; function deletePostFromTopicAndUser(pid, callback) { diff --git a/src/posts/parse.js b/src/posts/parse.js index 4317f024b3..efa5667ccc 100644 --- a/src/posts/parse.js +++ b/src/posts/parse.js @@ -1,8 +1,8 @@ 'use strict'; -var cache = require('./cache'), - plugins = require('../plugins'); +var cache = require('./cache'); +var plugins = require('../plugins'); module.exports = function(Posts) { diff --git a/src/posts/tools.js b/src/posts/tools.js index 11b01b785a..64ddf155d7 100644 --- a/src/posts/tools.js +++ b/src/posts/tools.js @@ -1,9 +1,9 @@ 'use strict'; -var async = require('async'), +var async = require('async'); - privileges = require('../privileges'), - cache = require('./cache'); +var privileges = require('../privileges'); +var cache = require('./cache'); module.exports = function(Posts) { Posts.tools = {}; @@ -40,25 +40,20 @@ module.exports = function(Posts) { if (!canEdit) { return next(new Error('[[error:no-privileges]]')); } - next(); - } - ], function (err) { - if (err) { - return callback(err); - } - if (isDelete) { - cache.del(pid); - Posts.delete(pid, callback); - } else { - Posts.restore(pid, function(err, postData) { - if (err) { - return callback(err); - } - Posts.parsePost(postData, callback); - }); + if (isDelete) { + cache.del(pid); + Posts.delete(pid, uid, next); + } else { + Posts.restore(pid, uid, function(err, postData) { + if (err) { + return next(err); + } + Posts.parsePost(postData, next); + }); + } } - }); + ], callback); } Posts.tools.purge = function(uid, pid, callback) { @@ -71,7 +66,7 @@ module.exports = function(Posts) { return next(new Error('[[error:no-privileges]]')); } cache.del(pid); - Posts.purge(pid, next); + Posts.purge(pid, uid, next); } ], callback); };