A platform for community discussion. Free, open, simple.
FIX: Allow to delete first posts with bulk destroy (#37180)
Loïc Guitaut3ac5a0fa5deb
diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rbindex 205c654ff9d..a15ba79f549 100644--- a/app/controllers/posts_controller.rb+++ b/app/controllers/posts_controller.rb@@ -424,8 +424,7 @@ class PostsController < ApplicationControllerposts = Post.where(id: post_ids_including_replies).order(:id)raise Discourse::InvalidParameters.new(:post_ids) if posts.blank?- # Make sure we can delete the posts- posts.each { |p| guardian.ensure_can_delete!(p) }+ posts.each { |p| guardian.ensure_can_delete_post_or_topic!(p) }Post.transaction doposts.each_with_index do |p, i|diff --git a/spec/requests/posts_controller_spec.rb b/spec/requests/posts_controller_spec.rbindex 059adfaf526..3e95564c7cd 100644--- a/spec/requests/posts_controller_spec.rb+++ b/spec/requests/posts_controller_spec.rb@@ -360,22 +360,22 @@ RSpec.describe PostsController doenddescribe "#destroy_many" do- include_examples "action requires login",- :delete,- "/posts/destroy_many.json",- params: {- post_ids: [123, 345],- }-- describe "when logged in" do+ it_behaves_like "action requires login",+ :delete,+ "/posts/destroy_many.json",+ params: {+ post_ids: [123, 345],+ }++ context "when logged in" dofab!(:poster, :moderator)- fab!(:post1) { Fabricate(:post, user: poster, post_number: 2) }+ fab!(:post1) { Fabricate(:post, user: poster, post_number: 1) }fab!(:post2) doFabricate(:post,topic: post1.topic,user: poster,- post_number: 3,+ post_number: 2,reply_to_post_number: post1.post_number,)end@@ -405,6 +405,7 @@ RSpec.describe PostsController dodelete "/posts/destroy_many.json", params: { post_ids: [post1.id, post2.id] }expect(response.status).to eq(200)end+# bookmarkit "triggers DiscourseEvent with :posts_destroyed and correct params" dosign_in(poster)@@ -424,17 +425,19 @@ RSpec.describe PostsController dodelete "/posts/destroy_many.json", params: { post_ids: [post1.id, post2.id] }end- describe "can delete replies" do- before { PostReply.create(post_id: post1.id, reply_post_id: post2.id) }+ context "with replies" do+ fab!(:post_reply) { PostReply.create(post: post1, reply: post2) }++ before { sign_in(poster) }it "deletes the post and the reply to it" do- sign_in(poster)- PostDestroyer.any_instance.expects(:destroy).twice- delete "/posts/destroy_many.json",- params: {- post_ids: [post1.id],- reply_post_ids: [post1.id],- }+ expect do+ delete "/posts/destroy_many.json",+ params: {+ post_ids: [post1.id],+ reply_post_ids: [post1.id],+ }+ end.to change { Post.count }.by(-2).and change { Topic.count }.by(-1)endend