From c4d1833588391a0f4d9053a8ed0c50189e40d789 Mon Sep 17 00:00:00 2001 From: Bianca Nenciu Date: Fri, 12 Jul 2019 12:42:57 +0300 Subject: [PATCH] FIX: Do not show bootbox if post has no replies. (#7866) When we delete a post that has replies, we show a modal asking if the user wants to delete the post, the post and its direct replies or the post and all its replies. If replies are deleted before a post, that modal would ask the user if they want to delete the post and 0 replies. That commit ensure we skip the modal and directly delete the post in this case. --- .../discourse/controllers/topic.js.es6 | 10 +++++ .../javascripts/controllers/topic-test.js.es6 | 39 +++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/app/assets/javascripts/discourse/controllers/topic.js.es6 b/app/assets/javascripts/discourse/controllers/topic.js.es6 index e358cd34c3fc0..1e86063b0d792 100644 --- a/app/assets/javascripts/discourse/controllers/topic.js.es6 +++ b/app/assets/javascripts/discourse/controllers/topic.js.es6 @@ -523,6 +523,16 @@ export default Ember.Controller.extend(bufferedProperty("model"), { if (user.get("staff") && hasReplies) { ajax(`/posts/${post.id}/reply-ids.json`).then(replies => { + if (replies.length === 0) { + return post + .destroy(user) + .then(refresh) + .catch(error => { + popupAjaxError(error); + post.undoDeleteState(); + }); + } + const buttons = []; buttons.push({ diff --git a/test/javascripts/controllers/topic-test.js.es6 b/test/javascripts/controllers/topic-test.js.es6 index 98575b66291ac..bed3cc19cd8e1 100644 --- a/test/javascripts/controllers/topic-test.js.es6 +++ b/test/javascripts/controllers/topic-test.js.es6 @@ -511,3 +511,42 @@ QUnit.test("topVisibleChanged", function(assert) { "it should work with a post-placehodler" ); }); + +QUnit.test( + "deletePost - no modal is shown if post does not have replies", + function(assert) { + /* global server */ + server.get("/posts/2/reply-ids.json", () => { + return [200, { "Content-Type": "application/json" }, []]; + }); + + let destroyed; + const post = Ember.Object.create({ + id: 2, + post_number: 2, + can_delete: true, + reply_count: 3, + destroy: () => { + destroyed = true; + return Ember.RSVP.Promise.resolve(); + } + }); + + const postStream = Ember.Object.create({ + stream: [2, 3, 4], + posts: [post, { id: 3 }, { id: 4 }] + }); + + const currentUser = Ember.Object.create({ staff: true }); + const model = Topic.create({ postStream }); + const controller = this.subject({ model, currentUser }); + + const done = assert.async(); + controller.send("deletePost", post); + + Ember.run.next(() => { + assert.ok(destroyed, "post was destroyed"); + done(); + }); + } +);