FIX: DiscourseEvent should not be triggered from within the controller.

This commit is contained in:
Guo Xiang Tan 2016-09-05 15:58:04 +08:00
parent ec90655c41
commit aabb7a8592
3 changed files with 41 additions and 20 deletions

View File

@ -342,8 +342,6 @@ class TopicsController < ApplicationController
first_post = topic.ordered_posts.first first_post = topic.ordered_posts.first
PostDestroyer.new(current_user, first_post, { context: params[:context] }).destroy PostDestroyer.new(current_user, first_post, { context: params[:context] }).destroy
DiscourseEvent.trigger(:topic_destroyed, topic, current_user)
render nothing: true render nothing: true
end end
@ -354,8 +352,6 @@ class TopicsController < ApplicationController
first_post = topic.posts.with_deleted.order(:post_number).first first_post = topic.posts.with_deleted.order(:post_number).first
PostDestroyer.new(current_user, first_post).recover PostDestroyer.new(current_user, first_post).recover
DiscourseEvent.trigger(:topic_recovered, topic, current_user)
render nothing: true render nothing: true
end end

View File

@ -47,6 +47,7 @@ class PostDestroyer
mark_for_deletion mark_for_deletion
end end
DiscourseEvent.trigger(:post_destroyed, @post, @opts, @user) DiscourseEvent.trigger(:post_destroyed, @post, @opts, @user)
DiscourseEvent.trigger(:topic_destroyed, @topic, @user) if @post.is_first_post?
end end
def recover def recover
@ -60,6 +61,7 @@ class PostDestroyer
topic.update_statistics topic.update_statistics
recover_user_actions recover_user_actions
DiscourseEvent.trigger(:post_recovered, @post, @opts, @user) DiscourseEvent.trigger(:post_recovered, @post, @opts, @user)
DiscourseEvent.trigger(:topic_recovered, topic, @user) if @post.is_first_post?
end end
def staff_recovered def staff_recovered

View File

@ -193,27 +193,50 @@ describe PostDestroyer do
end end
describe 'basic destroying' do describe 'basic destroying' do
it "as the creator of the post, doesn't delete the post" do it "as the creator of the post, doesn't delete the post" do
post2 = create_post begin
post2 = create_post
@orig = post2.cooked called = 0
PostDestroyer.new(post2.user, post2).destroy topic_destroyed = -> (topic, user) do
post2.reload expect(topic).to eq(post2.topic)
expect(user).to eq(post2.user)
called += 1
end
expect(post2.deleted_at).to be_blank DiscourseEvent.on(:topic_destroyed, &topic_destroyed)
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)
# lets try to recover @orig = post2.cooked
PostDestroyer.new(post2.user, post2).recover PostDestroyer.new(post2.user, post2).destroy
post2.reload post2.reload
expect(post2.version).to eq(3)
expect(post2.user_deleted).to eq(false)
expect(post2.cooked).to eq(@orig)
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 end
context "as a moderator" do context "as a moderator" do