FIX: Add topic_diff to PostRevisor (#12518)

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).

diff --git a/lib/post_revisor.rb b/lib/post_revisor.rb
index 29989c0..6d4e6e9 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 7926c48..118d791 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'])

GitHub sha: ea6f9af0

This commit appears in #12518 which was approved by lis2. It was merged by martin.