FIX: Do not show bootbox if post has no replies. (#7866)

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.

diff --git a/app/assets/javascripts/discourse/controllers/topic.js.es6 b/app/assets/javascripts/discourse/controllers/topic.js.es6
index e358cd3..1e86063 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 98575b6..bed3cc1 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();
+    });
+  }
+);

GitHub sha: c4d18335