Skip to content

A platform for community discussion. Free, open, simple.

FEATURE: Bump wiki topics when OP is edited (#35137)

Martin Brennan5faaa6f8696e
diff --git a/lib/post_revisor.rb b/lib/post_revisor.rb
index 5f6e05e0f5d..bd83e503bbd 100644
--- a/lib/post_revisor.rb
+++ b/lib/post_revisor.rb
@@ -207,15 +207,20 @@ class PostRevisor
tag_list.sort.map { |tag_name| "##{tag_name}" }.join(", ")
end
- # AVAILABLE OPTIONS:
- # - revised_at: changes the date of the revision
- # - force_new_version: bypass grace period edit window
- # - bypass_rate_limiter:
- # - bypass_bump: do not bump the topic, even if last post
- # - skip_validations: ask ActiveRecord to skip validations
- # - skip_revision: do not create a new PostRevision record
- # - skip_staff_log: skip creating an entry in the staff action log
- # - silent: don't send notifications to user
+ # Revises a post with the given fields and options.
+ #
+ # @param editor [User] The user performing the revision
+ # @param fields [Hash] Hash of fields to update
+ # @param opts [Hash] Optional parameters for the revision
+ # @option opts [Time] :revised_at Changes the date of the revision
+ # @option opts [Boolean] :force_new_version Bypass grace period edit window
+ # @option opts [Boolean] :bypass_rate_limiter Bypass the max limits per day rate limiter
+ # @option opts [Boolean] :bypass_bump Do not bump the topic. Takes precedence over should_bump_topic plugin modifier, and any other should_bump? logic
+ # @option opts [Boolean] :skip_validations Ask ActiveRecord to skip validations
+ # @option opts [Boolean] :skip_revision Do not create a new PostRevision record
+ # @option opts [Boolean] :skip_staff_log Skip creating an entry in the staff action log
+ # @option opts [Boolean] :silent Don't send notifications to user
+ # @return [Boolean] Returns true if the revision was successful, false otherwise
def revise!(editor, fields, opts = {})
@editor = editor
@fields = fields.with_indifferent_access
@@ -654,19 +659,28 @@ class PostRevisor
end
def bump_topic
- return if bypass_bump?
+ return if !should_bump?
@topic.update_column(:bumped_at, Time.now)
TopicTrackingState.publish_muted(@topic)
TopicTrackingState.publish_unmuted(@topic)
TopicTrackingState.publish_latest(@topic)
end
- def bypass_bump?
- return true if @opts[:bypass_bump] == true
- return true if @post.whisper? || !@post_successfully_saved || post_changes.any?
- return true if @topic_changes.errored?
- return true if topic_title_changed? || topic_category_changed? || topic_tags_changed?
- return true if only_hidden_tags_changed?
+ def should_bump?
+ return false if @opts[:bypass_bump] == true
+
+ should_bump_topic_modifier_result =
+ DiscoursePluginRegistry.apply_modifier(
+ :should_bump_topic,
+ nil,
+ @post,
+ post_changes,
+ @topic_changes,
+ @editor,
+ )
+ return should_bump_topic_modifier_result if !should_bump_topic_modifier_result.nil?
+
+ return true if @post.is_first_post? && @post.wiki? && post_changes.any?
false
end
diff --git a/spec/lib/post_revisor_spec.rb b/spec/lib/post_revisor_spec.rb
index 9d8c0dcec37..5a18110cce3 100644
--- a/spec/lib/post_revisor_spec.rb
+++ b/spec/lib/post_revisor_spec.rb
@@ -548,47 +548,6 @@ describe PostRevisor do
expect(post.public_version).to eq(1)
expect(post.revisions.size).to eq(0)
end
-
- it "doesn't bump the topic when editing the last post" do
- expect {
- post_revisor.revise!(
- post.user,
- { raw: "updated body" },
- revised_at: post.updated_at + SiteSetting.editing_grace_period + 1.seconds,
- )
- }.not_to change { post.topic.bumped_at }
- end
-
- it "doesn't bump the topic when editing a post that isn't the last post" do
- create_post(topic_id: post.topic.id)
- expect {
- post_revisor.revise!(
- post.user,
- { raw: "updated body" },
- revised_at: post.updated_at + SiteSetting.editing_grace_period + 1.seconds,
- )
- }.not_to change { post.topic.bumped_at }
- end
-
- it "doesn't bump the topic when editing the topic title" do
- expect {
- post_revisor.revise!(
- post.user,
- { title: "This is an updated topic title" },
- revised_at: post.updated_at + SiteSetting.editing_grace_period + 1.seconds,
- )
- }.not_to change { post.topic.bumped_at }
- end
-
- it "doesn't bump the topic when editing the topic category" do
- expect {
- post_revisor.revise!(
- post.user,
- { category_id: Fabricate(:category).id },
- revised_at: post.updated_at + SiteSetting.editing_grace_period + 1.seconds,
- )
- }.not_to change { post.topic.bumped_at }
- end
end
describe "edit reasons" do
@@ -1298,12 +1257,6 @@ describe PostRevisor do
expect(post.topic.tags.size).to eq(0)
end
- it "doesn't bump the topic when editing tags" do
- expect { post_revisor.revise!(post.user, { tags: %w[totally update] }) }.not_to change {
- post.topic.bumped_at
- }
- end
-
it "can't add staff-only tags" do
create_staff_only_tags(["important"])
result =
@@ -1403,58 +1356,6 @@ describe PostRevisor do
]
end
- it "doesn't bump topic if only staff-only tags are added" do
- expect {
- result =
- post_revisor.revise!(
- Fabricate(:admin),
- raw: post.raw,
- tags: topic.tags.map(&:name) + ["secret"],
- )
- expect(result).to eq(true)
- }.to_not change { topic.reload.bumped_at }
- end
-
- it "doesn't bump topic if only staff-only tags are removed" do
- expect {
- result =
- post_revisor.revise!(
- Fabricate(:admin),
- raw: post.raw,
- tags: topic.tags.map(&:name) - %w[important secret],
- )
- expect(result).to eq(true)
- }.to_not change { topic.reload.bumped_at }
- end
-
- it "doesn't bump topic if only staff-only tags are removed and there are no tags left" do
- topic.tags = Tag.where(name: %w[important secret]).to_a
- expect {
- result = post_revisor.revise!(Fabricate(:admin), raw: post.raw, tags: [])
- expect(result).to eq(true)
- }.to_not change { topic.reload.bumped_at }
- end
-
- it "doesn't bump topic if empty string is given" do
- topic.tags = Tag.where(name: %w[important secret]).to_a
- expect {
- result = post_revisor.revise!(Fabricate(:admin), raw: post.raw, tags: [""])
- expect(result).to eq(true)
- }.to_not change { topic.reload.bumped_at }
- end
-
- it "doesn't bump topic if non staff-only tags are added" do
- expect {
- result =
- post_revisor.revise!(
- Fabricate(:admin),
- raw: post.raw,
- tags: topic.tags.map(&:name) + [Fabricate(:tag).name],
- )
- expect(result).to eq(true)
- }.to_not change { topic.reload.bumped_at }
- end
-
it "creates a hidden revision" do
post_revisor.revise!(
Fabricate(:admin),
@@ -1693,4 +1594,198 @@ describe PostRevisor do
}.not_to change(ReviewablePost, :count)
end
end
+
+ describe "topic bumping" do
+ subject(:post_revisor) { PostRevisor.new(post) }
+
+ let(:post) { Fabricate(:post, post_args) }
+
+ it "doesn't bump the topic when editing the last post" do
+ expect {
+ post_revisor.revise!(
+ post.user,
+ { raw: "updated body" },
+ revised_at: post.updated_at + SiteSetting.editing_grace_period + 1.seconds,
+ )
+ }.not_to change { post.topic.bumped_at }
+ end
+
+ it "doesn't bump the topic when editing a post that isn't the last post" do
+ create_post(topic_id: post.topic.id)
+ expect {
+ post_revisor.revise!(
+ post.user,
+ { raw: "updated body" },
+ revised_at: post.updated_at + SiteSetting.editing_grace_period + 1.seconds,
+ )
+ }.not_to change { post.topic.bumped_at }
+ end
+
+ it "doesn't bump the topic when editing the topic title" do
+ expect {
+ post_revisor.revise!(
+ post.user,
+ { title: "This is an updated topic title" },
+ revised_at: post.updated_at + SiteSetting.editing_grace_period + 1.seconds,
+ )
+ }.not_to change { post.topic.bumped_at }
+ end
+
+ it "doesn't bump the topic when editing the topic category" do
+ expect {
+ post_revisor.revise!(
+ post.user,
+ { category_id: Fabricate(:category).id },
+ revised_at: post.updated_at + SiteSetting.editing_grace_period + 1.seconds,
+ )
+ }.not_to change { post.topic.bumped_at }
+ end
+
+ it "doesn't bump the topic when editing tags" do
+ expect { post_revisor.revise!(post.user, { tags: %w[totally update] }) }.not_to change {
+ post.topic.bumped_at
+ }
+ end
+
+ describe "should_bump_topic plugin modifier" do
+ let(:plugin_instance) { Plugin::Instance.new }
+ let(:modifier_return_value) { nil }
+ let(:modifier_block) do
+ Proc.new do |value, modifier_post, modifier_post_changes, modifier_topic_changes, editor|
+ modifier_return_value
+ end
+ end
+
+ before { plugin_instance.register_modifier(:should_bump_topic, &modifier_block) }
+
+ after do
+ DiscoursePluginRegistry.unregister_modifier(
+ plugin_instance,
+ :should_bump_topic,
+ &modifier_block
+ )
+ end
+
+ context "when the modifier returns false" do
+ let(:modifier_return_value) { false }
+
+ it "prevents bumping" do
+ expect {
+ post_revisor.revise!(
+ post.user,
+ { raw: "updated body" },
+ revised_at: post.updated_at + SiteSetting.editing_grace_period + 1.seconds,
+ )
+ }.not_to change { post.topic.bumped_at }
+ end
+ end
+
+ context "when the modifier returns true" do
+ let(:modifier_return_value) { true }
+
+ it "bumps the topic" do
+ expect {
+ post_revisor.revise!(
+ post.user,
+ { raw: "updated body" },
+ revised_at: post.updated_at + SiteSetting.editing_grace_period + 1.seconds,
+ )
+ }.to change { post.topic.bumped_at }
+ end
+ end
+ end
+
+ context "for a wiki topic" do
+ before { post.update!(wiki: true) }
+
+ it "bumps the topic when the OP is edited" do
+ expect {
+ post_revisor.revise!(
+ post.user,
+ { raw: "updated body" },
+ revised_at: post.updated_at + SiteSetting.editing_grace_period + 1.seconds,
+ )
+ }.to change { post.topic.bumped_at }
+ end
+
+ it "doesn't bump the topic when another post is edited" do
+ other_post = Fabricate(:post, topic: topic)
+ post_revisor_other = PostRevisor.new(other_post)
+
+ expect {
+ post_revisor_other.revise!(
+ post.user,
+ { raw: "updated body" },
+ revised_at: post.updated_at + SiteSetting.editing_grace_period + 1.seconds,
+ )
+ }.not_to change { post.topic.bumped_at }
+ end
+ end
+
+ context "with hidden tags" do
+ let(:bumped_at) { 1.day.ago }
+
+ before do
+ post.topic.update!(bumped_at: bumped_at)
+ create_hidden_tags(%w[important secret])
+ post.topic.tags = [
+ Fabricate(:tag, name: "super"),
+ Tag.where(name: "important").first,
+ Fabricate(:tag, name: "stuff"),
+ ]
+ end
+
+ it "doesn't bump topic if only staff-only tags are added" do
+ expect {
+ result =
+ post_revisor.revise!(
+ Fabricate(:admin),
+ raw: post.raw,
+ tags: post.topic.tags.map(&:name) + ["secret"],
+ )
+ expect(result).to eq(true)
+ }.to_not change { post.topic.reload.bumped_at }
+ end
+
+ it "doesn't bump topic if only staff-only tags are removed" do
+ expect {
+ result =
+ post_revisor.revise!(
+ Fabricate(:admin),
+ raw: post.raw,
+ tags: post.topic.tags.map(&:name) - %w[important secret],
+ )
+ expect(result).to eq(true)
+ }.to_not change { post.topic.reload.bumped_at }
+ end
+
+ it "doesn't bump topic if only staff-only tags are removed and there are no tags left" do
+ post.topic.tags = Tag.where(name: %w[important secret]).to_a
+ expect {
+ result = post_revisor.revise!(Fabricate(:admin), raw: post.raw, tags: [])
+ expect(result).to eq(true)
+ }.to_not change { post.topic.reload.bumped_at }
+ end
+
+ it "doesn't bump topic if empty string is given" do
+ post.topic.tags = Tag.where(name: %w[important secret]).to_a
+ expect {
+ result = post_revisor.revise!(Fabricate(:admin), raw: post.raw, tags: [""])
+ expect(result).to eq(true)
+ }.to_not change { post.topic.reload.bumped_at }
+ end
+
+ it "doesn't bump topic if non staff-only tags are added" do
+ expect {
+ result =
+ post_revisor.revise!(
+ Fabricate(:admin),
+ raw: post.raw,
+ tags: post.topic.tags.map(&:name) + [Fabricate(:tag).name],
+ )
+ expect(result).to eq(true)
+ }.not_to change { post.topic.reload.bumped_at }
+ end
+ end
+ end
end