From 59d035376b2d79a4d54550b01cca1b2b49950eea Mon Sep 17 00:00:00 2001 From: barisusakli Date: Wed, 12 Oct 2016 14:46:04 +0300 Subject: [PATCH] closes #5025 --- src/socket.io/posts/tools.js | 93 ++++++++++++++++++++++++------------ 1 file changed, 62 insertions(+), 31 deletions(-) diff --git a/src/socket.io/posts/tools.js b/src/socket.io/posts/tools.js index fcd076f124..e67819965e 100644 --- a/src/socket.io/posts/tools.js +++ b/src/socket.io/posts/tools.js @@ -60,11 +60,61 @@ module.exports = function(SocketPosts) { }; SocketPosts.delete = function(socket, data, callback) { - doPostAction('delete', 'event:post_deleted', socket, data, callback); + if (!data || !data.pid) { + return callback(new Error('[[error:invalid-data]]')); + } + var postData; + async.waterfall([ + function(next) { + posts.tools.delete(socket.uid, data.pid, next); + }, + function(_postData, next) { + postData = _postData; + isMainAndLastPost(data.pid, next); + }, + function(results, next) { + if (results.isMain && results.isLast) { + deleteTopicOf(data.pid, socket, next); + } else { + next(); + } + }, + function(next) { + websockets.in('topic_' + data.tid).emit('event:post_deleted', postData); + + events.log({ + type: 'post-delete', + uid: socket.uid, + pid: data.pid, + ip: socket.ip + }); + + next(); + } + ], callback); }; SocketPosts.restore = function(socket, data, callback) { - doPostAction('restore', 'event:post_restored', socket, data, callback); + if (!data || !data.pid) { + return callback(new Error('[[error:invalid-data]]')); + } + + posts.tools.restore(socket.uid, data.pid, function(err, postData) { + if (err) { + return callback(err); + } + + websockets.in('topic_' + data.tid).emit('event:post_restored', postData); + + events.log({ + type: 'post-restore', + uid: socket.uid, + pid: data.pid, + ip: socket.ip + }); + + callback(); + }); }; SocketPosts.deletePosts = function(socket, data, callback) { @@ -85,29 +135,6 @@ module.exports = function(SocketPosts) { }, callback); }; - function doPostAction(command, eventName, socket, data, callback) { - if (!data) { - return callback(new Error('[[error:invalid-data]]')); - } - - posts.tools[command](socket.uid, data.pid, function(err, postData) { - if (err) { - return callback(err); - } - - websockets.in('topic_' + data.tid).emit(eventName, postData); - - events.log({ - type: 'post-' + command, - uid: socket.uid, - pid: data.pid, - ip: socket.ip - }); - - callback(); - }); - } - SocketPosts.purge = function(socket, data, callback) { function purgePost() { posts.tools.purge(socket.uid, data.pid, function(err) { @@ -151,15 +178,19 @@ module.exports = function(SocketPosts) { return callback(new Error('[[error:cant-purge-main-post]]')); } - posts.getTopicFields(data.pid, ['tid', 'cid'], function(err, topic) { - if (err) { - return callback(err); - } - socketTopics.doTopicAction('delete', 'event:topic_deleted', socket, {tids: [topic.tid], cid: topic.cid}, callback); - }); + deleteTopicOf(data.pid, socket, callback); }); }; + function deleteTopicOf(pid, socket, callback) { + posts.getTopicFields(pid, ['tid', 'cid'], function(err, topic) { + if (err) { + return callback(err); + } + socketTopics.doTopicAction('delete', 'event:topic_deleted', socket, {tids: [topic.tid], cid: topic.cid}, callback); + }); + } + function isMainAndLastPost(pid, callback) { async.parallel({ isMain: function(next) {