From 71c1b8b9b92b19413ffd48e9e2acf40662ef6d41 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Wed, 4 Sep 2013 20:50:58 -0400 Subject: [PATCH] When deleting a post as staff, ask if you want to delete direct replies too --- .../discourse/controllers/topic_controller.js | 29 +++++++++++++++++-- .../javascripts/discourse/models/post.js | 22 ++++++++++---- app/controllers/posts_controller.rb | 3 +- config/locales/client.en.yml | 6 ++++ spec/controllers/posts_controller_spec.rb | 6 ++-- 5 files changed, 53 insertions(+), 13 deletions(-) diff --git a/app/assets/javascripts/discourse/controllers/topic_controller.js b/app/assets/javascripts/discourse/controllers/topic_controller.js index a1c969011ed..549682818da 100644 --- a/app/assets/javascripts/discourse/controllers/topic_controller.js +++ b/app/assets/javascripts/discourse/controllers/topic_controller.js @@ -104,7 +104,6 @@ Discourse.TopicController = Discourse.ObjectController.extend(Discourse.Selected postSelected: function(post) { if (this.get('allPostsSelected')) { return true; } if (this.get('selectedPosts').contains(post)) { return true; } - if (this.get('selectedReplies').findProperty('post_number', post.get('reply_to_post_number'))) { return true; } return false; @@ -458,7 +457,33 @@ Discourse.TopicController = Discourse.ObjectController.extend(Discourse.Selected }, deletePost: function(post) { - post.destroy(Discourse.User.current()); + var user = Discourse.User.current(), + replyCount = post.get('reply_count'), + self = this; + + // If the user is staff and the post has replies, ask if they want to delete replies too. + if (user.get('staff') && replyCount > 0) { + bootbox.confirm(I18n.t("post.controls.delete_replies.confirm", {count: replyCount}), + I18n.t("post.controls.delete_replies.no_value"), + I18n.t("post.controls.delete_replies.yes_value"), + function(result) { + + // If the user wants to delete replies, do that, otherwise delete the post as normal. + if (result) { + Discourse.Post.deleteMany([post], [post]); + self.get('postStream.posts').forEach(function (p) { + if (p === post || p.get('reply_to_post_number') === post.get('post_number')) { + p.setDeletedState(user); + } + }); + } else { + post.destroy(user); + } + + }); + } else { + post.destroy(user); + } }, removeAllowedUser: function(username) { diff --git a/app/assets/javascripts/discourse/models/post.js b/app/assets/javascripts/discourse/models/post.js index 7e6f56560d2..1927a769ed6 100644 --- a/app/assets/javascripts/discourse/models/post.js +++ b/app/assets/javascripts/discourse/models/post.js @@ -225,17 +225,18 @@ Discourse.Post = Discourse.Model.extend({ }, /** - Deletes a post + Changes the state of the post to be deleted. Does not call the server, that should be + done elsewhere. - @method destroy - @param {Discourse.User} deleted_by The user deleting the post + @method setDeletedState + @param {Discourse.User} deletedBy The user deleting the post **/ - destroy: function(deleted_by) { + setDeletedState: function(deletedBy) { // Moderators can delete posts. Regular users can only trigger a deleted at message. - if (deleted_by.get('staff')) { + if (deletedBy.get('staff')) { this.setProperties({ deleted_at: new Date(), - deleted_by: deleted_by, + deleted_by: deletedBy, can_delete: false }); } else { @@ -248,7 +249,16 @@ Discourse.Post = Discourse.Model.extend({ user_deleted: true }); } + }, + /** + Deletes a post + + @method destroy + @param {Discourse.User} deletedBy The user deleting the post + **/ + destroy: function(deletedBy) { + this.setDeletedState(deletedBy); return Discourse.ajax("/posts/" + (this.get('id')), { type: 'DELETE' }); }, diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index c3748b7a553..78de8e35aa3 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -159,8 +159,7 @@ class PostsController < ApplicationController Post.transaction do topic_id = posts.first.topic_id - posts.each {|p| p.destroy } - Topic.reset_highest(topic_id) + posts.each {|p| PostDestroyer.new(current_user, p).destroy } end render nothing: true diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 1f7bdadb1b7..cf82f4fdbe1 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -816,6 +816,12 @@ en: undelete: "undelete this post" share: "share a link to this post" more: "More" + delete_replies: + confirm: + one: "Do you also want to delete the direct reply to this post?" + other: "Do you also want to delete the {{count}} direct replies to this post?" + yes_value: "Yes, delete the replies too" + no_value: "No, just this post" actions: flag: 'Flag' diff --git a/spec/controllers/posts_controller_spec.rb b/spec/controllers/posts_controller_spec.rb index 70e5d401d40..6a63fe5d61a 100644 --- a/spec/controllers/posts_controller_spec.rb +++ b/spec/controllers/posts_controller_spec.rb @@ -180,12 +180,12 @@ describe PostsController do end it "deletes the post" do - Post.any_instance.expects(:destroy).twice + PostDestroyer.any_instance.expects(:destroy).twice xhr :delete, :destroy_many, post_ids: [post1.id, post2.id] end it "updates the highest read data for the forum" do - Topic.expects(:reset_highest) + Topic.expects(:reset_highest).twice xhr :delete, :destroy_many, post_ids: [post1.id, post2.id] end @@ -196,7 +196,7 @@ describe PostsController do end it "deletes the post and the reply to it" do - Post.any_instance.expects(:destroy).twice + PostDestroyer.any_instance.expects(:destroy).twice xhr :delete, :destroy_many, post_ids: [post1.id], reply_post_ids: [post1.id] end