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