A platform for community discussion. Free, open, simple.
DEV: Add post_id parameter to reset_bump_date route (#25372)
Penar Musarajc1577019c862
diff --git a/app/controllers/topics_controller.rb b/app/controllers/topics_controller.rbindex 91c50d76e84..9b90d47818e 100644--- a/app/controllers/topics_controller.rb+++ b/app/controllers/topics_controller.rb@@ -1151,12 +1151,14 @@ class TopicsController < ApplicationControllerdef reset_bump_dateparams.require(:id)+ params.permit(:post_id)+guardian.ensure_can_update_bumped_at!topic = Topic.find_by(id: params[:id])raise Discourse::NotFound.new unless topic- topic.reset_bumped_at+ topic.reset_bumped_at(params[:post_id])render body: nilenddiff --git a/app/models/topic.rb b/app/models/topic.rbindex cc7a42a8177..053380d95f1 100644--- a/app/models/topic.rb+++ b/app/models/topic.rb@@ -1858,13 +1858,21 @@ class Topic < ActiveRecord::Base@is_category_topic ||= Category.exists?(topic_id: self.id.to_i)end- def reset_bumped_at+ def reset_bumped_at(post_id = nil)post =- ordered_posts.where(- user_deleted: false,- hidden: false,- post_type: Post.types[:regular],- ).last || first_post+ (+ if post_id+ Post.find_by(id: post_id)+ else+ ordered_posts.where(+ user_deleted: false,+ hidden: false,+ post_type: Post.types[:regular],+ ).last || first_post+ end+ )++ return if !postself.bumped_at = post.created_atself.save(validate: false)diff --git a/config/routes.rb b/config/routes.rbindex 3344551a184..2fc68179f6b 100644--- a/config/routes.rb+++ b/config/routes.rb@@ -1237,7 +1237,11 @@ Discourse::Application.routes.draw doput "t/:id/convert-topic/:type" => "topics#convert_topic"put "t/:id/publish" => "topics#publish"put "t/:id/shared-draft" => "topics#update_shared_draft"- put "t/:id/reset-bump-date" => "topics#reset_bump_date"+ put "t/:id/reset-bump-date/(:post_id)" => "topics#reset_bump_date",+ :constraints => {+ id: /\d+/,+ post_id: /\d+/,+ }put "topics/bulk"put "topics/reset-new" => "topics#reset_new"put "topics/pm-reset-new" => "topics#private_message_reset_new"diff --git a/spec/requests/topics_controller_spec.rb b/spec/requests/topics_controller_spec.rbindex 1cc9aa15512..e0f582734bc 100644--- a/spec/requests/topics_controller_spec.rb+++ b/spec/requests/topics_controller_spec.rb@@ -5298,6 +5298,28 @@ RSpec.describe TopicsController doexpect(topic.reload.bumped_at).to eq_time(timestamp)endend++ context "with a post_id parameter" do+ before { sign_in(admin) }++ it "resets bump correctly" do+ post1 = Fabricate(:post, user: post_author1, topic: topic, created_at: 2.days.ago)+ post2 = Fabricate(:post, user: post_author1, topic: topic, created_at: 1.day.ago)++ put "/t/#{topic.id}/reset-bump-date/#{post1.id}.json"+ expect(response.status).to eq(200)+ expect(topic.reload.bumped_at).to eq_time(post1.created_at)+ end++ it "does not raise an error for an inexistent post" do+ id = (SecureRandom.random_number * 100_000_000).to_i+ original_bumped_at = topic.bumped_at++ put "/t/#{topic.id}/reset-bump-date/#{id}.json"+ expect(response.status).to eq(200)+ expect(topic.reload.bumped_at).to eq_time(original_bumped_at)+ end+ endenddescribe "#private_message_reset_new" do