From ea6f9af08b2db75ee880b6af058a2d8c4e93ebcf Mon Sep 17 00:00:00 2001 From: Martin Brennan Date: Thu, 25 Mar 2021 10:24:50 +1000 Subject: [PATCH] FIX: Add topic_diff to PostRevisor (#12518) The instance of the PostRevisor is passed to the post_edited event. It is useful to know what has happened to the topic in this event (we already pass a boolean for topic_changed? but that is not so helpful by itself). --- lib/post_revisor.rb | 4 ++++ spec/components/post_revisor_spec.rb | 31 ++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/lib/post_revisor.rb b/lib/post_revisor.rb index 29989c01be..6d4e6e9ce6 100644 --- a/lib/post_revisor.rb +++ b/lib/post_revisor.rb @@ -536,6 +536,10 @@ class PostRevisor @post.previous_changes.slice(*POST_TRACKED_FIELDS) end + def topic_diff + @topic_changes.diff + end + def perform_edit return if bypass_rate_limiter? EditRateLimiter.new(@editor).performed! diff --git a/spec/components/post_revisor_spec.rb b/spec/components/post_revisor_spec.rb index 7926c48723..118d791197 100644 --- a/spec/components/post_revisor_spec.rb +++ b/spec/components/post_revisor_spec.rb @@ -44,6 +44,22 @@ describe PostRevisor do end context 'editing category' do + it "triggers the :post_edited event with topic_changed?" do + category = Fabricate(:category) + category.set_permissions(everyone: :full) + category.save! + post = create_post + events = DiscourseEvent.track_events do + post.revise(post.user, category_id: category.id) + end + + event = events.find { |e| e[:event_name] == :post_edited } + + expect(event[:params].first).to eq(post) + expect(event[:params].second).to eq(true) + expect(event[:params].third).to be_kind_of(PostRevisor) + expect(event[:params].third.topic_diff).to eq({ "category_id" => [SiteSetting.uncategorized_category_id, category.id] }) + end it 'does not revise category when no permission to create a topic in category' do category = Fabricate(:category) @@ -855,6 +871,21 @@ describe PostRevisor do expect(post.topic.tags.map(&:name).sort).to eq(['important', 'stuff']) end + it "triggers the :post_edited event with topic_changed?" do + topic.tags = [Fabricate(:tag, name: "super"), Fabricate(:tag, name: "stuff")] + + events = DiscourseEvent.track_events do + subject.revise!(user, raw: "lets totally update the body", tags: []) + end + + event = events.find { |e| e[:event_name] == :post_edited } + + expect(event[:params].first).to eq(post) + expect(event[:params].second).to eq(true) + expect(event[:params].third).to be_kind_of(PostRevisor) + expect(event[:params].third.topic_diff).to eq({ "tags" => [["super", "stuff"], []] }) + end + context "with staff-only tags" do before do create_staff_only_tags(['important'])