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).
This commit is contained in:
Martin Brennan 2021-03-25 10:24:50 +10:00 committed by GitHub
parent 9eb7c37098
commit ea6f9af08b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 0 deletions

View File

@ -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!

View File

@ -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'])