Skip to content

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

FIX: Avoid reload side effects in PostDestroyer#recover (#36983)

Régis Hanol974733b703f4
diff --git a/app/models/post_mover.rb b/app/models/post_mover.rb
index a42b8ce7bb8..dde0e94b43d 100644
--- a/app/models/post_mover.rb
+++ b/app/models/post_mover.rb
@@ -637,8 +637,8 @@ class PostMover
end
def update_statistics
- destination_topic.update_statistics
- original_topic.update_statistics
+ destination_topic.update_statistics!
+ original_topic.update_statistics!
TopicUser.update_post_action_cache(
topic_id: [original_topic.id, destination_topic.id],
post_id: @post_ids,
diff --git a/app/models/topic.rb b/app/models/topic.rb
index 7714b7da354..639e7aaa0d7 100644
--- a/app/models/topic.rb
+++ b/app/models/topic.rb
@@ -996,7 +996,7 @@ class Topic < ActiveRecord::Base
post_type =
archetype == Archetype.private_message ? " AND post_type <> #{Post.types[:small_action]}" : ""
- result = DB.query_single(<<~SQL, topic_id: topic_id)
+ result = DB.query_single(<<~SQL, topic_id:)
UPDATE topics
SET
highest_staff_post_number = (
@@ -1053,6 +1053,8 @@ class Topic < ActiveRecord::Base
WHERE topic_id = :topic_id
AND last_read_post_number > :highest
SQL
+
+ highest
end
cattr_accessor :update_featured_topics
@@ -1364,10 +1366,10 @@ class Topic < ActiveRecord::Base
# Updates the denormalized statistics of a topic including featured posters. They shouldn't
# go out of sync unless you do something drastic live move posts from one topic to another.
# this recalculates everything.
- def update_statistics
+ def update_statistics!
feature_topic_users
update_action_counts
- Topic.reset_highest(id)
+ self.highest_post_number = Topic.reset_highest(id)
end
def update_action_counts
diff --git a/app/services/post_owner_changer.rb b/app/services/post_owner_changer.rb
index 7880aa95f25..c06d68e87b9 100644
--- a/app/services/post_owner_changer.rb
+++ b/app/services/post_owner_changer.rb
@@ -43,7 +43,7 @@ class PostOwnerChanger
@topic.last_poster = @new_owner
end
- @topic.update_statistics
+ @topic.update_statistics!
@new_owner.user_stat.update(
first_post_created_at: @new_owner.reload.posts.order("created_at ASC").first&.created_at,
diff --git a/lib/post_destroyer.rb b/lib/post_destroyer.rb
index 9cf467393b5..a39d146f935 100644
--- a/lib/post_destroyer.rb
+++ b/lib/post_destroyer.rb
@@ -121,10 +121,9 @@ class PostDestroyer
@topic.update_column(:user_id, Discourse::SYSTEM_USER_ID) if [email protected]_id
@topic.recover!(@user) if @post.is_first_post?
- @topic.update_statistics
+ @topic.update_statistics!
Topic.publish_stats_to_clients!(@topic.id, :recovered)
- @topic.reload
@topic.reset_bumped_at(@post) if @post.is_last_reply? && [email protected]?
UserActionManager.post_created(@post)
diff --git a/spec/models/topic_spec.rb b/spec/models/topic_spec.rb
index feb5c2925dd..8a7fee43d70 100644
--- a/spec/models/topic_spec.rb
+++ b/spec/models/topic_spec.rb
@@ -942,9 +942,7 @@ RSpec.describe Topic do
freeze_time(start + 5.minutes)
- expect { topic.invite(topic.user, user1.username) }.not_to raise_error(
- RateLimiter::LimitExceeded,
- )
+ expect { topic.invite(topic.user, user1.username) }.not_to raise_error
end
end
end
@@ -3389,6 +3387,48 @@ RSpec.describe Topic do
end
end
+ describe ".reset_highest" do
+ fab!(:topic)
+ fab!(:first_post) { Fabricate(:post, topic:, post_number: 1) }
+ fab!(:second_post) { Fabricate(:post, topic:, post_number: 2) }
+ fab!(:third_post) { Fabricate(:post, topic:, post_number: 3) }
+
+ it "returns the highest post number" do
+ expect(Topic.reset_highest(topic.id)).to eq(3)
+ end
+
+ it "excludes deleted posts from the highest post number" do
+ third_post.update!(deleted_at: 1.hour.ago)
+
+ expect(Topic.reset_highest(topic.id)).to eq(2)
+ end
+
+ it "excludes whisper posts from the highest post number" do
+ third_post.update!(post_type: Post.types[:whisper])
+
+ expect(Topic.reset_highest(topic.id)).to eq(2)
+ end
+ end
+
+ describe "#update_statistics!" do
+ fab!(:topic)
+
+ it "updates the in-memory highest_post_number" do
+ Fabricate(:post, topic:, post_number: 1)
+ Fabricate(:post, topic:, post_number: 2)
+ Fabricate(:post, topic:, post_number: 3)
+
+ topic.update_column(:highest_post_number, 1)
+ topic.reload
+
+ expect(topic.highest_post_number).to eq(1)
+
+ topic.update_statistics!
+
+ expect(topic.highest_post_number).to eq(3)
+ end
+ end
+
describe "#access_topic_via_group" do
let(:open_group) { Fabricate(:group, public_admission: true) }
let(:request_group) do