diff --git a/app/controllers/topics_controller.rb b/app/controllers/topics_controller.rb index acae3c2af9b..95f26ce4ab7 100644 --- a/app/controllers/topics_controller.rb +++ b/app/controllers/topics_controller.rb @@ -342,8 +342,6 @@ class TopicsController < ApplicationController first_post = topic.ordered_posts.first PostDestroyer.new(current_user, first_post, { context: params[:context] }).destroy - DiscourseEvent.trigger(:topic_destroyed, topic, current_user) - render nothing: true end @@ -354,8 +352,6 @@ class TopicsController < ApplicationController first_post = topic.posts.with_deleted.order(:post_number).first PostDestroyer.new(current_user, first_post).recover - DiscourseEvent.trigger(:topic_recovered, topic, current_user) - render nothing: true end diff --git a/lib/post_destroyer.rb b/lib/post_destroyer.rb index c0ac588d942..29f7e239675 100644 --- a/lib/post_destroyer.rb +++ b/lib/post_destroyer.rb @@ -47,6 +47,7 @@ class PostDestroyer mark_for_deletion end DiscourseEvent.trigger(:post_destroyed, @post, @opts, @user) + DiscourseEvent.trigger(:topic_destroyed, @topic, @user) if @post.is_first_post? end def recover @@ -60,6 +61,7 @@ class PostDestroyer topic.update_statistics recover_user_actions DiscourseEvent.trigger(:post_recovered, @post, @opts, @user) + DiscourseEvent.trigger(:topic_recovered, topic, @user) if @post.is_first_post? end def staff_recovered diff --git a/spec/components/post_destroyer_spec.rb b/spec/components/post_destroyer_spec.rb index bbd3cfc34c8..6a506fd6a00 100644 --- a/spec/components/post_destroyer_spec.rb +++ b/spec/components/post_destroyer_spec.rb @@ -193,27 +193,50 @@ describe PostDestroyer do end describe 'basic destroying' do - it "as the creator of the post, doesn't delete the post" do - post2 = create_post + begin + post2 = create_post - @orig = post2.cooked - PostDestroyer.new(post2.user, post2).destroy - post2.reload + called = 0 + topic_destroyed = -> (topic, user) do + expect(topic).to eq(post2.topic) + expect(user).to eq(post2.user) + called += 1 + end - expect(post2.deleted_at).to be_blank - expect(post2.deleted_by).to be_blank - expect(post2.user_deleted).to eq(true) - expect(post2.raw).to eq(I18n.t('js.post.deleted_by_author', {count: 24})) - expect(post2.version).to eq(2) + DiscourseEvent.on(:topic_destroyed, &topic_destroyed) - # lets try to recover - PostDestroyer.new(post2.user, post2).recover - post2.reload - expect(post2.version).to eq(3) - expect(post2.user_deleted).to eq(false) - expect(post2.cooked).to eq(@orig) + @orig = post2.cooked + PostDestroyer.new(post2.user, post2).destroy + post2.reload + expect(post2.deleted_at).to be_blank + expect(post2.deleted_by).to be_blank + expect(post2.user_deleted).to eq(true) + expect(post2.raw).to eq(I18n.t('js.post.deleted_by_author', {count: 24})) + expect(post2.version).to eq(2) + expect(called).to eq(1) + + called = 0 + topic_recovered = -> (topic, user) do + expect(topic).to eq(post2.topic) + expect(user).to eq(post2.user) + called += 1 + end + + DiscourseEvent.on(:topic_recovered, &topic_recovered) + + # lets try to recover + PostDestroyer.new(post2.user, post2).recover + post2.reload + expect(post2.version).to eq(3) + expect(post2.user_deleted).to eq(false) + expect(post2.cooked).to eq(@orig) + expect(called).to eq(1) + ensure + DiscourseEvent.off(:topic_destroyed, &topic_destroyed) + DiscourseEvent.off(:topic_recovered, &topic_recovered) + end end context "as a moderator" do