From 7a6b685aa0d96f5ddec4988f0bccdc71698e3fa0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Wed, 26 Apr 2017 14:19:48 -0400 Subject: [PATCH] if the main and last post is purged, purge the topic as well --- src/socket.io/posts/tools.js | 33 ++++++++++++++++++++------------- test/posts.js | 6 +++--- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/src/socket.io/posts/tools.js b/src/socket.io/posts/tools.js index c1ad05b119..c075a96a8e 100644 --- a/src/socket.io/posts/tools.js +++ b/src/socket.io/posts/tools.js @@ -138,39 +138,46 @@ module.exports = function (SocketPosts) { return callback(new Error('[[error:invalid-data]]')); } var postData; + var topicData; + var isMainAndLast = false; async.waterfall([ function (next) { isMainAndLastPost(data.pid, next); }, function (results, next) { if (results.isMain && !results.isLast) { - return callback(new Error('[[error:cant-purge-main-post]]')); + return next(new Error('[[error:cant-purge-main-post]]')); } - if (results.isMain && results.isLast) { - return deleteTopicOf(data.pid, socket, next); - } - setImmediate(next); - }, - function (next) { - posts.getPostField(data.pid, 'toPid', next); + isMainAndLast = results.isMain && results.isLast; + + posts.getPostFields(data.pid, ['toPid', 'tid'], next); }, - function (toPid, next) { - postData = { pid: data.pid, toPid: toPid }; + function (_postData, next) { + postData = _postData; + postData.pid = data.pid; posts.tools.purge(socket.uid, data.pid, next); }, function (next) { websockets.in('topic_' + data.tid).emit('event:post_purged', postData); - topics.getTopicField(data.tid, 'title', next); + topics.getTopicFields(data.tid, ['title', 'cid'], next); }, - function (title, next) { + function (_topicData, next) { + topicData = _topicData; events.log({ type: 'post-purge', uid: socket.uid, pid: data.pid, ip: socket.ip, - title: String(title), + title: String(topicData.title), }, next); }, + function (next) { + if (isMainAndLast) { + socketTopics.doTopicAction('purge', 'event:topic_purged', socket, { tids: [postData.tid], cid: topicData.cid }, next); + } else { + setImmediate(next); + } + }, ], callback); }; diff --git a/test/posts.js b/test/posts.js index 49a4783872..50441a58e1 100644 --- a/test/posts.js +++ b/test/posts.js @@ -274,16 +274,16 @@ describe('Post\'s', function () { }); }); - it('should purge posts and delete topic', function (done) { + it('should purge posts and purge topic', function (done) { createTopicWithReply(function (topicPostData, replyData) { socketPosts.purgePosts({ uid: voterUid }, { pids: [replyData.pid, topicPostData.postData.pid], tid: topicPostData.topicData.tid }, function (err) { assert.ifError(err); posts.exists('post:' + replyData.pid, function (err, exists) { assert.ifError(err); assert.equal(exists, false); - topics.getTopicField(topicPostData.topicData.tid, 'deleted', function (err, deleted) { + topics.exists(topicPostData.topicData.tid, function (err, exists) { assert.ifError(err); - assert.equal(parseInt(deleted, 10), 1); + assert(!exists); done(); }); });