diff --git a/app/models/post.rb b/app/models/post.rb index 7b2f036ac89b4..7157cae2a2224 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -706,6 +706,8 @@ def unhide! should_update_user_stat = false end + self.topic.reset_bumped_at(self) if is_last_reply? && !whisper? + # We need to do this because TopicStatusUpdater also does the increment # and we don't want to double count for the OP. UserStatCountUpdater.increment!(self) if should_update_user_stat diff --git a/app/models/topic.rb b/app/models/topic.rb index 348f136bd6b5d..b541ed8f7782e 100644 --- a/app/models/topic.rb +++ b/app/models/topic.rb @@ -1922,11 +1922,13 @@ def is_category_topic? @is_category_topic ||= Category.exists?(topic_id: self.id.to_i) end - def reset_bumped_at(post_id = nil) + def reset_bumped_at(post_or_post_id = nil) post = - ( - if post_id - Post.find_by(id: post_id) + if post_or_post_id.is_a?(Post) + post_or_post_id + else + if post_or_post_id + Post.find_by(id: post_or_post_id) else ordered_posts.where( user_deleted: false, @@ -1934,7 +1936,7 @@ def reset_bumped_at(post_id = nil) post_type: Post.types[:regular], ).last || first_post end - ) + end return if !post diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb index 6747125c38313..329a4c7b81c57 100644 --- a/spec/models/post_spec.rb +++ b/spec/models/post_spec.rb @@ -1655,6 +1655,42 @@ def post_with_body(body, user = nil) 1, ) end + + context "in a topic with multiple replies" do + let!(:second_last_reply) do + freeze_time 1.day.from_now + create_post(topic:, user: coding_horror, hidden: true) + end + fab!(:user) + let!(:last_reply) do + freeze_time 2.days.from_now + create_post(topic:, user:, hidden: true) + end + let!(:whisper_post) do + freeze_time 3.days.from_now + create_post(topic:, user: user, post_type: Post.types[:whisper], hidden: true) + end + + before { topic.update_columns(bumped_at: 1.day.from_now) } + + it "does not reset the topic's bumped_at when unhiding a whisper" do + whisper_post.unhide! + + expect(topic.reload.bumped_at).to eq_time(1.day.from_now) + end + + it "resets the topic's bumped_at when unhiding the last visible reply" do + last_reply.unhide! + + expect(topic.reload.bumped_at).to eq_time(last_reply.created_at) + end + + it "does not reset the topic's bumped_at when unhiding a reply that is not the last" do + second_last_reply.unhide! + + expect(topic.reload.bumped_at).to eq_time(1.day.from_now) + end + end end it "will unhide the post but will keep the topic invisible/unlisted" do