DEV: Add post_id parameter to reset_bump_date route (#25372)

This would allow a theme component (or an API call) to reset the bump
date of a topic to a given post's created_at date.

I picked `post_id` as the parameter here because it provides a bit of
extra protection against accidentally resetting the bump date to a date
that doesn't make sense.
This commit is contained in:
Penar Musaraj 2024-02-15 00:42:42 -05:00 committed by GitHub
parent 2b30cca0e4
commit c1577019c8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 44 additions and 8 deletions

View File

@ -1151,12 +1151,14 @@ class TopicsController < ApplicationController
def reset_bump_date
params.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: nil
end

View File

@ -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 =
(
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 !post
self.bumped_at = post.created_at
self.save(validate: false)

View File

@ -1237,7 +1237,11 @@ Discourse::Application.routes.draw do
put "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"

View File

@ -5298,6 +5298,28 @@ RSpec.describe TopicsController do
expect(topic.reload.bumped_at).to eq_time(timestamp)
end
end
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
end
end
describe "#private_message_reset_new" do